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

Paul Paterson hamonlypaulpaterson at houston.rr.com
Fri Feb 7 20:23:28 EST 2003


<snip PEP>

-1 (to the x if c else y)

For me, Python's strength is in its balanced expressiveness and scaling
behaviour.

I measure expressiveness in terms of how clearly the intention behind a
piece of code can be see. When I'm writing code I want to be able to easily
translate my intention into code on the page. When I'm reading some code I
want to easily see the author's intention.

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.

Ok, so this isn't too hard. But now look at the scaling behaviour. As you go
to more complex expressions the writing part is fine. In my head I can
easily think "I want val to be x if c is true but then I need it to be y is
d is true otherwise it should be z". Again, this is probably how I am
thinking when I write this and the PEP let me write it very easily. I'd say
the complexity of writing has scaled linearly with the complexity of my
intention.

However, when I try to read,

val = x if c else y if d else z

I have to work a lot harder to read this than I did to write it. I think the
complexity of reading has risen out of proportion to the cmplexity of the
intent of the code. When reading I'd like to think "val = select on of x, y,
z based on c and then d".

So, because the PEP favours low complexity on the writing side rather than
on the reading side I would suspect that we would start to see obscure
(cute) coding. I think I would be tempted to do it because it is very
natural to write in this way.

I think I would be +1 if an alternative form could be found where the
complexity rose in a balanced way for writing and reading these things.

Paul






More information about the Python-list mailing list