Overriding methods inherited from a superclass with methods from a mixin

dieter dieter at handshake.de
Mon Jun 13 03:13:54 EDT 2016


alanqueiros at gmail.com writes:

> I'm trying to override methods inherited from a superclass by methods defined in a mixin class.
> Here's an sscce:
> https://bpaste.net/show/6c7d8d590658 (never expires)
>
> I've had problems finding the proper way to do that, since at first the base class wasn't to the right and I've assumed the correct order was from left to right.

The class' MRO ("Method Resolution Order") determines in which
order attributes are looked up.
Either, you must order your base classes in such a way that the MRO
controlled lookup finds the methods you want to be used or
you must explicitely put a definition for those methods in your
derived class (it may have the form "overridden_method = <BaseClass>.overridden_method").

The rules to determine the MRO are complex. The "inspect" module contains
a function ("get_mro") to show the MRO of a given class. Use it
to get your inheritance order right.




More information about the Python-list mailing list