The rap against "while True:" loops

Hendrik van Rooyen hendrik at microcorp.co.za
Sun Oct 18 10:08:18 CEST 2009


On Saturday, 17 October 2009 16:30:55 Aahz wrote:
> In article <mailman.1589.1255781573.2807.python-list at python.org>,
>
> Tim Rowe  <digitig at gmail.com> wrote:
> >The point is that an exception causes a change in program flow, so of
> >course they're used for flow control. It's what they do. The question
> >is in what cases it's appropriate to use them.
>
> Standard Python idiom:
>
> try:
>     d[key] += value
> except KeyError:
>     d[key] = value
>
> Maybe you need to re-think "appropriate".

Standard Python idiom:

if key in d:
  d[key] += value
else:
  d[key] = value

 Maybe you need to re-think "appropriate".

- Hendrik




More information about the Python-list mailing list