Ask Your Question

What are all the types we can declare in Cython?

asked 2013-07-02 04:43:20 -0500

Let me be more precise: I know we can declare "int", "list", "float", "double", but what else ?

Is it possible to declare a matrix ?
Is it possible to declare combined types, as list of list of integers...?

My goal is that the program doesn't lose time to find, for example, in which type are the elements of a list...

If it's to long to describe all such available types here, maybe you can indicate to me where I can find this information.

edit retag flag offensive close merge delete

2 answers

Sort by » oldest newest most voted

answered 2013-07-03 02:30:53 -0500

niles gravatar image

The documentation at is probably a good place to start. After that, you could look at the Sage source code, either in your local install or at For example, you can see that other types are declared in sage/matrix/misc.pyx.

edit flag offensive delete link more

answered 2013-07-03 07:52:26 -0500

Volker Braun gravatar image

The biggest speed benefit comes from using C primitive types instead of Python objects, for example machine integers instead of Python / Sage integer objects. Of course thats a pretty small list of types.

You can also declare any Cython cdef class, but that mostly saves you from having to cast the Python object to the cdef class all the time. Not so much speed benefit.

Python lists are still hash tables containing Python objects even if you declare them as cdef list foo. In particuar, it is not possible to have a memory-contiguous array of a particular C datatype that way. You shoud use C arrays or C++ std::vector if you need arrays of a fixed C datatype.

Note that your question smells of premature optimization. If you haven't benchmarked your Python implementation yet then don't bother.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2013-07-02 04:43:20 -0500

Seen: 2,558 times

Last updated: Jul 03 '13