On Thu, May 27, 2021 at 04:53:17PM +0200, Ronald Oussoren via Python-ideas wrote:
Statics are still hidden global state
How are they *global* state when they are specific to an individual function?
We can already get the basic behaviour of statics right now, only with an ugly hack that pollutes the function parameter list and is inconvenient to use.
static = 
def spam(arg, static=): static += arg return static
def eggs(arg, static=): static -= arg return static
Is it your argument that all three `static` variables are using shared global state? If not, then I have no idea what you mean by insisting that statics are "hidden global state". They are hidden state, but not global. Just like closures and instance attributes.
and those can be problematic regardless of being function local or module global. Having global state like this affects testability and can affect threading as well.
It sounds like your problem is with *mutable state* in general. Global variables, instance attributes, class attributes, they all have exactly the same issues.
So don't use mutable state. Nobody is forcing you to use this feature if you prefer to write in a functional style with no mutable state.
The factory function doesn’t need to be part of the public API of a module, I’ve used a pattern like this to create APIs with some hidden state:
def make_api(): state = ... def api1(…): … def ap2(…): … return api1, api2 api1, api2 = make_api()
Congratulations, you've just used static local variables. You just used closures for the implementation.
I’m not saying that this is a particularly good way to structure code, in general just using a private module global is better (assuming the design calls for some kind of global state).
You: "Global state is bad! Don't use global state!"
Also you: "Don't use local state (closures)! Use global state, it's better!"