Static variables [was Re: syntax difference]

MRAB python at mrabarnett.plus.com
Sat Jun 23 13:29:51 EDT 2018


On 2018-06-23 05:16, Chris Angelico wrote:
> On Sat, Jun 23, 2018 at 1:51 PM, Steven D'Aprano
> <steve+comp.lang.python at pearwood.info> 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. There is only one of it.
>>> What happens in Python? For instance:
>>>
>>> def f():
>>>     def g():
>>>         static x = 0
>>>         x += 1
>>>         return x
>>>     return g
>>>
>>> Does the static variable exist once for each instance of g()? If so,
>>> it'll behave like a closure variable; if not, it'll behave like a
>>> global. Either way, I'm pretty much certain that people will expect the
>>> other.
>>
>> Yes, but given the normal execution model of Python, only one solution is
>> valid. Since the function g is created fresh each time f is called, each
>> one gets a fresh static x.
>>
>> If you want all the g's to share the same x, you would write:
>>
>> def f():
>>     static x = 0
>>     def g():
>>         x += 1
>>         return x
>>     return g
>>
>>
>> In this case, every invocation of f shares the same static x, and all the
>> g's refer to that same x, using the ordinary closure mechanism. In the
>> earlier case, each invocation of f creates a brand new g with its own x.
>>
>> Simple and elegant.
>>
>> This could at last get rid of that useful but ugly idiom:
>>
>>     def function(real, arguments, len=len, int=int, str=str):
>>         ...
>>
>> if we allowed the "static" declaration to access the values from the
>> surrounding scope:
>>
>>     def function(real, arguments):
>>         static len=len, int=int, str=str
>>
>> But I think nicer than that would be a decorator:
>>
>>     @static(len=len, int=int, str=str)
>>     def function(real, arguments):
>>         ...
>>
>> which adds local variables len, int, str to the function, with the given
>> values, and transforms all the bytecode LOAD_NAME len to LOAD_FAST len
>> (or whatever).
>>
>> (We might need a new bytecode to SET_STATIC.)
>>
>> That would be a nice bytecode hack to prove the usefulness of the concept!
>>
> 
> Okay, that makes sense. So in a way, static variables would be like
> closure variables with an invisible outer function. These would be
> roughly equivalent:
> 
 > def f():
 >      static x = 0
 >      x += 1
 >      return x
 >
You can already do something similar like this:

def f():
      f.x += 1
      return f.x
f.x = 0

[snip]



More information about the Python-list mailing list