How do I share global variables across modules - python

+1 vote
How do I share global variables across modules in python?
Jul 24, 2019 in Python by Dipti
23,593 views

3 answers to this question.

0 votes

The best way to share global variables across modules across a single program is to create a config module. Just import the config module in all modules of your application; the module then becomes available as a global name.

config.py

x = 0

mod.py:

import config
config.x = 1

main.py:

import config
import mod
print(config.x)
answered Jul 24, 2019 by Faiza
0 votes

Global variables can be used by everyone, both inside of functions and outside.

  1. Create a variable outside of a function, and use it inside the function. ...
  2. Create a variable inside a function, with the same name as the global variable. ...
  3. If you use the global keyword, the variable belongs to the global scope:

.

answered Dec 15, 2020 by Gitika
• 65,870 points
0 votes

The canonical way to share information across modules within a single program is to create a special module (often called config or cfg). Just import the config module in all modules of your application; the module then becomes available as a global name. Because there is only one instance of each module, any changes made to the module object get reflected everywhere. For example:

config.py:

x = 0   # Default value of the 'x' configuration setting

mod.py:

import config
config.x = 1

main.py:

import config
import mod
print(config.x)

Note that using a module is also the basis for implementing the Singleton design pattern, for the same reason.

  1. :

.

answered Dec 15, 2020 by Roshni
• 10,420 points

Related Questions In Python

0 votes
2 answers

How do I copy a file in python?

copy a file in python  from shutil ...READ MORE

answered Mar 27, 2019 in Python by rajesh
• 1,230 points
225 views
0 votes
1 answer

How can I get a list of locally installed Python modules?

Solution My 50 cents for getting a pip freeze-like ...READ MORE

answered May 23, 2018 in Python by charlie_brown
• 7,780 points
450 views
0 votes
1 answer

How do you get the logical xor of two variables in Python?

If you're already normalizing the inputs to ...READ MORE

answered May 29, 2018 in Python by aryya
• 7,420 points
9,573 views
0 votes
1 answer

How do I copy a file in python?

Use the shutil module. copyfile(src, dst) Copy the contents ...READ MORE

answered Jul 31, 2018 in Python by Priyaj
• 58,100 points
150 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,840 views
0 votes
0 answers
+4 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,810 points
1,418 views
0 votes
2 answers

In Python, how do I read a file line-by-line into a list?

readline function help to  read line in ...READ MORE

answered Jun 21, 2020 in Python by sahil
• 540 points
392 views
0 votes
1 answer

How do I convert a Python program to a runnable .exe Windows program?

Understand that every 'freezing' application for Python ...READ MORE

answered Oct 12, 2018 in Python by SDeb
• 13,250 points
409 views