Home > front end >  How can a nested/inner class get a variable from an outer class in Python?
How can a nested/inner class get a variable from an outer class in Python?

Time:12-28

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()

CodePudding user response:

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()

CodePudding user response:

class A:
  moduleId = 1

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

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