Exceptions as a Control Structure

Casey Duncan casey at zope.com
Mon Aug 9 21:23:36 CEST 2004

On Mon, 09 Aug 2004 15:01:38 +0200
Olivier Parisy <olivier.parisy at free.fr> wrote:

> Hi all,
> I am new to Python (I just finished Guido's tutorial).
> I was very surprised to learn there that the StopIteration
> is used to end for loops in a standard iterator setting.
> I come from C++, where the use of exceptions as control
> structures is frowned upon for efficiency reasons.
> What is the Python canon on this topic ? Are exceptions
> considered as reasonable control structures, or is
> StopIteration alone of its kind ?

Actually StopIteration is a semantic peer of the long standing
IndexError and KeyError exceptions used in sequences and dicts.

In general I think you will find python uses exceptions often for flow
control. The try: except: block is more or less free if no exception
actually occurs. 



More information about the Python-list mailing list