In Python, I can have the equivalent of C# static members:
class MyClass:
i = 0 # This is like a C# static member
print(MyClass.i)
gives output
0
But maybe my static member needs to be calculated somehow. I can do:
class MyClass:
i = 0
i += 10
print(MyClass.i)
gives output
10
In practice, I'm writing a config class which needs to read a json file from disk, validate it, and then populate some static members. The closest thing to what I want to do in Python would look like:
class GlobalConfig:
with open('config.json', 'r') as f:
config_dict = json.read(f)
# Maybe do some validation here.
i = config_dict["i"]
a = config_dict["hello_world"]
Truth be told, I wouldn't really do this in Python, but I'm borrowing from C# in that everything needs to go in classes.
In practice in my C# code, I would have a GlobalConfig
class in a Config.cs
file and all my other files would have access to this.
But it seems I can't do anything other than declare/define members in the class body. How can I do the work of loading up and parsing my config file and have the result of that be accessible as static members to the rest of my program?
PS: I don't really want this to influence the answers I get in unless it has to, but FYI I am working with Unity.