Re: What makes functions special?

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

  1. Eric Snow

    Eric Snow Guest

    On Sat, Jul 9, 2011 at 6:21 PM, Terry Reedy <> 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
    detail.

    -eric

    > Terry Reedy
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
    Eric Snow, Jul 10, 2011
    #1
    1. Advertising

  2. 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
    allowed?"

    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.




    --
    Steven
    Steven D'Aprano, Jul 10, 2011
    #2
    1. Advertising

  3. Eric Snow

    Eric Snow Guest

    On Sat, Jul 9, 2011 at 7:34 PM, Steven D'Aprano
    <> wrote:
    > 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.
    >


    Yeah, that's pretty much it. Is that all there is to it? I was
    saying optimized, but I guess there isn't much special optimization
    going on then. Thanks for taking the time.

    -eric

    > 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
    > allowed?"
    >
    > 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.
    >
    >
    >
    >
    > --
    > Steven
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
    Eric Snow, Jul 10, 2011
    #3
  4. Eric Snow

    Terry Reedy Guest

    On 7/9/2011 6:34 PM, Steven D'Aprano wrote:

    > Suppose instead an implementation of Python did not pre-compile the
    > function. Each time you called spam(n), the implementatio n would have to
    > locate the source code and interpret it on the spot. Would that be
    > allowed?"
    >
    > 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.


    while and for loops would also be terribly slow if their bodies were not
    compiled but were reinterpreted at the source code level for each loop.
    Having to reinterpred the compiled byte code for each loop does make
    them slower than compiled to native code C. Same as for functions.
    Terry Reedy, Jul 10, 2011
    #4
    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,203
    Keith A. Lewis
    Feb 5, 2004
  4. Juanjo
    Replies:
    0
    Views:
    245
    Juanjo
    Sep 10, 2008
  5. Eric Snow

    What makes functions special?

    Eric Snow, Jul 9, 2011, in forum: Python
    Replies:
    2
    Views:
    172
    Eric Snow
    Jul 10, 2011
Loading...

Share This Page