I have a function that gets the user's preferred directory for use in many other functions:
def get_pref():
q1 = "What is your pref. dir.?"
path = input(q1)
...
return path
Then, the path is used to specify locations in numerous functions (only 2 are shown here.)
Option A:
def do_stuff1():
thing = path + file_name # using global path variable
...
def do_stuff2():
thing = path + file_name # using global path variable
...
path = get_pref()
do_stuff1()
do_stuff2()
Option B:
def do_stuff1(path_):
thing = path_ + file_name # accepts path as argument
...
def do_stuff2(path_):
thing = path_ + file_name # accepts path as argument
...
path = get_pref()
do_stuff1(path)
do_stuff2(path)
Option A accesses the global variable path
in each function. Option B accepts the path as an argument in each function.
B seems to be repetitive since the same variable is passed each time, but I know globals are strongly discouraged in Python. Would it be acceptable to declare the path as a global constant, or is there a more conventional way?