Optional and named parameters to Python functions/methods
Archive - Originally posted on "The Horse's Mouth" - 2010-05-15 07:07:06 - Graham Ellis
Do you want to call a function with a differing number of arguments - sometimes simply looking for a function to run with all defaults, and at other times passing in some specific values / variables? Do you have so many different possible parameters that you would like to be able to "I want to give you THIS one and THAT one?" You have all those possibilities open to you when you define a function or method in Python. Let's see an example with as may possibilities as practical included:
So that function requires a minimum of two parameters - values stored in must and also variables. If further positional parameters are given, they're saved into the may and praps variables ... and then any more are saved into a list called others. Finally, any values which are given with a "name = value" format are placed in a dictionary called stuff.
There's a set of worked examples, showing sample calls and sample output too, [here].