Hi Yury,

This is happening because the Python lambda does not create a real closure.
It only captures the scope, and then does lazy evaluation of the variables.
What that means is, if you are attempting to create a closure over
variables in a loop, what you get is a capture of the loop scope, and then
when the lambda actually evaluates, the loop variable is then evaluated.
The last value of the loop variable is 4, so all of your callbacks end up
using the value 4.

>From a general standpoint (not applicable to your case) you can fix lambda
usage by making it take an argument:

fn = lambda i: blah(i)
fn(i)

This means the lambda has to be passed the argument instead of capturing
one from the scope. But in your case, you can't use that.

You can fix your lambda, by using the known "double lambda" trick:

c=(lambda i: lambda x:blah(10*i))(i)

This creates a lambda that takes an arg, and then returns a lambda that
contains a closure over the outer lambda function. Then we call it with our
counter variable, so it gets properly captured. This, as you can tell, is
ugly. So best just use "functools.partial":

c=functools.partial(blah, i)

But you will find here, that because maya always wants to pass other
arguments to the callback, you would have to modify blah() to accept extra
args. So you can use a mixture of partial() and lambda:

c=functools.partial(lambda i, *args: blah(i) , i)


Justin



On Tue, Nov 10, 2015 at 1:53 PM ynedelin <[email protected]> wrote:

> hey guys
> I have this little code.
>
>
>
>
>
>
>
>
>
>
> *import maya.cmds as mcdef blah(value):    print "I was given:",
> valuemc.window(w=500, h=500)mc.columnLayout()for i in range(0, 5):
> mc.button(l="Touch!"+str(i), c=lambda x:blah(10*i))mc.showWindow()*
>
> if you run this it will create a window with 5 buttons and they all will
> have unique names, but they all will pass the same value to blah function,
> the last value of the loop.
>
> why is that and how to fix it?
>
> what I want to see is each button returns 10* by index it was created in.
>
> Thanks
>
> Yury
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Python Programming for Autodesk Maya" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/python_inside_maya/9e7b0efc-3d79-4719-bbb6-243a33d9b0c6%40googlegroups.com
> <https://groups.google.com/d/msgid/python_inside_maya/9e7b0efc-3d79-4719-bbb6-243a33d9b0c6%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA38ck7JG5AeJbvemDbe0Q9d09jdz8v%3DYxJ4Q5fci_hvSA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to