Is it possible to achieve this in python? Basically, shared_fun() is a very frequently used utility function, used in multiple modules (user_module1 and user_module2). However, each user wants a slightly different parameter SHARE_GLOBAL_VAR.
Some motivation why I would want it this way:
A. Imagine there are a lot of shared_fun(), shared_fun1(), shared_fun2() ...... and all of them rely on the same SHARE_GLOBAL_VAR but do different things with it. Therefore I really don't want to make SHARE_GLOBAL_VAR an argument for every one of those shared_fun(SHARE_GLOBAL_VAR)
B. I want to make the maintenance of shared_fun() easy so that I don't want to copy the code into each user_module.py file individually.
I think the task boils down to make a copy of share module namespace inside each user_module namespace, which I'm not sure if it's legal in python.
#share.py:
SHARE_GLOBAL_VAR = 0
def shared_fun():
return SHARE_GLOBAL_VAR
#user_module1.py:
import share
share.SHARE_GLOBAL_VAR = 1
def user1_fun():
return share.shared_fun()
#user_module2.py:
import share
share.SHARE_GLOBAL_VAR = 2
def user2_fun():
return share.shared_fun()
#main.py:
import user_module1
import user_module2
# expecting a result of 1
print(user_module1.user1_fun())
# expecting a result of 2
print(user_module2.user2_fun())