Format Strings -- Real vs. Expected Behaviour

Erik Max Francis max at alcyone.com
Mon Apr 16 22:02:46 EDT 2001


Brad Bollenbach wrote:

> Shouldn't Python be smart enough to know that even without ()'s around
> the
> whole thing, this is all one line (therefore avoiding the current
> odd/unexpected IMHO behaviour with the format string)? After all, I'm
> telling it this much by using the line continuation character "\"
> aren't I?

It has no trouble telling that it's all one line.  Its problem is you
are not following operator precedence.  You'd get the same error if it
were all on one line:

     print "%s " + "%s" % ("hello", "world")

The problem is that % has higher precedence than +, so it interprets
this as

     print "%s " + ("%s" % ("hello", "world"))

which isn't what you meant.

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ Forgiveness is another word for letting go.
\__/ Matthew Fox
    Interstelen / http://www.interstelen.com/
 A multiplayer, strategic, turn-based Web game on an interstellar scale.



More information about the Python-list mailing list