是否有通過類名和實例獲取類的屬性的通用方法?獲取類的屬性
class A:
def __init__(self):
self.prop = 1
a = A()
for attr, value in a.__dict__.items():
print(attr, value) # prop, 1
。
class A:
def __init__(self):
self.prop = 1
for attr, value in A.__dict__.items():
print(attr, value)
#__dict__, __doc__, __init__, __module__, __weakref__
爲什麼最後一個例子返回dir
attibutes爲何結果不同?
,你會看到什麼給你? – grifaton 2012-03-03 16:53:00
>有沒有通過類名和實例獲取類的屬性的通用方法? <我不明白這個 – warvariuc 2012-03-03 16:57:24
我的意思是如何獲得第二個例子中的類屬性(我想獲得'prop,1')? – Opsa 2012-03-03 17:01:20