Usability problem: Cannot access .shape member of numpy.ndarray as tuple
See original GitHub issueOn typed variable of type numpy.ndarray, the .shape member does not work
cdef numpy.ndarray var = numpy.zeros((3,))
print var.shape
gives the cython compiler error “Cannot convert ‘numpy.npy_intp *’ to Python object”
Comment by dagss (mailing list):
This is mostly a question of semantics. shape is defined to be a C integer array for speed of var.shape[0]. How can we define a generic rule in Cython which makes both possible?
Should the behaviour of “var.shape” e.g. depend on the requested return value? This means overloading on return value which is a non-trivial language change, though perhaps appropriate here.
Thoughts welcome!
Migrated from http://trac.cython.org/ticket/302
Issue Analytics
- State:
- Created 14 years ago
- Reactions:2
- Comments:12 (7 by maintainers)
Top Results From Across the Web
Cython numpy array shape, tuple assignments - Stack Overflow
which is possibly the result of the fact that "shape" gets converted to a C array (for faster access), so it's no longer...
Read more >numpy.ndarray.shape — NumPy v1.24 Manual
The shape property is usually used to get the current shape of an array, ... to reshape the array in-place by assigning a...
Read more >numpy.asarray — NumPy v1.24 Manual
Input data, in any form that can be converted to an array. This includes lists, lists of tuples, tuples, tuples of tuples, tuples...
Read more >Structured arrays — NumPy v1.24 Manual
You can access and modify individual fields of a structured array by ... shape) where shape is optional. fieldname is a string (or...
Read more >NumPy quickstart — NumPy v1.25.dev0 Manual
The length of the shape tuple is therefore the number of axes, ndim . ... because we will access the elements in an...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
bfroehle commented
Not very satisfying, but of course the following will work:
BTW, doesn’t
arr.shape[:3]
(for a 3D array) work, and provide a nicer work-around? That’s basically what Cython would have to do internally then.