Securing a future for anonymous functions in Python

Jacek Generowicz jacek.generowicz at cern.ch
Fri Jan 7 11:38:56 EST 2005


Nick Coghlan <ncoghlan at iinet.net.au> writes:

> Jacek Generowicz wrote:
> > [*] Funnily enough, getting them to understand that "lambda x: fn(x)"
> >     is just a very silly way of writing "fn", can be quite a struggle
> >     at times ... but that's probably a consequence of the context in
> >     which lambda is introduced.
> 
> If you genuinely taught them that, you may have done them a disservice:

Yes, I was wondering whether I should add lots of caveats to the above.

> Py> def f(x):
> ...    print x
> ...
> Py> f1 = f
> Py> f2 = lambda x: f(x)
> Py> f1("hi")
> hi
> Py> f2("hi")
> hi
> Py> def f(x):
> ...   print x * 2
> ...
> Py> f1("hi")
> hi
> Py> f2("hi")
> hihi

There are far less contrived situations in which my original statement
(taken at face value) is wrong. Functions with optional arguments, for
example.

What actually happens is that some of them end up writing:

    lambda x: int(x)

in a situation where they want to specify something which will perform
conversions of the type:

    "123" -> 123   

ie, string to integer conversions.

Usually one or two have trouble grasping that "int" would be perfectly
adequate in this situation.



More information about the Python-list mailing list