為什么會出錯 ??
class B:
x=1
def delshu(self):
del self.x
>>> b=B()
>>> b.x
1
>>> b.delshu()
Traceback (most recent call last):
File "<pyshell#96>", line 1, in <module>
b.delshu()
File "<pyshell#93>", line 4, in delshu
del self.x
AttributeError: x
uj5u.com熱心網友回復:
class B:x=1
這里X是類屬性。
del self.x
這里self.x是實體屬性
關鍵來了
del self.x
洗掉實體屬性,這個實體b有self.x這個實體屬性嗎???
uj5u.com熱心網友回復:
x=1那里要寫成self.x=1.直接寫x 代表類的屬性而不是物件的屬性uj5u.com熱心網友回復:
實體化之后 實體物件不擁有類屬性的屬性嗎 ?b=B()
b.x 是可以呼叫的
那這個b.x是類屬性,還是實體屬性
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/33188.html
上一篇:presence_of_element_located問題
下一篇:python魔法方法
