Hi, I've been trying to get some sort of singleton working in python, but I struggle a bit and I thought I'd ask for advice.
The first approach was simply to use a module, and every variable in it will be seen by all who import the module. That works in some cases, but not if I have the following structure: one/ __init__.py mod1.py run.py two/ __init__.py mod2.py run.py looks like this: #!/usr/bin/env python import mod1 print mod1.number import two.mod2 print two.mod2.number mod1.py looks like this: import random number=random.randint(0,100) mod2.py looks like this import one.mod1 number = one.mod1.number PYTHONPATH is set to the directory containing the 'one' and 'two' directories. Now when I run the 'run.py', it will print two different numbers. sys.modules tells me that 'mod1' is imported as both 'one.mod1' and 'mod1', which explains the result. Looking at: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52558 I find a singleton class, but it has the same problem. If it's imported in two different ways, I have not a singleton but a ... 'doubleton' or something. It is possible to solve this by always importing with the complete path like 'one.mod1', even when inside the 'one' directory, but that's an error waiting to happen. So how do people solve this? Is there an obvious way that I missed? I'm thankful for any advice you might provide. -- http://mail.python.org/mailman/listinfo/python-list