Marcus, partial() is a formal version of a closure (lambda) so if you recommend a lambda, then a partial() is a safer version of this. It does a proper closure (lambda has the potential to have scoping issues if not careful).
I am not here to judge whether Gabriele is more capable of comprehending classes vs closures. I only presented options, and am answering questions to the best of my abilities when asked. Gabriele, please disregard Marcus's interjections. functools.partial() is not a 'workaround'. It is a tool for performing a closure. It is like you said, a 'wrapper' that says "I will give you back a new callable that will automatically use these default arguments and keywords that you have given me. You can have a function called foo(x). foo() requires a single argument 'x'. If I write: wrapped = functools.partial(foo, x) , then 'wrapped' is a new callable that is a 'closure' of foo() over x. When I call wrapped(), it is really calling foo(x) for me with the baked in x value. Honestly though, I can't see classes being any more or less complex than closures so they might as well be met on the same level. A class has methods, and methods are functions that are 'baked' with an implicit first argument that is the current instance at that moment. Yes, you seem to have explained your understanding of the callable just fine. The only correction I would make is where you are describing what "command" is in this context. "command" is a parameter to a function. Namely, the button function that accepts a callable as a parameter. Within this function it will receive the callable, passed through the parameter "command". Within the scope of that function (which is completely transparent to you), it would be working with a local variable "command" to save your callback, and use it later when the button is clicked. On Fri, Jul 18, 2014 at 11:31 PM, Marcus Ottosson <[email protected]> wrote: > Justin, I honestly wouldn't introduce Python to someone just starting out > by going through functools. In his case, classes is the more > straightforward approach and it encourages better design; partial is in my > opinion more a workaround than a solution, probably best used as a last > resort. Using lambda would save him from importing an additional package > and give him less of a learning curve, but again, both of those are > workarounds imo. > > > On 18 July 2014 12:14, Gabriele Bartoli <[email protected]> wrote: > >> Ok, let me see if I'm getting this straight: >> >> if I want to assign the returned value of a function to a variable, I do >> >> a = f() >> >> which means that the function is executed, does what it has to do and >> returns a value which is assigned to the defined a variable. >> >> On the other hand, when working with the command flag like this >> >> command = f >> >> I'm not asking for the function to be executed when the code is compiled, >> but to assign the target of the variable 'f' to the variable 'command'. >> When the button is clicked, that is when the function is executed. >> >> So using partial like this >> >> command = partial(f, val) >> >> I'm wrapping two values together, telling command to target the wrapper >> and, when the button is clicked, the wrapper is unwrapped and the function >> executed with 'val' as its argument. >> >> Did I get this right? :P >> >> >> >> On Friday, 18 July 2014 12:06:37 UTC+2, Justin Israel wrote: >> >>> It is the difference between calling a callable object to get its return >>> value, vs passing a reference to the object. >>> You call a function by doing: aFunc() >>> But aFunc is an object which can be passed around. So if you pass aFunc >>> to something else, then that target has a reference to the same callable. >>> This is how "callbacks" work. You don't want to execute the function at >>> that moment in time. You just want to pass a reference to it, so that at a >>> later point in time the function can be called by a different owner. >>> >>> For more detailed information, you can look at the __call__() special >>> method of objects: >>> https://docs.python.org/2/reference/datamodel.html#object.__call__ >>> >>> This is what defines the behavior for when you do aFunc() on the object >>> aFunc >>> >>> Consider this: >>> >>> def f(): >>> print "hello" >>> >>> f()# hello >>> f.__call__()# hello >>> >>> >>> >>> >>> >>> On Fri, Jul 18, 2014 at 9:51 PM, Gabriele Bartoli <[email protected]> >>> wrote: >>> >>>> Thanks man, I appreciate the feedback. >>>> >>>> Needless to say that I'm not working only on a test script, but I have >>>> something else up my sleeve. Would you like to give it a try when I am done >>>> with it? >>>> >>>> Cheers >>>> >>>> P.S. I'm still trying to understand why i have to remove the empty >>>> parentheses when calling the callable function. I've been looking around >>>> the internet but I can't find an answer. :( >>>> >>>> To make it clearer, why >>>> >>>> command = my_function >>>> >>>> >>>> instead of >>>> >>>> command = my_function() >>>> >>>> I think I am missing something syntax-wise. >>>> >>>> >>>> On Thursday, 17 July 2014 21:57:17 UTC+2, Justin Israel wrote: >>>> >>>>> Definitely not the first one. Avoid relying on globals when possible. >>>>> The second and third are equally fine right now. But if you start >>>>> needing more state such as storing more widgets or values, then the class >>>>> will end up becoming the better choice. >>>>> >>>>>> >>>>>> -- >>>> 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/630eee2f-b4db-4c0c-a164- >>>> 2bf33bfa7ad7%40googlegroups.com >>>> <https://groups.google.com/d/msgid/python_inside_maya/630eee2f-b4db-4c0c-a164-2bf33bfa7ad7%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/9fcb19aa-8bc9-44b4-bec3-bfedb4694469%40googlegroups.com >> <https://groups.google.com/d/msgid/python_inside_maya/9fcb19aa-8bc9-44b4-bec3-bfedb4694469%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> >> For more options, visit https://groups.google.com/d/optout. >> > > > > -- > *Marcus Ottosson* > [email protected] > > -- > 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/CAFRtmOAhbVTnKaZ_QOEVV%3Dgj6bG0akV401TUj1amWHCBc46vYg%40mail.gmail.com > <https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOAhbVTnKaZ_QOEVV%3Dgj6bG0akV401TUj1amWHCBc46vYg%40mail.gmail.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/CAPGFgA1c_m0z1va4Wa5qFMQiCtzu3mqNHQ2SJeDf2Zbhp-ROQg%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
