Why does this (not) work?

Fredrik Lundh fredrik at pythonware.com
Tue Aug 19 17:36:17 EDT 2003


Michael C. Neel wrote:

> But I want to use the * to make life easier to read, so I tried:
>
> >>> ("test",)*3
> ('test', 'test', 'test')
> >>> "%s - %s - %s" % ("test",)*3
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: not enough arguments for format string
> >>>

    >>> "%s - %s - %s" % ("test",)*3

is the same thing as

    >>> ( "%s - %s - %s" % ("test",) ) * 3

but you really want

    >>> "%s - %s - %s" % ( ("test",)*3 )

(your eval/str construct is just a really inefficient way to add
parentheses to an expression...)

</F>








More information about the Python-list mailing list