简体   繁体   中英

How can a nested/inner class get a variable from an outer class in Python?

I have class with a nested/inner class. How to get the class variable moduleId within class B 's init method to pass it into ExtraSuperClass ?

class A:
  moduleId = 1

  class B(ExtraSuperClass):
     def __init__(self):
        super().__init__(moduleId)

  def __init__(self):
     n = self.B()

In example you provided you could access it like this:

A.moduleId

Also, you can't call B like that, you should do it like this:

self.B()

class A: moduleId = 1

class B(ExtraSuperClass): def init (self): super(). init (moduleId)

def init (self): n = self.B()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM