[BangPypers] ElementTree nodes

bhaskar jain bhaskar.jain2002 at gmail.com
Sun Oct 25 21:42:54 CET 2009

On Mon, Oct 26, 2009 at 1:53 AM, Sidharth Kuruvila <
sidharth.kuruvila at gmail.com> wrote:
>>>It turns out any object that returns len(o) as 0
>>>will evaluate as false.

Not always.

doc says - "instances of user-defined classes, if the class defines a
__nonzero__() or __len__() method, when that method returns the integer zero
or bool value False."


Called to implement truth value testing and the built-in operation bool();
should return False or True, or their integer equivalents 0 or 1. When this
method is not defined, __len__() is called, if it is defined, and the object
is considered true if its result is nonzero. If a class defines neither
__len__() nor __nonzero__(), all its instances are considered true.

First it looks for __nonzero__ and if its absent then only __len__

>>> class c:
... def __nonzero__(self):
...     return False
... def __len__(self):
...    return True
>>> o = c()
>>> bool(o)


More information about the BangPypers mailing list