I have the following code, in Django:
JavaScript
x
13
13
1
class Parent(models.Model):
2
def save(self):
3
# Do Stuff A
4
5
class Mixin(object):
6
def save(self):
7
# Do Stuff B
8
9
class A(Parent, Mixin):
10
def save(self):
11
super(A, self).save()
12
# Do stuff C
13
Now, I want to use the mixin without blatting the behaviour of the save in Parent. So I when I save, I want to do stuff C, B, and A. I’ve read Calling the setter of a super class in a mixin however I don’t get it and having read the super docs it doesn’t seem to answer my question.
THe question is, what do I put in the mixin to make sure it does stuff B and doesn’t stop Stuff A from happening?
Advertisement
Answer
How about calling super in your mixin class?
JavaScript
1
20
20
1
class Parent(object):
2
def test(self):
3
print("parent")
4
5
6
class MyMixin(object):
7
def test(self):
8
super(MyMixin, self).test()
9
print("mixin")
10
11
12
class MyClass(MyMixin, Parent):
13
def test(self):
14
super(MyClass, self).test()
15
print("self")
16
17
if __name__ == "__main__":
18
my_obj = MyClass()
19
my_obj.test()
20
This will give you the output as:
JavaScript
1
5
1
$ python test.py
2
parent
3
mixin
4
self
5