[Python-Dev] bool(container) [was bool(iter([])) changedbetween 2.3 and 2.4]

Fredrik Lundh fredrik at pythonware.com
Fri Sep 30 18:29:58 CEST 2005

Guido van Rossum wrote:

> "Containerish" behavior isn't enough to warrant empty <--> false; in
> some sense every object is a container (at least every object with a
> __dict__ attribute) and you sure don't want to map __len__ to
> self.__dict__.__len__...

the ElementTree experience shows that doing

    def __getitem__(self, index):
        return self.items[index]
    def __len__(self):
        return len(self.items)
    def __nonzero__(self):
        return True

is sometimes a good idea, even for objects that are mostly sequences.

(ET 1.2.X lacks the __nonzero__ method, and accidentally treating
elements with no subelements as if the element itself doesn't exist is by
far the most common gotcha in ET code).


More information about the Python-Dev mailing list