classification
Title: Add a builtin method to 'int' for base/radix conversion
Type: feature request Stage:
Components: Interpreter Core, Library (Lib) Versions: Python 3.2, Python 2.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: cvrebert, mark.dickinson, ubershmekel (3)
Priority: Keywords

Created on 2009-08-25 19:35 by ubershmekel, last changed 2009-08-27 19:04 by mark.dickinson.

Messages (5)
msg91958 - (view) Author: Yuv Gre (ubershmekel) Date: 2009-08-25 19:35
In javascript and java each number has a toString method which allows 
for easy string representation in different bases. If the function int(x[, base]) exists as a builtin, the inverse should at least be 
somewhere in the standard library. Personally I believe a "to_string" 
method would work but would defeat "There should be one way to do it". 
Perhaps a method called "radix" or "base_convert" which requires a base 
to convert parameter won't take the place of str(number).

Here are a few possible implementations:
http://code.activestate.com/recipes/222109/
http://code.activestate.com/recipes/111286/
http://pastebin.com/f54dd69d6
msg91959 - (view) Author: Mark Dickinson (mark.dickinson) Date: 2009-08-25 19:45
This seems like the sort of idea that should be floated on the python-
ideas mailing list or comp.lang.python first, both to find out what 
support there is and to pin down precise semantics.

We already have int to string conversions in bases 2, 8, 10 and 16;  what 
are the use-cases for conversions in other bases?
msg91960 - (view) Author: Mark Dickinson (mark.dickinson) Date: 2009-08-25 19:53
See http://mail.python.org/pipermail/python-dev/2006-January/059789.html 
for a previous related discussion.
msg91996 - (view) Author: Yuv Gre (ubershmekel) Date: 2009-08-27 07:03
Use case - 'hashing' a counter for example like video ID's in youtube. 
One could use a regular int internally and publish a shorter 62-base id 
for links.

Guido said on http://mail.python.org/pipermail/python-dev/2006-
January/059923.html
"I think we ought to let this sit for a while and come back to it in a
few week's time. Is 'base' really the right name? It could just as
well be considered a conversion in the other direction. In common
usage, 'base' and 'radix' are about synonymous (except no-one uses
radix). Pethaps the 2nd argument should not be a keyword argument?

Also, this discussion made me wonder if conversions using other bases
than 10 should even be built-ins. A library module seems a more
appropriate place"

I'm hoping 182 weeks of clarity could help iron this issue out.
msg92010 - (view) Author: Mark Dickinson (mark.dickinson) Date: 2009-08-27 19:04
> I'm hoping 182 weeks of clarity could help iron this issue out.

:-)

I really think should bring this up on the python-ideas mailing list[1];  
it's much more likely to get resolved one way or the other if you do.

[1] http://mail.python.org/mailman/listinfo/python-ideas
History
Date User Action Args
2009-08-27 19:04:40mark.dickinsonsetmessages: + msg92010
2009-08-27 07:03:21ubershmekelsetmessages: + msg91996
2009-08-26 02:07:01cvrebertsetnosy: + cvrebert
2009-08-25 19:53:35mark.dickinsonsetmessages: + msg91960
2009-08-25 19:45:25mark.dickinsonsetnosy: + mark.dickinson
messages: + msg91959
2009-08-25 19:35:40ubershmekelcreate