Python code written in 1998, how to improve/change it?

Carl Cerecke cdc at maxnet.co.nz
Sun Jan 22 14:53:59 EST 2006


Bengt Richter wrote:
> On Thu, 19 Jan 2006 23:16:57 -0500, Peter Hansen <peter at engcorp.com> wrote:

> How about something like
> 
>  >>> actions = dict(
>  ...    a=compile('print "A"; state="b"','','exec'),
>  ...    b=compile('print "B"; state="c"','','exec'),
>  ...    c=compile('print "C"; state=None','','exec')
>  ... )
>  >>> state = 'a'
>  >>> while state: eval(actions[state])
>  ...
>  A
>  B
>  C

Good idea. But we can eliminate the dictionary lookup:

a1 = compile('print "A"; state=b1','','exec')
b1 = compile('print "B"; state=c1','','exec')
c1 = compile('print "C"; state=None','','exec')

state = a1
while state:
     eval(state)


Cheers,
Carl



More information about the Python-list mailing list