best way to share an instance of a class among modules?
chaelon at gmail.com
Thu Feb 7 01:43:14 CET 2013
On Feb 6, 7:03 pm, Michael Torrie <torr... at gmail.com> wrote:
> On 02/06/2013 03:41 PM, CM wrote:
> > Thank you. But, I'm sorry, I'm not following this enough to get it to
> > work. Shouldn't it be a little more like this:
> No, not exactly.
> > # in utilities module
> > shared_cursor = DatabaseAccess_instance #but how? see my question
> > below...
> How what?
> > # in importer
> > import utilities
> > self.shared_cursor = utilities.shared_cursor ("self" is here to make
> > cursor available to all functions in importer
> Umm no. For one you're using self incorrectly. For two, it already is
> visible to all functions in the module. You just have to refer to it as
I was using self correctly, I think; but I should have said that the
code in the importing module would be within a class, so self there
refers to that class. But that's a side point.
I agree that utilities.shared_cursor is visible within the importing
module. But the problem below remains for me...
> > My only problem, then, is I create the shared_cursor from within a
> > function within the instance of DatabaseAccess(). How then do I pass
> > it from within the function's namespace to the module's namespace so
> > that I can do that first line?
> Every function in a module has access to the module's global namespace.
> And your shared_cursor is there, inside of the utilities reference,
> since utilities was imported into your module, "importer."
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?
More information about the Python-list