What makes functions special?

Eric Snow ericsnowcurrently at gmail.com
Sun Jul 10 03:10:55 CEST 2011

On Sat, Jul 9, 2011 at 6:21 PM, Terry Reedy <tjreedy at udel.edu> wrote:
> On 7/9/2011 2:28 PM, Eric Snow wrote:
>> A tracker issue [1] recently got me thinking about what makes
>> functions special.  The discussion there was regarding the distinction
>> between compile time (generation of .pyc files for modules and
>> execution of code blocks), [function] definition time, and [function]
>> execution time.  Definition time actually happens during compile time,
> Not true. For main modules, execution of each statement immediately follows
> compilation, but not for other modules, where compilation and caching of
> code objects may happen years before the function object is created.

So for non-main modules the function definition happens during module
compilation, and for all other code blocks (__main__, exec, etc.) it
happens during execution of the code block?

>> Functions are a special case in Python for providing a more optimized
>> execution of a code block in pure Python code.  And how is that?  When
>> the function is defined, a code object is generated for the function
>> body along with a few "static" details that will be used during
>> execution.  No other objects have code objects.  No other objects in
>> Python have this special optimization.
> A .pyc file is a serialized code object for a module.

I hadn't thought of it like that.  Nice insight.  In that case, do
[non-main] module definition and execution time have the same logical
separation as function phases do?

> As for the rest, I am not sure what you are asking.

Yeah, I have a real knack for communicating.  :)  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


> Terry Reedy
> --
> http://mail.python.org/mailman/listinfo/python-list

More information about the Python-list mailing list