In Python, default values are evaluated at import time
by Etienne Millon on January 12, 2016
Tagged as: python.
This is a minimal example reproducing a bug I found in html2text. Suppose we have configuration module, a library that uses the configuration, and a main function.
# config.py
= False
default
# lib.py
import config
def f(x=config.default):
print x
# main.py
import config
import lib
= True
config.default lib.f()
The main function sets the configuration, then calls f
.
One would expect that the program prints True
…
but it actually prints False
.
This behavior can be surprising, but it is perfectly logical once you know the rule:
In Python, default values are evaluated at import time.
This is all there is to know about this problem. Here is what happens at runtime in the above example:
- The main program first imports
config
. The definition ofdefault
is evaluated and its value isFalse
. lib
is imported, and the definition oflib.f
is evaluated. The value of this function includes the default value forx
. So, the definition of this default value,config.default
, is evaluated and it isFalse
.- When the value
True
is assigned toconfig.default
, it is too late: the valueFalse
is already part of the function’s value.
That last part is not only a metaphor, the default value is actually part of the function object:
>>> def f(x=3):
print x
...
...>>> f.func_defaults
3,) (
In order to avoid this caveat, the usual solution is to use None
for default
values:
def f(x=None):
if x is None:
= config.default
x print x
That way, the evaluation of config.default
will happen at runtime, which is
what we want here. The above program will indeed print True
.