On 02/01/13 16:55, Dave Angel wrote:
On 01/02/2013 11:41 AM, Steven D'Aprano wrote:

The bit about __index__ refers to using trunc():

OK, that partially solves it :-)


I don't know what this "index() builtin" is, it doesn't appear to exist.

That was also what confused me. The only indexz() I could find was the one that found the index of an item in a collection

>>> [1,2,3].index(2)
1

And I didn't see how trunc() or division helped there...

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:

And this was the new bit I didn't know about.

import operator
print operator.index(myobject)

But I did try

>>> import operator as op
>>> help(op.index)
Help on built-in function index in module operator:

index(...)
    index(a) -- Same as a.__index__()

Which was no help at all, so I tried

>>> help(a.__index__)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
NameError: name 'a' is not defined

Which was what I expected!

So now with your input I can try:

>>> help(int.__index__)
Help on wrapper_descriptor:

__index__(...)
    x[y:z] <==> x[y.__index__():z.__index__()]


Bingo! Although still doesn't anything explicitly about the need for an integer!

But that was harder than it should have been! :-(

Thanks guys,

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to