FactoryObject-Oriented Currying for Python | |
Download |
Factory Ranking & Summary
Advertisement
- License:
- BSD License
- Price:
- FREE
- Publisher Name:
- Peter Fein
- Publisher web site:
- http://code.google.com/u/peter.fein/
Factory Tags
Factory Description
Object-Oriented Currying for Python Factory is an object-oriented approach to partial function application, also known as currying. It is a more powerful implementation of this pattern.Using Factories can: * simplify writing callbacks * reduce bugs in concurrent applications * provide easy lazy evaluationAbout CurryingCurrying creates a new function from an existing one by binding some of the original's arguments:>>> def adder(x, y):... return x + y>>> add_lambda = lambda y: adder(1, y)>>> add_lambda(10)11As of Python 2.5, this pattern is built in with the partial function.>>> add_partial = functools.partial(adder, 1)>>> add_partial(y=10)11FactoriesFactories are better implementation of the currying pattern:>>> from Factory import *>>> add_factory = Factory(adder, x=1)>>> add_factory #doctest: +ELLIPSIS>>> add_factory(y=10)11Unlike lambdas and partial, factories can be inspected and modified:>>> add_factory.x1>>> add_factory.x = 2>>> add_factory(y=10)12The arguments that would be passed to the function can be examined, which is sometimes helpful in debugging:>>> import pprint>>> args, kwargs = add_factory.generateArgs(y=10)>>> pprint.pprint(kwargs){'x': 2, 'y': 10}>>> args[] Here are some key features of "Factory": · safer, as invalid arguments are detected immediately, instead of at call time · intelligent support for classes, instance methods & all other callables · bound arguments can be inspected and modified as attributes · several convenient methods for (re)binding arguments · no "Russian dolls" of nested lambdas Requirements: · Python
Factory Related Software