ACCEPTED: PEP 285

gbreed at cix.compulink.co.uk gbreed at cix.compulink.co.uk
Thu Apr 4 10:22:02 EST 2002


Paul Rubin wrote:

> Guido van Rossum <guido at python.org> writes:
> > > I think there will need to be a format code for truth values:
> > > 
> > >   ("%b" % True) => "True"
> > 
> > There's no need.  "%s" % True already yields 'True'.  if you want
> > coercion to bool, use "%s" % bool(b).
> 
> In that case there's also no need for %d, since "%s" % 17 already
> yields '17'.  If ints have a format code and bools don't, one is
> left with the impression that bools aren't as respectable a type
> as ints.  That doesn't seem in the spirit of the PEP.

The format code for integers is %i, not %d.  There's a very good reason 
for %d being there.  How else to you propose getting

>>> '%d'%1
'1'
>>> '%d'%1.0
'1'
>>> '%d'%1.3
'1'
>>> '%d'%(10L**100)
'1000000000000000000000000000000000000000000000000000000000000000000000000
000000
0000000000000000000000'
>>> '%d'%bool.True
'1'

to all work?  (bool module cut and pasted from the PEP)  %i itself is the 
simplest way to format ints, longs, bools and the integer part of floats 
so that they all look like integers between 1.5 and 2.3 (he says, not 
having 1.5 to check).  Also a good way of getting bools looking the same 
before and after the PEP.  And a nice simple way to do

>>> '%03i'%5
'005'

without importing string.

I'll bet most of the time you want to stringify booleans it won't be as 
'true' of 'false' either.  Rather something like

('It worked!', 'It didn't work!')[success]

Still, yes, if one type can have a format code why not the others?  Let's 
see what would make sense for xrange, function, slice, frame, ...


                  Graham

     <http://www.microtonal.co.uk/>



More information about the Python-list mailing list