anonymous function objects?

Uwe Mayer merkosh at hadiko.de
Thu Apr 28 19:15:30 EDT 2005


Friday 29 April 2005 00:06 am Paul Rubin wrote:
> Closest you can come is:
> 
>    f = lambda: sys.stdout.write("hello world\n")
 
Ah. :))
Why does the "print" statement return a syntax error here?

>>> lambda: print("hallo")
  File "<stdin>", line 1
    lambda: print("hallo")
                ^
SyntaxError: invalid syntax

> Of course if you're trying to capture the function in a named variable
> like f, just use a def statement.

Unfortunately I want to assign a handler function to an object and something
like this does not work:

>>> class Foobar(object): pass
...
>>> a = Foobar()
>>> def a.handler():
  File "<stdin>", line 1
    def a.handler():
         ^
SyntaxError: invalid syntax

But wrapping print into a myprint function works :)


Thanks,
Ciao
Uwe     



More information about the Python-list mailing list