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


Please Help Members By Posting Answers For Below Questions

What are python packages?

465


What are the supported data types in python?

514


What is *kwargs and **kwargs?

506


Is instagram written in python?

420


What is a namedtuple?

527






How to count the occurrences of a particular element in the list?

474


What are the possible ways to load an array from a text data file in python? How can the efficiency of the code to load data file be improved?

517


What is the difference between locals() and globals ()?

421


Explain the uses of python?

436


What are the different types of operators in python?

430


What is break in python?

425


Difference between mutable and immutable in python?

489


Is python good for coding?

474


What is a python method?

447


What are the different types of reuest in python?

475