Why does __builtins__ mean different things...

James Stroud jstroud at mbi.ucla.edu
Thu Dec 20 18:22:39 EST 2007


Jean-Paul Calderone wrote:
> On Thu, 20 Dec 2007 14:29:35 -0800, James Stroud <jstroud at mbi.ucla.edu> 
> wrote:
>> Hello all,
>>
>> Say I have the following module
>>
>> # amodule.py
>> print __builtins__
>> # end of module
>>
>> Then I have the following little helper script:
>>
>> # helper.py
>> import amodule
>> # end of helper script
>>
>> Now, I do this at the command line:
>>
>> python amodule.py
>>
>> And I get the following output:
>>
>> <module '__builtin__' (built-in)>
>>
>> Which is good.
>>
>> Now, I do this:
>>
>> python helper.py
>>
>> and I get the following output:
>>
>> {'IndexError': <type 'exceptions.IndexError'>, 'all;:
>> ... [etc]
>>
>> I.e.: __builtins__ is a dict when referenced in an imported module, but
>> in *the exact same code*, when executed as a script, it becomes a
>> module! Is this a bug?
>>
>> In other words, what is the good of this? For the purposes of argument,
>> the bad of this is the behavior above. I'm guessing its a bug.
>>
> 
> __builtins__ is "a CPython implementation detail".  In other words, unless
> you are doing really funny stuff, you shouldn't use it.
> 
> What you're probably looking for is the __builtin__ module (difference in
> spelling intentional).  You have to import it explicitly.  Once you do so,
> it behaves in a very unsurprising manner.
> 
> Jean-Paul

Thank you for your help. I will import the __builtin__ module as you 
have suggested--it is what I need.

But....what the heck? You mean if I drop an s I get predictable behavior 
and if I don't, I get unpredictable behavior? I don't think this rule 
should be universal for python. I'm thinking we should keep or drop "z"s 
because they are used much less used. Also, I think they will give the 
dusty corners of the language some street cred (i.e. "__builtinz__" vs. 
"__builtins__", the latter is so homeboy).

Word to the muthaz.

James



More information about the Python-list mailing list