How do we share global variables across modules in Python?
Answer / chaitanya
We can create a config file & store the entire global variable to be shared across modules or script in it. By simply importing config, the entire global variable defined it will be available for use in other modules.
For example I want a, b & c to share between modules.
config.py :
a=0
b=0
c=0
module1.py:
import config
config.a = 1
config.b =2
config.c=3
print “ a, b & resp. are : “ , config.a, config.b, config.c
------------------------------------------------------------------------
output of module1.py will be
1 2 3
| Is This Answer Correct ? | 0 Yes | 0 No |
How to use the slicing operator in Python?
What is a lambda in programming?
What is the use of try, except, finally and raise in error handling?
How do you invoke the python interpreter for interactive use?
Python list of lists, changes reflected across sublists unexpectedly
Explain the ternary operator in python?
Explain the use of the split function in python?
In python, how do I check if a list is empty?
What is the use of isupper keyword in python?
Can we concat bytes to str?
How do you debug a python program?
What is the output of print str + “test” if str = ‘hello world!’?