![](https://secure.gravatar.com/avatar/0a2191a85455df6d2efdb22c7463c304.jpg?s=120&d=mm&r=g)
On 21.01.2016 09:48, Victor Stinner wrote:
The difference between "def hello(print=print): ..." and Serhiy's const idea (or my optimization) is that "def hello(print=print): ..." changes the signature of the function which can be a serious issue in an API.
Note: The other optimization "local_print = print" in the function is only useful for loops (when the builtin is loaded multiple times) and it still loads the builtin once per function call, whereas my optimization uses a constant and so no lookup is required anymore.
Then guards are used to disable the optimization if builtins are modified. See the PEP 510 for an explanation on that part.
I ran performance tests on these optimization tricks (and others) in 2014. See this talk: http://www.egenix.com/library/presentations/PyCon-UK-2014-When-performance-m... (slides 33ff.) The keyword trick doesn't really pay off in terms of added performance vs. danger of introducing weird bugs. Still, it would be great to have a way to say "please look this symbol up at compile time and stick the result in a local variable" (which is basically what the keyword trick does), only in a form that's easier to detect when reading the code and doesn't change the function signature. A decorator could help with this (by transforming the byte code and localizing the symbols), e.g. @localize(len) def f(seq): z = 0 for x in seq: if x: z += len(x) return z but the more we move language features to decorators, the less readable the code will get by having long tails of decorators on many functions (we don't really want our functions to resemble snakes, do we ? :-)). So perhaps it is indeed time for a new keyword to localize symbols in a function or module, say: # module scope localization, applies to all code objects in # this module: localize len def f(seq): ... or: def f(seq): # Localize len in this function, since we need it in # tight loops localize len ... All that said, I don't really believe that this is a high priority feature request. The gained performance win is not all that great and only becomes relevant when used in tight loops. -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Experts (#1, Jan 21 2016)
Python Projects, Coaching and Consulting ... http://www.egenix.com/ Python Database Interfaces ... http://products.egenix.com/ Plone/Zope Database Interfaces ... http://zope.egenix.com/
::: We implement business ideas - efficiently in both time and costs ::: eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 http://www.egenix.com/company/contact/ http://www.malemburg.com/