print statement in lambda expression

Gerson Kurz gerson.kurz at
Sat Feb 2 23:08:49 EST 2002

On Sun, 03 Feb 2002 03:14:53 GMT, "Michael P. Soulier"
<msoulier at mcss.mcmaster.ca_.nospam> wrote:

>    Hello people. 
>    I just recently found that I couldn't put a print statement in a lambda
>expression as a binding in Tk. Could someone explain why? 

The reason was explained already by other people, here is a hack that
makes it work nonetheless: use sys.stdout.write; e.g.

import sys

lambda x,y: return sys.stdout.write("...")

if you need multiple args, make a varargs output function:

def out(*x):
    print " ".join(map(str,x))

f = lambda x,y: out(x,y)

f("Hello", 42)

More information about the Python-list mailing list