CARVIEW |
Select Language
HTTP/2 302
server: nginx
date: Thu, 24 Jul 2025 23:11:56 GMT
content-type: text/plain; charset=utf-8
content-length: 0
x-archive-redirect-reason: found capture at 20110812062251
location: https://web.archive.org/web/20110812062251/https://wiki.python.org/moin/StateProgramming
server-timing: captures_list;dur=0.580300, exclusion.robots;dur=0.020094, exclusion.robots.policy;dur=0.009660, esindex;dur=0.015741, cdx.remote;dur=27.000606, LoadShardBlock;dur=194.857465, PetaboxLoader3.datanode;dur=148.906043
x-app-server: wwwb-app213
x-ts: 302
x-tr: 248
server-timing: TR;dur=0,Tw;dur=0,Tc;dur=0
set-cookie: SERVER=wwwb-app213; 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, 24 Jul 2025 23:11:56 GMT
content-type: text/html; charset=utf-8
x-archive-orig-date: Fri, 12 Aug 2011 06:22:51 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=847d65fedbd7439577f007f5ad24a5ad35f19288; expires=Fri, 12-Aug-2011 07:22:00 GMT; Max-Age=3600; Path=/
x-archive-orig-content-length: 35048
x-archive-orig-connection: close
x-archive-guessed-content-type: text/html
x-archive-guessed-charset: utf-8
memento-datetime: Fri, 12 Aug 2011 06:22:51 GMT
link: ; rel="original", ; rel="timemap"; type="application/link-format", ; rel="timegate", ; rel="first memento"; datetime="Mon, 11 Sep 2006 15:21:46 GMT", ; rel="prev memento"; datetime="Sat, 08 Dec 2007 10:26:37 GMT", ; rel="memento"; datetime="Fri, 12 Aug 2011 06:22:51 GMT", ; rel="next memento"; datetime="Tue, 20 Apr 2021 20:51:05 GMT", ; rel="last memento"; datetime="Fri, 08 Jul 2022 22:20:42 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-20110812060208-crawl438/WPO-20110812061501-01692.warc.gz
server-timing: captures_list;dur=1.702568, exclusion.robots;dur=0.018202, exclusion.robots.policy;dur=0.008261, esindex;dur=0.009911, cdx.remote;dur=6.999876, LoadShardBlock;dur=100.376863, PetaboxLoader3.datanode;dur=127.774021, load_resource;dur=111.593254, PetaboxLoader3.resolve;dur=58.611868
x-app-server: wwwb-app213
x-ts: 200
x-tr: 296
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
StateProgramming - PythonInfo Wiki
User
State Programming
Why, When
Very often, the response of a function will depend on the state of this object. With this pattern, It's easy to do such a thing ! You just have to write several sub-classes, each per state, inherit the State class and call the setState when the object need to change state.
Code
1 # Code is Public Domain.
2 class State:
3
4 def setState(self,stateClass):
5 #print stateClass.__dict__
6 for (name,attr) in stateClass.__dict__.iteritems():
7 if not name.startswith('_') and callable(attr):
8 f = getattr(stateClass,name)
9 l = f.__get__(self,self.__class__)
10 setattr(self,name,l)
Example
1 # Code is Public Domain.
2 class test(State):
3 def __init__(self,a):
4 self.a = a
5
6 def showMe(self):
7 print self.a
8
9 def showYou(self,other):
10 print self.a,other
11
12 def changeToOne(self):
13 self.setState(StateOne)
14
15 def changeToTwo(self):
16 self.setState(StateTwo)
17
18 class StateOne(test):
19
20 def showMe(self):
21 print self.a,'State One'
22
23 def showYou(self,other):
24 print self.a,'State One',other
25
26 class StateTwo(test):
27
28 def showMe(self):
29 print self.a,'State Two'
30
31 def showYou(self,other):
32 print self.a,'State Two',other
33
34
35 t1 = test('t1')
36 t2 = test('t2')
37 t1.showMe()
38 t2.showMe()
39 t1.showYou("you")
40 t1.changeToOne()
41 t1.showMe()
42 t2.changeToTwo()
43 t2.showMe()
44 t1.showYou("you")
45 t1.changeToTwo()
46 t2.changeToOne()
47 t1.showMe()
48 t2.showMe()
49 t1.showYou("you")
50 t2.showYou('you')
Output is
t1 t2 t1 you t1 State One t2 State Two t1 State One you t1 State Two t2 State One t1 State Two you t2 State One you
StateProgramming (last edited 2008-11-15 14:00:03 by localhost)