ACCEPTED: PEP 285

Quinn Dunkan quinn at hurl.ugcs.caltech.edu
Thu Apr 4 15:28:12 EST 2002


On 04 Apr 2002 06:34:33 -0800, Paul Rubin <phr-n2002a at nightsong.com> 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.

"'%03s' % 17" is not the same as "'%03d' % 17".

There are really only two types supported by %---numeric types and strings (and
C holdovers like %c (which is just a variant on strings for python)).  And I
think the string formatting is just a clever pun on the meaning of precision
and leading zero.

So % is not really about formatting types, it's about formatting numbers and
strings.  There's no useful interpretation of precision and the other % flags
for booleans, and even if there were I wouldn't want to further overload
things.

The way to format types is to give them  __str__ and use %s.  If you want
formatting with arguments, I think a perfectly reasonable approach is
"'%s ...' % obj.fmt(1, 2)".  You could do "'%(1,2)s ...' % d(obj)" with a
suitably devious definition of d(), but I'd go with obj.fmt().



More information about the Python-list mailing list