Numpy object arrays Numpy object arrays numpy numpy

Numpy object arrays


In the first case a = np.array([c], dtype=np.object), numpy knows nothing about the shape of the intended array.

For example, when you define

d = range(10)a = np.array([d])

Then you expect numpy to determine the shape based on the length of d.

So similarly in your case, numpy will attempt to see if len(c) is defined, and if it is, to access the elements of c via c[i].

You can see the effect by defining a class such as

class X(object):    def __len__(self): return 10    def __getitem__(self, i): return "x" * i

Then

print numpy.array([X()], dtype=object)

produces

[[ x xx xxx xxxx xxxxx xxxxxx xxxxxxx xxxxxxxx xxxxxxxxx]]

In contrast, in your second case

a = np.empty((1,), dtype=np.object)a[0] = c

Then the shape of a has already been determined. Thus numpy can just directly assign the object.

However to an extent this is true only since a is a vector. If it had been defined with a different shape then method accesses will still occur. The following for example will still call ___getitem__ on a class

a = numpy.empty((1, 10), dtype=object)a[0] = X()print a

returns

[[ x xx xxx xxxx xxxxx xxxxxx xxxxxxx xxxxxxxx xxxxxxxxx]]