For review: PEP 308 - If-then-else expression
Paul Moore
gustav at morpheus.demon.co.uk
Sat Feb 8 17:38:35 EST 2003
David Eppstein <eppstein at ics.uci.edu> writes:
> As far as I can remember, the last time I wanted to use that construct,
> I couldn't, because b was false. Hah! Found it:
>
> if i < 0:
> col = None
> else:
> col = cols[i]
>
> Here cols[i] is a string, and I didn't want to assume that it is always
> nonempty, so the and-or construct doesn't work. I think this would be a
> good candidate for a one-liner:
>
> col = cols[i] if i >= 0 else None
As Andrew Dalke already pointed out, this is a good example in that
most of the standard workarounds don't work.
But I don't like the look of the one-liner, if I read it in someone
else's code. I also don't think I'd naturally think to write it that
way in my own code.
It's shorter, which means that if I had a lot of these, my function
would be hard to read because it was too big. But I think I'd split
the function up before that happened.
Dunno, it's a good example, but I *still* don't feel convinced of the
need.
Paul.
--
This signature intentionally left blank
More information about the Python-list
mailing list