Fraction Object Doesn't Have __int__ But Int(fraction(...)) Still Works
In Python, when you have an object you can convert it to an integer using the int function. For example int(1.3) will return 1. This works internally by using the __int__ magic me
Solution 1:
The __trunc__ method is used.
>>> classX(object):
def__trunc__(self):
return2.>>> int(X())
2__float__ does not work
>>> classX(object):
def__float__(self):
return2.>>> int(X())
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
int(X())
TypeError: int() argument must be a string, a bytes-like objector a number, not'X'The CPython source shows when __trunc__ is used.
Post a Comment for "Fraction Object Doesn't Have __int__ But Int(fraction(...)) Still Works"