Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so dont bother with any of their useless mail servers here and just use oauth login instead. Thank the nice Russians for causing that. :)
Paste
Pasted as Python by Александр Молофеев ( 14 years ago )
#!/usr/bin/python
# -*- coding:u8 -*-
__author__ = '_killed_'
class Singleton(object):
__instance = {}
#__tags={}
def __new__(cls,*a,**k):
__singleton_name__ = k.get('__singleton__',None)
if not cls.__instance:
cls.__instance[__singleton_name__] = super(Singleton, cls).__new__(cls,*a,**k)
return cls.__instance[__singleton_name__]
else:
if __singleton_name__ not in cls.__instance:
cls.__instance[__singleton_name__] = super(Singleton, cls).__new__(cls,*a,**k)
return cls.__instance[__singleton_name__]
else:return cls.__instance[__singleton_name__]
def __init__(self,*argc,**kwargs):
self.__dict__.update(kwargs)
def __str__(self):
return self.__singleton__
single = Singleton()
c1 = [Singleton(__singleton__='c1') for i in xrange(5)]
c2 = [Singleton(__singleton__='c2') for i in xrange(5)]
c3 = [Singleton(__singleton__='c3') for i in xrange(5)]
for i in [c1,c2,c3]:
print map(str,i)
print map(id,i)
print map(hash,i)
print "*"*30
print single._Singleton__instance
'''
C:\Python27\python.exe E:/develop/tester/test.py
['c1', 'c1', 'c1', 'c1', 'c1']
[26077456, 26077456, 26077456, 26077456, 26077456]
[1629841, 1629841, 1629841, 1629841, 1629841]
******************************
['c2', 'c2', 'c2', 'c2', 'c2']
[26078096, 26078096, 26078096, 26078096, 26078096]
[1629881, 1629881, 1629881, 1629881, 1629881]
******************************
['c3', 'c3', 'c3', 'c3', 'c3']
[26077680, 26077680, 26077680, 26077680, 26077680]
[1629855, 1629855, 1629855, 1629855, 1629855]
******************************
{'c3': <__main__.Singleton object at 0x018DE9F0>, 'c2': <__main__.Singleton object at 0x018DEB90>, 'c1': <__main__.Singleton object at 0x018DE910>, None: <__main__.Singleton object at 0x018DE850>}
Process finished with exit code 0
'''
Revise this Paste