Python Idiom Question

Remco Gerlich scarblac at pino.selwerd.nl
Wed Jul 11 03:03:27 EDT 2001


Chris Barker <chrishbarker at home.net> wrote in comp.lang.python:
> This is the approved idiom, but I never use it. I always use:
> 
> x = raw_input()
> while x:
>     x = raw_input()
>     DoSomethingInteresting()
>     x = raw_input()

This bugs, of course; delete the second raw_input() line.

> I really don't like any "while 1" constructs, you have to then go
> looking for the "break" to find out what is controlling the loop. I
> don't like having to put "x = raw_input" in two places either, but it's
> more clear to me this way.

Just that you don't like it in two is no reason to put it in three places ;)

It's usually a single break statement, right at the top of the loop after
the 'x = raw_input()', in this case. That's pretty clear, especially in
Python where the idiom is common. Having it in two places isn't clearer, and
may be a maintenance problem (change one, forget the other).

-- 
Remco Gerlich



More information about the Python-list mailing list