On Thu, 14 Oct 2021 11:52:11 +1300
Greg Ewing <greg.ew...@canterbury.ac.nz> wrote:

> On 14/10/21 11:19 am, Greg Ewing wrote:
> > Not really -- __int__ is expected to return something of type
> > int, whereas __trunc__ is expected to return the same type as
> > its operand.  
> 
> Scratch that, it seems __trunc__ also returns an int, at least
> for floats. Not sure what the logic behind that is.
> 
> There are differences between the functions int() and trunc()
> though:
> 
>  >>> int("42")  
> 42
> 
>  >>> trunc("42")  
> Traceback (most recent call last):
>    File "<stdin>", line 1, in <module>
> TypeError: type str doesn't define __trunc__ method

That is behind the point, because str doesn't define __int__ either.
The int() function does more than just call __int__, it checks a whole
lot of different possibilites (including checks for str and buffer-like
objects, indeed).

Similarly:

>>> int(memoryview(b"123"))
123
>>> memoryview(b"123").__int__()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'memoryview' object has no attribute '__int__'

Regards

Antoine.


_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/IRRRKKFXCS64LXXJF5TQPBF5MX4UOTIE/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to