Created
April 22, 2018 11:25
-
-
Save nkartashov/6eb56f942a18c7ee237689f9c71a3b27 to your computer and use it in GitHub Desktop.
nditer doesn't preserve the class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
class CustomArray(np.ndarray): | |
def __new__(cls, matrix, custom_attribute): | |
obj = np.asarray(matrix).view(cls) | |
obj.custom_attribute = custom_attribute | |
return obj | |
def __array_finalize__(self, obj): | |
if obj is None: | |
return | |
self.custom_attribute = getattr(obj, 'custom_attribute', None) | |
x = CustomArray(np.asarray([1,2,3]), 'some_value') # a custom array | |
y = np.asarray([4,5,6]) # a regular array | |
bar = np.nditer([x, x])[0] | |
print(type(bar)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment