Newbie namespace question
Steve Holden
steve at holdenweb.com
Wed Dec 22 10:05:34 EST 2004
bcarlso at gmail.com 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
# mymod.py
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 mymod.py when it's imported:
>>> __builtins__.myname = "MyValue"
>>> myname
'MyValue'
>>> import mymod
MyValue
>>>
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"
category.
regards
Steve
--
Steve Holden http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/
Holden Web LLC +1 703 861 4237 +1 800 494 3119
More information about the Python-list
mailing list