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

Ethan Furman ethan at stoneleaf.us
Thu Feb 7 02:36:28 CET 2013


On 02/06/2013 04:57 PM, Ethan Furman wrote:
> On 02/06/2013 04:43 PM, c 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?
>
> In the class namespace you have something like:
>
>          self.shared_cursor = ...
>
> to get that into the module namespace instead, just remove the 'self':
>
>          shared_cursor = ...

Correction:

As Michael Torrie pointed out, the 'global' keyword is needed:

            global shared_cursor
            shared_cursor = ...




More information about the Python-list mailing list