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

Michael Torrie torriem at gmail.com
Wed Feb 6 19:14:27 EST 2013


On 02/06/2013 05:03 PM, Michael Torrie wrote:
> 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."

Just to be clear:

mod1.py:
a=5

------------------------
mod2.py:
import mod1

def testfunc1():
    print mod1.a

def testfunc2():
    mod1.a = 6

-------------------------
main.py:
import mod1
import mod2

mod2.testfunc1()
mod2.testfunc2()
mod2.testfunc1()

-----------------------

The main.py program will print out "5" and "6."

If I understand your original question, this example will demonstrate
it.  But I could have understood wrong!






More information about the Python-list mailing list