Message400063
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. |
|
Date |
User |
Action |
Args |
2021-08-22 09:17:57 | mark.dickinson | set | recipients:
+ mark.dickinson |
2021-08-22 09:17:57 | mark.dickinson | set | messageid: <1629623877.06.0.26111591106.issue44977@roundup.psfhosted.org> |
2021-08-22 09:17:57 | mark.dickinson | link | issue44977 messages |
2021-08-22 09:17:56 | mark.dickinson | create | |
|