Skip to main content

Role based development

Project description

Library for Role based development.

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:
...     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(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'
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.

Project details

Release history Release notifications | RSS feed

This version


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for py-roles, version 0.1
Filename, size File type Python version Upload date Hashes
Filename, size py_roles-0.1-py3-none-any.whl (8.9 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size py_roles-0.1.tar.gz (13.8 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page