On Fri, May 28, 2021 at 6:04 AM Brendan Barnwell <brenb...@brenbarn.net> wrote: > > On 2021-05-27 12:33, Chris Angelico wrote: > > With statics, you could write it like this: > > > > def merge_shortest(things): > > static len=len > > ... > > > > Simple. Easy. Reliable. (And this usage would work with pretty much > > any of the defined semantics.) There's no more confusion. > > You can already do that: > > def merge_shortest(things): > len=len > ... > > Yes, it does require a single global lookup on each function call, but > if that's really a bottleneck for you I don't think there's much hope. :-)
Hmmmmmmmm.... let's see. >>> def merge_shortest(things): ... len=len ... ... ... >>> merge_shortest([]) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 2, in merge_shortest UnboundLocalError: local variable 'len' referenced before assignment There are languages in which you're allowed to do this (using a name in an initializer to fetch from a parent scope), but Python isn't one of them. At best, you could write "_len=len", but then you have to rewrite the function body to use _len, leaving the question of "why is this _len and not len?" for every future maintainer. Since a static declaration is evaluated at function definition time (just like a default argument is), this problem doesn't come up, because the local name "len" won't exist at that point. > >> 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. > > > > Question: When does this constant get looked up? > > > > def merge_shortest(things): > > constant len=len > > ... > > > > Is it looked up as the function begins execution, or when the function > > is defined? How much are you going to assume that it won't change? > > Sorry, I was a bit vague there. What I was envisioning is that you > would specify len as a constant at the GLOBAL level, meaning that all > functions in the module could always assume it referred to the same > thing. (It's true this might require something different from what was > proposed in the other thread about constants.) > Gotcha, gotcha. I think module-level constants could *also* be useful, but they're orthogonal to this proposal. Unless it's a compile-time constant (so, as the module gets imported, all references to "len" become LOAD_CONST of whatever object was in the builtins at that point), I doubt it would have the same performance benefits, and it obviously couldn't handle the mutable statics use-case. I think there are very good use-cases for module-level constants, but the trouble is, there are so many variants of the idea and so many not-quite-overlapping purposes that they can be put to :) ChrisA _______________________________________________ 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/I6L52IIK7LUWSN26WRU3653KXHGZ4YCL/ Code of Conduct: http://python.org/psf/codeofconduct/