PEP 308: A PEP Writer's Experience - PRO

Andrew Koenig ark at research.att.com
Sat Feb 8 12:27:45 EST 2003


I agree completely with this note, so I won't quote all of it.
However, I do want to augment ont of its points:

Michael> [2] It distinguishes choice from branching

Michael> If I have the following snippet of code:

Michael>        if condition:
Michael>            x = a
Michael>        else:
Michael>            x = b

Michael> then I think of it as doing one thing -- it sets x to one of
Michael> two different values. That is VERY different from this:

Michael>        if condition:
Michael>            x = a
Michael>        else:
Michael>            y = b

Michael> which I think of as doing one of TWO things -- either modifying
Michael> x or y.

Or perhaps it's a bug and you meant to assign to x in both places.

The reason such bugs are possible is that there are two separate
assignments.  If, instead, you write

        x = a if condition else b

then you have factored out the assignment to x and it becomes
impossible to assign to two different variables by mistake.


-- 
Andrew Koenig, ark at research.att.com, http://www.research.att.com/info/ark




More information about the Python-list mailing list