Reputation: 2034
In python 3.5.2, the following class hierarchy:
class Foobaz(object):
def __init__(self):
pass
class Foo(object):
def __init__(self):
pass
class Baz(Foobaz):
class Bar(Foo):
def __init__(self):
super(Bar, self).__init__()
def __init__(self):
super(Baz, self).__init__()
self.bar = self.Bar()
if __name__ == '__main__':
b = Baz()
instantiating the Baz class yields
super(Bar, self).__init__()
NameError: name 'Bar' is not defined
Having an internal class subclassed directly from object - that is, no call to super - is just fine. I have no clue why. Can someone explain it please?
Upvotes: 0
Views: 47
Reputation: 160467
Bar
isn't visible, it's a class variable. You'll need to be explicit:
super(Baz.Bar, self).__init__()
Take note that the no argument form of super
takes care of this for you:
super().__init__()
works fine.
Upvotes: 2