Message265196
| Author |
serhiy.storchaka |
| Recipients |
mark.dickinson, serhiy.storchaka |
| Date |
2016年05月09日.14:02:31 |
| SpamBayes Score |
-1.0 |
| Marked as misclassified |
Yes |
| Message-id |
<1462802551.63.0.282711121635.issue26984@psf.upfronthosting.co.za> |
| In-reply-to |
| Content |
The int constructor can return an instance of int subclass.
>>> class BadTrunc:
... def __trunc__(self):
... return True
...
>>> int(BadTrunc())
True
When __int__ returns non-exact int, at least a warning is emitted:
>>> class BadInt:
... def __int__(self):
... return True
...
>>> int(BadInt())
__main__:1: DeprecationWarning: __int__ returned non-int (type bool). The ability to return an instance of a strict subclass of int is deprecated, and may be removed in a future version of Python.
True
The constructor of int subclass always return an instance of correct type:
>>> class IntSubclass(int):
... pass
...
>>> type(IntSubclass(BadTrunc()))
<class '__main__.IntSubclass'>
>>> type(IntSubclass(BadInt()))
__main__:1: DeprecationWarning: __int__ returned non-int (type bool). The ability to return an instance of a strict subclass of int is deprecated, and may be removed in a future version of Python.
<class '__main__.IntSubclass'>
I don't know if it is worth to deprecate __trunc__ returning non-exact int, since this special method is used in math.trunc(). But I think that the int constructor should convert its result to exact int. If some preparatory period is needed, it can first start to emit FutureWarning. |
|
History
|
|---|
| Date |
User |
Action |
Args |
| 2016年05月09日 14:02:31 | serhiy.storchaka | set | recipients:
+ serhiy.storchaka, mark.dickinson |
| 2016年05月09日 14:02:31 | serhiy.storchaka | set | messageid: <1462802551.63.0.282711121635.issue26984@psf.upfronthosting.co.za> |
| 2016年05月09日 14:02:31 | serhiy.storchaka | link | issue26984 messages |
| 2016年05月09日 14:02:31 | serhiy.storchaka | create |
|