[Tutor] lexical scoping efficiency

Kent Johnson kent37 at tds.net
Tue Nov 21 21:02:14 CET 2006


Matthew Gordon wrote:
> This isn't python specific, but it seems pertinent to the interests of
> this list regardless.
> 
> Defining functions inside of other functions seems conceptually neater
> than defining those functions in the global environment if they're
> never going to be used by anything except that one function. In terms
> of efficiency though, it seems terrible to lexically scope because
> each time the function is called it will have to reinstantiate the
> sub-function.
> 
> Anyone have an opinion/suggestion?

It's actually pretty quick to create the nested function because the 
heavy work of compiling is just done once. According to Fredrik Lundh on 
c.l.py, "all that happens is that a new function object is created from
prebuilt parts, and assigned to a local variable.  it's not slower than, 
say, a method call."

See http://tinyurl.com/ylh8g9

Kent



More information about the Tutor mailing list