0

Of course there should be no global variables - vars should be passed with function calls. My actual implementation needs lots of variables which are used in different "xxx.py" modules. So I tried to make an own class including these variables and put it in an own module named e.g. "vars.py":

class GlobalVariables():
    def __init__():
        self.a = 42
        self.e = 2.71828
        self.c = ["good","music"]
# ---> should I place:
gv = GlobalVariables()     # (1)here

The main program would look like for this case (1)

from vars.py import gv
def main():
    gv.a = 73
    ...

and in case (2)

from vars.py include GlobalVariables
gv = GlobalVariables()     # (2)in the main program

def main():
    gv.b = 2.71828
    ...

I prefer the first method, since gv is used in many separate modules. Defining gv in each module separately there are certainly different gv variables in each module.

Reading lots of instructions and posts about classes and global variables I got confused which way is applicable - or is there a completely different approach to this problem?

5
  • 2
    there are certainly different gv variables in each module. It is not clear why you need globals if they are different in each module. Perhaps you could explain more about the actual problem you are trying to solve. Commented Oct 7, 2024 at 8:12
  • 1
    Instead of a class you could assign literal names for your variables e.g., "a", "e", "c" then just use a dictionary. Also, the assignments in your __init__ function are pointless as they are local to that function and will not persist once a class instance has been constructed. Don't forget that __init__ requires at least one argument - typically self Commented Oct 7, 2024 at 8:22
  • @SIGHUP: thanks - of course; edited to self.___ . A dictionary is an interesting approach. Commented Oct 7, 2024 at 8:34
  • @OldBoy: this is part of my question; of course I need the same values in all modules; hoping method 1 does this. Commented Oct 7, 2024 at 8:37
  • option 1 is just using mutable global state with an extra step (basically the singleton pattern) Commented Oct 7, 2024 at 11:32

1 Answer 1

1

Which method to use would depend on how you plan to use these variables.

  • If you would like to change them and have that change affect all files, then initializing or creating an instance once in var.py or using a dictionary would be fine. If you don't plan to change the values of the variables, this method would be easier.
  • If, however you would like to use the variables but change them differently in each file, you should consider initializing it individually.
  1. Changing values between files or keeping same values (your option 1):
    vars.py:
#Using a class
class GlobalVariables():
    def __init__():
        self.a = 42
        self.e = 2.71828
        self.c = ["good","music"]

gv = GlobalVariables()     # (1)here

################################################

#Using a dictionary
globalVariable = { 
    'a' : 42,
    'e' : 2.71828,
    'c' : ["good", "music"],
}

Because your files all import from the same source and the variable locations are the same, if you try importing code from different files, changes made in one file will remain true for any other files.
Example:
test.py:

from vars import *
print(gv.a)
gv.a += 1
print(gv.a)
from test2 import *

test2.py:

from vars import *
print(gv.a)
gv.a += 1
print(gv.a)

Output:

>>> 42
>>> 43
>>> 43
>>> 44
  1. Initializing individually and changing differently (your option 2):
    vars.py:
class GlobalVariables(): #Using a class but creating instance individually
    def __init__():
        self.a = 42
        self.e = 2.71828
        self.c = ["good","music"]

Because you are creating a new instance in every file and each one has its own attributes, any changes made in one file will not affect other files.
Example:
test.py:

from vars import * #import code from vars.py
gv = GlobalVariables() #create instance
print(gv.a)
gv.a += 1
print(gv.a)
from test2 import * #import code from test2.py

test2.py:

from vars import *
gv = GlobalVariables() #initialize instance
print(gv.a)
gv.a += 3
print(gv.a)

Output:

>>> 42
>>> 43
>>> 42
>>> 45

All in all, there is almost never one right answer. It all depends on what you're trying to accomplish.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot for your detailed description and your patience. Now, as I understood it, it is hard to believe that it was not clear to me before. The data in question is a list of dictionaries containing properties of graphical elements. len(list) is typically > 20 Million items. Additionally there are lots of settings which are used from different functions in several modules. Dictionaries are a good idea for these. 73, Peets

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.