a little trap revealed (was Re: Let's Talk About Lambda Functions!)

Daniel Fackrell unlearned at learn2think.org
Sun Jul 28 01:59:50 EDT 2002


On Sat, 27 Jul 2002 23:50:01 -0600, Gerald Squelart wrote:

> "Erik Max Francis" <max at alcyone.com> wrote
>> Michele Simionato wrote:
>> > I know a print statement cannot stay in a lambda function, however as
>> > a newbie I ask : why ?
>> Because a lambda is an expression, not a statement.
> Therefore, a lambda *expects* an expression, not a statement.
> 
> However, you could indirectly include a print statement in the course of
> a running lambda expression:
> def printArg(arg):
>     print arg
>     return arg
> l = lambda x: printArg(x)
> a = l(10) # will print 10 and assign 10 to a map(l, range(5)) # will
> print numbers 0 to 4 and return [0, 1, 2, 3, 4] It may be useful to
> debug a lamdba expression.

Correct me if I'm wrong, but doesn't this just create an anonymous
function that calls the named function printArg?  Besides the added
function-call overhead, how are the two following lines functionally
different?

l = lambda x: printArg(x)
l = printArg

Daniel Fackrell



More information about the Python-list mailing list