juventusryp
V2EX  ›  问与答

求教下 Python 中子类如何获取到父类方法中的变量

  •  
  •   juventusryp · Sep 24, 2022 · 841 views
    This topic created in 1337 days ago, the information mentioned may be changed or developed.
    class A:
      def __init__(self, name, age):
        self.name = name
        self.age = age
            
      def __bar(self):
        data = 1
            
    class B(A):
      def __init__(self):
        super().__init__()
        
      def foo():
        pass
        
    

    求教下上述代码中,foo 里面该如何写可以拿到__bar 里的 data 的值?

    hsfzxjy
        1
    hsfzxjy  
       Sep 24, 2022
    拿不到,那是局部变量
    Yourshell
        2
    Yourshell  
       Sep 25, 2022
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   884 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 466ea39e · 41ms · UTC 22:14 · PVG 06:14 · LAX 15:14 · JFK 18:14
    ♥ Do have faith in what you're doing.