python 面对对象创建单实例

 1 #!/usr/bin/env python
 2 #coding:utf-8
 3 class instance(object):
 4     __instance = None    #类私有方法
 5     def __new__(cls):
 6         if cls.__instance == None:
 7             cls.__instance = object.__new__(cls)
 8             return cls.__instance
 9         else:
10             return cls.__instance
11     
12 test1 = instance()
13 print id(test1)
14 test2 = instance()
15 print id(test2)
16 
17 显示结果
18 140558160170640
19 140558160170640

 

posted @ 2017-10-19 11:34  lichenxi  阅读(136)  评论(0编辑  收藏  举报