I’ve been trying to implement an Env class with “dynamic” static attributes that get set at runtime. My goal was to try to mimic the implementation of libraries like dotenv, while avoiding needing to pass around an instantiated Env object (or even constantly re-instantiating) it in different modules. Finally, I got these two lines to work, my goal of the last night:
Env.load()
print(Env.vars.foo) == bar
The Problem: I was only able to get this to work through a hacky method of setting Env.vars to an instance of Env(). This has resulted in infinite/recursive chaining of memory addresses. For example, this is now valid too: Env.vars.vars.vars
. Here is a visualization of it during debug:
I thought I was being cute and thought I had finally found a solution that works. I suppose I could leave it as is, since the memory address is just the same repeating one. But this feels like something beyond a mere code smell, and just feels so very wrong. But I can’t for the life of me figure out the “right” way of implementing what I want.
Here is my current implementation:
…
Is this monstrosity actually ok, even if hacky? Is there another way you’d go about doing this, while having Env.vars.forever
still be valid code?
1