juventusryp
V2EX  ›  问与答

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

  •  
  •   juventusryp · Sep 24, 2022 · 869 views
    This topic created in 1344 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   ·   2750 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 06:26 · PVG 14:26 · LAX 23:26 · JFK 02:26
    ♥ Do have faith in what you're doing.