[Tutor] imported scope

Kent Johnson kent37 at tds.net
Wed Apr 29 16:05:04 CEST 2009


On Wed, Apr 29, 2009 at 9:56 AM, spir <denis.spir at free.fr> wrote:
> Hello,
>
> I have an issue with scopes (namespaces).
> Say I have a source file 'test.py' with the single line:
>   print vars()         # same with locals()
>
> Now, from the command-line:
>
> spir at o:~/prog/pijnu$ python test.py
> {'__builtins__': <module '__builtin__' (built-in)>, '__name__': '__main__', '__file__': 'test.py', '__doc__': None}
>
> Right, but when imported (my question is at the bottom of the output ;-):
>
> spir at o:~/prog/pijnu$ python
> Python 2.5.2 (r252:60911, Oct  5 2008, 19:24:49)
> [GCC 4.3.2] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> import test
> {'__builtins__': {'IndexError': <type 'exceptions.IndexError'>, 'all': <built-in function all>, 'vars': <built-in function vars>, 'SyntaxError': <type 'exceptions.SyntaxError'>, 'unicode': <type 'unicode'>,
> [... here 5918 characters ...]
> 'AttributeError': <type 'exceptions.AttributeError'>, 'OverflowError': <type 'exceptions.OverflowError'>}, '__name__': 'test', '__file__': 'test.pyc', '__doc__': None}
>>>>
>
> Can someone explain?

There is some discussion here:
http://mail.python.org/pipermail/python-3000/2007-March/006161.html

Short answer: __builtins__ is an implementation detail that you should ignore.

Kent


More information about the Tutor mailing list