I've recently run into issues when creating Numpy object arrays using e.g.
a = np.array([c], dtype=np.object)
where c is an instance of some complicated class, and in some cases Numpy tries to access some methods of that class. However, doing:
a = np.empty((1,), dtype=np.object)
a[0] = c
solves the issue. I'm curious as to what the difference is between these two internally. Why in the first case might Numpy try and access some attributes or methods of c
?
EDIT: For the record, here is example code that demonstrates the issue:
import numpy as np
class Thing(object):
def __getitem__(self, item):
print "in getitem"
def __len__(self):
return 1
a = np.array([Thing()], dtype='object')
This prints out getitem
twice. Basically if __len__
is present in the class, then this is when one can run into unexpected behavior.
np.object
and not justobject
? – JBernardo Oct 5 '11 at 21:17object == np.object
returnsTrue
) so this is not related to the issues I'm seeing. – astrofrog Oct 5 '11 at 21:21