[Python-Dev] str with base

Donovan Baarda abo at minkirri.apana.org.au
Tue Jan 17 12:54:58 CET 2006


On Tue, 2006-01-17 at 10:05 +0000, Nick Craig-Wood wrote:
> On Mon, Jan 16, 2006 at 11:13:27PM -0500, Raymond Hettinger wrote:
[...]
> Another suggestion would be to give hex() and oct() another parameter,
> base, so you'd do hex(123123123, 2). Perhaps a little
> counter-intuitive, but if you were looking for base conversion
> functions you'd find hex() pretty quickly and the documentation would
> mention the other parameter.

Ugh!

I still favour extending % format strings. I really like '%b' for
binary, but if arbitary bases are really wanted, then perhaps also
leverage off the "precision" value for %d to indicate base such that '%
3.3d' % 5 = " 12"

If people think that using "." is for "precision" and is too ambiguous
for "base", you could do something like extend the whole conversion
specifier to (in EBNF)

conversion=%[mapping][flags][width][.precision][@base][modifier]type

which would allow for weird things like "%8.4 at 3f" % 5.5 == " 12.1111"

Note: it is possible for floats to be represented in non-decimal number
systems, its just extremely rare for anyone to do it. I have in my
distant past used base 16 float notation for fixed-point numbers.

I personally think %b would be adding enough. The other suggestions are
just me being silly :-)

-- 
Donovan Baarda <abo at minkirri.apana.org.au>
http://minkirri.apana.org.au/~abo/



More information about the Python-Dev mailing list