From here, if you define some objects like that:
class Mixin1(object):
def test(self):
print "Mixin1"
class Mixin2(object):
def test(self):
print "Mixin2"
class BaseClass(object):
pass
class MyClass(Mixin2, Mixin1, BaseClass):
pass
You'll get:
>>> obj = MyClass()
>>> obj.test()
Mixin2
Is there a way to call Mixin1
test()
method?
Call it explicitly:
Mixin1.test(obj)
The attribute process in Python is relatively complex. For your given example, this is the process for finding the value of obj.test
:
test
attribute.obj
is an instance of: MyClass
. MyClass
does not have a test
attribute.MyClass
. In this case, MyClass.__mro__
tells you to look first at Mixin2
, then Mixin1
, then object
.Mixin2
has a test
attribute, so we finally have a match.Mixin2.test
is a function with a __get__
method, so that is called and the return value is used.You can safely ignore step 5 here, and just assume that Mixin2.test
is a method. One that is returned, you can see that obj.test()
calls Mixin2.test
.
This might help explain why I asked the question I did in a comment. There is a wide variety of ways you can fiddle with the program to get obj.test()
to produce a call to Mixin1.test()
instead. You can patch the object, you can fiddle with MyClass.__mro__
, you can tweak what Mixin2.test
actually does, etc.
Override the test
method and call Mixin1.test
explicitly:
class MyClass(Mixin2, Mixin1, BaseClass):
def test(self):
Mixin1.test(self)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With