我有以下代码。
class Foo(object):
def __init__(self):
self.__baz = 40
def foo(self):
print self.__baz
class Bar(Foo):
def __init__(self):
#super(Bar, self).__init__()
self.__baz = 21
def bar(self):
print self.__baz
x = Bar()
x.foo()
x.bar()
我收到此错误:
Traceback (most recent call last):
File "classes.py", line 15, in <module>
x.foo()
File "classes.py", line 5, in foo
print self.__baz
AttributeError: 'Bar' object has no attribute '_Foo__baz'
为什么foo
方法没有继承在Bar
.
编辑:它工作正常,如果你调用 super 被注释掉。