On Fri, May 28, 2021 at 04:20:15AM +1000, Chris Angelico wrote: > def f(): > static x = 0 > x += 1 > yield x > > next(f()) > next(f()) > next(f()) > > will yield 1 every time?
I think that this example has just about convinced me that Chris' approach is correct. I wasn't thinking about generators or recursion. I think that closure nonlocals are almost as fast as locals, so we might be able to use the closure mechanism to get this. Something vaguely like this: def func(): static var = initial body is transformed into: def factory(): var = initial def func(): nonlocal var body return func func = factory() except that the factory is never actually exposed to Python code. It would be nice if there was some way to introspect the value of `var` but if there is a way to do it I don't know it. We might not even need new syntax if we could do that transformation using a decorator. @static(var=initial) def func(): body -- Steve _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/XKE7QFNCBFZH2WRUC2CWCKMEOHZPA4ET/ Code of Conduct: http://python.org/psf/codeofconduct/