Search code examples
pythonfunctionparametersarguments

Python: Passing parameters by name


Hi I was wondering how to implement this in python. Lets say for example you have a function with two parameters and both print out to console

def myFunc(varA, varB):
    print 'varA=', varA
    print 'varB=', varB

I have seen libraries (pymel being the one that comes to mind) where it allows you to specify the parameter you are parsing data too by name in no specific order. for example

myFunc(varB=12, varA = 'Tom')

I am not sure what I am missing as this doesn't seem to work when I try to declare my own functions inside or outside the maya environment.

Any clues would be wonderful, thank you in advanced.


Solution

  • That's normal Python behavior. If you're seeing errors then you're goofing up something else (e.g. missing a required parameter, trying to pass positional arguments by name, etc.).

    >>> def func(foo, bar):
    ...   print foo, bar
    ... 
    >>> func(bar='quux', foo=42)
    42 quux