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. :-)
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.)