Exceptions as a Control Structure

Olivier Parisy olivier.parisy at free.fr
Mon Aug 9 09:30:40 EDT 2004


Roy Smith wrote:

> Lots of things you do in C++ are frowned upon in Python.  Lots of things 
> you do in Python are frowned upon in C++.

This is something I need to learn. I suppose there's not much written
information on this informal topic, and that I'll have to read some
source code... Any advice on cleanly written projects from that point
of view ?

> Specifically, exceptions in C++ are generally considered fairly 
> heavy-weight, but not so in Python.  Here's another common python idiom 
> that uses exceptions in a way which would probably horrify most C++ 
> people:
> 
> for key in sequence:
>    try:
>       foo = dictionary [key]
>    except KeyError
>       foo = "not found"
> 
> instead of:
> 
> for key in sequence:
>    if dictionary.has_key (key):
>       foo = dictionary [key]
>    else
>       foo = "not found"

Hum, I stumbled on this FAQ a short time after I posted here, indeed :-)

Regards,
Olivier.




More information about the Python-list mailing list