What makes functions special?

E

Eric Snow

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,
but it has its own label to mark the contrast with execution time. So
why do functions get this special treatment?

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.

Maybe I am missing something, or maybe it is super obvious, but isn't
this a critical point? Is it just a CPython implementation detail
that code objects should provide an optimization, or is it a
specification of the language? From the docs, the code objects in of
function objects are the latter, but the optimization expectation is
not clearly indicated. Are there other motivations behind code
objects that I am missing? Am I wrong about the optimization
expectation?

Thoughts?

-eric

[1] http://bugs.python.org/issue12374
 
E

Eric Snow

Eric Snow said:
A tracker issue [1] recently got me thinking about what makes
functions special.

As you describe, functions are special for your scenario because a
function definition needs to result in executable code as an object.
Definition time actually happens during compile time, but it has its
own label to mark the contrast with execution time. So why do
functions get this special treatment?

You answer this question.
No other objects have code objects. No other objects in Python have
this special optimization.

Yes. The two facts are directly related.
Maybe I am missing something, or maybe it is super obvious, but isn't
this a critical point?

What is the crisis (“a stark change from one state to another”) that
you're referring to by “a critical point”?

Yes, functions are different and are treated differently. What's your
question?
From the docs, the code objects in of function objects are the latter,
but the optimization expectation is not clearly indicated. Are there
other motivations behind code objects that I am missing? Am I wrong
about the optimization expectation?

What optimisation expectation?
Thoughts?

I think yours need to be expressed more explicitly; I'm not seeing the
issue that concerns you.

My point is that functions are special in Python because they provide
a built in optimization via the special execution of code objects. I
would like to know if it is really that big a deal, and if the
optimized execution of code objects is a CPython implementation detail
or a specification of the language.

-eric
 
E

Eric Snow

Eric Snow said:
No other objects have code objects. No other objects in Python have
this special optimization.

Yes. The two facts are directly related.
[…]
Yes, functions are different and are treated differently. What's
your question?

My point is that functions are special in Python because they provide
a built in optimization via the special execution of code objects.

Functions are special because they define a code object.

Right. But the point is that the code objects (in CPython at least)
allow a special execution of the function body. What does that
special execution give us? I am guessing a sufficient performance
increase. Is there anything else? And do other Python
implementations do anything special with code objects?

I am not questioning why it was done a certain way, but rather trying
to understand how Python works.
Is *what* really that big a deal?

Perhaps this could be clearer if you'd describe what it is that
surprises you, and how you'd expect it to be different.

I don't have any unexpected failure that I ran into or anything like
that. I am just trying to learn more about the ins and outs of Python
and that tracker issue got me thinking. And I know that there are
plenty of people on this list that know a lot more about Python than I
do. :) So I thought I would ask (in my own obscure way) if I was
understanding the definition/execution model correctly. Sorry for any
confusion.

-eric
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top