【发布时间】:2011-06-12 12:28:06
【问题描述】:
当我尝试从 NewOne 类访问属性 'self.b' 时,为什么会收到“AttributeError:'NewOne' 对象没有属性 'self.b'”错误消息。我的意思是它就在那里。
class NewOne(object):
def __init__(self):
self.b = 'Cat' # this is what i want to access
def child(self):
self.c = 'kitten'
return self.c
class FatherClass(object):
def __init__(self, a):
self.a = a
def son(self):
self.i = 'I and my father'
return self.i
def father(self):
self.x = 'are one'
return self.x
def father_son(self):
u = NewOne()
k = getattr(u, 'self.b') #why does it tell me NewOne has no self.b attr
return self.a, k()
getattr 不是用来访问方法的吗?为什么叫 getattr 而不是 getmeth 什么的? 谢谢
【问题讨论】:
标签: python