A note about the ordering of mixin classes

February 22, 2007

In Python, when you have a class that inherits from both a primary class and some mixin classes (for example, if you're using the SocketServer stuff), it's conventional to declare your class's inheritance list with the primary class first:

class Real(primary, mixin1, mixin2):
    ....

However, an important safety tip: if your mixin class overrides methods of the primary class, it has to be first. Failure to observe this safety tip can cause head-scratching bugs followed by head-smacking embarrassment.

(Since I was mixing stuff in to standard types like list and tuple and str, I spent a certain amount of time wondering if the interpreter had special direct magic for them that meant I couldn't hijack and augment their standard behavior. I felt somewhat foolish when there turned out to be a much simpler explanation.)

Written on 22 February 2007.
« Fixing Python's string .join()
My zeroth law of compromised machines »

Page tools: View Source.
Search:
Login: Password:

Last modified: Thu Feb 22 11:13:10 2007
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.