How do we share global variables across modules in Python?
Answer Posted / 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 |
Post New Answer View All Answers
What does __ init __ py do in python?
What is pip and when it is used ?
Tell us what is module and package in python?
What is tuple?
Explain me what is unittest in python?
What does map function in python do?
What does .join mean in python?
What is called loop?
Is c++ easier than python?
How can you prevent abnormal termination of a python program?
Why is that none of my threads are not running? How can I make it work?
What is == in python?
What are your weaknesses examples?
What is the use of repr function in python?
Do you need python to run a python program?