[Python-ideas] Different bases format specification

Nick Coghlan ncoghlan at gmail.com
Sat Dec 3 05:31:51 CET 2011


On Sat, Dec 3, 2011 at 1:16 PM, T.B. <bauertomer at gmail.com> wrote:
> For weird math scenarios I know there are already many modules and packages.
> But what about ternary? en.wikipedia.org/wiki/Ternary_numeral_system has
> some points that include that base 9 and 27 are used [no citation].

Weird math scenarios are no justification for changing the behaviour
of a builtin type. b/o/d/x/X cover all the common use cases,
everything else can be handled by libraries (including providing
custom string.Formatter subclasses).

Code and functionality are not free - we need solid gains in easier
(or otherwise improved) coding and maintenance for real world problems
before we add more of either.

>> 2. The explicit 'b', 'o' and 'x' codes are related to integer literal
>> notation (0b10, 0o777, 0x1F), not to the second argument to int()
>>
> That one reason I wrote: "It might be a nice mnemonic using 'b' instead,
> standing for 'base'. Then the default base will be 2."

And the symmetry with the integer literal codes will still be lost.

> Anyway, I think there should be 'B' and 'O' presentation types, that
> will be used for outputting '0B' and '0O' prefixes.

If you really want that (Why would you?) and so long as the numbers
aren't negative:

    "0B{:b}".format(number)
    "0O{:o}".format(number)

The only reason 'X' is provided for hexadecimal formatting is to
capitalize the letters that appear within the number itself.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia



More information about the Python-ideas mailing list