My concern about thread safety is about how easy it would be to make it thread unsafe accidentally.

Sure, global is not thread safe, but it is well known that use of global is, to newbies, “bad”, and to more experienced programmers, “to be used with caution, understanding the risks”.

But particularly if static provides a performance boost, people will be very tempted to use it without considering the implications. 

If people want a high performance local constant— that sounds something like the constant proposal the OP brought up earlier.

-CHB



On Thu, May 27, 2021 at 12:18 PM Brendan Barnwell <brenbarn@brenbarn.net> wrote:
On 2021-05-27 05:18, Steven D'Aprano wrote:
> On Thu, May 27, 2021 at 07:56:16AM -0000, Shreyan Avigyan wrote:
>
>> Lot of programming languages have something known as static variable
>> storage in *functions* not *classes*. Static variable storage means a
>> variable limited to a function yet the data it points to persists
>> until the end of the program.
>
> +1 on this idea.
>
> One common use for function defaults is to optimize function lookups to
> local variables instead of global or builtins:
>
>      def func(arg, len=len):
>          # now len is a fast local lookup instead of a slow name lookup
>
> Benchmarking shows that this actually does make a significant difference
> to performance, but it's a technique under-used because of the
> horribleness of a len=len parameter.
>
> (Raymond Hettinger is, I think, a proponent of this optimization trick.
> At least I learned it from his code.)

        I don't see this as a great motivation for this feature.  If the goal
is to make things faster I think that would be better served by making
the interpreter smarter or adding other global-level optimizations.  As
it is, you're just trading one "manual" optimization (len=len) for
another (static len).

        Yes, the new one is perhaps slightly less ugly, but it still puts the
onus on the user to manually "declare" variables as local, not because
they are semantically local in any way, but just because we want a
faster lookup.  I see that as still a hack.  A non-hack would be some
kind of JIT or optimizing interpreter that actually reasons about how
the variables are used so that the programmer doesn't have to waste time
worrying about hand-tuning optimizations like this.  So basically for me
anything that involves the programmer saying "Please make this part
faster" is a hack. :-)  We all want everything to be as fast as possible
all the time, and in sofar as we're concerned about speed we should
focus on making the entire interpreter smarter so everything is faster,
rather than adding new ways for the programmer do extra work to make
just a few things faster.

        Even something like a way of specifying constants (which has been
proposed in another thread) would be better to my eye.  That would let
certain variables be marked as "safe" so that they could always be
looked up fast because we'd be sure they're never going to change.

        As to the original proposal, I'm not in favor of it.  It's fairly
uncommon for me to want to do this, and in the cases where I do, Python
classes are simple enough that I can just make a class with a method (or
a __call__ if I want to be really cool) that stores the data in a way
that's more transparent and more clearly connected to the normal ways of
storing state in Python.  It just isn't worth adding yet another
complexity to the language for this minor use case.

--
Brendan Barnwell
"Do not follow where the path may lead.  Go, instead, where there is no
path, and leave a trail."
    --author unknown
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-leave@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/AT5HA2Y7ADBCXSYRQ5NWZZM4VJLS4TVE/
Code of Conduct: http://python.org/psf/codeofconduct/
--
Christopher Barker, PhD (Chris)

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython