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

Lisandro Dalcin dalcinl at gmail.com
Fri Sep 30 22:21:47 CEST 2005

On 9/30/05, Raymond Hettinger <raymond.hettinger at verizon.net> wrote:
> [Guido van Rossum]
> > __len__ is for sequences and mappings specifically -- everything that
> > supports __getitem__ should have __len__ and everything that has
> > __len__ should have __getitem__.
> That's going a bit far.  Unordered collections (like sets and bags) are
> a good counter-example.

I've implemented many extension types in C, always to interface
external libraries (the main examples, MPI and PETSc). In those
libraries, some object are invalid (MPI_COMM_NULL) or can get
deallocated in the C side. I always implement a __nonzero__ method for
testing those situations. It sounds a bit bizarre to me to have to
implement a number method for a object that is far from being a
number, and of course __len__ do not make any sense.

Perhaps Py3k could have a more explicit mechanism for definig the
truth value of an object? Something like __bool__ method to be filled
in type object? Any opinions?

Lisandro Dalcín

More information about the Python-Dev mailing list