基于角色的发展

py-roles的Python项目详细描述


基于角色开发的库。

与MIXIN的最大区别是,这个角色只应用于主题实例,而不应用于主题类(唉,构建一个新的类)。

可以分配和撤消角色。一个实例可以应用多个角色。撤销可以按任何特定顺序发生。

使用角色 作为一个基本示例,考虑一个域类:

>>> class Person:
...     def __init__(self, name):
...         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)
True
… and can do carpenter things:
>>> person.chop()
'chop, chop'
Context
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.

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何创建Restful服务并将其部署到OSGi容器?   java如何获取用户在EditText中输入的时间并从中扣除5小时30分钟?   java用户无法注销firebase 安卓   java Undertow始终将字符集添加到ContentType头,即使它不是文本MIME类型   java LocalBroadcastManager未正确接收消息   foreach在Java中有没有比For循环更好的方法可以灵活地遍历集合?   java如何在安卓上移动球   在OSGi下的Log4j中使所有记录器异步时,java ClassNotFoundException   java未知输入字节长度转换为int   java测试工厂在使用前缀命名时抛出NPE   对象的副本(JAVA)   java SP20310:无法打开文件。sql   java Spring安全性仅为一条路径添加安全性   java在idea中获取菱形运算符的编译错误