Tkinter.Button(... command) lambda and argument problem

Paul Rubin http
Sat Sep 16 03:11:26 EDT 2006


James Stroud <jstroud at mbi.ucla.edu> writes:
> Actually, lambda is not necessary for event binding, but a closure (if
> I have the vocab correct), is: ...
> 
> def make_it(x):
>    def highliter(x=x):
>      print "highlight", x
>    return highliter

For that version you shouldn't need the x=x:

 def make_it(x):
    def highliter():
      print "highlight", x
    return highliter

The reason is each time you call make_it, you're creating a new scope
where x has the correct value, and highliter keeps referring to that
scope even after make_it has returned.



More information about the Python-list mailing list