For point of reference, other languages are similarly broken (or perhaps they just choose to not guess). Ruby for example exhibits the following behaviour:

'0x123'.to_i
# => 0
'0x123'.to_i 16
# => 291
'123'.to_i
# => 123

And for what it is worth, int has a default parameter base with the value of 10. If you look at the documentation that is present:
 |  int(x, base=10) -> int or long

In other words, int is always expecting a base 10 number unless otherwise specified. Guessing at a number's base to save you from having to call int('0x123', 16) is not a good thing.

Regarding internal consistency (int vs float), I would guess (but I don't know) that this has to do with how floats are represented typically. But I'll let someone with more specific knowledge of that difference answer that concern.


On Thu, Feb 6, 2014 at 6:30 AM, Ram Rachum <ram@rachum.com> wrote:
Yep, that's what I meant :)


On Thu, Feb 6, 2014 at 2:28 PM, spir <denis.spir@gmail.com> wrote:
On 02/06/2014 11:24 AM, Ram Rachum wrote:
What do you think about letting the `int` constructor automatically
understand the number type without specifying base if given a prefix, so
int('0x3414fa') would immediately work without specifying a base of 16?

Do you mean int(numeral), where numeral is a *variable* string, with a python base prefix? (Else, just type in the constant 0x3414fa ;-) If yes, then I find it a good idea. When int() is used to decode variable numerals, it could/should/would decode all correct python numeral notations.

Note that int() also does not decode 'e' postfixes:

Python 3.3.2+ (default, Oct  9 2013, 14:50:09)
[GCC 4.8.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
int(123e2)
12300
int("123e2")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '123e2'

But float() does:

float(-1.23e4)
-12300.0
float("-1.23e4")
-12300.0

!

After all, it's just a question of practical notational conventions (we don't use "hundred and twenty-three" or "CXXIII" or "v^^^^^v^^"). Python's own decoding builtins should be consistent with its own choice of notations.

d
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


--

--- You received this message because you are subscribed to a topic in the Google Groups "python-ideas" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python-ideas/RKQviWz9BYk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to python-ideas+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/