string conversion and formatters

Peter Hansen peter at engcorp.com
Sat Dec 8 01:51:32 CET 2001


Chris Liechti wrote:
> 
> "Spiffy" <clams17 at yahoo.com> wrote:
> 
> > Traceback (most recent call last):
> >   File "C:\Program Files\Python2\convertor.py", line 17, in ?
> >     print "Decimal: %d" % (hex)
>                             ^^^^^
> these parentheses don't do anything useful here. if you want a single
> element tuple use "(hex,)". note also that your variable "hex" shadows the
> built in function "hex". i would sugest a difrent name.

Agreed about the name, but not the comment about the parentheses
not doing something useful.  More accurately, they are not *necessary*, 
but they are quite useful.  Specifically they serve as a guide to the 
maintainer that the second argument to % is a tuple, not a comma-separated
sequence of items.  I used to forget to add the parentheses
when I changed things like 

  print "%d" % val

into

  print "%d (%s)" % val, name

so now I always include the parentheses (without the trailing
comma though) even with single-item format strings, and have
ceased to make the above mistake.

(Just a minor item, I know.)

-- 
----------------------
Peter Hansen, P.Eng.
peter at engcorp.com



More information about the Python-list mailing list