Newbie pythoner, with bizzare problem

Gordon McMillan gmcm at hypernet.com
Sat Jul 22 22:16:26 EDT 2000


Paul Prescod wrote:

>Gordon McMillan wrote:
>> 
>> Only sometimes. At other times __builtins__ is a dictionary, so
>> 
>> inp = __builtins__.open("...")
>> 
>> would fail, and you'd have to use
>> 
>> inp = __builtins__['open']("....")
>
>Why is __builtins__ a shape-shifter?

Here's a hint:

    	>>> exec('print type(__builtins__)', {},{})
    	<type 'dictionary'>
    	>>> exec('print type(__builtins__)', globals(),locals())
    	<type 'module'>
    	>>>

Similarly, __builtins__ is a module in a script, but it's a dict inside a 
module. Someplace in the docs it says not to use __builtins__ directly 
since it's basically a contrivance to allow controlling the environment in 
which code runs.

-Gordon



More information about the Python-list mailing list