Background: I'm working with a device vendor-supplied API module which stores device login data (device hostname, session ID, etc) as a global variable; I'm trying to figure out if it's possible to have multiple instances of the module to represent logins to multiple devices.
So far I've attempted a couple strategies with test code, none of which have worked:
Test module code: statictest.py
count = 0
class Test():
@classmethod
def testcount(cls):
global count
count += 1
return count
First attempt: import module multiple times and instantiate:
>>> import statictest as s1
>>> import statictest as s2
>>> s1.Test.testcount()
1
>>> s1.Test.testcount()
2
>>> s2.Test.testcount()
3
Second try: import module inside class, instantiate class:
#!/usr/bin/env python2.7
class TestMod():
s = __import__('statictest')
def test(self):
ts = self.s.Test()
return ts.testcount()
t = TestMod()
u = TestMod()
print t.test()
print u.test()
That one didn't work either:
[~/]$ ./moduletest.py
1
2
This seems like it should be obvious but is there any way to encapsulate a module such that multiple instances are available?