Sergey Kishchenko a écrit :
In Python empty container equals False in 'if' statements:

Yes.

# 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:

Yes.

#prints "Ouch!"
f=Foo()
if f:
    print "Ouch!"

So, default __nonzero__ impl is to return True.

Yes. It's clearly documented FWIW.

I think, this
behaviour conflicts with 'Explicit is better than implicit'

Why so ? It *is* explicit that the default for an object is to have a true value in a boolean context.

and
'Practicality beats purity'

Quite on the contrary. From a practical POV, the default truth values of Python objects are most of the time what you practically want in a boolean context - that is, any non-None object, non-empty sequence and non-zero numeric objects are true. __nonzero__ is here for the *very few* corner cases where this is not the sensible default.

statements. I think, throwing a TypeError
exception would be better.  It will result in more explicit code with
fewer errors.

I can understand that you've been bitten by the rules regarding truth values of Python objects. But if so, please remember that it's only because *you* assumed something different from what's documented.
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to