Closed
Description
Description
Normally, to test if an list, array, dictionary, etc. is None or empty in Python I can use:
if not my_list:
return "The list is empty!"
else:
return "The list is not empty!"
However, Numpy seems to base this on the lists' content rather than length. Furthermore, it raises an exception if the length is > 1
. This:
- Is confusing
- Breaks Pythons duck-typing, forcing an expensive recast before I provide Numpy arrays to standard Python functions.
- Doesn't appear to have a strong use case: the current functionality can be reproduced with
a.any
ora.all
with less ambiguity.
Steps to reproduce
import numpy
bool( [] ) # False
bool( [0] ) # True
bool( [0, 0] ) # True
bool( {} ) # False
bool( {0: 0} ) # True
bool( {0: 0, 1: 0} ) # True
bool( numpy.array( [] ) ) # False
bool( numpy.array( [0] ) ) # False (???)
bool( numpy.array( [0, 1] ) ) # ValueError: The truth value of an array with more than one element is ambiguous.
Metadata
Metadata
Assignees
Labels
No labels