From 853a18de30219a0d25709caac0410de6dfeb4e95 Mon Sep 17 00:00:00 2001 From: Aaron Meurer Date: Tue, 23 Feb 2021 18:21:35 -0700 Subject: Implement a simple passthrough __str__ and __repr__ on the array_api ndarray class These methods aren't required by the spec, but without them, the array object is harder to use interactively. --- numpy/_array_api/_array_object.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'numpy/_array_api') diff --git a/numpy/_array_api/_array_object.py b/numpy/_array_api/_array_object.py index 5ce650ae9..09f5e5710 100644 --- a/numpy/_array_api/_array_object.py +++ b/numpy/_array_api/_array_object.py @@ -40,6 +40,23 @@ class ndarray: def __new__(cls, *args, **kwargs): raise TypeError("The array_api ndarray object should not be instantiated directly. Use an array creation function, such as asarray(), instead.") + # These functions are not required by the spec, but are implemented for + # the sake of usability. + + def __str__(x: array, /) -> str: + """ + Performs the operation __str__. + """ + return x._array.__str__() + + def __repr__(x: array, /) -> str: + """ + Performs the operation __repr__. + """ + return x._array.__repr__() + + # Everything below this is required by the spec. + def __abs__(x: array, /) -> array: """ Performs the operation __abs__. -- cgit v1.2.1