Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 255
SacredGeometry Avatar asked Jul 15 '12 19:07

SacredGeometry


People also ask

How do you pass a named parameter in Python?

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.

How do you pass parameters to a name?

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.

Does Python pass objects by reference?

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.


1 Answers

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
like image 140
Ignacio Vazquez-Abrams Avatar answered Sep 26 '22 06:09

Ignacio Vazquez-Abrams