Skip to main content
# do mixin for python

This project wants to implement a mixin library like ruby’s module.

## What mixin is In object-oriented programming languages, a mixin is a class which contains a combination of methods from other classes. How such a combination is done depends on the language, but it is not by inheritance. If a combination contains all methods of combined classes it is equivalent to multiple inheritance. For more information, please refer to this wiki: http://en.wikipedia.org/wiki/Mixin

## install pymixin

install it from pip:

pip install mixin

In fact, the pymix package only has one file, it is mixin.py, so you can also copy it directly to your project directory.

## how to use pymixin

### a simple example

Import the Mixin class and mixin decorator from the mixin module:

>>> from mixin import Mixin, mixin

The user’s mixin class should always inherit from the Mixin class:

>>> class MixinA(Mixin):
...     def func_mixin_a(self):
...             return 'do_func_mixin_a'
...

and then use the mixin decorator to add the mixin to a class:

>>> @mixin(MixinA)
... class A(object): pass
...
>>> a = A()
>>> a.func_mixin_a()
'do_func_mixin_a'

### add a mixin to another mixin

The mixin should not be inherited by another class:

>>> class MixinA(Mixin): pass
...
>>> class A(MixinA): pass
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "mixin.py", line 58, in __new__
    raise InheritMixinError(clsname)
__main__.InheritMixinError: A

But you can add a mixin to another mixin:

>>> class MixinA(Mixin):
...     def func_mixin_a(self):
...             return 'do_func_mixin_a'
...
>>> @mixin(MixinA)
... class MixinB(Mixin):
...     def func_mixin_b(self):
...             return 'do_func_mixin_b'
...

Then add the MixinB to a normal class

>>> @mixin(MixinB)
... class M(object): pass
...
>>> m = M()
>>> m.func_mixin_a()
'do_func_mixin_a'
>>> m.func_mixin_b()
'do_func_mixin_b'

### a mixin class can not be instantiated

>>> class MixinA(Mixin): pass
...
>>> a = MixinA()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "mixin.py", line 44, in mixin_new
    raise InstantiationMixinError(cls)
__main__.InstantiationMixinError: <class '__main__.MixinA'>

### inherit multi mixin

Two kind of methods to inherit multi mixins, one is writing multi mixins in one decorator:

>>> class MixinA(Mixin): pass
...
>>> class MixinB(Mixin): pass
...
>>> @mixin(MixinA, MixinB)
... class M(object): pass
...

Another is using multi decorators:

>>> @mixin(MixinA)
... @mixin(MixinB)
... class M(object): pass
...

### inherit priority

When writes multi mixins to one decorator, the left mixin has a higher priority

>>> class MixinA(Mixin):
...     def mixin_func(self):
...             return 'do_mixin_a'
...
>>> class MixinB(Mixin):
...     def mixin_func(self):
...             return 'do_mixin_b'
...
>>> @mixin(MixinA, MixinB)
... class M(object): pass
...
>>> m = M()
>>> m.mixin_func()
'do_mixin_a'

When writes multi mixins in multi decorators, the up mixin has a higher priority:

>>> @mixin(MixinA)
... @mixin(MixinB)
... class M(object): pass
...
>>> m = M()
>>> m.mixin_func()
'do_mixin_a'

If the class has a funciton which has the same name as the mixin, the mixin method will be overwrite:

>>> class MixinA(Mixin):
...     def mixin_func(self):
...             return 'do_mixin_a'
...
>>> @mixin(MixinA)
... class A(object):
...     def mixin_func(self):
...             return 'real_class_mixin_a'
...
>>> a = A()
>>> a.mixin_func()
'real_class_mixin_a'

If a class has a father class, and the father class has a same name method as the mixin, the father class’ method will be overwrite:

>>> class MixinA(Mixin):
...     def mixin_func(self):
...             return 'do_mixin_a'
...
>>> class Father(object):
...     def mixin_func(self):
...             return 'father_mixin'
...
>>> @mixin(MixinA)
... class A(Father): pass
...
>>> a=A()
>>> a.mixin_func()
'do_mixin_a'

## python version support

Tested on python2.6, python2.7 and python3.4

Metadata

Release files for mixin 1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mixin 1.1
File Size Uploaded Provenance Actions
mixin-1.1.tar.gz 3.0 kB Without provenance Details for mixin-1.1.tar.gz

Release files / mixin-1.1.tar.gz

Download URL mixin-1.1.tar.gz
Size 3.0 kB
Tags Source
SHA-256 checksum
How to use checksums
8bb037ca43911c93a8cfa5c8741aa8237c555c6fbaf3d367ffd695a33e842941
BLAKE2b-256 checksum
How to use checksums
7af67e59ac066a9f1d9c98a1e16f4f54986a224680353f91a78ea3965513042e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for mixin 1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.1 This release

1 release file No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page