當我嘗試從NewOne類訪問屬性'self.b'時,爲什麼我得到一個「AttributeError:'NewOne'對象沒有屬性'self.b'」錯誤消息。我的意思是在那裏。python OOP,訪問屬性
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或什麼? 感謝
+1:「更好......」 – 2011-01-20 14:52:59