How many times inner functions are compiled ?
Thomas Wouters
thomas at xs4all.net
Tue Jun 13 08:16:00 EDT 2000
On Tue, Jun 13, 2000 at 02:01:20PM +0200, Jerome Quelin wrote:
> def outer():
> def inner():
> print "spam"
> inner()
> outer()
> outer()
> In the snippet code above, I call outer() two times. I would like to
> know how many times python will compile the inner function ? One time
> when compiling the script/module, or two times, ie at every invocation of the
> outer function ?
Once for every invocation of outer(). Function definitions are to be
considered 'assignment' statements, similar to something like this:
def outer():
inner = Function("code")
When looking at that bit of code, there is no doubt in your mind that the
creation of the Function object is done once each call, is there ? Nested
functions are not very useful, currently, except to reduce namespace
polution and for simulating closures, like this:
def mk_inner(spam):
def inner(spam=spam):
print spam
return inner
inner = mk_inner(spam)
inner()
inner()
inner()
(Of course, you can do the same by making a class with a __call__ method :)
--
Thomas Wouters <thomas at xs4all.net>
Hi! I'm a .signature virus! copy me into your .signature file to help me spread!
More information about the Python-list
mailing list