1

I am trying to write a custom array container following numpy's guide and I can't understand why the following code always returns NotImplemented.

import numpy as np
a = np.array([1])
shape = (2, 2)
a.__array_function__(func=np.broadcast_to, types=(np.ndarray, type(shape)), args=(a, shape), kwargs={})
Out[5]: NotImplemented

Even though the normal method obviously works

np.broadcast_to(a, shape)
Out[6]: 
array([[1, 1],
       [1, 1]])

The function np.broacast_to is just an example, np.reshape has the same issue.

Does anyone know what my mistake is?

1 Answer 1

2

types is only supposed to include types that actually implement __array_function__. type(shape) doesn't implement __array_function__, so it shouldn't be in types.

Sign up to request clarification or add additional context in comments.

2 Comments

That was definitely the problem, so how does __array_function__ know which type corresponds to which argument if the two iterables have different lengths?
Have a look here. The first few paragraphs before the example hold the answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.