How to print lambda result ?

Tim Northover T.P.Northover at sms.ed.ac.uk
Tue Jan 20 07:57:25 EST 2009


alex23 <wuwei23 at gmail.com> writes:

> On Jan 20, 10:34 pm, "Barak, Ron" <Ron.Ba... at lsi.com> wrote:

>>>> for num in range(1, 4):
> ...     string_ = "%d event%s" % (num, (lambda num: num > 1 and "s" or
> "")(num))
> ...     print string_

The notation here suggests Ron is sligtly confused about what he
created. It was equivalent to

string_ = "%d event%s" % (num, lambda x: x > 1 and "s" or "")

Notice that there's no actual mention of num there, it's a function that
takes one parameter. If that parameter happens to be num it does what
you want, but there's no way for the interpreter to know what was
intended.

Tim.



More information about the Python-list mailing list