Global variable visibility

Aahz aahz at pythoncraft.com
Fri Nov 1 09:03:09 EST 2002


In article <mailman.1036134421.16482.python-list at python.org>,
David LeBlanc <whisper at oz.net> wrote:
>
>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?

It would be a rebinding, but what I was actually referring to was
rebinding Db in main.py.  The only strong argument against using
commands.Db to set it is that someone looking at commands.py in
isolation can't see how Db gets set.  Here's a variation that's closer
to what I usually do:

    class cfg: pass

    def Initialize(Db):
        cfg.Db = Db

You could use a dict instead of a class, but I find the direct attribute
access simpler.

BTW, please do not top-post; it makes it difficult to see the context.
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

Project Vote Smart: http://www.vote-smart.org/



More information about the Python-list mailing list