Skip to content

Commit d385323

Browse files
author
tuntun
committed
Python3中的单例模式示例
1 parent ddd053f commit d385323

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

singleton.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# encoding:utf-8
2+
3+
# -*- Python单例模式示例 -*-
4+
5+
class Singleton(object):
6+
def __new__(cls):
7+
if not hasattr(cls, 'instance'):
8+
cls.instance = super(Singleton, cls).__new__(cls)
9+
return cls.instance
10+
11+
if __name__ == '__main__':
12+
a = Singleton()
13+
b = Singleton()
14+
print(id(a))
15+
print(id(b))

0 commit comments

Comments
 (0)