Skip to content

Instantly share code, notes, and snippets.

@mdboom
Created July 3, 2013 15:29
Show Gist options
  • Save mdboom/5919376 to your computer and use it in GitHub Desktop.
Save mdboom/5919376 to your computer and use it in GitHub Desktop.
__array_prepare__ doesn't get called.
import numpy as np
class Quantity(object):
def __init__(self, value, unit):
self.unit = unit
self.value = value
self.exception = False
def __getitem__(self, key):
return Quantity(self.value[key], unit=self.unit)
def __len__(self):
if self.exception:
raise RuntimeError()
return len(self.value)
def __array_prepare__(self):
print "__array_prepare__"
self.exception = True
def __array_wrap__(self):
print "__array_wrap__"
self.exception = False
import numpy as np
q = Quantity(np.array([1,2,3]), 'degree')
print(repr(np.array(q)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment