CARVIEW |
Select Language
HTTP/2 302
server: nginx
date: Wed, 16 Jul 2025 15:33:33 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=1.436068, exclusion.robots;dur=0.062704, exclusion.robots.policy;dur=0.025781, esindex;dur=0.021011, cdx.remote;dur=12.281748, LoadShardBlock;dur=241.494307, PetaboxLoader3.datanode;dur=172.469994
x-app-server: wwwb-app219
x-ts: 302
x-tr: 335
server-timing: TR;dur=0,Tw;dur=0,Tc;dur=0
set-cookie: SERVER=wwwb-app219; 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, 16 Jul 2025 15:33:34 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=4.144600, exclusion.robots;dur=0.022229, exclusion.robots.policy;dur=0.010408, esindex;dur=0.010356, cdx.remote;dur=44.542097, LoadShardBlock;dur=123.384178, PetaboxLoader3.datanode;dur=192.659298, load_resource;dur=219.027838, PetaboxLoader3.resolve;dur=114.678703
x-app-server: wwwb-app219
x-ts: 200
x-tr: 464
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