I currently have code like this:
cache = 1
def foo():
global cache
# many
# lines
# of code
cache = 2
However, this may lead to hard-to-find-bugs in the future, because the reader may not notice that global cache appears somewhere above cache = 2. Alternatively, a contributor may mistakenly add def bar(): cache = 2 and forget to add the global cache.
How can I avoid this pitfall?
This way,
Cache.myvaris practically a “global”. It’s possible to read/write to it from anywhere.I prefer this over the dictionary alternative, because it allows for auto-complete of the variable names.