Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Library for Role based development
Python Shell
Branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
django_dci Updated tests for Python 2.7
docs updated history
roles Updated context: let it do role binding.
test Acknowledged issue with slots. No fix.
.gitignore Update ignore file. Updated docs
README.txt Updated context: let it do role binding.
license.txt Added BSD license.
wercker.yml Add wercker config file.



Library for Role based development.

Pythonic implementation of the DCI (Data Context Interaction) pattern

The big difference with mixins is that this role is applied only to the subject
instance, not to the subject class (alas, a new class is constructed).

Roles can be assigned and revoked. Multiple roles can be applied to an
instance. Revocation can happen in any particular order.



Using Roles

As a basic example, consider a domain class:

>>> class Person(object):
...     def __init__(self, name):
... = name
>>> person = Person("John")

The instance should participate in a collaboration in which it fulfills a
particular role:

>>> from roles import RoleType
>>> class Carpenter(object):
...     __metaclass__ = RoleType
...     def chop(self):
...          return "chop, chop"

Assign the role to the person:

>>> Carpenter(person)				# doctest: +ELLIPSIS
<Person+Carpenter object at 0x...>
>>> person					# doctest: +ELLIPSIS
<Person+Carpenter object at 0x...>

The person is still a Person:

>>> isinstance(person, Person)

... and can do carpenter things:

>>> person.chop()
'chop, chop'

See ` <>`_ for more examples.


Roles make a lot of sense when used in a context. A classic example is the
money transfer example. Here two accounts are used and an amount of money is
transfered from one account to the other. So, one account playes the role of
source account and the other plays the role of target account.

An example can be found in ` <>`_ and ` <>`_.

Something went wrong with that request. Please try again.