On Mon, 23 Jan 2006 08:53:59 +1300, Carl Cerecke <[EMAIL PROTECTED]> wrote:
>Bengt Richter wrote: >> On Thu, 19 Jan 2006 23:16:57 -0500, Peter Hansen <[EMAIL PROTECTED]> 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 -- http://mail.python.org/mailman/listinfo/python-list