Default arguments to functions

Default argument values are assigned when the function is defined. Therefore, if a function is defined with an argument defaulting to the value of some variable, subsequently changing that variable will not change the default:

>>> default_units = 'm'
>>> def report_length(value, units=default_units):
...     return 'The length is {:.2f} {}'.format(value, units)
...
>>> report_length(10.1)
'The length is 10.10 m'
>>> default_units = 'cubits'
>>> report_length(10.1)
'The length is 10.10 m'

The default units used by the function report_length are unchanged by the reassignment of the variable name default_units: the default value is set to the string object referred to by default_units when the def statement is encountered by the Python compiler ('m') and cannot be changed subsequently.