Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete attribute of a class using its instance

Tags:

python

I've a class say QClass and an instance Q of QClass.

I've somehow created an attribute an_attribute at run time in QClass. How do I delete that an_attribute using del Q.an_attribute?

I know that deleting that attribute from class will make it inaccessible from all of its instances.

Update: Q is exposed to user and they can only go with del Q.an_attribute. I can only change code of Q or QClass.

like image 948
Sudhanshu Mishra Avatar asked Mar 15 '23 19:03

Sudhanshu Mishra


1 Answers

del in python is more efficient than delattr. See https://stackoverflow.com/a/1121068/4698026

So, go with

del type(Q).an_attribute
like image 105
Himanshu Mishra Avatar answered Mar 29 '23 03:03

Himanshu Mishra