简单安全的合作方法
cooper的Python项目详细描述
库珀
Making super safe, a cooperative methods library
python的super是编写协作方法的非常有用的工具。 cooperative方法是一种与另一种方法协作的方法 在同一层次结构中重写。一个很好的例子是__init__,如 所有重写都必须按类层次结构升序调用 正确构建对象。
安装
pip install cooper
问题
在线性层次结构中创建协作方法很简单,但是 不是多重继承的情况。问题 是因为下一个要调用的重写在 类定义时间。James Knight rant对super很清楚 阐述问题并提出使用^{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__
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!
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
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
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
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.