[Python-Dev] %b format?

Greg Ewing greg@cosc.canterbury.ac.nz
Thu, 31 May 2001 17:17:16 +1200 (NZST)


Tim:

> On the fifth hand, I doubt anyone would want to add new % format codes for
> bases {2..36} - {2, 8, 10, 16}.

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".

Literals:

  0b(5)21403       general
  0b11001101       binary

Conversion functions:

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

Type slots:

  __base__(x, n)

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 ?-)

Greg Ewing, Computer Science Dept, +--------------------------------------+
University of Canterbury,	   | A citizen of NewZealandCorp, a	  |
Christchurch, New Zealand	   | wholly-owned subsidiary of USA Inc.  |
greg@cosc.canterbury.ac.nz	   +--------------------------------------+