Skip to content

Instantly share code, notes, and snippets.

@nkartashov
Created April 22, 2018 11:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nkartashov/6eb56f942a18c7ee237689f9c71a3b27 to your computer and use it in GitHub Desktop.
Save nkartashov/6eb56f942a18c7ee237689f9c71a3b27 to your computer and use it in GitHub Desktop.
nditer doesn't preserve the class
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