The basic idea of using while
/else
is certainly Pythonic—it wouldn't be in the language otherwise.
However, there are two bugs in your code that each mean that your else
code never runs. It's probably not Pythonic to include code that never executes and is only there to mislead the reader. :) More importantly, it's probably not what you wanted.
First, there's no way for you to ever exit the loop (except by exception). readline
returns an empty string at EOF, so that's probably what you want to check for:
while True:
line = sys.stdin.readline()
if line:
print(line)
else:
break
else:
print("Done")
But next, I'm not sure while
/else
does what you think it does. I think you're expecting it to run the else
however you exit the loop. But as the docs explain:
A break
statement executed in the first suite terminates the loop without executing the else
clause’s suite.
In other words, the else
part only runs if you exit normally, by the condition becoming false. Obviously while True
never becomes false.
And that's the whole point: sometimes you need to distinguish between the loop finishing normally vs. exiting with an if something: break
; that's when you use else:
. If you want to do the same thing either way, just do it:
while True:
line = sys.stdin.readline()
if line:
print(line)
else:
break
print("Done")
If you wanted something "similar to a finally
" because you may have exceptions, the answer is simple: use a finally
:
try:
while True:
line = sys.stdin.readline()
if line:
print(line)
else:
break
finally:
print("Done")
As a side note, looping over readline
with a while
and break
is almost never necessary; a file-like object like sys.stdin
is an iterable of lines, the same lines that readline
will return, so you can just do:
for line in sys.stdin:
print(line)
print("Done")
As a side note, it's generally not considered Pythonic to use unnecessary parentheses and semicolons in your code—especially when it makes your code look misleadingly like C or Java or something. While PEP 8 is only meant as a standard for the Python stdlib, many people use it as a guide for their own Python software, and it's a good idea to follow its recommendations unless you have a good reason not to. You can use tools like pep8
to check whether you're following the guidelines. This only catches the simple stuff, like your while(True):
instead of while True
and print("Done");
instead of print("Done")
, but violating that simple stuff distracts readers from noticing the possible higher-level violations that you're asking about.