[PyQt] QPyNullVariant and bool()

Phil Thompson phil at riverbankcomputing.com
Wed Apr 25 10:47:07 BST 2012


On Wed, 25 Apr 2012 04:26:10 -0400, Deniz Turgut <dturgut at gmail.com>
wrote:
> On Wed, Apr 25, 2012 at 3:26 AM, Phil Thompson
> <phil at riverbankcomputing.com> wrote:
>> On Tue, 24 Apr 2012 14:28:02 -0400, Deniz Turgut <dturgut at gmail.com>
>> wrote:
>>> Would it be logical to add a __nonzero__ method to QPyNullVariant
>>> which always returns False?
>>>
>>> I was going through a code of mine which broke with a new version of
>>> PyQt. I realized that it was because there is no __nonzero__ method in
>>> QPyNullVariant so bool(QPyNullVariant) is always True.
>>>
>>> Right now, if you use QVariant api 2, you can't check nicely (without
>>> typechecking) whether you get a nonzero value or Null/Falsy value.
>>
>> From a purist point of view I don't associate Null with False (or with
>> True either but the current behaviour reflects the Python default).
>>
>> From a portability point of view it's a change in the API.
>>
>> From a pragmatic point of view I need to be persuaded - I've not been
>> affected by the issue myself.
> 
> Actually, pragmatic part is not much of a big deal. Although it looks
> a bit ugly to me, I can live with code like:
> 
> if value and not isinstance(value, QtCore.QPyNullVariant):
>    do_something_with(value)
> 
> Or I could deal with it much before, and eliminate a Null possibility.
> 
> I guess my issue is closer to the purist view. I do agree that Null is
> neither True, nor False, but I'd expect it to behave like None.

That sounds reasonable.

> It is
> not False but rather Falsy. That's why, I spent some time figuring out
> why a test like 'if value:' was passing when a Null was returned.

Happy to implement it on the None precedent.

Phil


More information about the PyQt mailing list