Postfix conditionals

Chris Angelico rosuav at gmail.com
Tue Feb 4 06:23:26 CET 2014


On Tue, Feb 4, 2014 at 4:16 PM, Göktuğ Kayaalp <self at gkayaalp.com> wrote:
> With my proposal implemented, the language would
> would be encouraging having multiple statements in one line, that looks
> like a single statement, but is indeed a composition of two.

I wouldn't have a problem with

if not i: break

in Python, as long as the condition is short. In something that reads
from a socket until the other end closes, for instance, I'm fine with
this:

while True:
    data = sock.read(1024)
    if not data: break
    do_stuff_with(data)

which will stop as soon as sock.read() returns "", which it does when
the other end is gone. (I wrote something doing exactly this today,
and did exactly this. Probably could have made the code a bit simpler
if I could depend on Python 3.3, but it has to run on 2.7 and maybe
2.6 so I had to stick with their facilities.)

Yes, it's two statements, but a list comprehension is a whole pile of
statement-y things, and that's usually a single line. If it's doing
one conceptual action, it's okay to not split it.

ChrisA



More information about the Python-list mailing list