4

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.

Sudhanshu Mishra
  • 2,024
  • 1
  • 22
  • 40

3 Answers3

5

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

So, go with

del type(Q).an_attribute
Community
  • 1
  • 1
Himanshu Mishra
  • 8,510
  • 12
  • 37
  • 74
2

try del type(Q).an_attribute.

type(Q) will return QClass and then you use del with it.

Juca
  • 479
  • 3
  • 5
1

Try:

delattr(Q, 'an_attribute')