Why can't attributes of inherited object be deleted?
class A(object):
def delete(self):
pass
def dont_delete(self):
pass
class B(A):
pass
del A.delete
del B.dont_delete
AttributeError: dont_delete
dont_delete
is not copied over to B
; you can only delete direct attributes from objects.
Python looks up attributes on instances and classes by delegating down the chain of inheritance, not by copying attributes across to sub-classes. As such there is nothing to delete from a subclass either.
You should not try and 'uninherit'; when are using A
as a base class, you are effectively stating that B
is the same thing as A
, only more specialised. That process only ever should add attributes, not remove. Design your classes accordingly; use a mixin base class for attributes and methods that should not always be inherited, for example.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.