Control of flow
Tim Peters
tim.one at home.com
Wed Aug 1 16:51:38 EDT 2001
[Campbell]
> I use python to write telephony apps, and I find that a 'goto' is often
> a nice tool (stop going 'aargh', its properly "useful" sometimes =).
Draw a diagram using boxes showing the states of your dialog. Draw arrows
between boxes, labelled with the conditions under which you can move from
one state to another ("user said 'yes'"; "user pushed '#'"; "error"; "this
specific error"; etc). Picture the "quick reference" card that comes with
most fancy phone systems -- that's the idea. Design a data structure using
a dict to map a state to a pair (or class instance) containing:
1. A function implementing the state's logic, returning "a condition"
(corresponding to the labels on your arrows).
2. A dict of (condition, new_state) pairs (these are the labelled edges
on hand diagram).
Then write incredibly simple and uniform code to walk the data structure in
a loop. Then initialize the main dict. You're done <wink>. But I'm not
kidding: if you had gotos, you'd get hopelessly lost as soon as the dialogs
you're working with now get more complicated. You can *make* this very
pleasant indeed, but not with masses of twisted control-flow logic. Highly
irregular problems are much better handled via dead-uniform simple code
marching over a lumpy data structure.
believe-me-now-or-believe-me-later<wink>-ly y'rs - tim
More information about the Python-list
mailing list