Any other Python flaws?

Martijn Faassen m.faassen at vet.uu.nl
Sat Jun 16 07:18:01 EDT 2001


Robert Roy <rjroy at takingcontrol.com> wrote:
> On 14 Jun 2001 22:33:50 GMT, m.faassen at vet.uu.nl (Martijn Faassen)
> wrote:
[snip]
>>This may be considered a minor quibble; the mandatory use of () in
>>case of multiple arguments to the % string interpolation operator:
>>
>>"%s" % "foo"
>>
>>"%s %s" % "foo", "bar" # doesn't work right
>>
>>"%s %s" % ("foo", "bar")

> Would that not allow for more ambiguities than its worth? It would
> make nested tuples would be extremely dangerous.

Oh, sure; I'm not saying the mandatory ( and ) aren't there for 
reasons. It's just something that could be considered a wart.

The reason is that of operator precedence; the % binds more strongly
than the ',' which tries to construct a tuple. Therefore:

"foo %s" % "bar", "baz"

Will get us:

("foo bar", "baz")

And not an error like:

"foo %s" % ("bar", "baz")
  
> This is clear

> ('this', '%s %s' % ('foo', 'bar'), 'that') 

> If I change my format string to '%s %s %s' the interpreter will yell
> at me if I do not add an element to my tuple

> This is not

> ('this', '%s %s' % 'foo', 'bar', 'that')

> If I change my format string as above and fail to add the appropriate
> element to the tuple, I have an extremely hard to find bug.

Um, for the % operator it goes that far, and 'magic' behavior probably
wouldn't be desirable, so the parens may be the best bet, but tuples in
Python in general can exist without surrounding (), as long as things
aren't ambiguous. 

Regards,

Martijn 
-- 
History of the 20th Century: WW1, WW2, WW3?
No, WWW -- Could we be going in the right direction?



More information about the Python-list mailing list