[Python-ideas] Re: Add static variable storage in functions

2021-05-28 Thread Brendan Barnwell
On 2021-05-28 04:53, Steven D'Aprano wrote: On Thu, May 27, 2021 at 07:13:56PM -0700, Brendan Barnwell wrote: On 2021-05-27 14:33, Steven D'Aprano wrote: >But even if we did have actual constants, how does that help get static >*variables*, you know, things that aren't constant but can vary?

[Python-ideas] Re: A __decoration_call__ method for Callable objects (WAS: Decorators on variables)

2021-05-28 Thread micro codery
Ah, I think you might be missing the context of the original proposal? I do mean bare unbound identifiers - at lease as they occur in this new syntax. # currently works spam = “eggs” spam: eggs # currently a NameError spam # proposed to work, currently illegal @spam eggs @spam(“eggs”) cheese

[Python-ideas] Re: A __decoration_call__ method for Callable objects (WAS: Decorators on variables)

2021-05-28 Thread micro codery
On Fri, May 28, 2021 at 5:07 PM Rob Cliffe Co wrote: > > > On 29/05/2021 00:51, micro codery wrote: > > > I also don't know what should happen for complicated assignments, and I > think this > has been the death of such variable decorator discussions in the past, so > I would > still push for only

[Python-ideas] Re: A __decoration_call__ method for Callable objects (WAS: Decorators on variables)

2021-05-28 Thread Rob Cliffe via Python-ideas
On 29/05/2021 00:51, micro codery wrote: I also don't know what should happen for complicated assignments, and I think this has been the death of such variable decorator discussions in the past, so I would still push for only bare identifiers, with or without a type hint (but maybe it will

[Python-ideas] Re: A __decoration_call__ method for Callable objects (WAS: Decorators on variables)

2021-05-28 Thread micro codery
On Thu, May 27, 2021 at 12:39 PM Ricky Teachey wrote: > On Thu, May 27, 2021 at 2:32 PM micro codery wrote: > >> >> But what is actually valid to follow a decorator in this proposal? >> Any simple expression, any expression? Is it limited to assignment >> espressions? >> > > At this point, I hav

[Python-ideas] Re: Introduce constants in Python (constant name binding)

2021-05-28 Thread Paul Sokolovsky
Hello, On Tue, 25 May 2021 11:53:20 - "Shreyan Avigyan" wrote: > I posted my previous idea regarding this on the mailing list. This > idea is a little different. This idea suggests introducing constant > name bindings. This is similar to const pointer in C/C++. Once a name > has been assigne

[Python-ideas] Re: Introduce constants in Python (constant name binding)

2021-05-28 Thread Stephen J. Turnbull
Christopher Barker writes: > i really hate lists that don't have the the list as the default reply > setting :-( This is something that lists should *not* do, because your MUA can and should do it for you (and not for me). If you want the feature, get a decent MUA (unfortunately, that probably

[Python-ideas] Re: Add static variable storage in functions

2021-05-28 Thread Shreyan Avigyan
I was thinking about introducing new opcodes for implementing static variables. Not sure though. All of the ideas actually do the same thing. The difference is approach. On Fri, May 28, 2021 at 6:57 PM Chris Angelico wrote: > On Fri, May 28, 2021 at 10:11 PM Steven D'Aprano > wrote: > > > > On

[Python-ideas] Re: Add static variable storage in functions

2021-05-28 Thread Chris Angelico
On Fri, May 28, 2021 at 10:11 PM Steven D'Aprano wrote: > > 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 exampl

[Python-ideas] Re: Add static variable storage in functions

2021-05-28 Thread Paul Moore
On Fri, 28 May 2021 at 13:11, Steven D'Aprano wrote: > We might not even need new syntax if we could do that transformation > using a decorator. > > > @static(var=initial) > def func(): > body The problem here is injecting the "nonlocal var" statement and adjusting all of the ref

[Python-ideas] Re: Add static variable storage in functions

2021-05-28 Thread Steven D'Aprano
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 a

[Python-ideas] Re: Add static variable storage in functions

2021-05-28 Thread Steven D'Aprano
On Thu, May 27, 2021 at 07:13:56PM -0700, Brendan Barnwell wrote: > On 2021-05-27 14:33, Steven D'Aprano wrote: > >But even if we did have actual constants, how does that help get static > >*variables*, you know, things that aren't constant but can vary? > > All of those use cases can alread

[Python-ideas] Re: Add static variable storage in functions

2021-05-28 Thread Steven D'Aprano
On Thu, May 27, 2021 at 08:06:22PM -0700, Christopher Barker wrote: > Well, there is thread safe, and there is thread dangerous. I have an > enormous amount of code that is not strictly thread safe, but works fine > when run under a multi-threaded web server because there are no cases where > the

[Python-ideas] Re: Add static variable storage in functions

2021-05-28 Thread Chris Angelico
On Fri, May 28, 2021 at 7:28 PM Ronald Oussoren via Python-ideas wrote: > I honestly don’t see the difference between: > > def spam(arg, static=[0]): … > > and > > _static = 0 > def spam(arg): global _static; … > > The difference is where the state is stored. State in the latter example is > less

[Python-ideas] Re: Add static variable storage in functions

2021-05-28 Thread Ronald Oussoren via Python-ideas
> On 27 May 2021, at 18:15, Steven D'Aprano wrote: > > 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 t

[Python-ideas] Re: Add static variable storage in functions

2021-05-28 Thread Ronald Oussoren via Python-ideas
> On 27 May 2021, at 17:24, Steven D'Aprano wrote: > > On Thu, May 27, 2021 at 04:37:10PM +0200, Ronald Oussoren wrote: > >>> One common use for function defaults is to optimize function lookups to >>> local variables instead of global or builtins: >>> >>> def func(arg, len=len): >>>