To super or not to super (Re: Accessing parent objects)

Ian Kelly ian.g.kelly at gmail.com
Tue Mar 27 10:52:53 EDT 2018


On Tue, Mar 27, 2018 at 8:47 AM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
> On Tue, Mar 27, 2018 at 12:21 AM, Gregory Ewing
> <greg.ewing at canterbury.ac.nz> wrote:
>> The trouble is, those conditions don't always hold.
>> Often when overriding a method, you want to do something
>> *instead* of what the base method does.
>
> As noted above, unless the method or class is abstract then this
> violates LSP. If the method is abstract and does nothing, then just
> call it. If the method is abstract and raises an exception, then
> that's a little more tricky. Ideally, don't write abstract methods
> that raise NotImplementedError unless they're not intended to be used
> with multiple inheritance.
>
> If you really need to though, you can solve this by creating a guard
> class that implements the method and does nothing, ending the super
> call chain. Then just make sure that at least one subclass in the
> multiple inheritance diagram inherits from the guard class rather than
> the original class.

Er, this should say "make sure that every subclass ... inherits from
the guard class ..." since we want to make sure that nothing else ends
up between the guard class and the original class.



More information about the Python-list mailing list