You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Numpy supports various ways to convert an instance of a class to a numpy array. For example, using the __array__ syntax:
importnumpyasnpclassVec3d:
x=1y=2z=3def__array__(self, dtype):
returnnp.array([self.x, self.y, self.z])
v=Vec3d()
n=np.zeros((2,3))
n[0] =v# v can be used in assignments like this
However, when the class supports PEP 3118, then this overwrites all other ways. For example, the following Vec3dS class is a subclass of ctypes' Structure (which implements PEP 3118):
When converting an instance of Vec3dS to a numpy array, the resulting array has dtype([('x', '<f8'), ('y', '<f8'), ('z', '<f8')]). The __array__ function (as well as __array_interface__) is ignored. This prevents any subclass of ctypes.Structure to be used in assignments like this:
vs=Vec3dS(1,2,3)
n[1] =vs# this will fail
Of course, there are several other ways to create a numpy array from a ctypes Structure. But unless I'm missing something, none of them allow a user to use the simple n[1] = vs syntax in the above example.
It would be nice to have a way to explicitly tell numpy how to convert an instance of a class to a numpy array rather than always prioritizing PEP 3118. Correct me if I'm wrong, but I don't think I can overwrite the PEP 3118 interface from the python side.
The text was updated successfully, but these errors were encountered:
xref gh-17134 which this is a duplicate of. As I said there, my gut feeling is that we could reorder it if we want to, although the buffer protocol is by far the fastest to test for unfortunately.
This feels like something that should be handled in Python itself; such as being able to set __buffer__ = None to entirely discard the buffer interface on a subtype. I think PyPy already behaves in this way.
I quite sure I saw Python discussing implementing __buffer__ recently. But I don't want to search for it right now. That would indeed at least solve the issue by allowing to override the superclass (and some other niceties).
Proposed new feature or change:
Numpy supports various ways to convert an instance of a class to a numpy array. For example, using the
__array__
syntax:However, when the class supports PEP 3118, then this overwrites all other ways. For example, the following
Vec3dS
class is a subclass of ctypes'Structure
(which implements PEP 3118):When converting an instance of
Vec3dS
to a numpy array, the resulting array hasdtype([('x', '<f8'), ('y', '<f8'), ('z', '<f8')])
. The__array__
function (as well as__array_interface__
) is ignored. This prevents any subclass ofctypes.Structure
to be used in assignments like this:Of course, there are several other ways to create a numpy array from a ctypes Structure. But unless I'm missing something, none of them allow a user to use the simple
n[1] = vs
syntax in the above example.It would be nice to have a way to explicitly tell numpy how to convert an instance of a class to a numpy array rather than always prioritizing PEP 3118. Correct me if I'm wrong, but I don't think I can overwrite the PEP 3118 interface from the python side.
The text was updated successfully, but these errors were encountered: