optimization

Robert Kern robert.kern at gmail.com
Mon Dec 1 20:06:24 EST 2008


bearophileHUGS at lycos.com wrote:
> Robert Kern:
>> I only expect functions to be defined inside of functions if they are going to use lexical scoping for some reason.<
> 
> There are other reasons to nest functions. One of them is to represent
> logical nesting of some functionality.

Is that any different than Neal's "if it's only used inside"?

> So you will find some exceptions to your self-created rule :-)

It's not a rule; it's just what I expect after many years of programming Python 
and reading lots of Python code. Most people define functions at the top-level 
regardless of whether they are used once or not. Defining a function inside of a 
function is an oddity. Lexical scoping requires that you define a function 
inside of a function, so that is always my first assumption about why the author 
defined the function there. I only fall back to "the author just wanted to 
organize things in a different way" when I fail to find the lexically-scoped 
variables, or I see a comment explaining it. I'm pretty sure that lexical 
scoping (and its poor-man implementation via keyword arguments before) is what 
the def-in-a-def feature was for, not organization.

As Neal has observed, there is a performance hit for creating functions inside 
of another function. Every time you go through the outer function, you are 
creating new function objects for all of the inner functions. That's how you can 
get lexical scoping. It is not equivalent to defining the functions all at the 
top-level, where all of the function objects are created at once. The compiler 
can't optimize that overhead away because the overhead arises from implementing 
a real feature.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list