简单安全的合作方法

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第三方库


热门话题
java错误:无法解析方法   如何用相同的父标记和子标记在Java中解析XML?   日期使用Java中的时区偏移将本地时间转换为UTC   java如何在多进程、多线程环境中读取文件   WebView中的java弹出式对话框不在当前显示范围内,但在chrome浏览器中运行良好   只有2个参数的java递归二进制搜索方法   无法在java中调用函数   java JavaMail在Tomcat服务器上运行时停止工作   反射通过java程序生成、编译和运行java类   java Android:如何使ListView即使在应用程序关闭后仍保持禁用状态   在JAVA中识别匿名类实例   java渲染一个由三角形组成的立方体,在旋转时会产生奇怪的角度   函数式编程如何基于比较连续的列表元素将Java流减少为布尔值   java如何替换列表中的多个项目?   java Android如何获取随机sqlite数据?   java我已经将useSSL设置为false,但仍然收到警告   java使用动态变量生成jlabel   apachespark:java。lang.NoClassDefFoundError v2TableWithV1回退