Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > What makes functions special?

Reply
Thread Tools

What makes functions special?

 
 
Eric Snow
Guest
Posts: n/a
 
      07-09-2011
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
 
Reply With Quote
 
 
 
 
Eric Snow
Guest
Posts: n/a
 
      07-09-2011
On Sat, Jul 9, 2011 at 4:41 PM, Ben Finney <(E-Mail Removed)> wrote:
> Eric Snow <(E-Mail Removed)> writes:
>
>> 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

> --
> *\ * * * *“The reason we come up with new versions is not to fix bugs. |
> *`\ * * * * * * * * * * It's absolutely not.” —Bill Gates, 1995-10-23 |
> _o__) * * * * * * * * * * * * * * * * ** * * * * * * * * * * * * * * *|
> Ben Finney
> --
> http://mail.python.org/mailman/listinfo/python-list
>

 
Reply With Quote
 
 
 
 
Eric Snow
Guest
Posts: n/a
 
      07-10-2011
On Sat, Jul 9, 2011 at 6:38 PM, Ben Finney <(E-Mail Removed)> wrote:
> Eric Snow <(E-Mail Removed)> writes:
>
>> On Sat, Jul 9, 2011 at 4:41 PM, Ben Finney <(E-Mail Removed)> wrote:
>> > Eric Snow <(E-Mail Removed)> writes:
>> >> 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.

>> I would like to know if it is really that big a deal

>
> 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

>> and if the optimized execution of code objects is a CPython
>> implementation detail or a specification of the language.

>
> I don't know that it's a specification. But functions result in code
> objects, and other statements don't; I am not seeing why treating them
> differently is surprising.
>
> --
> *\ * * * * *“I see little commercial potential for the Internet for at |
> *`\ * * * * * * * * * * * * * * * least ten years.” —Bill Gates, 1994 |
> _o__) * * * * * * * * * * * * * * * * ** * * * * * * * * * * * * * * *|
> Ben Finney
> --
> http://mail.python.org/mailman/listinfo/python-list
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: What makes functions special? Eric Snow Python 3 07-10-2011 07:04 AM
Shared functions vs Non-Shared Functions tshad ASP .Net 11 05-27-2005 05:53 PM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM
Help! Passing Templates functions to template functions ILLOGIC C++ 1 06-01-2004 10:51 PM
Exportable class functions as stand alone functions to .DLL or .SO Timothy Wong C++ 3 05-20-2004 01:44 PM



Advertisments