petrum
petrum

Reputation: 1

'__builtins__' from a module different than main?

I've noticed the globals() dictionary contains different data inside a module vs the main script. I see the main difference is the '__builtins__' variable.

This is unexpected, can someone please explain why this happens?

$> cat main.py
import foo
print "From main", globals()

$> cat foo.py
print "From module:", globals()

$> python main.py
From module: {'__builtins__': {'bytearray': <type 'bytearray'>, 
'IndexError': <type 'exceptions.IndexError'>, 'all': <built-in function all>, 
'help': Type help() for interactive help, or help(object) for help about object.
#...much more stuff
From main {'__builtins__': <module '__builtin__' (built-in)>, 
'__file__': 'main.py', '__package__': None, '__name__': '__main__', 
'foo': <module 'foo' from '.../foo.pyc'>, '__doc__': None}

I'm using the Python version 2.6.6

$> python --version
Python 2.6.6

Upvotes: 0

Views: 58

Answers (1)

Brian Cain
Brian Cain

Reputation: 14619

The reason why they're different is described in the documentation.

globals() Return a dictionary representing the current global symbol table. This is always the dictionary of the current module (inside a function or method, this is the module where it is defined, not the module from which it is called).

Upvotes: 1

Related Questions