question about True values
Robert Kern
robert.kern at gmail.com
Wed Oct 25 19:57:27 EDT 2006
George Sakkis wrote:
> Martin v. Löwis wrote:
>> Not-so-obviously, arbitrary user-defined values can also be
>> treated as false: If they implement __nonzero__, they are
>> false if False is returned from __nonzero__; otherwise,
>> if they implement __len__, they are false if 0 is returned
>> from __len__. Under these rules, array.array objects can
>> also be false, as can UserList and UserDict objects.
>
> A notable exception are numarray arrays (probably true for numpy too, I
> haven't tested it though):
>
>>>> from numarray import array
>>>> bool(array([1,2]))
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> RuntimeError: An array doesn't make sense as a truth value. Use any(a)
> or all(a).
numpy also has this behavior. Numeric yielded to the temptation to guess any(a)
in the face of this ambiguity. Much buggy code was written as a result.
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
More information about the Python-list
mailing list