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
How is print statement represented in python 3 (v/s python2)?
How does break work in python?
Can we use break in if statement in python?
Does python have a switch-case statement?
Does every class need a constructor?
What is a pass in python?
What is constructor in python?
What does it mean if something is static?
How will you get a titlecased version of string?
How many variables are in python?
Is elif a keyword in python?
How will you uit the python?
How would you check if MySQLdb is installed?
What are python modules? Name some commonly used built-in modules in python?
Define flask in python?