Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > whassup? builtins? python3000? Naah can't be right?

Reply
Thread Tools

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

 
 
_wolf
Guest
Posts: n/a
 
      01-31-2010
dear pythoneers,

i would be very gladly accept any commentaries about what this
sentence, gleaned from http://celabs.com/python-3.1/referen...tionmodel.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
 
Reply With Quote
 
 
 
 
Terry Reedy
Guest
Posts: n/a
 
      02-01-2010
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


 
Reply With Quote
 
 
 
 
Andrej Mitrovic
Guest
Posts: n/a
 
      02-01-2010
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...."


 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      02-01-2010
En Sun, 31 Jan 2010 18:17:04 -0300, _wolf <(E-Mail Removed)>
escribió:

> dear pythoneers,
>
> i would be very gladly accept any commentaries about what this
> sentence, gleaned from
> http://celabs.com/python-3.1/referen...tionmodel.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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
googling for fun (and profit...? naah!-) Alex Martelli Python 0 10-31-2003 11:36 AM



Advertisments