str(<int>, base=<int>) as complement to int(<str>, base=<int>)
Hello! Python's int type has an optional argument base which allows people to specify a base for the conversion of a string to an integer.
int('101', 2) 5 int('a', 16) 10
I've sometimes missed a way to reverse the process. How would you like an optional second argument to str() that takes an int from 2 to 36?
str(5, 2) '101' str(10, 16) 'a'
I know it's not a killer feature but it feels right to have a complement. How do you like the idea? Christian
On 10/31/07, Christian Heimes
Python's int type has an optional argument base which allows people to specify a base for the conversion of a string to an integer.
int('101', 2) 5 int('a', 16) 10
I've sometimes missed a way to reverse the process. How would you like an optional second argument to str() that takes an int from 2 to 36?
str(5, 2) '101' str(10, 16) 'a'
I know it's not a killer feature but it feels right to have a complement. How do you like the idea?
This was discussed before: http://mail.python.org/pipermail/python-dev/2006-January/059789.html Seemed like people liked the concept, but there was a fair debate about syntax (should it be the str constructor, should it be an int method, etc.) Steve -- I'm not *in*-sane. Indeed, I am so far *out* of sane that you appear a tiny blip on the distant coast of sanity. --- Bucky Katt, Get Fuzzy
Steven Bethard wrote:
On 10/31/07, Christian Heimes
wrote: Python's int type has an optional argument base which allows people to specify a base for the conversion of a string to an integer.
int('101', 2) 5 int('a', 16) 10
I've sometimes missed a way to reverse the process. How would you like an optional second argument to str() that takes an int from 2 to 36?
str(5, 2) '101' str(10, 16) 'a'
I know it's not a killer feature but it feels right to have a complement. How do you like the idea?
This was discussed before:
http://mail.python.org/pipermail/python-dev/2006-January/059789.html
Seemed like people liked the concept, but there was a fair debate about syntax (should it be the str constructor, should it be an int method, etc.)
Steve
Or should it be a function in the math or string module? Ron
It should *definitely* *not* be the str() constructor, which is
already overloaded. Remember str() takes arguments of *any* type.
Overloading variants on the conversion to string via arguments to
str() doesn't scale.
On 10/31/07, Steven Bethard
On 10/31/07, Christian Heimes
wrote: Python's int type has an optional argument base which allows people to specify a base for the conversion of a string to an integer.
int('101', 2) 5 int('a', 16) 10
I've sometimes missed a way to reverse the process. How would you like an optional second argument to str() that takes an int from 2 to 36?
str(5, 2) '101' str(10, 16) 'a'
I know it's not a killer feature but it feels right to have a complement. How do you like the idea?
This was discussed before:
http://mail.python.org/pipermail/python-dev/2006-January/059789.html
Seemed like people liked the concept, but there was a fair debate about syntax (should it be the str constructor, should it be an int method, etc.)
Steve -- I'm not *in*-sane. Indeed, I am so far *out* of sane that you appear a tiny blip on the distant coast of sanity. --- Bucky Katt, Get Fuzzy _______________________________________________ Python-ideas mailing list Python-ideas@python.org http://mail.python.org/mailman/listinfo/python-ideas
-- --Guido van Rossum (home page: http://www.python.org/~guido/)
On 31 Oct 2007, at 06:02, Christian Heimes wrote:
I know it's not a killer feature but it feels right to have a complement. How do you like the idea?
Christian
How about extending the int type's (and other numeric types', perhaps) implementation of __format__ (for py3k -- PEP 3101) so that it can take an optional format specifier component indicating the base?
We go over this about once a year. The conclusion is always the same:
there isn't enough use for bases other than 2, 8, 10, 16 to bother
including anything, and these are already covered by bin(), oct(),
str() and hex(). (bin() is in 3.0 and to be backported to 2.6.)
On 11/1/07, Adam Atlas
On 31 Oct 2007, at 06:02, Christian Heimes wrote:
I know it's not a killer feature but it feels right to have a complement. How do you like the idea?
Christian
How about extending the int type's (and other numeric types', perhaps) implementation of __format__ (for py3k -- PEP 3101) so that it can take an optional format specifier component indicating the base?
_______________________________________________ Python-ideas mailing list Python-ideas@python.org http://mail.python.org/mailman/listinfo/python-ideas
-- --Guido van Rossum (home page: http://www.python.org/~guido/)
On 11/1/07, Guido van Rossum
We go over this about once a year. The conclusion is always the same: there isn't enough use for bases other than 2, 8, 10, 16 to bother including anything, and these are already covered by bin(), oct(), str() and hex(). (bin() is in 3.0 and to be backported to 2.6.)
Of course, if part of the deal were dropping bin, oct, and hex, that might be a good trade. But it may already be too late even for Py3. -jJ
participants (6)
-
Adam Atlas
-
Christian Heimes
-
Guido van Rossum
-
Jim Jewett
-
Ron Adam
-
Steven Bethard