无法在Python中访问父成员变量

明天:

我正在尝试从扩展类访问父成员变量。但是运行以下代码...

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'

我究竟做错了什么?

伊格纳西奥·巴斯克斯(Ignacio Vazquez-Abrams):

您正在混合类和实例属性。

print self._haircolor

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章