For review: PEP 308 - If-then-else expression

Carel Fellinger carel.fellinger at chello.nl
Fri Feb 7 21:51:30 EST 2003


On Sat, Feb 08, 2003 at 01:23:28AM +0000, Paul Paterson wrote:
...
> It seems to me that the current PEP favours the writing side more than the
> reading side. When I write,
> 
> val = x if c then y
> 
> Then this is exactly how I am thinking when I write the code. In my head I
> am saying "I want val to equal x, but only if c is true, then I'd like it to
> be y". So the PEP allows for expressive writing of code.
> 
> When I read the code, I have to try a bit harder because to represent the
> original intention the code I need to be thinking, "val could either be x or
> y depending upon condition c". To do this I have to parse the line in
> reverse.

That's funny, the few times I was lured into using the old idiom it
was more often then not during reading:)  I normally started of with an
if-statement to only replace it with an and-or expression when I found
the if-statement taking up to much of the precious screen estate.

On the other hand, I've not really been exposed to Perl, so I don't
know how hard it can be to read from right to left some of the time.
I know it took me a while for list-comprehensions to make sense and
part of that was due to getting used to the expression being upfront,
but now I find that they can help make code more readable.

Likewise i hope that such a true conditional operator will make code
a tat more readable.  For one you don't have to look out for those
pesky false values in the and-part of that would be conditional
operator and-or.  Besides, the intention of its use is made explicit.

-- 
groetjes, carel





More information about the Python-list mailing list