I'm writing a utility where I would like to have global variables that change the way a function operates. By default I'd like all the functions to follow one style, but in certain cases I'd also like the ability to force the way a function operates.
Say I have a file Script_Defaults.py with
USER_INPUT = True
In another python file I have many functions like this:
from Script_Defaults import USER_INPUT
def DoSomething(var_of_data, user_input = USER_INPUT):
if user_input:
... #some code here that asks the user what to change in var_of_data
.... # goes on to do something
The problem I face here is that the default parameter only loads once when the file starts.
I want to be able to set USER_INPUT
as False
or True
during the run of the program. To get this behaviour I'm currently using it like this...
from Script_Defaults import USER_INPUT
def DoSomething(var_of_data, user_input = None):
if user_input is None:
user_input = USER_INPUT
if user_input:
... #some code here that asks the user what to change in var_of_data
.... # goes on to do something
This seems like a lot of unnecessary code, especially if I have a lot of conditions like USER_INPUT
, and many functions that need them. Is there a better to get this functionality, or is this the only way?