What makes functions special?

Discussion in 'Python' started by Eric Snow, Jul 9, 2011.

  1. Eric Snow

    Eric Snow Guest

    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
    Eric Snow, Jul 9, 2011
    #1
    1. Advertising

  2. Eric Snow

    Eric Snow Guest

    On Sat, Jul 9, 2011 at 4:41 PM, Ben Finney <> wrote:
    > Eric Snow <> 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
    >
    Eric Snow, Jul 10, 2011
    #2
    1. Advertising

  3. Eric Snow

    Eric Snow Guest

    On Sat, Jul 9, 2011 at 6:38 PM, Ben Finney <> wrote:
    > Eric Snow <> writes:
    >
    >> On Sat, Jul 9, 2011 at 4:41 PM, Ben Finney <> wrote:
    >> > Eric Snow <> 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
    >
    Eric Snow, Jul 10, 2011
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Razvan
    Replies:
    1
    Views:
    349
    Tor Iver Wilhelmsen
    Sep 30, 2004
  2. Xiangliang Meng
    Replies:
    1
    Views:
    1,576
    Victor Bazarov
    Jun 21, 2004
  3. Joe Blow

    C routines for Special Functions

    Joe Blow, Jan 31, 2004, in forum: C Programming
    Replies:
    26
    Views:
    1,202
    Keith A. Lewis
    Feb 5, 2004
  4. Juanjo
    Replies:
    0
    Views:
    245
    Juanjo
    Sep 10, 2008
  5. Eric Snow

    Re: What makes functions special?

    Eric Snow, Jul 10, 2011, in forum: Python
    Replies:
    3
    Views:
    207
    Terry Reedy
    Jul 10, 2011
Loading...

Share This Page