这篇文章主要介绍了设计模式中的单例模式Python实例,需要的朋友可以参考下
注:使用的是Python 2.7。
一个简单实现
复制代码 代码如下:
class Foo(object):
__instance = None
def __init__(self):
pass
@classmethod
def getinstance(cls):
if(cls.__instance == None):
cls.__instance = Foo()
return cls.__instance
if __name__ == '__main__':
foo1 = Foo.getinstance()
foo2 = Foo.getinstance()
print id(foo1)
print id(foo2)
print id(Foo())
以上就是Python设计模式之单例模式实例的详细内容,更多请关注0133技术站其它相关文章!