Why doesn't this simple Curry-like implemention work?
Eyal Lotem
eyal at hyperroll.com
Sun Oct 28 17:23:02 EST 2001
Michael R. Head wrote:
> # for some reason this function doesn't work in python 1.5.2.
> def curry(f, p):
> return lambda x: apply(f, p, x)
apply(f, p, x) is in the context of an internal function. Without
nested_scopes (optionally introduced in Python 2.1, and in by default in
Python 2.2), internal functions cannot use containing-function's scopes.
Python 1.5 code:
def curry(f, p):
return lambda x,f=f,p=p: apply(f, p, x)
or in Python 2.1:
from __future__ import nested_scopes
def curry(f, p):
return lambda x: apply(f, p, x)
will work.
I would suggest considering moving to Python 2.1, as nested_scopes seem to
be safer, at least for this case. With the default-argument trick, you
allow more arguments than you really want to allow, which could cause
trouble.
More information about the Python-list
mailing list