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

W

_wolf

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
 
T

Terry Reedy

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
 
A

Andrej Mitrovic

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

:)
 
G

Gabriel Genellina

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top