Martin Geisler wrote:

A somewhat related question: do I pay a performance penalty when I let a
function define an inner function like this:

  def foo():

    def bar()
      ...

    bar()

Some. The *code* for the body of bar is compiled as part of compiling the body of foo, but each call of foo creates a new *function* object.

compared to just defining it once outside:

  def bar():
    ...

  def foo():
    ...
    bar()

I'm thinking that each execution of the first foo could spend a little
time defining a new bar each time, or is that not how things work?

I realize that defining bar as an inner function has the advantage of
being able to see variables in the namespace of foo.

The alternative is to pass in the value(s) needed.

tjr

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to