best way to share an instance of a class among modules?

Michael Torrie torriem at
Thu Feb 7 02:08:49 CET 2013

On 02/06/2013 05:45 PM, CM wrote:
> But the function in the module is also within a *class* so I don't
> think the function does have access to the module's global namespace.
> Here's the hierarchy:
> -- Module namespace....
>     ---- class namespace (DatabaseAccess is the name of the class)
>          ---- function namespace
>               This is where the cursor is created.  How do I get it
> into the module namespace?

Oh I see.  You're trying to set a variable in the current module's
global namespace.  There's two ways to do this, the first one is cleaner
and preferred:

1.  Have a function in DatabaseAccess return the cursor then then set it
to a name in code that runs at the module level.

2. use the "global" keyword in the method:

def mymethod(self):
    global blah

    blah = something

Then your module has has blah in it's global method now.  If you want to
stick a variable in another method's global namespace, as long as you've
imported it you just go:

module.newvariable = something

More information about the Python-list mailing list