[Tutor] lexical scoping efficiency

Matthew Gordon mrgordon at mit.edu
Wed Nov 22 01:15:25 CET 2006


Interesting. I'll have to look a bit more into it. I thought it was a bit
too simplistic to assume that Python would allow for me to mishandle
resources so much. Thanks to all three of you!

- Matt


On 11/21/06, Roel Schroeven <rschroev_nospam_ml at fastmail.fm> wrote:
>
> Alan Gauld schreef:
> > "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?
>
> In a recent thread on comp.lang.python Fredrik Lundh said
> (http://groups.google.com/group/comp.lang.python/msg/81462fcf73104d8c):
>
> "(yes, an inner function is *created* every time you execute the outer
> function.  but it's created from prefabricated parts, so that's not a
> very expensive process)."
>
> --
> If I have been able to see further, it was only because I stood
> on the shoulders of giants.  -- Isaac Newton
>
> Roel Schroeven
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20061121/74d97f97/attachment.html 


More information about the Tutor mailing list