I was reading about Default Parameter Values in Python on Effbot.
There is a section later in the article where the author talks about Valid uses for mutable defaults and cites the following example:
and, for highly optimized code, local rebinding of global names:
import math
def this_one_must_be_fast(x, sin=math.sin, cos=math.cos):
...
I haven't been able to locate how this causes fast/highly optimised execution of code. Can somebody enlighten on this with a well informed (and preferably with citations) answer?
math.cos
only once, at definition time. After that, it's found locally. Otherwise, the interpreter has to go looking through locals, nonlocals, imports every timemath.cos
is called.