[PyQt] Floats accepted for C++ ints?
Phil Thompson
phil at riverbankcomputing.com
Wed Jul 6 09:23:47 BST 2016
On 5 Jul 2016, at 11:07 pm, Florian Bruhin <me at the-compiler.org> wrote:
>
> * Phil Thompson <phil at riverbankcomputing.com> [2016-07-05 22:18:16 +0100]:
>> On 5 Jul 2016, at 9:51 pm, Florian Bruhin <me at the-compiler.org> wrote:
>>>
>>> * Phil Thompson <phil at riverbankcomputing.com> [2016-07-05 18:33:37 +0100]:
>>>> On 5 Jul 2016, at 6:22 pm, Florian Bruhin <me at the-compiler.org> wrote:
>>>>>
>>>>> I've noticed Python floats are accepted when a C++ int is expected,
>>>>> and simply truncated:
>>>>>
>>>>> >>> from PyQt5.QtCore import QRect
>>>>> >>> QRect(0.5, 0.5, 0.5, 0.5)
>>>>> PyQt5.QtCore.QRect()
>>>>> >>> QRect(1.5, 1.5, 1.5, 1.5)
>>>>> PyQt5.QtCore.QRect(1, 1, 1, 1)
>>>>>
>>>>> Is this intended behaviour? I'd have expected to raise a TypeError
>>>>> (or, if anything, accept floats where .is_integer() is true)
>>>>
>>>> It's standard Python behaviour. Floats implement __int__().
>>>
>>> They also implement __str__, yet I obviously can't use them where Qt
>>> expects a QString ;)
>>>
>>> Isn't __int__ just there to customize what should happen when using
>>> int(), just like __str__ is for str()?
>>>
>>> float is not a subclass of int (i.e. isinstance(1.5, int) == False),
>>> which is why this not raising a TypeError confuses me.
>>
>> It's how PyLong_AsLong() works...
>>
>> https://docs.python.org/3/c-api/long.html#c.PyLong_AsLong
>
> Fair enough - I guess you could call PyLong_Check() on it first
> ("Return true if its argument is a PyLongObject or a subtype of
> PyLongObject"), but it might also be too late to do that now?
About 18 years too late.
Phil
More information about the PyQt
mailing list