New submission from Mark Dickinson <[email protected]>:
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 <[email protected]>
<https://bugs.python.org/issue44977>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com