Main Content

Passing parameters to Python functions - the options you have

Archive - Originally posted on "The Horse's Mouth" - 2011-05-07 08:18:38 - Graham Ellis

Parameters to Python functions / methods are (by default) position dependent. If you def a function with 2 parameters, and call it with two, the first incoming address is taken as being the incoming address saved accessed through the first variable, and the second incoming address is taken as being the incoming address accessed through the second variable.

Thus
  def pax(npacks,njokers)
and
  canasta = pax(2,nj)
will pass the value 2 into npacks, and it will pass nj into njokers. Pedantically speaking, you're passing addresses - so if you were to pass in a list or something else that's muttable (changeable in situ), a change to the object within the function will change the object in the calling code too. Where the value passed in the result of a calculation, it will be saved in a temporary object so changes won't effect anything in the calling code, and where it's a constant passed in, that's going to be immutable so a fresh object at a new address will be created if you should change it - thus not altering the constant value in the caling code.

Where you are calling an object method rather than a function in Python, you'll have the object on which the method is called picked up as the first parameter, so the function declaration will appear to have one more parameter than the call (it hasn't really - the first parameter in the calling syntax is outside the brackets:
  def morepeeps(this,that):
and
  larger = bertie.morepeeps(thomas)

You can specify default values for parameters too - by giving an assignment of a default value in the declaration:
  def pax(npacks=gdv(),njokers=0):
so now pax called with 2 parameters sets npacks and njokers to values in the call, with one parameter sets npacks to that value and sets njokers to 0, and without parameters sets njokers to 0 and npacks to the result of running the gdv function.

You are also allowed to call by name to fill specific parameters:
  crackle = pax(njokers=2)
There are examples of calls as I've described so far in a complete demo program [here].

Definitions may include as their very last parameter a parameter who's name is preceeeded by ** - and that's a dictionary that mops up all remaining named parameters that the function has been called with, and as their last parameter (except for any ** one) they may be given the name of a list that will pick up any other unnamed parameters. So you may end up with a call like:
  demofunc(values,34,65,44,43,34,22,ice="solid",water="liquid")
being collected into a function defined like
  def demofunc(must, may=15, *others, **named):
Full sample - [here].

Finally, note that a function may return a comma separated tuple of values which you may pick up in your calling code into multiple variables - flexible stuff!
  tom, dick = demofunc(values)
may be used to call a function with a return line like
  return may/2, may+2