Message265191
The float constructor can return an instance of float subclass.
>>> class FloatSubclass(float):
... pass
...
>>> class BadFloat:
... def __float__(self):
... return FloatSubclass(1.2)
...
>>> type(float(BadFloat()))
<class '__main__.FloatSubclass'>
Comparing with other types, complex() always returns complex:
>>> class ComplexSubclass(complex):
... pass
...
>>> class BadComplex:
... def __complex__(self):
... return ComplexSubclass(1.2, 3.4)
...
>>> type(complex(BadComplex()))
<class 'complex'>
And int() can return an instance of int subclass, but this behavior is deprecated:
>>> 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
May be we should either deprecate __float__ returning non-float (as for int), or convert the result to exact float (as for complex).
The constructor of float subclass always returns an instance of correct type.
>>> class FloatSubclass2(float):
... pass
...
>>> type(FloatSubclass2(BadFloat()))
<class '__main__.FloatSubclass2'> |
|
Date |
User |
Action |
Args |
2016-05-09 13:46:33 | serhiy.storchaka | set | recipients:
+ serhiy.storchaka, mark.dickinson |
2016-05-09 13:46:33 | serhiy.storchaka | set | messageid: <1462801593.23.0.789372695786.issue26983@psf.upfronthosting.co.za> |
2016-05-09 13:46:32 | serhiy.storchaka | link | issue26983 messages |
2016-05-09 13:46:32 | serhiy.storchaka | create | |
|