I have a module where some constants are defined and also used in several functions. How can I over-ride their values from my main file ?
Say this is the module, test_import.py
MY_CONST = 1
def my_func(var = MY_CONST):
print(var)
And this is my main.py
file:
import test_import
MY_CONST = 2
test_import.MY_CONST = 3
test_import.my_func()
This code still prints "1". I want it to print some other value (obviously, without passing a value when calling my_func()
)
The default value is stored with the function itself when the function is defined; it is not looked up in the global scope when the function is called without an argument.
>>> test_import.my_func.__defaults__[0]
1
You can, if you really want to, assign a new default value to be used.
>>> test_import.my_func.__defaults__[0] = 9
>>> test_import.my_func()
9