无法在Python中访问父成员变量 [英] Can't access parent member variable in Python
本文介绍了无法在Python中访问父成员变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从扩展类访问父成员变量.但是运行以下代码...
I'm trying to access a parent member variable from an extended class. But running the following code...
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'
我在做什么错了?
推荐答案
您正在混淆类和实例属性.
You're mixing up class and instance attributes.
print self._haircolor
这篇关于无法在Python中访问父成员变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文