How to create functors?

Jan Kaliszewski zuo at chopin.edu.pl
Tue Aug 18 17:01:26 EDT 2009


Dnia 18-08-2009 o 22:42:59 Robert Dailey <rcdailey at gmail.com> napisał(a):

> I see what you're saying now. However, why am I able to use print as a
> function in general-purpose code in my Python 2.6 script, like so:
>
> def SomeFunction():
>    print( "Hello World" )
>
> But, I am not able to do this:
>
> SomeFunction = lambda: print( "Hello World" )
>
> ??????

Because (unless you do 'from __future__ import print_function' in Py2.6)
it's statement, not a functions. In Python you can put any expression
in parentheses -- it's useful for wraping long lines but changes nothing
semantically (unless you add a comma -- then you create a tuple, even
without parentheses, but it'a another story...).

*j

-- 
Jan Kaliszewski (zuo) <zuo at chopin.edu.pl>



More information about the Python-list mailing list