CARVIEW |
Select Language
HTTP/2 302
server: nginx
date: Wed, 23 Jul 2025 08:15:00 GMT
content-type: text/plain; charset=utf-8
content-length: 0
x-archive-redirect-reason: found capture at 20110813004223
location: https://web.archive.org/web/20110813004223/https://wiki.python.org/moin/SingletonProgramming
server-timing: captures_list;dur=0.436837, exclusion.robots;dur=0.015644, exclusion.robots.policy;dur=0.007410, esindex;dur=0.009885, cdx.remote;dur=31.016305, LoadShardBlock;dur=346.582124, PetaboxLoader3.resolve;dur=224.168528, PetaboxLoader3.datanode;dur=80.214314
x-app-server: wwwb-app214
x-ts: 302
x-tr: 412
server-timing: TR;dur=0,Tw;dur=0,Tc;dur=0
set-cookie: SERVER=wwwb-app214; path=/
x-location: All
x-rl: 0
x-na: 0
x-page-cache: MISS
server-timing: MISS
x-nid: DigitalOcean
referrer-policy: no-referrer-when-downgrade
permissions-policy: interest-cohort=()
HTTP/2 200
server: nginx
date: Wed, 23 Jul 2025 08:15:00 GMT
content-type: text/html; charset=utf-8
x-archive-orig-date: Sat, 13 Aug 2011 00:42:23 GMT
x-archive-orig-server: Apache/2.2.16 (Debian)
x-archive-orig-vary: Cookie,User-Agent,Accept-Language
x-archive-orig-set-cookie: MOIN_SESSION_80_ROOT_moin=b254df1d686a48d83ce9bed5abf554a5dff63736; expires=Sat, 13-Aug-2011 01:42:00 GMT; Max-Age=3600; Path=/
x-archive-orig-content-length: 27421
x-archive-orig-connection: close
x-archive-guessed-content-type: text/html
x-archive-guessed-charset: utf-8
memento-datetime: Sat, 13 Aug 2011 00:42:23 GMT
link: ; rel="original", ; rel="timemap"; type="application/link-format", ; rel="timegate", ; rel="first memento"; datetime="Sat, 06 May 2006 22:13:58 GMT", ; rel="prev memento"; datetime="Sat, 08 Dec 2007 11:51:12 GMT", ; rel="memento"; datetime="Sat, 13 Aug 2011 00:42:23 GMT", ; rel="next memento"; datetime="Fri, 08 Jul 2022 05:18:08 GMT", ; rel="last memento"; datetime="Wed, 28 Feb 2024 02:04:00 GMT"
content-security-policy: default-src 'self' 'unsafe-eval' 'unsafe-inline' data: blob: archive.org web.archive.org web-static.archive.org wayback-api.archive.org athena.archive.org analytics.archive.org pragma.archivelab.org wwwb-events.archive.org
x-archive-src: WPO-20110812231327-crawl438/WPO-20110813003427-01798.warc.gz
server-timing: captures_list;dur=0.693699, exclusion.robots;dur=0.025799, exclusion.robots.policy;dur=0.012400, esindex;dur=0.018569, cdx.remote;dur=56.122692, LoadShardBlock;dur=233.420674, PetaboxLoader3.datanode;dur=255.972002, load_resource;dur=179.572644, PetaboxLoader3.resolve;dur=127.443489
x-app-server: wwwb-app214
x-ts: 200
x-tr: 556
server-timing: TR;dur=0,Tw;dur=0,Tc;dur=0
x-location: All
x-rl: 0
x-na: 0
x-page-cache: MISS
server-timing: MISS
x-nid: DigitalOcean
referrer-policy: no-referrer-when-downgrade
permissions-policy: interest-cohort=()
content-encoding: gzip
SingletonProgramming - PythonInfo Wiki
User
Singleton
See also https://c2.com/cgi/wiki?PythonSingleton and https://www.python.org/workshops/1997-10/proceedings/savikko.html
classmethod
pro
- You can use both as simple class or as a singleton.
- You do not need to write code for each class you want to act as singleton.
cons
1 # Code is Public Domain.
2 class Singleton:
3 _singleton = None
4 def getSingleton(cls):
5 if not isinstance(cls._singleton,cls):
6 cls._singleton = cls()
7 return cls._singleton
8
9 getSingleton = classmethod(getSingleton)
10
11 if __name__=='__main__':
12 class Test(Singleton):
13 def test(self):
14 print self.__class__,id(self)
15
16 class Test1(Test):
17 def test1(self):
18 print self.__class__,id(self),'Test1'
19
20 t1 = Test.getSingleton()
21 t2 = Test.getSingleton()
22
23 t1.test()
24 t2.test()
25 assert(isinstance(t1,Test))
26 assert(isinstance(t2,Test))
27 assert(id(t1)==id(t2))
28
29 t1 = Test1.getSingleton()
30 t2 = Test1.getSingleton()
31
32 assert(isinstance(t1,Test1))
33 assert(isinstance(t2,Test1))
34 assert(id(t1)==id(t2))
35
36 t1.test()
37 t1.test1()
38 t2.test()
39 t1.test1()
40
41 t3 = Test.getSingleton()
42
43 t3.test()
44 assert(isinstance(t3,Test))
SingletonProgramming (last edited 2008-11-15 14:00:11 by localhost)