[英]Delete attribute of a class using its instance
我有一類說QClass
和實例Q
的QClass
。
我在an_attribute
以某種方式在運行時創建了一個屬性QClass
。 如何刪除an_attribute
使用del Q.an_attribute
?
我知道從類中刪除該屬性將使其無法從所有實例中訪問。
更新 : Q
向用戶公開,他們只能使用del Q.an_attribute
。 我只能更改Q
或QClass
代碼。
del
in python比delattr
更有效。 請參閱https://stackoverflow.com/a/1121068/4698026
所以,順其自然
del type(Q).an_attribute
嘗試del type(Q).an_attribute
。
type(Q)將返回QClass,然后使用del與它。
嘗試:
delattr(Q, 'an_attribute')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.