Python code written in 1998, how to improve/change it?
Bengt Richter
bokr at oz.net
Mon Jan 23 05:45:11 EST 2006
On Mon, 23 Jan 2006 08:53:59 +1300, Carl Cerecke <cdc at maxnet.co.nz> wrote:
>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)
>
Cool. But unfortunately, neither version works inside a function's local namespace.
Using exec instead of eval seems to do it in either context though.
Now, how can we get optimized code (i.e., LOAD_FAST vs LOAD_NAME etc in a1 etc)
without byte code hackery?
Regards,
Bengt Richter
More information about the Python-list
mailing list