简单安全的合作方法

cooper的Python项目详细描述


PyPI versionBuild StatusCode Coverage

库珀

Making super safe, a cooperative methods library
https://cdn.rawgit.com/arximboldi/cooper/master/doc/gary-cooper.png

python的super是编写协作方法的非常有用的工具。 cooperative方法是一种与另一种方法协作的方法 在同一层次结构中重写。一个很好的例子是__init__,如 所有重写都必须按类层次结构升序调用 正确构建对象。

安装

pip install cooper

问题

在线性层次结构中创建协作方法很简单,但是 不是多重继承的情况。问题 是因为下一个要调用的重写在 类定义时间。James Knight rantsuper很清楚 阐述问题并提出使用^{tt1}的方法$ 始终如一,如果使用的话。

我们相信super非常有用,并且有很多有趣的 当使用水平的 等级制度广泛。这个图书馆试图使这些更安全 可用于大型项目。

示例

fromcooperimport*

合作构造器
classBase(Cooperative):@cooperatedef__init__(self):print"Base.__init__"classDeriv(Base):@cooperatedef__init__(self):print"Deriv.__init__"Deriv()

Output:

Base.__init__
Deriv.__init__

自动参数转发
classBase(Cooperative):@cooperatedef__init__(base_param=None)print"base_param = ",base_paramclassDeriv(Base):@cooperatedef__init__(deriv_param=None)print"deriv_param = ",base_paramBase(deriv_param="Hello",base_param="world!")

Output::

base_param = Hello
deriv_param = World!

其他方法
classEntity(Cooperative):@cooperativedefupdate(self,timer):print"Entity.update"classPlayer(Entity):@cooperatedefupdate(self,timer):print"Player.update"Player().update(0)

Output::

Entity.update
Player.update

抽象方法
classAbstract(Cooperative):@abstractdefmethod(self):passclassConcrete(Abstract):@cooperatedefmethod(self):print"Concrete.method"try:obj=Abstract()exceptTypeError:print"Abstract could not be instantiated".obj=Concrete()obj.method()

Output::

Abstract could not be instantiated
Concrete.method

与标准抽象方法的兼容性
importabcclassAbstract(Cooperative):@abc.abstractmethoddefmethod(self):passAbstract()# Error

合作后
classEntity(Cooperative):@cooperativedefdispose(self):print"Entity.dispose"classConcreteEntity(Entity):@post_cooperatedefdispose(self):print"ConcreteEntity.dispose"ConcreteEntity().dispose()

Output::

ConcreteEntity.dispose
Entity.dispose

修复超类的参数
classTextWidget(Cooperative):@cooperatedef__init__(self,color="black",background="white"):print"color = ",colorprint"background = ",backgroundclassShadedTextWidget(TextWidget):@cooperate_with_params(color="gray")def__init__(self):passShadedTextWidget()

Output::

color = gray
background = white

内部合作
importrandomclassFunnyTextWidget(TextWidget):@inner_cooperatedef__init__(self,next_method):random_color=random.choice(["green","yellow","red"])next_method(color=random_color)

手动合作

classMockEntity(Entity):@manual_cooperatedefupdate(self,timer,**k):super(MockEntity,self).update(**k)self.updated_called=True

许可证

Copyright (c) 2012, 2015 Juan Pedro Bolivar Puente <raskolnikov@gnu.org>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

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

推荐PyPI第三方库


热门话题
bufferedreader JavasocketreadLine()方法奇怪的行为   JavaSpring表单:选择多个默认情况下不选择第一个选项   在Java中调用方法之前检查参数的数量   mysql如何在java上执行算术。sql。时间物体?   安卓版libgdx中的java实现库   web应用中的Java内存管理   java异步任务停止工作安卓   Java Android:确保测验答案彼此不同   java内核32。例子在JNA中找不到ReadProcessMemory   Java生产者/消费者并发性问题尝试读取对象时,NoSuchElementFound异常   java阻止返回或关闭应用程序   java如何测量丢弃的UDP消息的数量?   java是处理时间度量的最佳方法?   启动Eclipse应用程序时java Unset环境变量?   java将自定义验证器添加到Eclipse插件中的特定文件类型   找到java否定响应00000436   java如何从typeElement获取实际类型?   java使用vert处理异步操作。十、   java有人能建议简化代码吗?