Sometimes using mixin with multiple inheritance can help us improve reusability of our code.
For example, the following design
class FollowableMixin(object):
def get_followers(self):
...
...
class User(FollowableMixin):
...
may be better reused than simply adding get_followers
to User
:
class User(object):
def get_followers(self):
...
...
because later we may consider supporting other followable entities that are potential clients of get_followers
:
class BookStore(FollowableMixin):
...
However, if this pattern is overused, code may get too complex.
class User(FollowableMixin, RunnableMixin, FlyableMixin, WhatMixin ...):
...
With all these mixin classes injecting properties and methods to your class, it becomes very difficult to understand your code. For example, you don't know where the method you are calling is from, and this method may in turn include an invocation of a method in another mixin ...
What should I do to simplify this programme?
Advantages. It provides a mechanism for multiple inheritance by allowing one class to use common functionality from multiple classes, but without the complex semantics of multiple inheritance. Code reusability: Mixins are useful when a programmer wants to share functionality between different classes.
As defined in Wikipedia, a mixin is a class containing methods that can be used by other classes without a need to inherit from it. In other words, a mixin provides methods that implement a certain behavior, but we do not use it alone, we use it to add the behavior to other classes.
A mixin is a class that provides methods to other classes, but it's not considered a base class itself. 00:18 This special class is going to expose some methods that the derived class can utilize—methods that will essentially be mixed in to the derived class.
Sometimes it can help to collect together related features in a single class if they are often used together.
class FooMixin(FollowableMixin, RunnableMixin):
pass
Then when you come to use it you only have one or two direct base classes instead of many.
Obviously you should only do this if it makes sense - it can be abused. Without knowing more about your specific example it is hard to know whether it makes sense to do this or not in your case.
If your User class really has that many characteristics that are appropriate, then you may simply have a complex application. Having five mixins is better than having five functions copied from other places.
Some possibilities for simplifying:
Your User class is trying to do too much. Break it into smaller classes.
Aggregate some of your mixins. For example, you may find there are five classes each of which is Followable and Runnable and Flyable. Makes an intermediate class FollowRunFly that derives from those three mixins, then use FollowRunFly in your five classes.
Perhaps you don't have to slice your mixins so finely. Make one big mixin, and use it on your classes, and let the code determine at runtime whether the object can fly or be followed.
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