python 3 subclassing 在内部的情况下 class
python 3 subclassing in case of internal class
在 python 3.5.2 中,以下 class 层次结构:
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()
实例化 Baz class 产量
super(Bar, self).__init__()
NameError: name 'Bar' is not defined
直接从对象内部 class subclassed - 也就是说,不调用 super - 就可以了。我不知道为什么。有人可以解释一下吗?
Bar
不可见,它是一个 class 变量。你需要明确:
super(Baz.Bar, self).__init__()
请注意,super
的无参数形式会为您解决此问题:
super().__init__()
工作正常。
在 python 3.5.2 中,以下 class 层次结构:
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()
实例化 Baz class 产量
super(Bar, self).__init__()
NameError: name 'Bar' is not defined
直接从对象内部 class subclassed - 也就是说,不调用 super - 就可以了。我不知道为什么。有人可以解释一下吗?
Bar
不可见,它是一个 class 变量。你需要明确:
super(Baz.Bar, self).__init__()
请注意,super
的无参数形式会为您解决此问题:
super().__init__()
工作正常。