1.4 KiB
1.4 KiB
Python mixins
I learned something fun the other day. And it has made me consider writing more mixins and using Python's multiple inheritance more. This is not completely new to me, but I finally tested it a few days ago and what I thought I knew seems to have been confirmed.
Given some classes:
class Foo(object):
def frob(self):
print('foo')
class Bar(Foo):
def frob(self):
super(Bar, self).frob()
print('bar')
class BazMixin(object):
def frob(self):
super(BazMixin, self).frob()
print('baz')
class Unmixed(Bar, BazMixin):
def frob(self):
super(Unmixed, self).frob()
print('unmixed')
class Mixed(BazMixin, Bar):
def frob(self):
super(Mixed, self).frob()
print('mixed')
unmixed = Unmixed()
unmixed.frob()
foo bar unmixed
And here that it is not:
mixed = Mixed()
mixed.frob()
foo bar baz mixed