How to create functors?

Robert Dailey rcdailey at gmail.com
Tue Aug 18 22:52:21 CEST 2009


On Aug 18, 3:51 pm, "Jan Kaliszewski" <z... at chopin.edu.pl> wrote:
> 18-08-2009 o 22:32:55 Robert Dailey <rcdai... at gmail.com> wrote:
>
>
>
>
>
> > On Aug 18, 3:31 pm, Duncan Booth <duncan.bo... at invalid.invalid> wrote:
> >> Robert Dailey <rcdai... at gmail.com> wrote:
> >> > Hello,
>
> >> > I want to simply wrap a function up into an object so it can be called
> >> > with no parameters. The parameters that it would otherwise have taken
> >> > are already filled in. Like so:
>
> >> >       print1 = lambda: print( "Foobar" )
> >> >       print1()
>
> >> > However, the above code fails with:
>
> >> >   File "C:\IT\work\distro_test\distribute_radix.py", line 286
> >> >     print1 = lambda: print( "Foobar" )
> >> >                          ^
> >> > SyntaxError: invalid syntax
>
> >> > How can I get this working?
>
> >> def print1():
> >>     print "Foobar"
>
> >> It looks like in your version of Python "print" isn't a function. It  
> >> always
> >> helps if you say the exact version you are using in your question as the
> >> exact answer you need may vary.
>
> > I'm using Python 2.6. And using the legacy syntax in the lambda does
> > not work either.
>
> In Python 1.x/2.x 'print' is a keyword-based statement, not a function
> -- then you cannot use it in lambda (which in Python is limited to
> single expressions, and statements are not allowed in it).
>
> You can try using sys.stdout.write() instead.
>
> > I want to avoid using a def if possible.
>
> But what for? Usualy def is more readable than lambda and it's not worth
> to lose readibility just to save a few keystrokes.
>
> Cheers,
> *j
>
> --
> Jan Kaliszewski (zuo) <z... at chopin.edu.pl>

I posted a bit earlier than you did. See my previous post. Thanks for
the help.



More information about the Python-list mailing list