Also you can use functools.partial for this :

from functools import partial

def someDef(theList, someParameter):
    ...

cmds.button(command=partial(someDef, someList, someParameter=2))


On Thu, Jan 8, 2009 at 2:24 AM, Ofer Koren <[email protected]> wrote:

> Another way, somewhat similar to lambdas but without those 'buggy'
> behaviours, is to use a 'callback' object:
> class Callback:
>     def __init__(self,func,*args,**kwargs):
>         self.func = func
>         self.args = args
>         self.kwargs = kwargs
>     def __call__(self,*args, **kwargs):
>         return self.func(*self.args, **self.kwargs)
>
>
> def someDef(theList, someParameter):
>        ....
>
> cmds.button(command = Callback(someDef, someList, someParameter = 2))
>
>
>
> (FYI - Pymel has a more robust version of this object which supports Undo:
>  from pymel import Callback)
>
>
> On Wed, Jan 7, 2009 at 7:44 PM, Matthew Chapman <[email protected]>wrote:
>
>>        This is a common mistake, the way you have written this python will
>> call 'someDef(someList)' and pass its results to named argument 'command'.
>> There are a couple ways to get this to work the way you would like. My
>> personal choice is this
>>
>> # Define a function that can take any named or unnamed arguments
>> # the * tells python to put any unnamed arguments into a list
>> # the ** tell python to put any names arguments into a dictionary
>>
>> def someDef(  *args, **kwargs ):
>>     # call function or have inline code that creates someList
>>     someList = getSomeList()
>>     print someList
>>
>> def buildUI():
>>      # <insert all stuff that defines window>
>>
>>      # create button and pass the function it self to the argument
>>      # command=someDef() # will pass the result where as
>>      # command=someDef   # passes the actual function to the argument
>>
>>>  cmds.button(command=someDef)
>>
>>
>>       If you really want to pass  in data in the call as its being defined
>> you should use a lambda. A lambda is an unamed function. I have heard of
>> lambdas being buggy in certain instances because of how they are defined and
>> stored in memory in modules like pyqt. Here is how you could write is.
>>
>> def buildUI():
>>     # ' ' ' ' ' ' ' ' ' ' '  \/
>>
>>>  cmds.button(command=lambda *args : someDef(someList ))
>>
>>
>> If you new the namespace of you function you could always pass it as a
>> string to 'command'.
>>
>> def buildUI():
>>     # ' ' ' ' ' ' ' ' ' ' '  \/
>>
>>>  cmds.button(command="myModule.someDef([%s] )" % str(someList))
>>
>>
>>
>>
>>
>>
>
>
> --
>
>
> - Ofer
> www.mrbroken.com
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
Yours,
Maya-Python Club Team.
-~----------~----~----~----~------~----~------~--~---

Reply via email to