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

How to get the ascii value in python?

449


What is chmod 755 in python?

444


What is the method does join() in python belong?

511


Explain the interpretation in python?

460


What are the disadvantages of the python?

473






What does %d do in python?

460


How can you share global variables across modules?

497


If you installed a module with pip but it doesn’t import in your idle, what could it possibly be?

571


Why python is best for data science?

552


How will you check in a string that all characters are digits?

462


How can I find the methods or attributes of an object?

512


How do you create a project in python?

451


Can you write an efficient code to count the number of capital letters in a file?

438


How do you append to a list in python?

424


How to send an email in python language?

489