question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Usability problem: Cannot access .shape member of numpy.ndarray as tuple

See original GitHub issue

On 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:open
  • Created 14 years ago
  • Reactions:2
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
robertwbcommented, Aug 16, 2016

bfroehle commented

Not very satisfying, but of course the following will work:

cdef np.ndarray var = np.zeros((3,))
print (<object> var).shape
0reactions
scodercommented, Sep 15, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found