%a format

Dan Bishop danb_83 at yahoo.com
Thu Sep 9 02:11:03 CEST 2004


"John Roth" <newsgroups at jhrothjr.com> wrote in message news:<10jsqrm2rsqdc39 at news.supernews.com>...
> "Tor Iver Wilhelmsen" <tor.iver.wilhelmsen at broadpark.no> wrote in message 
> news:usm9tx5n3.fsf at broadpark.no...
> > John Lenton <john at grulic.org.ar> writes:
> >
> >> Is there any reason python's printf-style formatting is missing the
> >> (C99) '%a' specifier?
> >
> > Because it's not very well known or used? A google for "C printf
> > format string" gave lots of resources, none of which had %a.
> >
> > Java's JDK 1.5 java.util.Formatter class supports it, though.
> 
> I suspect there's no major reason more significant than that
> no one has written the patch to support it. That might suggest
> that no one really cares enough to do so - IIRC, Python
> shifted from using the C library printf() function to its own
> code some time ago, so there's no real commitment to
> following the C standard in this instance.
> 
> If the OP wants it, I would  suggest that he write the patch
> (complete with the doc changes, etc.) and submit it.  It might
> be a good idea to provide a use case more significant than
> "it's in the C99 standard" as well - but then I'm not
> channeling the core developers that well these days.

Why stick to just adding one conversion specifier to one data type? 
Why not allow a programmer to define their own custom specifiers?

class Bits(int):
   def __sprintf__(self, flags, precision, conversion):
      if conversion == 'b':
         # return a binary string
         return itoa(self, 2) # Why isn't this a standard function?
      else:
         return NotImplemented

>>> n = Bits(100)
>>> # The % operator would call __sprintf__
>>> print '%b' % n
1100100
>>> # If it isn't implemented, use the current behavior as a default.
>>> print '%d' % n
100

You'd be able to redefine the existing conversion too, in case you
want, for example, a high-precision numeric type to convert
'%.1000f'%x accurately.



More information about the Python-list mailing list