-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add __int__, __float__, and __bool__ to the array object #127
Conversation
|
||
- **out**: _<bool>_ | ||
|
||
- a Python `bool` object representing the single element of the array `x`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What should the behavior be if the array does not meet the requirement (no zero-dimensional, or different type)?.
Does the standard care whether ValueError
or TypeError
are raised?
NumPy can raise both:
In [2]: np.__version__
Out[2]: '1.20.0'
In [3]: X = np.zeros((2,2),dtype='d')
In [4]: X.__bool__
Out[4]: <method-wrapper '__bool__' of numpy.ndarray object at 0x2b67bdea50f0>
In [5]: X.__bool__()
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-5-2acecd4ea032> in <module>
----> 1 X.__bool__()
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
In [6]: X.__float__()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-6-78817c2ae92b> in <module>
----> 1 X.__float__()
TypeError: only size-1 arrays can be converted to Python scalars
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does the standard care whether
ValueError
orTypeError
are raised?
In general we've refrained from specifying this, because (a) error handling is broad, (b) existing libraries are very inconsistent, and (c) there are situations where error handling strategy for accelerators may be different from that on CPU on purpose.
Co-authored-by: Athan <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Fixed a merge conflict, in it goes. Thanks @asmeurer, and thanks @kgryte and @oleksandr-pavlyk for reviewing.
These are only required for dimension-0 arrays of a matching dtype.
See numpy/numpy#10404 for why it is not a good idea to require this for higher dimension size 1 arrays.