[Python-Dev] Let's just *keep* lambda
Josiah Carlson
jcarlson at uci.edu
Thu Feb 9 05:51:33 CET 2006
Jiwon Seo <seojiwon at gmail.com> wrote:
> On 2/8/06, Josiah Carlson <jcarlson at uci.edu> wrote:
> > Closures already exist in Python.
> >
> > >>> def foo(bar):
> > ... return lambda: bar + 1
> > ...
> > >>> a = foo(5)
> > >>> a()
> > 6
>
> Not in that we don't have anonymous function (or closure) with
> multiple statements.
As already said, lambdas (Python's anonymous functions) are limited to a
single expression. If you can't do what you want with a single
expression, then it probably SHOULD have a name, so you should use a
standard function definition.
> Also, current limited closure does not capture
> programming context - or variables.
You should clarify yourself. According to my experience, you can do
anything you want with Python closures, it just may take more work than
you are used to.
def environment():
env = {}
def get_variable(name):
return env[name]
def set_variable(name, value):
env[name] = value
def del_variable(name):
del env[name]
return get_variable, set_variable, del_variable
- Josiah
More information about the Python-Dev
mailing list