This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author madphysicist
Recipients docs@python, madphysicist
Date 2017-07-26.03:02:58
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1501038179.61.0.173139487282.issue31042@psf.upfronthosting.co.za>
In-reply-to
Content
The docs for [`operator.index`][1] and `operator.__index__` state that

> Return *a* converted to an integer. Equivalent to `a.__index__()`.

The first sentence is correct, but the second is not. First of all, we have the data model [docs][2]:

> For custom classes, implicit invocations of special methods are only guaranteed to work correctly if defined on an object’s type, not in the object’s instance dictionary.

Secondly, we can make a simple counter-example in code:

```
import operator

class A:
    def __index__(self): return 0

a = A()
a.__index__ = (lambda self: 1).__get__(a, type(a))
operator.index(a)
```

The result is of course zero and not one.

I believe that the docs should read something more like one of the following to avoid being misleading:

> Return *a* converted to an integer, if it is already an integral type.

> Return *a* converted to an integer. Equivalent to `type(a).__index__(a)`.

Or a combination of both:

> Return *a* converted to an integer, if it is already an integral type. Equivalent to `type(a).__index__(a)`.

  [1]: https://docs.python.org/3/library/operator.html#operator.index
  [2]: https://docs.python.org/3/reference/datamodel.html#special-method-lookup
History
Date User Action Args
2017-07-26 03:02:59madphysicistsetrecipients: + madphysicist, docs@python
2017-07-26 03:02:59madphysicistsetmessageid: <1501038179.61.0.173139487282.issue31042@psf.upfronthosting.co.za>
2017-07-26 03:02:59madphysicistlinkissue31042 messages
2017-07-26 03:02:58madphysicistcreate