One of our goals is to make Cython as compatible as possible with standard Python. This page lists the things that work in Python but not in Cython.
Function definitions (whether using def or cdef) cannot be nested within other function definitions.
def make_func():
def f(x):
return x*x
return f
(work in progress) This relies on functional closures
Using the yield keywords. (work in progress) This relies on functional closures
a = b%c
where b and c are floats will raise the error “Invalid operand types for ‘%’ (float; float)”
This can currently be worked around by putting:
cdef extern from "math.h":
double fmod(double x, double y)
somewhere is the source file and then using:
a = fmod(b,c)
In Python, referring to a method of a class inside the class definition, i.e. while the class is being defined, yields a plain function object, but in Cython it yields an unbound method [1]. A consequence of this is that the usual idiom for using the classmethod() and staticmethod() functions, e.g.:
class Spam:
def method(cls):
...
method = classmethod(method)
will not work in Cython. This can be worked around by defining the function outside the class, and then assigning the result of classmethod or staticmethod inside the class, i.e.:
def Spam_method(cls):
...
class Spam:
method = classmethod(Spam_method)
Footnotes
| [1] | The reason for the different behaviour of class scopes is that Cython-defined Python functions are PyCFunction objects, not PyFunction objects, and are not recognised by the machinery that creates a bound or unbound method when a function is extracted from a class. To get around this, Cython wraps each method in an unbound method object itself before storing it in the class’s dictionary. |