[Python-Dev] %b format?

Tim Peters tim_one@email.msn.com
Thu, 31 May 2001 02:28:04 -0400


[Greg Ewing]
> So, just add one general one:
>
>   %m.nb
>
> with n being the base. If n defaults to 2, you can read the "b"
> as either "base" or "binary".

Except .n has a different meaning already for integer conversions:

>>> "%.5d" % 2
'00002'
>>> "%.10o" % 377
'0000000571'
>>>

It would be inconsistent to hijack it to mean something else here.

> Literals:
>
>   0b(5)21403       general

I've actually got no use for bases outside {2, 8, 10, 16), and have never
heard a request for them either, so I'd be at best -0.  Better to stop
documenting the full truth about int() <0.9 wink>.

>   0b11001101       binary

+1.

> Conversion functions:
>
>   base(x, n)       general

-0, as above.

>   bin(x)           equivalent to base(x, 2) (for symmetry with
>                                              existing hex, oct)

+1 if binary literals are added.

> Type slots:
>
>   __base__(x, n)

Given the tenor of the above, add __bin__ and call it a day.

> Backwards compatibility measures:
>
>   hex(x) --> base(x, 16)
>   oct(x) --> base(x, 8)
>   bin(x) --> base(x, 2)
>
>   base(x, n) checks __hex__ and __oct__ slots for special cases
>              of n=16 and n=8, falls back on __base__
>
> There, that takes care of integers. Anyone want to do the
> equivalent for floats ?-)

Note that C99 introduces a hex notation for floats.