Inline assignments
Steven D'Aprano
steve at REMOVETHIScyber.com.au
Thu Mar 9 17:22:47 EST 2006
On Thu, 09 Mar 2006 15:45:13 +0000, Duncan Booth wrote:
> Like it or not, Python uses exceptions for normal loop flow control. That's
> a fact of life, live with it: every normal termination of a for loop is an
> exception. Real exceptions don't get masked: for loops terminate with
> StopIteration, and they only catch StopIteration, so any other exception is
> propagated.
In fairness, that is under the hood: the programmer doesn't (normally)
have to deal with the StopIteration itself.
The argument against exceptions is just a disguised argument against GOTO.
It fails to distinguish between bad ways to jump to another part of code
and good ways.
Bad ways include GOTO itself and the awful COME FROM construct.
Good ways include IF, loops of all sorts, function calls, break/continue
from inside a loop, and exceptions.
Of course exceptions can be abused, but then so can any piece of code.
Try...except blocks no more allow you to jump to arbitrary places in your
code than do if...else blocks.
As for using exceptions as signals, there is a reason they are called
EXCEPTION rather than ERROR: they don't necessarily represent errors, and
should not be treated that way. They represent exceptional states.
Exceptional states can be errors, they can signal the end of processing,
or they can signal expected states.
--
Steven.
More information about the Python-list
mailing list