Newbie namespace question

Steve Holden steve at
Wed Dec 22 16:05:34 CET 2004

bcarlso at wrote:

> I have a variable that I want to make global across all modules, i.e. I
> want it added to the builtin namespace.  Is there a way to do this?
Of course: you can do *anything* in Python. I'm not sure this is to be 
recommended, but since you ask ... if you have

print myname

then in some other module (and here specifically in the interactive 
interpreter) you can bind a value to "myname" in __builtins__ and it 
will be seen by when it's imported:

  >>> __builtins__.myname = "MyValue"
  >>> myname
  >>> import mymod

Having said all that, you have to be careful, since it's necessary to 
explicity assign to __builtins__.myname to change the value - if you 
just assign to myname then you create a new myname in the module's 
global namespace that will make the name in __builtins__ inaccessible.

So, what with that plus the way the names automagically appear it's 
probably something to relegate to the "definitely not best practice" 

Steve Holden     
Python Web Programming
Holden Web LLC      +1 703 861 4237  +1 800 494 3119

More information about the Python-list mailing list