Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   inconsistent value from __builtins__ (http://www.velocityreviews.com/forums/t336441-inconsistent-value-from-__builtins__.html)

Michael Hohn 10-01-2004 10:16 PM

inconsistent value from __builtins__
 
Hi,
using the file builtin_check.py with content

# Module builtin_check
# Inconstency in the binding of __builtins__

def get_binding(name):
return locals()[name]

def get_global_binding(name):
return globals()[name]

and running

import builtin_check
print type(builtin_check.get_global_binding('__builtins_ _'))
print type(__builtins__)

in the toplevel, I get the following results:

Python 2.2.3 (#2, Jun 16 2004, 21:14:24)
[GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import builtin_check
>>> print type(builtin_check.get_global_binding('__builtins_ _'))

<type 'dict'>
>>> print type(__builtins__)

<type 'module'>


Python 2.3.3 (#1, May 10 2004, 11:29:59)
[GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import builtin_check
>>> print type(builtin_check.get_global_binding('__builtins_ _'))

<type 'dict'>
>>> print type(__builtins__)

<type 'module'>

Is this difference in return value intentional?


Thanks,
Michael


Alex Martelli 10-01-2004 10:45 PM

Re: inconsistent value from __builtins__
 
Michael Hohn <hohn@hooknose.lbl.gov> wrote:
...
> >>> print type(builtin_check.get_global_binding('__builtins_ _'))

> <type 'dict'>
> >>> print type(__builtins__)

> <type 'module'>

...
> Is this difference in return value intentional?


Well, it's _documented_ that '__builtins__' can be either a dictionary
or a module, and it's been that way for a long time. Whether it's
intentional (or sensible), I don't know. In any case, the idea is that
if you need to access the built-in namespace directly, you should start
with "import __builtin__" (note, no 's') which will definitely give you
a module. Yeah, it _is_ somewhat confusing:-(.


Alex


All times are GMT. The time now is 11:55 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57