Default __nonzero__ impl doesn't throw a TypeError exception
Sergey Kishchenko
voidwrk at gmail.com
Thu Jan 8 08:53:23 EST 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:
pass
Now I can use Foo objects in 'if' statements:
#prints "Ouch!"
f=Foo()
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