Created
December 28, 2015 22:41
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "class CleanMixinBaseClean(CleanMixin, BaseClean)" | |
CleanMixinBaseClean().clean() | |
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