[Level 2] Singleton in Python.
#!/usr/bin/env python class Singleton(object): _instance = None def __new__(cls, *args, **kwargs): if not cls._instance: cls._instance = super(Singleton, cls).__new__(cls, *args, **kwargs) return cls._instance print me print you print me == you print id(me) == id(you)
$ ./testSingleton.py True True $
You could also use metaclass to implement it.
#!/usr/bin/env python class Singleton(type): def __init__(self, *args, **kwargs): super(Singleton, self).__init__(*args, **kwargs) self.__instance = None def __call__(self, *args, **kwargs): if self.__instance is None: self.__instance = super(Singleton, self).__call__(*args, **kwargs) return self.__instance class s(object): __metaclass__ = Singleton def __init__(self, size): self.__size = size def getSize(self): return self.__size class n(object): def __init__(self, size): self.__size = size def getSize(self): return self.__size a = s(1) print a.getSize() b = s(2) print b.getSize() c = n(3) print c.getSize() d = n(4) print d.getSize() print a == b print c == d
$ ./testMetaclassSingleton.py 1 1 3 4 True False $
in Python 2.6 or later, you could use decorator to implement Singleton:
#!/bin/env python def singleton(cls): instances = {} def getInstance(*args, **kwargs): if cls not in instances: instances[cls] = cls(*args, **kwargs) return instances[cls] return getInstance @singleton class s(object): def __init__(self, size): self.__size = size def getSize(self): return self.__size class n(object): def __init__(self, size): self.__size = size def getSize(self): return self.__size a = s(1) print a.getSize() b = s(2) print b.getSize() c = n(3) print c.getSize() d = n(4) print d.getSize() print a == b print c == d
Wish this helps.
regards,
Stanley Huang
Comments
Post a Comment