Early binding as an option

Discussion in 'Python' started by Chris Angelico, Aug 2, 2011.

  1. As I understand it, Python exclusively late-binds names; when you
    define a function, nothing is ever pre-bound. This allows a huge
    amount of flexibility (letting you "reach into" someone else's
    function and change its behaviour), but it's flexibility that most
    programs use seldom if at all.

    First off: Is there any way to request/cause some names to be bound
    early? and secondly, should there be?

    Argument against: Late binding is a Good Thing, and having some things
    bound early would be confusing.

    Argument in favour: Efficiency is also a Good Thing, and with staples
    like 'len', it's unlikely anyone will want to change them - yet the
    interpreter still has to do a namespace lookup every time.

    I would want the end programmer to have the ultimate power here (not
    the module designer). Something along the lines of: This global name
    will never change, so don't bother looking it up every time.

    As an example of this difference, Pike uses early binding for some
    things; when I did the perfect numbers testing in the other thread
    (discussion thread, not thread of execution!), Pike performed
    significantly better; I believe this is in part due to the formal
    declarations of variables, and the consequential simplification of
    local code, although since there are no globals being looked up here,
    there's little to be gained from those.

    Is this the realm of JIT compilation, or can it be done in regular CPython?

    Chris Angelico
    Chris Angelico, Aug 2, 2011
    #1
    1. Advertising

  2. Chris Angelico <> writes:

    > As I understand it, Python exclusively late-binds names; when you
    > define a function, nothing is ever pre-bound. This allows a huge
    > amount of flexibility (letting you "reach into" someone else's
    > function and change its behaviour), but it's flexibility that most
    > programs use seldom if at all.


    I agree with you on your last remark, but unfortunately it's part of the
    language. Therefore, there *are* programs that rely on the ability to
    rebind 'let' and others. Changing this would require changing the
    language, basically turning some builtins into keywords.

    (BTW, the dynamic binding also has implications for security.)

    [...]
    > Argument in favour: Efficiency is also a Good Thing, and with staples
    > like 'len', it's unlikely anyone will want to change them - yet the
    > interpreter still has to do a namespace lookup every time.


    Yes, and it can't do common subexpression elimination, code hoisting,
    etc. Basically, nothing can be optimized, and the interpreter has to
    execute bytecode that exactly represents source code.

    > I would want the end programmer to have the ultimate power here (not
    > the module designer). Something along the lines of: This global name
    > will never change, so don't bother looking it up every time.


    Maybe some module could provide specialized, "use-at-your-own-risk"
    versions of some functions/operators. An example is '+' which can mean
    so many things that any use of it probably spends more time finding the
    right version than actually doing the work.

    The problem with such pre-bound identifiers is that anybody with
    performance problems would start peppering his/her code with things like
    plus_float_float(x,y), leading to unreadable code, to all kinds of
    strange errors, etc. Nobody really wants this probably.

    [...]
    > Is this the realm of JIT compilation, or can it be done in regular
    > CPython?


    No, it's a matter of language definition. A JIT can't do much here
    (actually jitting is almost orthogonal to that question), at least it
    couldn't do much better than CPython. It just has to go through all the
    lookups. IIRC, unladden-swallow has tried the JIT route, using LLVM as
    the backend. It seems they gave up.

    -- Alain.
    Alain Ketterlin, Aug 2, 2011
    #2
    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. Shawn
    Replies:
    0
    Views:
    436
    Shawn
    Dec 8, 2003
  2. Max
    Replies:
    1
    Views:
    411
    Natty Gur
    Jan 4, 2004
  3. =?Utf-8?B?UGxhdA==?=
    Replies:
    5
    Views:
    1,885
    =?Utf-8?B?UGxhdA==?=
    Mar 16, 2005
  4. Replies:
    2
    Views:
    838
    Kevin Grover
    Oct 20, 2006
  5. Chris Angelico

    Re: Early binding as an option

    Chris Angelico, Aug 2, 2011, in forum: Python
    Replies:
    6
    Views:
    216
    Chris Torek
    Aug 4, 2011
Loading...

Share This Page