Function Definition with Default Parameters
by Christoph Schiessl on Python
A couple of weeks ago or so, I introduced you to Function Definitions with Simple Parameters. Today, we will build on that and explore functions with default parameters. We will again use the official grammar, taken from the official Language Reference, as a guide:
funcdef ::= [decorators] "def" funcname [type_params] "(" [parameter_list] ")"
["->" expression] ":" suite
decorators ::= decorator+
decorator ::= "@" assignment_expression NEWLINE
parameter_list ::= defparameter ("," defparameter)* "," "/" ["," [parameter_list_no_posonly]]
| parameter_list_no_posonly
parameter_list_no_posonly ::= defparameter ("," defparameter)* ["," [parameter_list_starargs]]
| parameter_list_starargs
parameter_list_starargs ::= "*" [parameter] ("," defparameter)* ["," ["**" parameter [","]]]
| "**" parameter [","]
parameter ::= identifier [":" expression]
defparameter ::= parameter ["=" expression]
funcname ::= identifier
If you look at the definition of defparamter
, you can see how default parameters work. You define the parameter as usual but append = expression
to each parameter for which you want to establish a default value. For instance, the following function has two parameters, a
and b
, both of which have a default value:
def foo(a=2, b=3,):
pass
Needless to say, parameters with default values don't have to be provided when the function is called, but you can provide them to overwrite the default. Observe:
Python 3.12.1 (main, Jan 1 2024, 16:22:47) [GCC 13.2.1 20230801] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> def exp(a=2, b=3):
... return a ** b # a to the power of b
...
>>> exp() # use only default values
8
>>> exp(a=3) # overwrite only `a` with keyword param
27
>>> exp(3) # overwrite only `a` with positional param
27
>>> exp(b=4) # overwrite only `b` with keyword param
16
>>> exp(a=3, b=4) # overwrite both with keyword params
81
>>> exp(3, 4) # overwrite both with positional params
81
Restrictions
Even though the grammar does not define it, there are some parameters for which default values are not allowed. The following restriction applies only to parameters the caller can provide as positional parameters ... Namely, parameters without default values must be listed before parameters with default values. If you violate this rule, you immediately get a SyntaxError
:
Python 3.12.1 (main, Jan 1 2024, 16:22:47) [GCC 13.2.1 20230801] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> def foo(a=1, b):
File "<stdin>", line 1
def foo(a=1, b):
^
SyntaxError: parameter without a default follows parameter with a default
Evaluation of Default Values
It is essential to understand that expressions defining the default values are evaluated only once when the function is defined — they are not evaluated when the function is called!
Python 3.12.1 (main, Jan 1 2024, 16:22:47) [GCC 13.2.1 20230801] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> default_value = 123
>>> def foo(a = default_value, b = default_value * 2):
... return (a, b)
...
>>> foo()
(123, 246)
>>> foo(234)
(234, 246)
>>> default_value = 345 # does not affect foo's default values!
>>> foo()
(123, 246)
>>> foo(234)
(234, 246)
You have to be careful, though if the default value is a mutable object:
Python 3.12.1 (main, Jan 1 2024, 16:22:47) [GCC 13.2.1 20230801] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> default_value = [123]
>>> def foo(a = default_value): # `a` defaults to a reference to `default_value`
... return a
...
>>> foo()
[123]
>>> foo([234])
[234]
>>> default_value.append(456) # modifies the list object
>>> foo()
[123, 456]
>>> foo([234])
[234]
>>> default_value = [] # now it's a new list object => doesn't affect any default values
>>> foo()
[123, 456]
The official Language Reference explains it as follows:
Default parameter values are evaluated from left to right when the function definition is executed.
That's everything for today. Thank you for reading, and see you soon!