[Python-ideas] Different bases format specification
T.B.
bauertomer at gmail.com
Sat Dec 3 06:06:42 CET 2011
On 2011-12-03 06:31, Nick Coghlan wrote:
>>> 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.
All is not lost. "{:b}".format(num) would still print numbers in binary
notion. Only when using an optional field it will change the output. One
far-reaching solution for the symmetry break is allowing ALL integer
literal codes to have that optional field, and each literal code will
have its own default base: b->2, o->8, d->10, x->16.
>> Anyway, I think there should be 'B' and 'O' presentation types, that
>> will be used for outputting '0B' and '0O' prefixes.
Horrors such as an uppercase-only file/shell/whatever still exist. They
are rare and you'll usually call upper() before sending a
message/writing to a file to them, but it might worth adding 'B'.
> 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.
0B111 and -0O755 are *current* valid python tokens. There is no
"elegant" way of outputting those tokens. I suggested 'B' and 'O' as a
side-effect, because bases > 10 have some digits as letters.
regards,
TB
More information about the Python-ideas
mailing list