Bug in % string formatting?

Gary Herron res0ne7x at verizon.net
Thu Dec 20 23:09:48 CET 2001


>
> Argggh! As they say in numerical recipes: you should brush your teeth every
> night in front of the operator precedence rules for your language of
> choice.
>
> It does surprise me though that % binds harder than +. I would naively
> think that by its nature, % would have fairly low precedence. Oh well, I'm
> sure there's a good reason for it being the way it is.

Here's a (probably good) reason for the precedence of the '%'
operator:

With numbers, 'a%b' returns the remainder of 'a' divided by 'b' (the
modulus operator).  This is akin to division, and the percent sign
looks like division, and even the concept of percent (meaning per 100)
is relateed to division.  Thus it make sense to give the '%' operator
the same precedence as division.

All those arguments are irrelevant when applied to strings, but an
operator's precedence is fixed, and cannot depend on the types of
operands.

Gary Herron




More information about the Python-list mailing list