Urizev wrote:
Do you see it now I snipped the irrelevant output?
The problem is the structure of your program. The myset module is imported
twice by Python, once as "myset" and once as "__main__". Therefore you get
two distinct MySet classes, and consequently two distinct MySet.__instanc e
class attributes.
Move the
if __name__ == "__main__": ...
statements into a separate module, e. g. main.py:
import myset
import member
if __name__ == "__main__":
print "Executing main"
set1 = myset.MySet()
set2 = myset.MySet()
mbr1 = member.Member()
mbr2 = member.Member()
mbr3 = member.Member()
Now main.py and member.py share the same instance of the myset module and
should work as expected.
Peter
Hi everyone
>
I have developed the singleton implementation. However I have found a
strange behaviour when using from different files. The code is
attached.
>
Executing main
>
I have developed the singleton implementation. However I have found a
strange behaviour when using from different files. The code is
attached.
>
Executing main
New singleton:
<__main__.Singl eton instance at 0x2b98be474a70>
<__main__.Singl eton instance at 0x2b98be474a70>
New singleton:
<myset.Singleto n instance at 0x2b98be474d88>
<myset.Singleto n instance at 0x2b98be474d88>
I do not know why, but it creates two instances of the singleton. Does
anybody know why?
anybody know why?
The problem is the structure of your program. The myset module is imported
twice by Python, once as "myset" and once as "__main__". Therefore you get
two distinct MySet classes, and consequently two distinct MySet.__instanc e
class attributes.
Move the
if __name__ == "__main__": ...
statements into a separate module, e. g. main.py:
import myset
import member
if __name__ == "__main__":
print "Executing main"
set1 = myset.MySet()
set2 = myset.MySet()
mbr1 = member.Member()
mbr2 = member.Member()
mbr3 = member.Member()
Now main.py and member.py share the same instance of the myset module and
should work as expected.
Peter
Comment