bool (__bool__) Returns whether all elements evaluate to True.
I’d be worried that this will trip people up who use the if l:
print l[0] # or whatever
pattern bool (__bool__) Returns whether all elements evaluate to True.
I’d be worried that this will trip people up who use the if l:
print l[0] # or whatever
patternThe reason you should use “if x” is the same reason you should use “if x not in y” rather than “if not x in y”. It better expresses the semantics of your operation with the side effect that it may be faster.
Good point. However setting
def __bool__(self): return self.nonEmpty
would mess up certain methods e.g. .index for nested Arrays as __eq__ is computed elementwise and bool(Array(False, False)) would evaluate to True.Maybe a warning would be appropriate? (as is the case with ndarrays)
Isn't that consistent with the built-in `list`, though, because `bool([False, False])` is True?
For example, when calling
Array((x, y), (z, w)).index((z, w))
the following piece of code is executed bool(Array((x, y)).__eq__((z, w)))
= bool(Array(False, False))
If __bool__ returned whether the Array is nonempty, bool(Array(False, False)) would evaluate to True and the method would wrongly return 0.You're right that it would be more clear if __bool__ would behave similarly, but since Array computes operations element-wise, it isn't possible.
If you used `all()` in your implementation instead, you could be compatible with the idiomatic use of `bool(my_list)` and the _very_ common `if my_list:` structure could be used with Arrays too (like most people probably would expect from a "better list type")
Regardless, Pandas struggles with the same problem, so you are at least in good company :)