Python: deleting a class attribute in a subclass

后端 未结 7 792
野性不改
野性不改 2020-12-25 11:22

I have a subclass and I want it to not include a class attribute that\'s present on the base class.

I tried this, but it doesn\'t work:

>         


        
相关标签:
7条回答
  • 2020-12-25 11:48

    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.

    0 讨论(0)
提交回复
热议问题