whassup? builtins? python3000? Naah can't be right?

Discussion in 'Python' started by _wolf, Jan 31, 2010.

  1. _wolf

    _wolf Guest

    dear pythoneers,

    i would be very gladly accept any commentaries about what this
    sentence, gleaned from http://celabs.com/python-3.1/reference/executionmodel.html,
    is meant to mean, or why gods have decided this is the way to go. i
    anticipate this guy named Kay Schluehr will have a say on that, or
    maybe even the BDFL will care to pronounce ``__builtins__`` the
    correct way to his fallovers, followers, and fellownerds::

    The built-in namespace associated with the execution of
    a code block is actually found by looking up the name
    __builtins__ in its global namespace; this should be a
    dictionary or a module (in the latter case the module’s
    dictionary is used). By default, when in the __main__
    module, __builtins__ is the built-in module builtins;
    when in any other module, __builtins__ is an alias for
    the dictionary of the builtins module itself.
    __builtins__ can be set to a user-created dictionary to
    create a weak form of restricted execution.

    it used to be the case that there were at least two distinct terms,
    ‘builtin’ (in the singular) and ‘builtins’ (in the plural), some of
    which existed both in module and in dict form (?just guessing?). now
    there is only ‘builtins’, so fortunately the ambivalence between
    singular and plural has gone—good riddance.

    but why does ``__builtins__`` change its meaning depending on whether
    this is the scope of the ‘script’ (i.e. the module whose name was
    present, when calling ``python foobar.py``) or whether this is the
    scope of a secondary module (imported or executed, directly or
    indirectly, by ``foobar.py``)? i cannot understand the reasoning
    behind this and find it highly confusing.

    rationale: why do i care?—i want to be able to ‘export names to the
    global namespace that were not marked private (by an underscore
    prefix) in a python module that i execute via ``exec( compile( get
    ( locator ), locator, 'exec' ), R )`` where ``R`` is supposed to going
    to hold the private names of said module’. it *is* a little arcane but
    the basic exercise is to by-pass python’s import system and get similr
    results... it is all about injecting names into the all-global and the
    module-global namespaces.

    still i get trapped by the above wordings in tzhe docs, and i have a
    weird case of a vanishing names, so maybe some people will care to
    share their thoughts.


    love & ~flow



    thank
     
    _wolf, Jan 31, 2010
    #1
    1. Advertising

  2. _wolf

    Terry Reedy Guest

    On 1/31/2010 4:17 PM, _wolf wrote:

    > but why does ``__builtins__`` change its meaning depending on whether
    > this is the scope of the ‘script’ (i.e. the module whose name was
    > present, when calling ``python foobar.py``) or whether this is the
    > scope of a secondary module (imported or executed, directly or
    > indirectly, by ``foobar.py``)?


    There is a technical reason, which I believe Guido once gave in a post
    to the now-closed py3k list as part of the thread on the change to a
    single name. I do not remember, but I presume it has something to do
    with a difference between executing the main module and imported
    modules. Rest assured that it is intentional and has a reason. You can,
    of course, check if __name__ == '__main__': to know how to access.

    Terry Jan Reedy
     
    Terry Reedy, Feb 1, 2010
    #2
    1. Advertising

  3. Hey, it's really simple! Just like the excerpt from the Learning
    Python book says:

    "Really, the built-in scope is just a built-in module called builtins,
    but
    you have to import builtins to query built-ins because the name
    builtins is not itself
    built-in...."

    :)
     
    Andrej Mitrovic, Feb 1, 2010
    #3
  4. En Sun, 31 Jan 2010 18:17:04 -0300, _wolf <>
    escribió:

    > dear pythoneers,
    >
    > i would be very gladly accept any commentaries about what this
    > sentence, gleaned from
    > http://celabs.com/python-3.1/reference/executionmodel.html,
    > is meant to mean, or why gods have decided this is the way to go. i
    > anticipate this guy named Kay Schluehr will have a say on that, or
    > maybe even the BDFL will care to pronounce ``__builtins__`` the
    > correct way to his fallovers, followers, and fellownerds::
    >
    > The built-in namespace associated with the execution of
    > a code block is actually found by looking up the name
    > __builtins__ in its global namespace; this should be a
    > dictionary or a module (in the latter case the module’s
    > dictionary is used). By default, when in the __main__
    > module, __builtins__ is the built-in module builtins;
    > when in any other module, __builtins__ is an alias for
    > the dictionary of the builtins module itself.
    > __builtins__ can be set to a user-created dictionary to
    > create a weak form of restricted execution.


    Short answer: use `import builtins` (spelled __builtin__, no 's' and
    double underscores, in Python 2.x) to access the module containing the
    predefined (built-in) objects.

    Everything else is an implementation detail.

    --
    Gabriel Genellina
     
    Gabriel Genellina, Feb 1, 2010
    #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. Sean 'Shaleh' Perry

    Re: Least used builtins?

    Sean 'Shaleh' Perry, Jul 4, 2003, in forum: Python
    Replies:
    2
    Views:
    332
    Lulu of the Lotus-Eaters
    Jul 5, 2003
  2. Alex Martelli

    googling for fun (and profit...? naah!-)

    Alex Martelli, Oct 31, 2003, in forum: Python
    Replies:
    0
    Views:
    364
    Alex Martelli
    Oct 31, 2003
  3. Raymond Hettinger
    Replies:
    8
    Views:
    312
    Daniel 'Dang' Griffith
    Apr 21, 2004
  4. Raymond Hettinger
    Replies:
    0
    Views:
    315
    Raymond Hettinger
    Apr 19, 2004
  5. Kamilche
    Replies:
    5
    Views:
    344
    Kamilche
    Jun 10, 2004
Loading...

Share This Page