我正在尝试从扩展类访问父成员变量。但是运行以下代码...
class Mother(object):
def __init__(self):
self._haircolor = "Brown"
class Child(Mother):
def __init__(self):
Mother.__init__(self)
def print_haircolor(self):
print Mother._haircolor
c = Child()
c.print_haircolor()
告诉我这个错误:
AttributeError: type object 'Mother' has no attribute '_haircolor'
我做错了什么?
最佳答案
您混淆了类和实例属性。
print self._haircolor
关于python - 无法在 Python 中访问父成员变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10064688/