jake77
jake77

Reputation: 2034

python 3 subclassing in case of internal class

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

Answers (1)

Dimitris Fasarakis Hilliard
Dimitris Fasarakis Hilliard

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

Related Questions