[New-bugs-announce] [issue44977] Deprecate delegation of int to __trunc__?

Mark Dickinson report at bugs.python.org
Sun Aug 22 05:17:57 EDT 2021


New submission from Mark Dickinson <dickinsm at gmail.com>:

The int constructor, when applied to a general Python object `obj`, first looks for an __int__ method, then for an __index__ method, and then finally for a __trunc__ method.

The delegation to __trunc__ used to be useful: it meant that users could write a custom class SomeNumber with the property that:

- SomeNumber instances supported 'int' calls, returning a truncated value, but
- SomeNumber instances weren't usable in indexing, chr() calls, and all the various other calls that implicitly invoked __int__.

class SomeNumber:
    def __trunc__(self):
       <return truncated value for self>

However, with Python >= 3.10, we no longer use __int__ implicitly for argument conversion in internal code. So the second point above is no longer a concern, and SomeNumber can now simply be written as

class SomeNumber:
    def __int__(self):
       <return truncated value for self>

This decouples int from __trunc__ and leaves __trunc__ as simply the support for the math.trunc function.

----------
messages: 400063
nosy: mark.dickinson
priority: normal
severity: normal
status: open
title: Deprecate delegation of int to __trunc__?

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue44977>
_______________________________________


More information about the New-bugs-announce mailing list