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.
Embrace keyword arguments in Python Consider using the * operator to require those arguments be specified as keyword arguments. And remember that you can accept arbitrary keyword arguments to the functions you define and pass arbitrary keyword arguments to the functions you call by using the ** operator.
When you pass an argument by name, you specify the argument's declared name followed by a colon and an equal sign ( := ), followed by the argument value. You can supply named arguments in any order. When you call this procedure, you can supply the arguments by position, by name, or by using a mixture of both.
Python utilizes a system, which is known as “Call by Object Reference” or “Call by assignment”. In the event that you pass arguments like whole numbers, strings or tuples to a function, the passing is like call-by-value because you can not change the value of the immutable objects being passed to the function.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With