[Python-ideas] divmod(): fallback to __floordiv__ and __mod__?

Ethan Furman ethan at stoneleaf.us
Sat Sep 17 13:42:48 EDT 2016


On 09/17/2016 10:34 AM, David Mertz wrote:
> On Sat, Sep 17, 2016 at 3:56 AM, Mark Dickinson wrote:
>> On Sat, Sep 17, 2016 at 10:01 AM, Spencer Brown wrote:

>>> Currently, calling divmod() on a class with __floordiv__ and __mod__
>>> defined, but not __divmod__ raises a TypeError. Is there any reason
>>> why it doesn't fallback to (self // x, self % x)?
>>
>> It's an interesting idea. I wonder whether the falling back shouldn't
>> be in the other direction, though: that is, if a class defines
>> `__divmod__` but not `__floordiv__` or `__mod__`, perhaps the `//` and
>> `%` operations should use `__divmod__`? That way, if you're writing a
>
> It seems like this could be something similar to `functools.total_ordering`
>and decorate a class.  In principle that transformation could go in either
> direction, but only if the decorator is used.

Not at all.  Currently Python will fallback to `not ==` if a class does not
define `!=`.  Having `//` and `%` fall back to `__divmod__` would not be
out of place.

--
~Ethan~


More information about the Python-ideas mailing list