type(__builtins__) strangeness

Gordon McMillan gmcm at hypernet.com
Sat Nov 10 16:59:20 EST 2001


Michael Hudson wrote:

> tavis at calrudd.com (Tavis Rudd) writes:

>> why does type(__builtins__) return <type 'module'> when called in the
>> __main__ scope and <type 'dictionary'> when called inside an imported
>> module? 
 
> Beause it's specially designed to confuse people who are too
> inquisitive :)
> 
> Seriously, I don't know, but I know that I don't need to know.
> Obscure implementation detail.  Ignore it.

Not that far off!

When you exec or eval code, you're allowed to pass in a globals
dictionary. If you want to override the exec'd / eval'd code's access to 
potentially dangerous builtins, you can override what it sees as 
__builtins__ by creating a dict named "__builtins__".

If no "__builtins__" is found in the dict you hand to exec / eval, then one 
is created. 

If you really need it, use "import __builtin__" to get the module.

- Gordon



More information about the Python-list mailing list