I am using the Borg pattern with mutual inclusion of modules. See the example code (not the real code but it shows the problem) below. In this case, I have two different Borgs because the class names (and I guess the class) are seen as different by the interpreter.
Is there a way to use the Borg in that case without reworking the module architecture?
Module borg.py
import borg2
class Borg:
_we_are_one = {}
def __init__(self):
self.__dict__ = Borg._we_are_one
try:
self.name
except AttributeError:
self.name = "?"
print self.__class__, id(self.__dict__)
def fct_ab():
a = Borg()
a.name = "Bjorn"
b = Borg()
print b.name
if __name__ == "__main__":
fct_ab()
borg2.fct_c()
Module borg2.py
import borg
def fct_c():
c = borg.Borg()
print c.name
The result is
__main__.Borg 40106720
__main__.Borg 40106720
Bjorn
borg.Borg 40106288
?
In order to clarify my problem:
Why does Python consider __main__.Borg and borg.Borg as two different classes?
The problem only occurs in your main-function. Move that code
to its own file and everything is as you’d expect. This code
delivers this output: