Adding objects to __builtins__

Peter Hansen peter at engcorp.com
Wed Jun 2 21:20:36 CEST 2004


Matteo Merli wrote:

> is there a way to save objects in the __builtins__ namespace? 
> the goal is to make them available in all the modules of my app, without
> reimporting all the things and keep references...
> 
> The curious thing is that when i try from the interactive interpreter the
> thing just works:

Does this help?

 >>> import __builtins__
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
ImportError: No module named __builtins__
 >>> import __builtin__

By the way, you *really* shouldn't use this as a "convenient"
way of getting global variables or other things around to your
various modules.  That would be almost the worst thing for
code maintainability that you could do.  You'll come to hate
yourself for doing it, down the road.

There are some valid use cases for doing such a thing, but
sticking just any old function in there is not it.

-Peter



More information about the Python-list mailing list