[Tutor] lexical scoping efficiency

Alan Gauld alan.gauld at btinternet.com
Tue Nov 21 22:04:04 CET 2006


"Matthew Gordon" <mrgordon at mit.edu> wrote

> Defining functions inside of other functions ...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.

I'm speaking from a position of profound ignorance however I'd be 
surprised if Python does that.

I suspect that when Python first parses the function definition 
it will create a cached reference to the nested function, probably 
via a dictionary. Python is usually pretty good at creating cached 
references to things that don't change and unlike local variables 
the function definition will be the same for each invocation of the 
function (although there is a possibility of some clever closure 
behaviour rendering that invalid, but I don't think so)

But I'm guessing, does anyone else know for sure?

Alan G



More information about the Tutor mailing list