I have a code module.py
that is imported into my main.py
so I can use its functions, which are defined with def
. The imported module only has def
functions.
There are some if
statements in the functions that I'd wish to manipulate automatically from main.py
.
Like for example
main.py
import module as md
BoolIfState=True
for i in range(10):
md.runfunction(i)
module.py
def runfuntion(i):
if BoolIfState:
print i
else:
print i-10
return None
Here the definition of BoolIfState
in main.py
changes how runfunction
from module
behaves. I tried adding global BoolIfState
inside the function but it didn't change the behavior.
Obviously, the actual codes are much more difficult but it's the same basic idea.
Is there a way to do this?
Edit: I am avoiding giving the variable as an input to the function because then it will become cluttered given all the variables that would need to be added. I need to the same thing to approximately 5 variables, and with it, the cleanliness of having functions defined inside the module are lost.
You could package all of the global variables into a singleton class ( singleton class example ).
This way you could maintain one class instance with member variables that could be used/accessed across your codebase.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.