Format Strings -- Real vs. Expected Behaviour

Carlos Ribeiro cribeiro at mail.inet.com.br
Mon Apr 16 22:35:21 EDT 2001


At 21:12 16/04/01 -0400, Tim Peters wrote:
>Note that % has higher precedence than +:  those parentheses in the second
>example are doing more than you think they're doing!

The precedence is obvious to anyone used to Python, but not so for the 
novice. I think that it could be a good idea to point out this kind of 
stuff *explicitly* in the documentation. After all, it's not a 
mathemathical expression; it's a expression anyway, so the precedence rules 
apply, but it can be confusing for novices and/or people that come from 
other languages (such as the C/Pascal that is taught at most scholl).

I'm thinking about writing a small doc called "Potential pitfalls in Python 
for the C/Pascal programmer", listing constructs such as the one cited, and 
several others that are pretty small in size. I myself got caught recently 
by the extra comma in the list comprehension; in the past few weeks, I've 
seen this kind of stuff being asked pretty frequently.

So, if you have any example that you think illustrates this kind of 
pitfall, send it to me by private email. I'll post the doc link to the list 
in the next few days.


Carlos Ribeiro






More information about the Python-list mailing list