[Python-ideas] Is there a reason some of the PyLong_As* functions don't call an object's __int__?

Nick Coghlan ncoghlan at gmail.com
Sat Dec 9 02:09:05 EST 2017

On 8 December 2017 at 22:33, Erik Bray <erik.m.bray at gmail.com> wrote:
> In other words, there are probably countless other cases in the stdlib
> at all where it "doesn't make sense" to accept a float, but that
> otherwise should accept objects that can be coerced to int without
> having to manually wrap those objects with an int(o) call.

Updating these to call __index__ is fine (since that sets the
expectation of a *lossless* conversion to an integer), but updating
them to call __int__ generally isn't (since that conversion is allowed
to be lossy, which may cause surprising behaviour).

Indexing & slicing were the primary original use case for that
approach (hence the method name), but it's also used for sequence
repetition, and other operations.


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-ideas mailing list