format sring question
Peter Otten
__peter__ at web.de
Fri Sep 3 05:09:53 EDT 2004
Lowell Kirsh wrote:
> In Peter Norvig's Infrequently Answered Questions he explains that the
> following 2 fnctions look almost identical but are not the same:
>
> def printf(format, *args): print format % args,
>
> def printf(format, *args): print str(format) % args,
>
> The only difference is that in the second one, str(format) replaces
> format. If args are not given and the format string contains a '%', the
> first will work but the second will not. Why is this so? It seems to me
> like '100%' and str('100%) are the same object, no?
>
> Lowell
The only difference is that the _second_ one will work for non-strings, too,
when only the 'format' argument is passed and str(format) does not contain
any '%' characters. Two consecutive '%' are always converted into one, I'd
say this is broken behaviour when the conversion is applied on the result
of str(format) and format is not a string.
IMHO the second form is extremely bad code and you better forget about it
fast.
Peter
More information about the Python-list
mailing list