[IronPython] lambda form

Jim Hugunin jimhug at exchange.microsoft.com
Sat May 7 20:06:51 CEST 2005

March Liu wrote:
> lambda key word may remove from CPython 3.0. Maybe we can replace it
> by the other way as List Comprehensions, funcation Factory...

I like to encourage people who come from a functional background to use
list comprehensions instead of lambda when possible.  I'm not sure that
lambda should go away, but I do wonder if map, filter and reduce
shouldn't be removed from builtins to more strongly encourage the use of
list comprehensions or generator expressions instead.

> 2005/5/7, Travis Watkins <alleykat at gmail.com>:
> > On 5/7/05, PhiHo Hoang <phiho.hoang at rogers.com> wrote:
> > > >>> def make_inc(n):
> > > ...     return lambda x : x + n
> > > ...
> > > >>> f = make_inc(100)
> > > >>> f(1)
> > > IronPython.Objects.PythonNameError: name 'n' is not defined
> > That works on CPython so I'd say it's a bug.

As Travis says, the right presumption is that this is a bug especially
since this works in recent versions of CPython.  IronPython is behaving
like CPython 2.1 and earlier by not supporting nested scopes.  Here's
this same example on CPython-2.0.  Notice that it behaves just like

Python 2.0.1 (#18, Jun 22 2001, 02:26:03) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
>>> def make_inc(n):
...     return lambda x: x + n
>>> f = make_inc(100)
>>> f(1)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 2, in <lambda>
NameError: There is no variable named 'n'

Today, IronPython is trying to be somewhere between Python 2.3 and 2.4
in features.  The goal for the 1.0 release is to be fully compatible
with Python 2.4.  Therefore, even though this behavior does match an
earlier version of CPython, it is a bug since it doesn't match either of
our two target versions.

This will take some work to fix so it might be a few releases before we
get nested scopes properly working in IronPython.

Thanks - Jim 

More information about the Ironpython-users mailing list