Re: Static variables [was Re: syntax difference]

2018-06-26 Thread Bart
To: Ben Bacarisse From: "Bart" To: Ben Bacarisse From: Bart On 24/06/2018 01:53, Ben Bacarisse wrote: > Bart writes: >> Wow. (Just think of all the times you write a function containing a >> neat bunch of local functions, every time it's called it has to create >> a new function

Re: Static variables [was Re: syntax difference]

2018-06-26 Thread Gregory Ewing
To: Bart From: "Gregory Ewing" To: Bart From: Gregory Ewing Bart wrote: > Wow. (Just think of all the times you write a function containing a neat > bunch of local functions, every time it's called it has to create a new > function instances for each of those functions, even if they are

Re: Static variables [was Re: syntax difference]

2018-06-26 Thread Ben Bacarisse
To: Bart From: "Ben Bacarisse" To: Bart From: Ben Bacarisse Bart writes: > On 23/06/2018 23:25, Ben Bacarisse wrote: >> Bart writes: >> >>> On 23/06/2018 21:13, Chris Angelico wrote: On Sat, Jun 23, 2018 at 10:41 PM, Bart wrote: >>> > (At what point would that happen anyway;

Re: Static variables [was Re: syntax difference]

2018-06-26 Thread Ben Bacarisse
To: Bart From: "Ben Bacarisse" To: Bart From: Ben Bacarisse Bart writes: > On 23/06/2018 21:13, Chris Angelico wrote: >> On Sat, Jun 23, 2018 at 10:41 PM, Bart wrote: > >>> (At what point would that happen anyway; if you do this: > >> NONE of your examples are taking copies of the

Re: Static variables [was Re: syntax difference]

2018-06-26 Thread Bart
To: Ben Bacarisse From: "Bart" To: Ben Bacarisse From: Bart On 23/06/2018 23:25, Ben Bacarisse wrote: > Bart writes: > >> On 23/06/2018 21:13, Chris Angelico wrote: >>> On Sat, Jun 23, 2018 at 10:41 PM, Bart wrote: >> (At what point would that happen anyway; if you do this: >> >>>

Re: Static variables [was Re: syntax difference]

2018-06-26 Thread Bart
To: Chris Angelico From: "Bart" To: Chris Angelico From: Bart On 23/06/2018 21:13, Chris Angelico wrote: > On Sat, Jun 23, 2018 at 10:41 PM, Bart wrote: >> (At what point would that happen anyway; if you do this: > NONE of your examples are taking copies of the function. They all are >

Re: Static variables [was Re: syntax difference]

2018-06-26 Thread Bart
To: Stefan Ram From: "Bart" To: Stefan Ram From: Bart On 23/06/2018 14:32, Stefan Ram wrote: > r...@zedat.fu-berlin.de (Stefan Ram) writes: >> def f(): >> def g(): >> g.x += 1 >> return g.x >> g.x = 0 >> return g > >Or, "for all g to share the same x": > >

Re: Static variables [was Re: syntax difference]

2018-06-26 Thread Stefan Ram
To: Stefan Ram From: "Stefan Ram" To: Stefan Ram From: r...@zedat.fu-berlin.de (Stefan Ram) r...@zedat.fu-berlin.de (Stefan Ram) writes: >def f(): >def g(): >g.x += 1 >return g.x >g.x = 0 >return g Or, "for all g to share the same x": main.py def f():

Re: Static variables [was Re: syntax difference]

2018-06-26 Thread Stefan Ram
To: Steven D'Aprano From: "Stefan Ram" To: Steven D'Aprano From: r...@zedat.fu-berlin.de (Stefan Ram) Steven D'Aprano writes: >def f(): >static x = 0 >def g(): >x += 1 >return x >return g What one can do today: main.py def g(): g.x += 1 return g.x

Re: Static variables [was Re: syntax difference]

2018-06-26 Thread Bart
To: Steven D'Aprano From: "Bart" To: Steven D'Aprano From: Bart On 23/06/2018 04:51, Steven D'Aprano wrote: > On Wed, 20 Jun 2018 14:18:19 +1000, Chris Angelico wrote: > >> Ah. Yeah, that would be a plausible feature to add to Python. But in C, >> a static variable is basically the same

Re: Static variables [was Re: syntax difference]

2018-06-26 Thread Chris Angelico
From: "Chris Angelico" From: Chris Angelico On Sat, Jun 23, 2018 at 1:51 PM, Steven D'Aprano wrote: > On Wed, 20 Jun 2018 14:18:19 +1000, Chris Angelico wrote: > >> Ah. Yeah, that would be a plausible feature to add to Python. But in C, >> a static variable is basically the same thing as a

Re: Static variables [was Re: syntax difference]

2018-06-26 Thread Chris Angelico
From: "Chris Angelico" From: Chris Angelico On Sat, Jun 23, 2018 at 2:16 PM, Chris Angelico wrote: > For getting rid of the "len=len" trick, though, I would REALLY like to > transform those into LOAD_CONST. That'd be a fun bytecode hack all on > its own. In fact, I'm gonna have a shot at

Re: Static variables [was Re: syntax difference]

2018-06-25 Thread Steven D'Aprano
On Mon, 25 Jun 2018 18:22:56 +, Grant Edwards wrote: > On 2018-06-24, Steven D'Aprano wrote: > >> Building functions is cheap. Cheap is not free. >> >> Inner functions that aren't exposed to the outside cannot be tested in >> isolation, you can't access them through help() interactively.

Re: Static variables [was Re: syntax difference]

2018-06-25 Thread Marko Rauhamaa
Grant Edwards : > IOW, you use a local function instead of a global one for the exact > same reasons you use local "variables" instead of global ones. > > In Python, functions are first class objects. Binding a name to a > function is no different than binding it to an integer, list, string, > or

Re: Static variables [was Re: syntax difference]

2018-06-25 Thread Grant Edwards
On 2018-06-24, Steven D'Aprano wrote: > Building functions is cheap. Cheap is not free. > > Inner functions that aren't exposed to the outside cannot be tested > in isolation, you can't access them through help() > interactively. Given the choice between: [...] > so not expensive, but not free

Re: Static variables [was Re: syntax difference]

2018-06-25 Thread Bart
To: Ben Bacarisse From: Bart On 24/06/2018 01:53, Ben Bacarisse wrote: > Bart writes: >> Wow. (Just think of all the times you write a function containing a >> neat bunch of local functions, every time it's called it has to create >> a new function instances for each of those functions, even

Re: Static variables [was Re: syntax difference]

2018-06-25 Thread Steven D'Aprano
From: Steven D'Aprano On Sun, 24 Jun 2018 11:23:12 +0100, Bart wrote: > On 24/06/2018 01:53, Ben Bacarisse wrote: >> Bart writes: > >>> Wow. (Just think of all the times you write a function containing a >>> neat bunch of local functions, every time it's called it has to create >>> a new

Re: Static variables [was Re: syntax difference]

2018-06-25 Thread Steven D'Aprano
From: Steven D'Aprano On Sat, 23 Jun 2018 18:29:51 +0100, MRAB wrote: > You can already do something similar like this: > > def f(): > f.x += 1 > return f.x > f.x = 0 > > [snip] You can, but only as an illustration, not as a serious implementation. The whole point of static local

Re: Static variables [was Re: syntax difference]

2018-06-25 Thread Steven D'Aprano
From: Steven D'Aprano On Sat, 23 Jun 2018 21:44:00 +0100, Bart wrote: > Since these references are created via the return g statement here: > > def f(): > def g(): > > return g > > (say to create function references i and j like this: > > i = f() >

Re: Static variables [was Re: syntax difference]

2018-06-25 Thread Steven D'Aprano
From: Steven D'Aprano On Sun, 24 Jun 2018 00:37:36 +0100, Bart wrote: > Do you mean that if the same 'def' block is re-executed, it will create > a different instance of the function? (Same byte-code, but a different > set of everything else the function uses.) That's not as slow as you think

Re: Static variables [was Re: syntax difference]

2018-06-25 Thread Gregory Ewing
To: Bart From: Gregory Ewing Bart wrote: > Wow. (Just think of all the times you write a function containing a neat > bunch of local functions, every time it's called it has to create a new > function instances for each of those functions, even if they are not used.) Fortunately, function

Re: Static variables [was Re: syntax difference]

2018-06-25 Thread Chris Angelico
From: Chris Angelico On Sun, Jun 24, 2018 at 9:37 AM, Bart wrote: > On 23/06/2018 23:25, Ben Bacarisse wrote: >> >> Bart writes: >> >>> On 23/06/2018 21:13, Chris Angelico wrote: On Sat, Jun 23, 2018 at 10:41 PM, Bart wrote: >>> >>> > (At what point would that happen anyway; if

Re: Static variables [was Re: syntax difference]

2018-06-25 Thread Ben Bacarisse
To: Bart From: Ben Bacarisse Bart writes: > On 23/06/2018 23:25, Ben Bacarisse wrote: >> Bart writes: >> >>> On 23/06/2018 21:13, Chris Angelico wrote: On Sat, Jun 23, 2018 at 10:41 PM, Bart wrote: >>> > (At what point would that happen anyway; if you do this: >>> NONE of

Re: Static variables [was Re: syntax difference]

2018-06-25 Thread Ben Bacarisse
To: Bart From: Ben Bacarisse Bart writes: > On 23/06/2018 21:13, Chris Angelico wrote: >> On Sat, Jun 23, 2018 at 10:41 PM, Bart wrote: > >>> (At what point would that happen anyway; if you do this: > >> NONE of your examples are taking copies of the function. They all are >> making

Re: Static variables [was Re: syntax difference]

2018-06-25 Thread Bart
To: Ben Bacarisse From: Bart On 23/06/2018 23:25, Ben Bacarisse wrote: > Bart writes: > >> On 23/06/2018 21:13, Chris Angelico wrote: >>> On Sat, Jun 23, 2018 at 10:41 PM, Bart wrote: >> (At what point would that happen anyway; if you do this: >> >>> NONE of your examples are taking

Re: Static variables [was Re: syntax difference]

2018-06-25 Thread Bart
To: Chris Angelico From: Bart On 23/06/2018 21:13, Chris Angelico wrote: > On Sat, Jun 23, 2018 at 10:41 PM, Bart wrote: >> (At what point would that happen anyway; if you do this: > NONE of your examples are taking copies of the function. They all are > making REFERENCES to the same

Re: Static variables [was Re: syntax difference]

2018-06-25 Thread Chris Angelico
From: Chris Angelico On Sat, Jun 23, 2018 at 10:41 PM, Bart wrote: > This is an example of a simple concept getting so out of hand that it will > either never be implemented, or the resulting implementation becomes > impractical to use. > > This is what we're trying to do: > > def nextx():

Re: Static variables [was Re: syntax difference]

2018-06-24 Thread MRAB
From: MRAB On 2018-06-23 05:16, Chris Angelico wrote: > On Sat, Jun 23, 2018 at 1:51 PM, Steven D'Aprano > wrote: >> On Wed, 20 Jun 2018 14:18:19 +1000, Chris Angelico wrote: >> >>> Ah. Yeah, that would be a plausible feature to add to Python. But in C, >>> a static variable is basically the

Re: Static variables [was Re: syntax difference]

2018-06-24 Thread Bart
To: Stefan Ram From: Bart On 23/06/2018 14:32, Stefan Ram wrote: > r...@zedat.fu-berlin.de (Stefan Ram) writes: >> def f(): >> def g(): >> g.x += 1 >> return g.x >> g.x = 0 >> return g > >Or, "for all g to share the same x": > >main.py > > def f(): >

Re: Static variables [was Re: syntax difference]

2018-06-24 Thread Stefan Ram
To: Steven D'Aprano From: r...@zedat.fu-berlin.de (Stefan Ram) Steven D'Aprano writes: >def f(): >static x = 0 >def g(): >x += 1 >return x >return g What one can do today: main.py def g(): g.x += 1 return g.x g.x = 0 print( g() ) print( g() ) print(

Re: Static variables [was Re: syntax difference]

2018-06-24 Thread Ed Kellett
98ecd8c1-13b7-8317-8177-6a3592171...@kellett.im> Subject: Re: Static variables [was Re: syntax difference] References: <72edc16a-69e0-41a2-bec3-290083f6e...@googlegroups.com> <01092eb6-172f-5ee0-91fb-4e3e1df99...@gmail.com> <6eUVC.491716$Qg7.378011@fx08.am4>

Re: Static variables [was Re: syntax difference]

2018-06-24 Thread Stefan Ram
To: Stefan Ram From: r...@zedat.fu-berlin.de (Stefan Ram) r...@zedat.fu-berlin.de (Stefan Ram) writes: >def f(): >def g(): >g.x += 1 >return g.x >g.x = 0 >return g Or, "for all g to share the same x": main.py def f(): def g(): f.x += 1

Re: Static variables [was Re: syntax difference]

2018-06-24 Thread Bart
To: Steven D'Aprano From: Bart On 23/06/2018 04:51, Steven D'Aprano wrote: > On Wed, 20 Jun 2018 14:18:19 +1000, Chris Angelico wrote: > >> Ah. Yeah, that would be a plausible feature to add to Python. But in C, >> a static variable is basically the same thing as a global variable, >> except

Re: Static variables [was Re: syntax difference]

2018-06-24 Thread Chris Angelico
From: Chris Angelico On Sat, Jun 23, 2018 at 1:51 PM, Steven D'Aprano wrote: > On Wed, 20 Jun 2018 14:18:19 +1000, Chris Angelico wrote: > >> Ah. Yeah, that would be a plausible feature to add to Python. But in C, >> a static variable is basically the same thing as a global variable, >> except

Re: Static variables [was Re: syntax difference]

2018-06-24 Thread Chris Angelico
From: Chris Angelico On Sat, Jun 23, 2018 at 2:16 PM, Chris Angelico wrote: > For getting rid of the "len=len" trick, though, I would REALLY like to > transform those into LOAD_CONST. That'd be a fun bytecode hack all on > its own. In fact, I'm gonna have a shot at that. An "early bind these >

Re: Static variables [was Re: syntax difference]

2018-06-24 Thread Steven D'Aprano
On Sun, 24 Jun 2018 11:23:12 +0100, Bart wrote: > On 24/06/2018 01:53, Ben Bacarisse wrote: >> Bart writes: > >>> Wow. (Just think of all the times you write a function containing a >>> neat bunch of local functions, every time it's called it has to create >>> a new function instances for each

Re: Static variables [was Re: syntax difference]

2018-06-24 Thread Bart
On 24/06/2018 01:53, Ben Bacarisse wrote: Bart writes: Wow. (Just think of all the times you write a function containing a neat bunch of local functions, every time it's called it has to create a new function instances for each of those functions, even if they are not used.) I am surprised

Re: Static variables [was Re: syntax difference]

2018-06-23 Thread Steven D'Aprano
On Sat, 23 Jun 2018 18:29:51 +0100, MRAB wrote: > You can already do something similar like this: > > def f(): > f.x += 1 > return f.x > f.x = 0 > > [snip] You can, but only as an illustration, not as a serious implementation. The whole point of static local variables is that they

Re: Static variables [was Re: syntax difference]

2018-06-23 Thread Steven D'Aprano
On Sun, 24 Jun 2018 00:37:36 +0100, Bart wrote: > Do you mean that if the same 'def' block is re-executed, it will create > a different instance of the function? (Same byte-code, but a different > set of everything else the function uses.) That's not as slow as you think it is. Everything that

Re: Static variables [was Re: syntax difference]

2018-06-23 Thread Steven D'Aprano
On Sat, 23 Jun 2018 21:44:00 +0100, Bart wrote: > Since these references are created via the return g statement here: > > def f(): > def g(): > > return g > > (say to create function references i and j like this: > > i = f() > j = f() > ) > >

Re: Static variables [was Re: syntax difference]

2018-06-23 Thread Gregory Ewing
Bart wrote: Wow. (Just think of all the times you write a function containing a neat bunch of local functions, every time it's called it has to create a new function instances for each of those functions, even if they are not used.) Fortunately, function objects are small and cheap,

Re: Static variables [was Re: syntax difference]

2018-06-23 Thread Ben Bacarisse
Bart writes: > On 23/06/2018 23:25, Ben Bacarisse wrote: >> Bart writes: >> >>> On 23/06/2018 21:13, Chris Angelico wrote: On Sat, Jun 23, 2018 at 10:41 PM, Bart wrote: >>> > (At what point would that happen anyway; if you do this: >>> NONE of your examples are taking copies of

Re: Static variables [was Re: syntax difference]

2018-06-23 Thread Chris Angelico
On Sun, Jun 24, 2018 at 9:37 AM, Bart wrote: > On 23/06/2018 23:25, Ben Bacarisse wrote: >> >> Bart writes: >> >>> On 23/06/2018 21:13, Chris Angelico wrote: On Sat, Jun 23, 2018 at 10:41 PM, Bart wrote: >>> >>> > (At what point would that happen anyway; if you do this: >>> >>>

Re: Static variables [was Re: syntax difference]

2018-06-23 Thread Bart
On 23/06/2018 23:25, Ben Bacarisse wrote: Bart writes: On 23/06/2018 21:13, Chris Angelico wrote: On Sat, Jun 23, 2018 at 10:41 PM, Bart wrote: (At what point would that happen anyway; if you do this: NONE of your examples are taking copies of the function. They all are making

Re: Static variables [was Re: syntax difference]

2018-06-23 Thread Ben Bacarisse
Bart writes: > On 23/06/2018 21:13, Chris Angelico wrote: >> On Sat, Jun 23, 2018 at 10:41 PM, Bart wrote: > >>> (At what point would that happen anyway; if you do this: > >> NONE of your examples are taking copies of the function. They all are >> making REFERENCES to the same function. That is

Re: Static variables [was Re: syntax difference]

2018-06-23 Thread Bart
On 23/06/2018 21:13, Chris Angelico wrote: On Sat, Jun 23, 2018 at 10:41 PM, Bart wrote: (At what point would that happen anyway; if you do this: NONE of your examples are taking copies of the function. They all are making REFERENCES to the same function. That is all. This is about your

Re: Static variables [was Re: syntax difference]

2018-06-23 Thread Chris Angelico
On Sat, Jun 23, 2018 at 10:41 PM, Bart wrote: > This is an example of a simple concept getting so out of hand that it will > either never be implemented, or the resulting implementation becomes > impractical to use. > > This is what we're trying to do: > > def nextx(): > static x = 0

Re: Static variables [was Re: syntax difference]

2018-06-23 Thread MRAB
On 2018-06-23 05:16, Chris Angelico wrote: On Sat, Jun 23, 2018 at 1:51 PM, Steven D'Aprano wrote: On Wed, 20 Jun 2018 14:18:19 +1000, Chris Angelico wrote: Ah. Yeah, that would be a plausible feature to add to Python. But in C, a static variable is basically the same thing as a global

Re: Static variables [was Re: syntax difference]

2018-06-23 Thread Bart
On 23/06/2018 14:32, Stefan Ram wrote: r...@zedat.fu-berlin.de (Stefan Ram) writes: def f(): def g(): g.x += 1 return g.x g.x = 0 return g Or, "for all g to share the same x": main.py def f(): def g(): f.x += 1 return f.x

Re: Static variables [was Re: syntax difference]

2018-06-23 Thread Ed Kellett
On 2018-06-23 06:21, Chris Angelico wrote: > Let's start finding all the edge cases that don't work, so I can work > on fixing them :) Very long functions (or, more specifically, functions with a very large number of consts) will likely prove annoying. signature.asc Description: OpenPGP

Re: Static variables [was Re: syntax difference]

2018-06-23 Thread Bart
On 23/06/2018 04:51, Steven D'Aprano wrote: On Wed, 20 Jun 2018 14:18:19 +1000, Chris Angelico wrote: Ah. Yeah, that would be a plausible feature to add to Python. But in C, a static variable is basically the same thing as a global variable, except that its name is scoped to the function.

Re: Static variables [was Re: syntax difference]

2018-06-22 Thread Chris Angelico
On Sat, Jun 23, 2018 at 2:16 PM, Chris Angelico wrote: > For getting rid of the "len=len" trick, though, I would REALLY like to > transform those into LOAD_CONST. That'd be a fun bytecode hack all on > its own. In fact, I'm gonna have a shot at that. An "early bind these > names" decorator.

Re: Static variables [was Re: syntax difference]

2018-06-22 Thread Chris Angelico
On Sat, Jun 23, 2018 at 1:51 PM, Steven D'Aprano wrote: > On Wed, 20 Jun 2018 14:18:19 +1000, Chris Angelico wrote: > >> Ah. Yeah, that would be a plausible feature to add to Python. But in C, >> a static variable is basically the same thing as a global variable, >> except that its name is scoped