Python Global State

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Feb 3 22:42:02 CET 2009


En Tue, 03 Feb 2009 17:10:18 -0200, er <erobererunc at gmail.com> escribió:

> It might be nice if Python could provide a global dictionary, perhaps  
> _G{},
> where you can throw things.  This is actually the solution provided by  
> the
> Lua scripting language.  Thanks for the global_ module solution, I was  
> just
> making sure that was the canonical way.

You *could* do that now, just inject a dictionary into the builtin module.  
sitecustomize.py would be the place to do that (it's executed before the  
main script).

	import __builtin__
	__builtin__.globalvars = {}

Then you can use globalvars[...] everywhere. But your code won't run in  
other locations, nor can you distribute it, you'll have to repeat the same  
steps when you upgrade your Python version, and people will wonder *where*  
such variable name comes from...

Too much trouble, only to avoid typing a single line:

	from config import globalvars

and it doesn't have any of the drawbacks listed above. I'd use import.

-- 
Gabriel Genellina




More information about the Python-list mailing list