Tuple "detection"

Skip Montanaro skip at pobox.com
Mon May 19 13:25:57 EDT 2003


    Facundo> But when I write 

    >>> print "%d %d" % (5, 5)
    5 5

    Facundo> It's not the same to write:

    >>> print "%d %d" % 5, 5
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    TypeError: not enough arguments for format string

    Facundo> Is this on purpose? Why?

Yes.  It's because the '%' operator has higher precedence than the ','
operator.  It's as if you typed

    print ("%d %d" % 5), 5

Specifying tuples without the surrounding parentheses can sometimes bite
you.  It's generally best to always include them.

Skip






More information about the Python-list mailing list