Default __nonzero__ impl doesn't throw a TypeError exception
Terry Reedy
tjreedy at udel.edu
Thu Jan 8 14:51:23 EST 2009
Sergey Kishchenko wrote:
> 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.
Sensible (and documented) defaults pervade Python.
More information about the Python-list
mailing list