QUOTE(Chadrik @ 04/03/09, 01:30 AM) [snapback]304757[/snapback]
don't feel bad, this is one of the most common misunderstandings when first building UIs in python.
Yes, its a very common mistake. Itsnot only in maya but in general however i find the explanation on the wiki a bit misleading*. Tough technically correct at some fringe. Its not actually going to be the value at the end of the loop. In fact ist not value at all. its a reference to the variable. So it is possible to change what the entire lambda stack does way after the loop has gone. Since thet value inside the for is now local.
so if you do
CODE
def foo(a):
print a
def run():
a=[]
for i in ["a","b","c"]:
tmp=(lambda :foo(i) )
a.append(tmp)
i="z"
for item in a:
item()
run()
Youd get it to print
CODE
z
z
z
Which is a slightly bit disturbing if you know any other language. And not
CODE
a
b
c
or
CODE
c
c
c
So you dont need gui development for thi to pop up just standard python stuff. Just happens a lot in gui stuff
IHMO ive only identified 3 drawbacks in the python language lambdas, and one of them is exactly this. Its a general problem of python as it is impossible to know wheter or not something got passed by value or by reference for certain base types. Which makes you very flabbergasted if you except either or. The second is related to this and its pythons scoping mechanism wich is weird to say the least. And third is no multiline lambdas. Nothing to fret over but causes problesm if you dont know of them.
*it probably suffices as depth but you could get into other troubles with this aswell if you asume ist the last loop that sets it.
PS: yes i am too pretty confident that the callback works right. Theres a easier way tough its a bit of a hack.