[Python-Dev] PEP 309 updated slightly
Alex Naanou
alex.nanou at gmail.com
Tue Aug 31 16:51:01 CEST 2004
though this might be a bit late, I would like to suggest something a
bit functionally different:
---cut---
class LCurry(object):
'''
this is the left curry class.
'''
def __new__(cls, func, *args, **kw):
obj = object.__new__(cls)
if isinstance(func, LCurry) or isinstance(func, RCurry):
obj._curry_func = func._curry_func
obj._curry_args = (func._curry_args[0] + args, func._curry_args[1])
obj._curry_kw = kw = kw.copy()
kw.update(func._curry_kw)
else:
obj._curry_func = func
obj._curry_args = (args, ())
obj._curry_kw = kw.copy()
return obj
def __call__(self, *args, **kw):
self._curry_func(*self._curry_args[0] + args +
self._curry_args[1], **dict(self._curry_kw.items() + kw.items()))
--uncut--
this mainly has one thing different from the reference implementation
in the pep:
1) it is recursive
that is we can curry/partial something more than one and yet avoid the
extra function call per curry level...
IMO this is a worthwhile optimisation....
taken from: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/222061
More information about the Python-Dev
mailing list