[Python-ideas] Add a builtin method to 'int' for base/radix conversion

Mark Dickinson dickinsm at gmail.com
Sun Sep 13 20:06:22 CEST 2009

On Fri, Sep 11, 2009 at 4:16 PM, Yuvgoog Greenle <ubershmekel at gmail.com> wrote:
> Does anybody have any more use cases, ideas or suggestions? I'm getting the
> feeling this suggestion is +0 to most people and +1 for the rest. I'm pretty
> new to these mailing lists so does that mean a yes or a no?

Just out of curiosity, I did a Google code search[*] for uses of the
inverse operation: int(<some_string> ,n).  I found a good handful of
uses of int(s, 36), almost all apparently to do with turning integers
into suitable id strings;  there was also evidence that people have
implemented the reverse 'integer -> base 36 string' conversion at
least twice.  I found no meaningful uses of any bases other than
2, 8, 10, 16, and 36.  So the main use case seems to be
serialization and deserialization of integers into some 'suitably nice'
alphabet, and that alphabet is likely to be application-dependent.

-0 for int.to_base(n) (2 <= n <= 36) or equivalent functionality in the core.

+0 for a pair of library functions converting to and from base n, with
explicitly given translation table.  I agree with MRAB that an implicit
digit set should only be allowed for 2 <= base <= 36, if at all.

By the way, _PyLong_Format in Objects/longobject.c *does* contain
code for general integer -> base b conversions, 2 <= b <= 36,
but that code is currently unused (as far as I can tell).


[*] http://www.google.com/codesearch?hl=en&lr=&q=%5CWint%5Cs*%5C%28.*%5C%2C%5Cs*36%5Cs*%5C%29+lang%3Apython&sbtn=Search

More information about the Python-ideas mailing list