[PyQt] pyqt behavior change

Phil Thompson phil at riverbankcomputing.com
Wed Aug 24 17:54:16 BST 2011


On Wed, 24 Aug 2011 12:51:52 +0100, Ingrid Dahl-Olsen <id at dneg.com> wrote:
> Hi,
> 
> Sorry for commenting on a post this old, but I've only just run into 
> this change as I'm using PyQt in maya at work, and so have been stuck 
> with using 4.7.2 until very recently when it was bumped up to 4.8.4.
> 
> Just out of curiosity, is it really wrong to expect an instance of a 
> widget like QComboBox to evaluate to True? I can understand the 
> implementation of the __len__ method as a neater, more pythonic way to 
> test for number of items in the widget (although personally, I think 
> just using count() is a bit clearer). But does that really mean it makes

> sense to evaluate to False if there are no items in the combo box? It's 
> still a widget with attributes etc, which potentially takes up screen 
> space. I would have thought the __nonzero__ method should also have been

> implemented, so 'if combo' would still evaluate to True, but len( combo 
> ) would return 0?
> 
> I expect this behaviour will stay as it is, as it's been that way for a 
> year now, but just thought I'd ask...

If you want to test whether an object is None then you should always do so
explicitly, ie. use...

if combo is not None:

...and not...

if combo:

You might argue that this is just a question of programming style, but (to
me) not testing explicitly is a bug in your code. In other words the change
only affected buggy programs and so was acceptable.

Phil


More information about the PyQt mailing list