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

What is means by string python?

0 Answers  


How does Python’s list.sort work at a high level? Is it stable? What’s the runtime?

0 Answers  


How do I know my python version?

0 Answers  


Consider multiple inheritances here. Suppose class c inherits from classes a and b as class c(a,b). Classes a and b both have their own versions of method func(). If we call func() from an object of class c, which version gets invoked?

0 Answers  


Narrate the difference between python arrays and lists.

0 Answers  


Describe about the libraries of python?

0 Answers  


Is there a tool to help find bugs or perform static analysis?

0 Answers  


What are Python modules? Name a few Python built-in modules that are often used.

3 Answers  


Can a constructor be final?

0 Answers  


Give one example for multiple statements in single statement?

0 Answers  


Is python a high level language?

0 Answers  


What is python coded in?

0 Answers  


Categories