Skip to content

Instantly share code, notes, and snippets.

@simonw
Created December 28, 2015 22:41
Show Gist options
  • Save simonw/a4130002a378816a6e96 to your computer and use it in GitHub Desktop.
Save simonw/a4130002a378816a6e96 to your computer and use it in GitHub Desktop.
An illustration of how Python multiple inheritance / mixins work. I always forget the order in which super() calls other methods.
class CleanMixin(object):
def clean(self):
print " clean() in CleanMixin"
super(CleanMixin, self).clean()
class BaseClean(object):
def clean(self):
print " clean() in BaseClean"
class BaseCleanCleanMixin(BaseClean, CleanMixin):
pass
class CleanMixinBaseClean(CleanMixin, BaseClean):
pass
class CustomCleanMixinBaseClean(CleanMixin, BaseClean):
def clean(self):
print " clean() in CustomCleanMixinBaseClean"
super(CustomCleanMixinBaseClean, self).clean()
print "class BaseCleanCleanMixin(BaseClean, CleanMixin)"
BaseCleanCleanMixin().clean()
print
print "class CleanMixinBaseClean(CleanMixin, BaseClean)"
CleanMixinBaseClean().clean()
print
print "class CustomCleanMixinBaseClean(CleanMixin, BaseClean)"
CustomCleanMixinBaseClean().clean()
"""
Output of this script looks like this:
class BaseCleanCleanMixin(BaseClean, CleanMixin)
clean() in BaseClean
class CleanMixinBaseClean(CleanMixin, BaseClean)
clean() in CleanMixin
clean() in BaseClean
class CustomCleanMixinBaseClean(CleanMixin, BaseClean)
clean() in CustomCleanMixinBaseClean
clean() in CleanMixin
clean() in BaseClean
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment