[Tutor] Limitation of int() in converting strings

Dave Angel d at davea.name
Wed Jan 2 17:55:21 CET 2013


On 01/02/2013 11:41 AM, Steven D'Aprano wrote:
> <snip>
>
> 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:

I suspect that at one time, an index() built-in was intended.  It's now
available as an operator, and simply calls the __index__() as you say.

import operator
print operator.index(myobject)

works, at least in 2.7 and 3.x



-- 

DaveA



More information about the Tutor mailing list