Package: cooper
cooperMaking super safe, a cooperative methods libraryPython’ssuperis a very useful tool to write cooperative methods.
Acooperative methodis one such that cooperates with the other
overrides in the same hierarchy.  A good example is__init__, as
all the overrides must be called in class-hierarchy ascending order to
properly build an object.InstallationpipinstallcooperThe problemMaking cooperative methods in linear hierarchies is simple, but that
is not the case in the presence of multiple inheritance.  The problem
lies on the fact that the next override to be called is not known at
class definition time.James Knight rantagainstsupermakes a very clear
exposition of the problem and proposes a methodology to usesuperconsistently, if used at all.We believe thatsuperis very useful and many interesting and
expressive programming patterns arise when using horizontal
hierarchies extensively.  This library is attempt to make these safer
and usable in large projects.Examplesfromcooperimport*Cooperative constructorsclassBase(Cooperative):@cooperatedef__init__(self):print"Base.__init__"classDeriv(Base):@cooperatedef__init__(self):print"Deriv.__init__"Deriv()Output:Base.__init__
Deriv.__init__Automatic argument forwardingclassBase(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!Other methodsclassEntity(Cooperative):@cooperativedefupdate(self,timer):print"Entity.update"classPlayer(Entity):@cooperatedefupdate(self,timer):print"Player.update"Player().update(0)Output::Entity.update
Player.updateAbstract methodsclassAbstract(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.methodCompatibility with standard abstract methodsimportabcclassAbstract(Cooperative):@abc.abstractmethoddefmethod(self):passAbstract()# ErrorPost-cooperationclassEntity(Cooperative):@cooperativedefdispose(self):print"Entity.dispose"classConcreteEntity(Entity):@post_cooperatedefdispose(self):print"ConcreteEntity.dispose"ConcreteEntity().dispose()Output::ConcreteEntity.dispose
Entity.disposeFix arguments to superclassclassTextWidget(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 = whiteInner cooperationimportrandomclassFunnyTextWidget(TextWidget):@inner_cooperatedef__init__(self,next_method):random_color=random.choice(["green","yellow","red"])next_method(color=random_color)Manual cooperationclassMockEntity(Entity):@manual_cooperatedefupdate(self,timer,**k):super(MockEntity,self).update(**k)self.updated_called=TrueReferencesPython’s super is nifty, but you can’t use itPython’s method resolution orderLicenseCopyright (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.