format sring question

  • Follow


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
0
Reply lkirsh (134) 9/3/2004 7:16:20 AM

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?

The both fail the same way for me; I can't reproduce
a difference.

 >>> def printf1(format, *args): print format % args,
....
 >>> def printf2(format, *args): print str(format) % args,
....
 >>> printf1("100%")
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
   File "<stdin>", line 1, in printf1
ValueError: incomplete format
 >>> printf2("100%")
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
   File "<stdin>", line 1, in printf2
ValueError: incomplete format
 >>>

 >>> printf1("100%%")
100%
 >>> printf2("100%%")
100%
 >>>

My guess is there's supposed to be some sort of
confusing between the trailing ',' when used in a
print statement, to suppress the trailing newline,
and when used to indicate a tuple, as in

   a = args,

to create a single element tuple.  In that case,
  % args, might be a single element tuple where the
first element is the tuple of args.

				Andrew
				dalke@dalkescientific.com
0
Reply adalke (604) 9/3/2004 8:07:06 AM


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

0
Reply __peter__ (3847) 9/3/2004 9:09:53 AM

thanks to both of you. bye bye second version.

Lowell

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
0
Reply lkirsh (134) 9/3/2004 11:02:30 PM

3 Replies
22 Views

(page loaded in 2.234 seconds)


Reply: