CARVIEW |
Select Language
HTTP/2 302
server: nginx
date: Fri, 18 Jul 2025 09:19:47 GMT
content-type: text/plain; charset=utf-8
content-length: 0
x-archive-redirect-reason: found capture at 20071208102637
location: https://web.archive.org/web/20071208102637/https://wiki.python.org/moin/StateProgramming
server-timing: captures_list;dur=0.542261, exclusion.robots;dur=0.021452, exclusion.robots.policy;dur=0.010015, esindex;dur=0.011764, cdx.remote;dur=91.869619, LoadShardBlock;dur=95.967324, PetaboxLoader3.datanode;dur=68.511423
x-app-server: wwwb-app220
x-ts: 302
x-tr: 219
server-timing: TR;dur=0,Tw;dur=0,Tc;dur=1
set-cookie: SERVER=wwwb-app220; 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: Fri, 18 Jul 2025 09:19:48 GMT
content-type: text/html;charset=utf-8
x-archive-orig-date: Sat, 08 Dec 2007 10:26:36 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 10:26:37 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, 11 Aug 2007 05:47:16 GMT", ; rel="memento"; datetime="Sat, 08 Dec 2007 10:26:37 GMT", ; rel="next memento"; datetime="Fri, 12 Aug 2011 06:22:51 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: 52_1_20071208084132_crawl105-c/52_1_20071208102225_crawl107.arc.gz
server-timing: captures_list;dur=0.560315, exclusion.robots;dur=0.019060, exclusion.robots.policy;dur=0.008719, esindex;dur=0.011209, cdx.remote;dur=28.402789, LoadShardBlock;dur=307.532648, PetaboxLoader3.resolve;dur=233.230647, PetaboxLoader3.datanode;dur=151.018500, load_resource;dur=111.826576
x-app-server: wwwb-app220
x-ts: 200
x-tr: 514
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
StateProgramming
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
EditText (last edited 2004-04-03 10:29:45 by proxy-a0)
DeleteCache (cached 2007-09-01 22:08:37)- Login
- Navigation
- Actions
- Your recent pages