Page 1 of 2
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.
Everything is an Object
Is now available as a print book: Amazon
- Hello Python World
- Variables, Objects and Attributes
- The Function Object
Extract - Function Objects
- Scope, Lifetime and Closure
Extract - Local and Global
Extract - Closure ***NEW
- Advanced Functions
Extract - Parameters
Extract - Decorators
- Class Methods and Constructors
Extract - Objects Become Classes
- Inside Class
- Advanced Attributes
Extract - Properties
- Custom Attribute Access
Extract - Custom Attributes
Extract - Default Methods
- Single Inheritance
- Multiple Inheritance
- Class and Type
Extract - Class & Type
- Type Annotation
Extract - Type Annotation
- More Magic - Operator Overloading
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.
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:
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.
You can assign a new tuple to __defaults__.
It is difficult to think of a practical use of this facility but you never know.
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.
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.