The issue here is it sounds like you want to use globals, without using the global
keyword.
As far as I'm concerned a global is a global - no matter what you call it.
So don't trick yourself here, rather then identifying globals as bad, just accept there are times when a state is global and try to do it in the least error-prone way.
Of course try avoid these situations - but lets just assume you checked the alternatives and came to the conclusion some global state is needed.
Rather then mixing global state in with existing classes, I prefer to explicitly define a class which stores state, especially if its shared between multiple functions/classes.
# Singleton for storing global state
class _MyState:
__slots__ = (
"uploading",
"token_reload",
"size_text",
)
def __init__(self):
self.uploading = False
self.token_reload = True
self.size_text = ""
# One state to rule them all (otherwise known as a singleton)
my_state = _MyState()
del _MyState
The advantage with using __slots__
is you clearly define whats global, and you can't accidentally assign some invalid var.
eg, .size_tetx = ""
- will raise an error, with very large scripts allowing such mistakes to fail silently can loose your time debugging silly typos.
Heres an example of such a class used in the Sketchfab addon