How do we share global variables across modules in Python?



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

Post New Answer

More Python Interview Questions

Tell me what are the built-in type does python provides?

0 Answers  


Tell us which python function will you use to convert a number to a string?

0 Answers  


Explain me inheritance in python with an example?

0 Answers  


How do you split a string in python?

0 Answers  


How do I copy an object in python?

0 Answers  


Explain the use of decorators?

0 Answers  


How you can convert a number to a string?

0 Answers  


Explain classifier.

2 Answers  


When you need ordered container of things, which will be manipulated, use lists?

0 Answers  


What is format () in python?

0 Answers  


How Python performs Compile-time and Run-time code checking?

0 Answers  


How will you verify different flags at once?

0 Answers  


Categories