Coding Style: Defining Functions within Methods?

JCM joshway_without_spam at
Sun Sep 7 10:11:15 CEST 2003

Bengt Richter <bokr at> wrote:
> I like defining nested functions except for the fact that a definition is executable code
> in itself, and will be re-executed each time the outer function or method is called. I'm
> not sure how long MAKE_FUNCTION or MAKE_CLOSURE take to execute, but IWT it must mean allocating
> and glueing together the dynamic elements necessary for a distinct function/closure instance,
> and then disposing of them at some point on/after their going out of scope, vs. e.g. just locating
> a sibling method.

This doesn't need to be slow--it's up to the implementation to try
to be as smart as possible.  A compiler can do lambda-lifting to
transform a program with nested functions to one without, so there
really is nothing inherently slow about nested functions.

More information about the Python-list mailing list