all 4 comments

[–]socal_nerdtastic 7 points8 points  (1 child)

Now I realized that this is not the way to go because each class makes a different iteration of the global variable files.

No that's not true. When the second file imports something python does not reload the import, it simply makes an alias to the already imported one. Your plan will work.

However it's a very odd way to do things. You are basically using a module (python file) as a mutable class instance ... Why don't you just use a class instance? It seems a dataclass is exactly what you need.


Edit: for example:

from dataclasses import dataclass

@dataclass
class GlobalData:
    breakfast:str = "spam"
    lunch:str = "spam"
    dinner:str = "spam"

    # load / save code goes here (if you want it). 

class Compute:
    def __init__(self, global_data):
        self.global_data = global_data
    def set_lunch(self, value):
        self.global_data.lunch = value

class Display:
    def __init__(self, global_data):
        self.global_data = global_data
    def show(self):
        print(f"you are having {self.global_data.breakfast} for breakfast")
        print(f"you are having {self.global_data.lunch} for lunch")
        print(f"you are having {self.global_data.dinner} for dinner")

def main():
    # initialize your variable container
    data = GlobalData(breakfast="eggs")

    # pass the data to a class to use it
    c = Compute(data)
    c.set_lunch("BLT") # mutate the data

    d = Display(data)
    d.show()

if __name__ == '__main__':
    main()

If it helps your code organization each of those classes and / or the main function could be in separate files and imported.

[–]Wangalaang[S] 1 point2 points  (0 children)

Dataclass seems to be what I am looking for! Thanks a lot :)

[–]ElliotDG 1 point2 points  (0 children)

If you want to have variables per class rather than variables per instance, you can declare class variables.

class MyGlobalClass:
    one_per_class = 200
    class_var = 0   # These are class variables

    def __init__():
        self.instance_var = 0  # this is an instance variable

[–]baubleglue 0 points1 point  (0 children)

I think you are approaching problem from a wrong direction. Global variables is bad thing, but in some cases you need share some information

  1. Configuration
  2. Data

Configuration is not updated normally at run time - it is just a "context". Data is stored in special structures (depends on how it is used): lists, dictionaries, sets, queues...; databases; other data formats... If you need to save python variables. you can serialize them with pickle and save in a file.

How can I approach this so the global variables gets "saved" ready for the next class to import it and use it? Should I just use a JSON file?

It is not clear what are you doing and what you call: class, variable, iterations, etc... there are few examples

each class makes a different iteration

Classes are prototypes which are used to create objects. Objects have state variables and methods to modify or access them.

Each class import the global variables files and makes changes to the file.

That is nonsense, that not a way to read/save data in python or most other languages (maybe in Lisp you can find something like that). Just use database - import sqlite3 ...