Peter Otten <[EMAIL PROTECTED]> wrote: > Alex Martelli wrote: > > > if len(somecontainer) > 0: > > > > instead of the obvious > > > > if somecontainer: > > > > so it's not as if pythonistas in general are blessed with some magical > > "redundancy avoidance spell"... > > That is not always equivalent: > > >>> z = Numeric.zeros(5) > >>> z > array([0, 0, 0, 0, 0]) > >>> bool(z) > False > >>> len(z) > 0 > True
Good point! Numeric sure has semantics here that can be confusing;-). numarray's arrays just can't be used as the argument to bool(...) -- you get a runtime error "An array doesn't make sense as a truth value. Use sometrue(a) or alltrue(a)" ("in the face of ambiguity, refuse the temptation to guess"). Still, this ALSO means that len(z)>0 is very different from bool(z), for numarray as for Numeric. Most containers don't choose to implement __nonzero__, so bool(z) just calls __len__ instead; that's the scenario I had in mind, of course. But sure, if somecontainer might define peculiar semantics in __nonzero__, then in order to test if it's empty you can't use the normal pythonic idiom of checking its truth value, but rather must check its length. (I still prefer "if len(z):", avoiding the redundant ">0", but that's just a small difference, of course). Alex -- http://mail.python.org/mailman/listinfo/python-list