What makes functions special?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sat Jul 9 21:34:50 EDT 2011

Eric Snow wrote:

> Mostly I am just
> trying to put together more pieces of the Python puzzle.  In this case
> I was trying to find out if the optimized execution of code objects
> for functions is a part of the language or just an implementation
> detail.

You keep using that phrase, "optimized execution of code objects for
functions", but I have no idea what that means.

The best I can think of is that you are thinking along these lines...

"Suppose we have the source code to a function:

def spam(n):
    return "SPAM"*n

To execute this, Python currently compiles the function into a code block,
and then when you call spam(n) elsewhere, Python executes the already
compiled code block.

Suppose instead an implementation of Python did not pre-compile the
function. Each time you called spam(n), the implementation would have to
locate the source code and interpret it on the spot. Would that be

If that's your question, then I would call that a Python interpreter using
c.1960 technology (as opposed to a byte-code compiler, which all the main
implementations currently are).

If that were the *only* difference, then I see no reason why it wouldn't be
allowed as an implementation of Python. A horribly slow implementation, but
still an implementation.

However, I doubt that would be the only difference. Given such a
simple-minded Python interpreter, it would be hard to provide expected
Python language features such as compiled code objects, closures, etc. You
would have to fake them somehow. Provided you could fake them sufficiently
well, then the lack of a byte-code compiler is just a quality of
implementation issue.

If that's *not* your question, them I'm stumped.


More information about the Python-list mailing list