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

Andrew Koenig ark at
Mon Feb 10 18:39:59 CET 2003

Laura> Well, _no_ _wonder_ you are hot for this change.  Parsing a
Laura> language where each line has a True/False branch condition
Laura> (which I assume is what you are doing) is an absolute natural
Laura> for such a construct.

It's not actually that universal, but it's certainly part of the
reason I want it.

However, I acknowledge that the mere fact that I have a use for it
should not by itself convince other people.

So let me try an alternative approach.  Just for fun, I went looking
for ``cond and expr or expr'' expressions in Idle.  I found four.

Two of them are obviously correct:

1) in

        if DEBUG:
            print "auto colorizing turned", self.allow_colorizing and "on" or "off"

2) in

        b = Button(self.buttonframe,
                   text=label, command=command,
                   default=isdef and "active" or "normal")

The other two, however, are more questionable:

3) in

        for raw in map(ord, uniphooey):
	    push(raw < 127 and chr(raw) or "x")

At first I thought that this might push the wrong value when raw==0.
It turns out to work correctly, however, because chr(0) is considered
true rather than false.  Still, I would much prefer to avoid the
and/or idiom in this example because of the extra effort needed to
understand it.


4) in

            text.mark_set("insert", self.engine.isback() and first or last)

It is not possible to determine by inspection whether this code is
correct.  Its correctness depends on proving that whenever
self.engine.isback() is true, first is also true (i.e. not zero, not
empty, etc.)

It turns out that this code is correct, but to show it, I had to hunt
back 12 lines to find

     first = "%d.%d" % (line, i)

I didn't actually expect it to be easy to find examples in distributed
python code that were provably incorrect.  However, I also did not
expect it to be this easy to find examples that were not obviously

More information about the Python-list mailing list