Coding Style: Defining Functions within Methods?
bokr at oz.net
Sun Sep 7 19:05:57 CEST 2003
On Sun, 7 Sep 2003 08:11:15 +0000 (UTC), JCM <joshway_without_spam at myway.com> wrote:
>Bengt Richter <bokr at oz.net> 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.
Agreed, but python is very dynamic, so "as smart as possible" may cost
more overall than calculatedly "dumb" in many cases. As it stands, the way
you code it will be pretty much the way it happens, so there will be some cost
to nesting functions. I just don't know how much without timing it. ... resisting ...
Succeeded, for now ;-)
More information about the Python-list