Global variable visibility

David LeBlanc whisper at oz.net
Fri Nov 1 02:11:19 EST 2002


Ah, the commands.Db idiom wasn't one I thought of. My "punt" was to create
an Initialize method in the commands.py file and call it with Db as a param:

Db = None

def Initialize(database)
	global Db
	Db = database

commands.Db is obviously simpler and more obvious too.

Not exactly sure what you mean by rebinding. Would (in commands.py) Db =
__main__.Db be a rebinding or another reference?

David LeBlanc
Seattle, WA USA

> Yes, but Don't Do That.  Here are two options:
>
>     import commands
>     commands.Db = Db
>
> or
>
>     import commands
>     commands.set_db(Db)
>
>     # in commands.py
>     def set_db(newDb):
>         global Db
>         Db = newDb
>
> Both of these only work if Db is either static or mutable; if you rebind
> __main__.Db, your program will have R-E-A-L problems.
> --
> Aahz (aahz at pythoncraft.com)           <*>
http://www.pythoncraft.com/





More information about the Python-list mailing list