How to print lambda result ?

D'Arcy J.M. Cain darcy at druid.net
Tue Jan 20 15:26:14 CET 2009


On Tue, 20 Jan 2009 12:34:04 +0000
"Barak, Ron" <Ron.Barak at lsi.com> wrote:
> Thanks Tino: your solutions without the lambda work nicely.
> What I still don't understand is why the print does not execute the lambda and prints the result, instead of printing the lambda's object description.

Because that's what you told it.  Consider the even simpler example...

 "%s" % lambda num: int(num)

This is equivalent to...

 "%s" % int

IOW, you are giving the function (anonymous in your case), not the
result of calling it.  Your code prints a representation of exactly
what you gave it, an anonymous function that would return a string if
you called it with a number.

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

-- 
D'Arcy J.M. Cain <darcy at druid.net>         |  Democracy is three wolves
http://www.druid.net/darcy/                |  and a sheep voting on
+1 416 425 1212     (DoD#0082)    (eNTP)   |  what's for dinner.



More information about the Python-list mailing list