Anybody working on a PEP308 summary?

Andrew Koenig ark at research.att.com
Sun Feb 9 16:11:49 CET 2003


Giles> Technique number 2:

Giles> if condition:
Giles>     x = expr1
Giles> else:
Giles>     x = expr2

Giles> Which is three extra lines and suffers from the possibility of
Giles> introducing a bug through mis-typing x.

I would like to strengthen this statement slightly.  Instead of

        x = condition and expr1 or expr2

let's look at

        print "The value is", x[i>0 and i else 0]

Here I have carefully chosen the value in the [] to avoid the
bug mentioned in Technique 1.  Now the alternatives are

        if i>0:
            print "The value is", x[i]
        else:
            print "The value is", x[0]

which repeats much more than just "x ="

        print "The value is",
        if i>0:
            print x[i]
        else:
            print x[0]

which some people will find obscure because of the print with the
trailing comma, or

        if i > 0:
            val = x[i]
        else:
            val = x[0]
        print "The value is", val

which introduces an extra variable.
       



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




More information about the Python-list mailing list