Dynamic mixins

class oioioi.base.utils.ObjectWithMixins[source]

Base class for objects which support mixins.

Mixins are nice tools in Python. But they have one drawback – you have to specify new class’ mixins at the point where you declare it. This class solves this problem. Mixins can be now be added on the fly by mix_in() method. This allows for a more flexible modular design.

For example:

# base.py
class UserController(ObjectWithMixins):
    def render_user_info(self, user):
        return "Login: " + user.username

# some_external_module.py
class UserControllerBeautifier(object):
    def render_user_info(self, user):
        super_info = super(UserControllerBeautifier, self)                                 .render_user_info(user)
        return '<font color="red">' + super_info + '</font>'

Mixins can also be specified by providing a mixins class attribute or by passing an additional keyword argument mixins to the constructor.

A class with a mixin behave as if it was replaced with a subclass which bases are the mixin and the original class.

The actual class with the mixins is created when the constructor is called or a subclass defined. Mixing in a new mixin to a class which have instances has an undefined effect on them.

mixins = []

A list of mixins to be automatically mixed in to all instances of the particular class and its subclasses.

allow_too_late_mixins = False

Setting this to True allows adding mixins to the class after it has been instantiated. Existing instances will not have new mixins added.

classmethod mix_in(mixin)[source]

Appends the given mixin to the list of class mixins.

Baseclasses that allow for adding mixins









Admin panel mixins