Python: deleting a class attribute in a subclass Python: deleting a class attribute in a subclass python python

Python: deleting a class attribute in a subclass


You can use delattr(class, field_name) to remove it from the class definition.


You don't need to delete it. Just override it.

class B(A):   x = None

or simply don't reference it.

Or consider a different design (instance attribute?).


Think carefully about why you want to do this; you probably don't. Consider not making B inherit from A.

The idea of subclassing is to specialise an object. In particular, children of a class should be valid instances of the parent class:

>>> class foo(dict): pass>>> isinstance(foo(), dict)... True

If you implement this behaviour (with e.g. x = property(lambda: AttributeError)), you are breaking the subclassing concept, and this is Bad.