Programmer's Python - Parameters
Written by Mike James   
Monday, 11 January 2021
Article Index
Programmer's Python - Parameters
Variable Parameters

Functions in Python are very important, even when you forget that they are objects. To make functions easier to use, there are a lot of additional features that have been added as Python developed. Parameters are explained in this extract from my book, Programmer's Python: Everything is an Object.

Programmer's Python
Everything is Data

Is now available as a print book: Amazon


  1. Python – A Lightning Tour
  2. The Basic Data Type – Numbers
       Extract: Bignum
  3. Truthy & Falsey
  4. Dates & Times
  5. Sequences, Lists & Tuples
  6. Strings
  7. Regular Expressions
  8. The Dictionary
  9. Iterables, Sets & Generators
    Extract  Iterables ***NEW!!!
  10. Comprehensions
       Extract  Comprehensions 
  11. Data Structures & Collections
  12. Bits & Bit Manipulation
  13. Bytes
        Extract Bytes And Strings 
  14. Binary Files
  15. Text Files
  16. Creating Custom Data Classes
  17. Python and Native Code
        Extract   Native Code
    Appendix I Python in Visual Studio Code
    Appendix II C Programming Using Visual Studio Code





Default Parameters

The standard way of specifying parameters means you have to supply all of the parameters when you call the function. It is often a simplification to allow some parameters to have default values that are applied if the call doesn’t supply them.

Defaults are specified by assignment to the parameters in the function definition.

For example:

def sum(a=0,b=0):
    return a+b

Now you can call the function as:


which sets both parameters to their default or:


which sets a to 1 and b to its default.

Notice that you can’t set b to a value and have a set to its default.

Defaults are generally set at the end of a parameter list.

You can use expressions to set defaults and these are evaluated just once, in left to right order, when the function is defined and not each time the function is called. This is a small but important point.

So for example:

def sum(a,b=i):
    return a+b

prints 1 followed by 1. The local variable i is zero when the function is defined and any changes to it after the function has been defined are not relevant.

This is reasonable behavior as you would not expect the value of a default to depend on when the function was called.

Notice that even though the default is evaluated only once, if it is a mutable object like a List then it can be modified by the function and be different on each call.

What exactly happens is that the parameter list is evaluated and the defaults are stored in the __defaults__ attribute as a tuple.

For example:

def sum(a=0,b=1):
    return a+b

prints (0,1).

You can assign a new tuple to __defaults__.

For example:


prints 5.

It is difficult to think of a practical use of this facility but you never know.

Keyword Parameters

In addition to positional parameters, functions can be called using keyword parameters.

You don’t have to do anything in the function definition, simply assign the values to the parameter when you call the function.

For example:


you can assign in any order, all that matters is that there is a parameter in the function definition of the same name:


You can mix positional and keyword parameters but it is fairly obvious that positional parameters have to follow positional parameters, but see keyword-only parameters later.

Notice that keyword parameters can have default values.

Last Updated ( Wednesday, 13 January 2021 )