pmw menu item's 'command' attribute

Tina Li tina_li23AThotmailDOTcom
Fri Aug 29 11:19:41 EDT 2003


Thanks so much! Both the explanation and the code worked. Basically when a
lambda function is created, any references to global variables are not
evaluated until when it's executed. So if the global var changes value
before that the lambda function follows suit and takes the last value it has
settled at.

Thanks again!

Tina


"Peter Otten" <__peter__ at web.de> wrote in message
news:bilrdk$6j5$04$1 at news.t-online.com...
| "Tina Li" <tina_li23 AT hotmail DOT com> wrote:
|
| > for size in ('tiny', 'small', 'average', 'big', 'huge'):
| >             self.menuBar.addmenuitem('Size', 'command', 'Set size to ' +
| > size,
| >                     command = lambda: cmd.do('change size ' + size),
| >                     label = size)
|
| The variable size in the lambda expression is bound to the global variable
| size, so if you do
|
| size = 42
|
| later, the menu command will even raise an exception.
| Solution:
|
| command=lambda size=size: cmd.do('change size ' + size)
|
| This creates a local variable size that defaults to the same string that
the
| outer size variable is bound to when the lambda expression is created.
| (Don't know if the explanation works, but the code should :-)
|
| Peter
|



-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----==  Over 100,000 Newsgroups - 19 Different Servers! =-----




More information about the Python-list mailing list