Default __nonzero__ impl doesn't throw a TypeError exception

Sergey Kishchenko voidwrk at
Thu Jan 8 14:53:23 CET 2009

In Python empty container equals False in 'if' statements:

# prints "It's ok"
if not []:
    print "It's ok"

Let's create a simple Foo class:

class Foo:

Now I can use Foo objects in 'if' statements:

#prints "Ouch!"
if f:
    print "Ouch!"

So, default __nonzero__ impl is to return True. I think, this
behaviour conflicts with 'Explicit is better than implicit' and
'Practicality beats purity' statements. I think, throwing a TypeError
exception would be better.  It will result in more explicit code with
fewer errors.

More information about the Python-list mailing list