PEP 308: A PEP Writer's Experience - PRO

Chermside, Michael mchermside at ingdirect.com
Mon Feb 10 07:43:26 EST 2003


Tim Peters writes:

> OTOH, when I read Micahel's post, I had no idea whether
> 
>     print "x = " + x.getDisplayString() if x else "None"
> 
> would group as:
> 
>     print ("x = " +   (x.getDisplayString() if x else "None"))
> 
> or as:
> 
>     print (("x = " + x.getDisplayString())   if x else "None")
> 
> and I didn't even know which way he *intended*.  Spelled with an if-
> statement instead, the intent is obvious instead of a puzzle.

That's an excellent point, Tim. In fact, that's EXACTLY why, whenever
I use ?: in C, I *always* use it with parentheses. I imagine that in
Python I would probably do the same thing. So I would have written
that as:

      print "x = " + (x.getDisplayString() if x else "None")

because it's unambiguous even WITHOUT knowledge of the precedence
rules. The only reason I didn't is that I was trying to simplify as
much as possible for the posting.

shouldn't-simplify-to-the-point-of-confusion-lly yours,

Michael Chermside







More information about the Python-list mailing list