[Tutor] Limitation of int() in converting strings

Steven D'Aprano steve at pearwood.info
Wed Jan 2 17:41:06 CET 2013


On 03/01/13 03:18, Alan Gauld wrote:
> This is going somewhat off-topic but my curiosity is roused...
>
> On 02/01/13 15:16, Oscar Benjamin wrote:
>>  When the idea was discussed in the run up to Python 3, Guido raised
>> exactly this case and said
>> """...
>> (BTW Pascal also had the division operator right, unlike C, and we're
>> ... If we had done it that way, we wouldn't have had to introduce
>> the index() builtin and the corresponding infrastructure (__index__
>> and a whole slew of C APIs).
>
> I don't get the reference to index here.
> Why would adopting Pascal style division remove the need for index?

No, the comment about division was a parenthetical aside:

"(BTW Pascal also had the division operator right, unlike C, and we're
finally fixing this in Py3k by following Pascal's nearly-40-year-old
lead.)"


The bit about __index__ refers to using trunc():

"I still really wish I had followed Pascal's lead instead of C's here:
Pascal requires you to use trunc() to convert a real to an integer. ...
If we had done it that way, we wouldn't have had to introduce the
index() builtin and the corresponding infrastructure (__index__
and a whole slew of C APIs)."


I don't know what this "index() builtin" is, it doesn't appear to exist.
But __index__ is a special method that converts to int without rounding
or truncating, intended only for types that emulate ints but not other
numeric types:


py> (123).__index__()
123
py> (123.0).__index__()
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
AttributeError: 'float' object has no attribute '__index__'


The purpose is to allow you to use custom integer-types in sequence
indexing, e.g. if n = MyInteger(42), you can use mylist[n] and Python
will call n.__index__() to convert to a proper int. In the past,
Python would only allow actual ints or longs for indexing.

Python cannot use the int() builtin or the regular __int__ method to do
the conversion because they will happily convert floats and strings, and
you don't want to allow mylist['42'] or mylist[42.0] to succeed. So
there needs to be a second special method for converting integer-types
to real ints.

As is often the case, this need was driven by the numpy community, if I
remember correctly, which has int8, int16, int32 and int64 types that
don't inherit from int.



-- 
Steven


More information about the Tutor mailing list