Draft Pep (was: Re: Let's Talk About Lambda Functions!)

Steve Holden sholden at holdenweb.com
Mon Aug 5 20:24:16 CEST 2002


"Tim Peters" <tim.one at comcast.net> wrote in message
news:mailman.1028567315.17074.python-list at python.org...
> [Bryan Olson, reading a lot into
>
>     def define_twice():
>        def _twice(x):
>            return x + x
>        return _twice
>     print define_twice()(17)
> ]
>
> > Check the rules on local variables.  The function was, at one
> > time, bound to the name _twice.  Nevertheless, the above code
> > passes 17 to a function that is not bound to any name.
>
> Nevertheless, it "has a name" (namely "_twice"), as you can see by doing
>
>     print define_twice().__name__
>
> and that's very helpful in tracebacks:
>
>     print define_twice()({1: 2})
>

Of course, it *does* get a bit more funky when you have to start considering
the addresses as well ...

>>> def build_adder(x):
...     def an_adder(y):
...         return y+x
...     return an_adder
...
>>> a = build_adder(42)
>>> b = build_adder(1000)
>>> a
<function an_adder at 0x101003d0>
>>> b
<function an_adder at 0x10100638>
>>> print build_adder(12)({1:2})
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 3, in an_adder
TypeError: unsupported operand types for +: 'dict' and 'int'

Here we have to consider whether a traceback refers to the 42-adder or the
1000-adder.

Since there doesn't seem to be much point in functions like define_twice()
unless their return values are a little more variable than in this example,
I'd suggest that this feature might not have quite the utility Tim's remarks
suggest.

but-then-apparently-i'm-an-opinionated-bigot-ly y'rs   - steve
-----------------------------------------------------------------------
Steve Holden                                 http://www.holdenweb.com/
Python Web Programming                http://pydish.holdenweb.com/pwp/
-----------------------------------------------------------------------








More information about the Python-list mailing list