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 to get the ascii value in python?
What is chmod 755 in python?
What is the method does join() in python belong?
Explain the interpretation in python?
What are the disadvantages of the python?
What does %d do in python?
How can you share global variables across modules?
If you installed a module with pip but it doesn’t import in your idle, what could it possibly be?
Why python is best for data science?
How will you check in a string that all characters are digits?
How can I find the methods or attributes of an object?
How do you create a project in python?
Can you write an efficient code to count the number of capital letters in a file?
How do you append to a list in python?
How to send an email in python language?