CARVIEW |
Select Language
HTTP/2 302
server: nginx
date: Thu, 17 Jul 2025 19:20:51 GMT
content-type: text/plain; charset=utf-8
content-length: 0
x-archive-redirect-reason: found capture at 20071208115112
location: https://web.archive.org/web/20071208115112/https://wiki.python.org/moin/SingletonProgramming
server-timing: captures_list;dur=0.550177, exclusion.robots;dur=0.020975, exclusion.robots.policy;dur=0.010442, esindex;dur=0.011215, cdx.remote;dur=41.419996, LoadShardBlock;dur=178.659277, PetaboxLoader3.datanode;dur=45.387276, PetaboxLoader3.resolve;dur=39.007947
x-app-server: wwwb-app211
x-ts: 302
x-tr: 248
server-timing: TR;dur=0,Tw;dur=0,Tc;dur=1
set-cookie: SERVER=wwwb-app211; 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: Thu, 17 Jul 2025 19:20:52 GMT
content-type: text/html;charset=utf-8
x-archive-orig-date: Sat, 08 Dec 2007 11:51:11 GMT
x-archive-orig-server: Apache/2.0.54 (Debian GNU/Linux) mod_fastcgi/2.4.2
x-archive-orig-connection: close
x-archive-guessed-content-type: text/html
x-archive-guessed-charset: utf-8
memento-datetime: Sat, 08 Dec 2007 11:51:12 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="Sun, 31 Dec 2006 17:50:57 GMT", ; rel="memento"; datetime="Sat, 08 Dec 2007 11:51:12 GMT", ; rel="next memento"; datetime="Sat, 13 Aug 2011 00:42:23 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: 52_1_20071208104348_crawl102-c/52_1_20071208114607_crawl107.arc.gz
server-timing: captures_list;dur=0.498066, exclusion.robots;dur=0.021201, exclusion.robots.policy;dur=0.010740, esindex;dur=0.010103, cdx.remote;dur=67.808868, LoadShardBlock;dur=459.341651, PetaboxLoader3.datanode;dur=145.781476, PetaboxLoader3.resolve;dur=438.889863, load_resource;dur=163.084945
x-app-server: wwwb-app211
x-ts: 200
x-tr: 756
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
SingletonProgramming
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))
EditText (last edited 2003-11-18 19:33:36 by yermat)
DeleteCache (cached 2007-08-31 23:47:07)- Login
- Navigation
- Actions
- Your recent pages