On Thu, May 27, 2021 at 7:24 PM Chris Angelico email@example.com wrote:
But `len_ = len` does work. However, that doesn't change the
calculus at all for me. My point wasn't about using the exact same variable name. It's that ANY ability to create a local variable that is a fast-lookup shortcut for a global one is enough.
The change of variable name is significant, though. It means that this is no longer a minor change to the function's header; in order to use this optimization, you have to replace every use of the name "len" with "len_" (or "_len").
sure. but this is only worth doing if you are using that name in a tight loop somewhere -- so you only need the special name in one or two places, and I've always put that hack right next to that loop anyway.
if you are calling, e.g. len() in hundreds of separate places in one function -- you've got a much larger problem.