[Python-ideas] Different bases format specification
Bruce Leban
bruce at leapyear.org
Sat Dec 3 03:19:24 CET 2011
On Fri, Dec 2, 2011 at 4:12 PM, T.B. <bauertomer at gmail.com> wrote:
> I suggest using the precision field in the format specification for
> integers for that. Examples:
>
> "{:.16d}".format(31) #Prints '1f'
>
> "{:.2d}".format(-19) # Prints '-10011'
>
> <snip>
>
I don't think this belongs in format.
> P.S. Bonus question: What "{:.-909d}".format(42) would print?
>
Any proposal which includes an inscrutable example doesn't bode well for
the usability of the feature. :-) Sure, negative bases are mathematically
meaningful but are they useful in Python? And why not complex bases then?
Or did you have something else strange in mind?
If there's enough need for encoding in different bases, including a
standard version of format_integer_in_base makes a lot more sense. We could
write format_integer_in_base(15, 16) to get "F" and
format_integer_in_base(64, "A23456789TJQK") to get "4K". But note that
standard base 64 encoding is not at all the same -- this function encodes
starting at LSB while base 64 encodes at word boundaries.
Finally, note that if you really want to mangle format strings you can do
it without changing the library. Just write it this
way "{:.16d}".format(arbitrarybase(31)) where you have defined
class arbitrarybase:
def __format__(self, format_spec):
return format_integer_in_base(parse format spec etc.)
--- Bruce
Follow me: http://www.twitter.com/Vroo http://www.vroospeak.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20111202/ccdca990/attachment.html>
More information about the Python-ideas
mailing list