tim.one at comcast.net
Thu Sep 26 11:39:04 EDT 2002
> Having written _many_ state machines in my life, which async programming
> boils down to in one form or another, I feel it depends on the task.
> Contrast this with a simple procedure call where a linear series of
> function calls are made, each with appropriate error handling and status
> adjustment, in a pleasant centrally located manner.
> I wonder if generators can come to a rescue here?
Absolutely. Read the start of PEP 255.
> Still trying to get them into my head...
Think of them as resumable functions. You're familiar with functions that
remember their own data state across calls, via abusing globals, or via
declaring private static vrbls, in C. Methods on objects often do the same
by stuffing their state into instance attributes between calls. A generator
remembers its data state across resumptions simply by virtue of that its
local variables don't vanish, and also remembers its control-flow state
across resumptions. Remembering local data state by magic is a real
convenience, but can be simulated in many other ways with reasonable effort;
it's the ability to remember and restore control-flow state by magic that
can make generators more than merely convenient.
More information about the Python-list