Language Basics

Declaring Data Types

As a dynamic language, Python encourages a programming style of consideringclasses and objects in terms of their methods and attributes, more than wherethey fit into the class hierarchy.

This can make Python a very relaxed and comfortable language for rapiddevelopment, but with a price - the ‘red tape’ of managing data types isdumped onto the interpreter. At run time, the interpreter does a lot of worksearching namespaces, fetching attributes and parsing argument and keyword tuples.This run-time ‘late binding’ is a major cause of Python’s relative slownesscompared to ‘early binding’ languages such as C++.

However with Cython it is possible to gain significant speed-ups throughthe use of ‘early binding’ programming techniques.

Note

Typing is not a necessity

Providing static typing to parameters and variables is convenience tospeed up your code, but it is not a necessity. Optimize where and when needed.In fact, typing can slow down your code in the case where thetyping does not allow optimizations but where Cython still needs tocheck that the type of some object matches the declared type.

C variable and type definitions

The cdef statement is used to declare C variables, either local ormodule-level:

  1. cdef int i, j, k
  2. cdef float f, g[42], *h

and C struct, union or enum types:

  1. cdef struct Grail:
  2. int age
  3. float volume
  4.  
  5. cdef union Food:
  6. char *spam
  7. float *eggs
  8.  
  9. cdef enum CheeseType:
  10. cheddar, edam,
  11. camembert
  12.  
  13. cdef enum CheeseState:
  14. hard = 1
  15. soft = 2
  16. runny = 3

See also Styles of struct, union and enum declaration

Note

Structs can be declared as cdef packed struct, which hasthe same effect as the C directive #pragma pack(1).

Declaring an enum as cpdef will create a PEP 435-style Python wrapper:

  1. cpdef enum CheeseState:
  2. hard = 1
  3. soft = 2
  4. runny = 3

There is currently no special syntax for defining a constant, but you can usean anonymous enum declaration for this purpose, for example,:

  1. cdef enum:
  2. tons_of_spam = 3

Note

the words struct, union and enum are used only whendefining a type, not when referring to it. For example, to declare a variablepointing to a Grail you would write:

  1. cdef Grail *gp

and not:

  1. cdef struct Grail *gp # WRONG

There is also a ctypedef statement for giving names to types, e.g.:

  1. ctypedef unsigned long ULong
  2.  
  3. ctypedef int* IntPtr

It is also possible to declare functions with cdef, making them c functions.

  1. cdef int eggs(unsigned long l, float f):
  2. ...

You can read more about them in Python functions vs. C functions.

You can declare classes with cdef, making them Extension Types. Those willhave a behavior very close to python classes, but are faster because they use a structinternally to store attributes.

Here is a simple example:

  1. from __future__ import print_function
  2.  
  3. cdef class Shrubbery:
  4. cdef int width, height
  5.  
  6. def __init__(self, w, h):
  7. self.width = w
  8. self.height = h
  9.  
  10. def describe(self):
  11. print("This shrubbery is", self.width,
  12. "by", self.height, "cubits.")

You can read more about them in Extension Types.

Types

Cython uses the normal C syntax for C types, including pointers. It providesall the standard C types, namely char, short, int, long,long long as well as their unsigned versions, e.g. unsigned int.The special bint type is used for C boolean values (int with 0/non-0values for False/True) and Py_ssize_t for (signed) sizes of Pythoncontainers.

Pointer types are constructed as in C, by appending a to the base typethey point to, e.g. int** for a pointer to a pointer to a C int.Arrays use the normal C array syntax, e.g. int[10], and the size must be knownat compile time for stack allocated arrays. Cython doesn’t support variable length arrays from C99.Note that Cython uses array access for pointer dereferencing, as x is not valid Python syntax,whereas x[0] is.

Also, the Python types list, dict, tuple, etc. may be used forstatic typing, as well as any user defined Extension Types.For example:

  1. cdef list foo = []

This requires an exact match of the class, it does not allowsubclasses. This allows Cython to optimize code by accessinginternals of the builtin class.For this kind of typing, Cython uses internally a C variable of type PyObject*.The Python types int, long, and float are not available for statictyping and instead interpreted as C int, long, and floatrespectively, as statically typing variables with these Pythontypes has zero advantages.

Cython provides an accelerated and typed equivalent of a Python tuple, the ctuple.A ctuple is assembled from any valid C types. For example:

  1. cdef (double, int) bar

They compile down to C-structures and can be used as efficient alternatives toPython tuples.

While these C types can be vastly faster, they have C semantics.Specifically, the integer types overflowand the C float type only has 32 bits of precision(as opposed to the 64-bit C double which Python floats wrapand is typically what one wants).If you want to use these numeric Python types simply omit thetype declaration and let them be objects.

It is also possible to declare Extension Types (declared with cdef class).This does allow subclasses. This typing is mostly used to accesscdef methods and attributes of the extension type.The C code uses a variable which is a pointer to a structure of thespecific type, something like struct MyExtensionTypeObject*.

Grouping multiple C declarations

If you have a series of declarations that all begin with cdef, youcan group them into a cdef block like this:

  1. from __future__ import print_function
  2.  
  3. cdef:
  4. struct Spam:
  5. int tons
  6.  
  7. int i
  8. float a
  9. Spam *p
  10.  
  11. void f(Spam *s):
  12. print(s.tons, "Tons of spam")

Python functions vs. C functions

There are two kinds of function definition in Cython:

Python functions are defined using the def statement, as in Python. They takePython objects as parameters and return Python objects.

C functions are defined using the new cdef statement. They takeeither Python objects or C values as parameters, and can return either Pythonobjects or C values.

Within a Cython module, Python functions and C functions can call each otherfreely, but only Python functions can be called from outside the module byinterpreted Python code. So, any functions that you want to “export” from yourCython module must be declared as Python functions using def.There is also a hybrid function, called cpdef. A cpdefcan be called from anywhere, but uses the faster C calling conventionswhen being called from other Cython code. A cpdef can also be overriddenby a Python method on a subclass or an instance attribute, even when called from Cython.If this happens, most performance gains are of course lost and even if it does not,there is a tiny overhead in calling a cpdef method from Cython compared tocalling a cdef method.

Parameters of either type of function can be declared to have C data types,using normal C declaration syntax. For example,:

  1. def spam(int i, char *s):
  2. ...
  3.  
  4. cdef int eggs(unsigned long l, float f):
  5. ...

ctuples may also be used:

  1. cdef (int, float) chips((long, long, double) t):
  2. ...

When a parameter of a Python function is declared to have a C data type, it ispassed in as a Python object and automatically converted to a C value, ifpossible. In other words, the definition of spam above is equivalent towriting:

  1. def spam(python_i, python_s):
  2. cdef int i = python_i
  3. cdef char* s = python_s
  4. ...

Automatic conversion is currently only possible for numeric types,string types and structs (composed recursively of any of these types);attempting to use any other type for the parameter of aPython function will result in a compile-time error.Care must be taken with strings to ensure a reference if the pointer is to be usedafter the call. Structs can be obtained from Python mappings, and again care must be takenwith string attributes if they are to be used after the function returns.

C functions, on the other hand, can have parameters of any type, since they’repassed in directly using a normal C function call.

Functions declared using cdef with Python object return type, like Python functions, will return a Nonevalue when execution leaves the function body without an explicit return value. This is incontrast to C/C++, which leaves the return value undefined.In the case of non-Python object return types, the equivalent of zero is returned, for example, 0 for int, False for bint and NULL for pointer types.

A more complete comparison of the pros and cons of these different methodtypes can be found at Early Binding for Speed.

Python objects as parameters and return values

If no type is specified for a parameter or return value, it is assumed to be aPython object. (Note that this is different from the C convention, where itwould default to int.) For example, the following defines a C function thattakes two Python objects as parameters and returns a Python object:

  1. cdef spamobjs(x, y):
  2. ...

Reference counting for these objects is performed automatically according tothe standard Python/C API rules (i.e. borrowed references are taken asparameters and a new reference is returned).



Warning


This only applies to Cython code. Other Python packages which
are implemented in C like NumPy may not follow these conventions.



The name object can also be used to explicitly declare something as a Pythonobject. This can be useful if the name being declared would otherwise be takenas the name of a type, for example,:

  1. cdef ftang(object int):
  2. ...

declares a parameter called int which is a Python object. You can also useobject as the explicit return type of a function, e.g.:

  1. cdef object ftang(object int):
  2. ...

In the interests of clarity, it is probably a good idea to always be explicitabout object parameters in C functions.

To create a borrowed reference, specify the parameter type as PyObject*.Cython won’t perform automatic Py_INCREF, or Py_DECREF, e.g.:

will display:

  1. Initial refcount: 2
  2. Inside owned_reference: 3
  3. Inside borrowed_reference: 2

Optional Arguments

Unlike C, it is possible to use optional arguments in cdef and cpdef functions.There are differences though whether you declare them in a .pyxfile or the corresponding .pxd file.

To avoid repetition (and potential future inconsistencies), default argument values arenot visible in the declaration (in .pxd files) but only inthe implementation (in .pyx files).

When in a .pyx file, the signature is the same as it is in Python itself:

  1. from __future__ import print_function
  2.  
  3. cdef class A:
  4. cdef foo(self):
  5. print("A")
  6.  
  7. cdef class B(A):
  8. cdef foo(self, x=None):
  9. print("B", x)
  10.  
  11. cdef class C(B):
  12. cpdef foo(self, x=True, int k=3):
  13. print("C", x, k)

When in a .pxd file, the signature is different like this example: cdef foo(x=*).This is because the program calling the function just needs to know what signatures arepossible in C, but doesn’t need to know the value of the default arguments.:

  1. cdef class A:
  2. cdef foo(self)
  3.  
  4. cdef class B(A):
  5. cdef foo(self, x=*)
  6.  
  7. cdef class C(B):
  8. cpdef foo(self, x=*, int k=*)

Note

The number of arguments may increase when subclassing,but the arg types and order must be the same, as shown in the example above.

There may be a slight performance penalty when the optional arg is overriddenwith one that does not have default values.

Keyword-only Arguments

As in Python 3, def functions can have keyword-only argumentslisted after a "" parameter and before a "*" parameter if any:

  1. def f(a, b, *args, c, d = 42, e, **kwds):
  2. ...
  3.  
  4.  
  5. # We cannot call f with less verbosity than this.
  6. foo = f(4, "bar", c=68, e=1.0)

As shown above, the c, d and e arguments can not bepassed as positional arguments and must be passed as keyword arguments.Furthermore, c and e are required keyword argumentssince they do not have a default value.

A single "*" without argument name can be used toterminate the list of positional arguments:

  1. def g(a, b, *, c, d):
  2. ...
  3.  
  4. # We cannot call g with less verbosity than this.
  5. foo = g(4.0, "something", c=68, d="other")

Shown above, the signature takes exactly two positionalparameters and has two required keyword parameters.

Function Pointers

Functions declared in a struct are automatically converted to function pointers.

For using error return values with function pointers, see the note at the bottomof Error return values.

Error return values

If you don’t do anything special, a function declared with cdef thatdoes not return a Python object has no way of reporting Python exceptions toits caller. If an exception is detected in such a function, a warning messageis printed and the exception is ignored.

If you want a C function that does not return a Python object to be able topropagate exceptions to its caller, you need to declare an exception value forit. Here is an example:

  1. cdef int spam() except -1:
  2. ...

With this declaration, whenever an exception occurs inside spam, it willimmediately return with the value -1. Furthermore, whenever a call to spamreturns -1, an exception will be assumed to have occurred and will bepropagated.

When you declare an exception value for a function, you should neverexplicitly or implicitly return that value. In particular, if the exceptional return valueis a False value, then you should ensure the function will never terminate via an implicitor empty return.

If all possible return values are legal and youcan’t reserve one entirely for signalling errors, you can use an alternativeform of exception value declaration:

  1. cdef int spam() except? -1:
  2. ...

The “?” indicates that the value -1 only indicates a possible error. In thiscase, Cython generates a call to PyErr_Occurred() if the exception value isreturned, to make sure it really is an error.

There is also a third form of exception value declaration:

  1. cdef int spam() except *:
  2. ...

This form causes Cython to generate a call to PyErr_Occurred() afterevery call to spam, regardless of what value it returns. If you have afunction returning void that needs to propagate errors, you will have to usethis form, since there isn’t any return value to test.Otherwise there is little use for this form.

An external C++ function that may raise an exception can be declared with:

  1. cdef int spam() except +

See Using C++ in Cython for more details.

Some things to note:

  • Exception values can only declared for functions returning an integer, enum,float or pointer type, and the value must be a constant expression.Void functions can only use the except * form.

  • The exception value specification is part of the signature of the function.If you’re passing a pointer to a function as a parameter or assigning itto a variable, the declared type of the parameter or variable must havethe same exception value specification (or lack thereof). Here is anexample of a pointer-to-function declaration with an exceptionvalue:

  1. int (*grail)(int, char*) except -1
  • You don’t need to (and shouldn’t) declare exception values for functionswhich return Python objects. Remember that a function with no declaredreturn type implicitly returns a Python object. (Exceptions on such functionsare implicitly propagated by returning NULL.)

Checking return values of non-Cython functions

It’s important to understand that the except clause does not cause an error tobe raised when the specified value is returned. For example, you can’t writesomething like:

  1. cdef extern FILE *fopen(char *filename, char *mode) except NULL # WRONG!

and expect an exception to be automatically raised if a call to fopen()returns NULL. The except clause doesn’t work that way; its only purpose isfor propagating Python exceptions that have already been raised, either by a Cythonfunction or a C function that calls Python/C API routines. To get an exceptionfrom a non-Python-aware function such as fopen(), you will have to check thereturn value and raise it yourself, for example:

  1. from libc.stdio cimport FILE, fopen
  2. from libc.stdlib cimport malloc, free
  3. from cpython.exc cimport PyErr_SetFromErrnoWithFilenameObject
  4.  
  5. def open_file():
  6. cdef FILE* p
  7. p = fopen("spam.txt", "r")
  8. if p is NULL:
  9. PyErr_SetFromErrnoWithFilenameObject(OSError, "spam.txt")
  10. ...
  11.  
  12.  
  13. def allocating_memory(number=10):
  14. cdef double *my_array = <double *> malloc(number * sizeof(double))
  15. if not my_array: # same as 'is NULL' above
  16. raise MemoryError()
  17. ...
  18. free(my_array)

Overriding in extension types

cpdef methods can override cdef methods:

  1. from __future__ import print_function
  2.  
  3. cdef class A:
  4. cdef foo(self):
  5. print("A")
  6.  
  7. cdef class B(A):
  8. cdef foo(self, x=None):
  9. print("B", x)
  10.  
  11. cdef class C(B):
  12. cpdef foo(self, x=True, int k=3):
  13. print("C", x, k)

When subclassing an extension type with a Python class,def methods can override cpdef methods but not cdefmethods:

  1. from __future__ import print_function
  2.  
  3. cdef class A:
  4. cdef foo(self):
  5. print("A")
  6.  
  7. cdef class B(A):
  8. cpdef foo(self):
  9. print("B")
  10.  
  11. class C(B): # NOTE: not cdef class
  12. def foo(self):
  13. print("C")

If C above would be an extension type (cdef class),this would not work correctly.The Cython compiler will give a warning in that case.

Automatic type conversions

In most situations, automatic conversions will be performed for the basicnumeric and string types when a Python object is used in a context requiring aC value, or vice versa. The following table summarises the conversionpossibilities.

C typesFrom Python typesTo Python types
[unsigned] char,[unsigned] short,int, longint, longint
unsigned int,unsigned long,[unsigned] long longint, longlong
float, double, long doubleint, long, floatfloat
char*str/bytesstr/bytes [3]
C arrayiterablelist [5]
struct,union dict [4]
[3]The conversion is to/from str for Python 2.x, and bytes for Python 3.x.
[4]The conversion from a C union type to a Python dict will adda value for each of the union fields. Cython 0.23 and later, however,will refuse to automatically convert a union with unsafe typecombinations. An example is a union of an int and a char*,in which case the pointer value may or may not be a valid pointer.
[5]Other than signed/unsigned char[].The conversion will fail if the length of C array is not known at compile time,and when using a slice of a C array.

Caveats when using a Python string in a C context

You need to be careful when using a Python string in a context expecting achar*. In this situation, a pointer to the contents of the Python string isused, which is only valid as long as the Python string exists. So you need tomake sure that a reference to the original Python string is held for as longas the C string is needed. If you can’t guarantee that the Python string willlive long enough, you will need to copy the C string.

Cython detects and prevents some mistakes of this kind. For instance, if youattempt something like:

  1. cdef char *s
  2. s = pystring1 + pystring2

then Cython will produce the error message Obtaining char* from temporary
Python value
. The reason is that concatenating the two Python stringsproduces a new Python string object that is referenced only by a temporaryinternal variable that Cython generates. As soon as the statement has finished,the temporary variable will be decrefed and the Python string deallocated,leaving s dangling. Since this code could not possibly work, Cython refuses tocompile it.

The solution is to assign the result of the concatenation to a Pythonvariable, and then obtain the char* from that, i.e.:

  1. cdef char *s
  2. p = pystring1 + pystring2
  3. s = p

It is then your responsibility to hold the reference p for as long asnecessary.

Keep in mind that the rules used to detect such errors are only heuristics.Sometimes Cython will complain unnecessarily, and sometimes it will fail todetect a problem that exists. Ultimately, you need to understand the issue andbe careful what you do.

Type Casting

Where C uses "(" and ")", Cython uses "<" and ">". For example:

  1. cdef char *p
  2. cdef float *q
  3. p = <char*>q

When casting a C value to a Python object type or vice versa,Cython will attempt a coercion. Simple examples are casts like <int>pyobj,which converts a Python number to a plain C int value, or <bytes>charptr,which copies a C char* string into a new Python bytes object.



Note


Cython will not prevent a redundant cast, but emits a warning for it.



To get the address of some Python object, use a cast to a pointer typelike <void> or <PyObject>.You can also cast a C pointer back to a Python object referencewith <object>, or a more specific builtin or extension type(e.g. <MyExtType>ptr). This will increase the reference count ofthe object by one, i.e. the cast returns an owned reference.Here is an example:

  1. from cpython.ref cimport PyObject
  2.  
  3. cdef extern from *:
  4. ctypedef Py_ssize_t Py_intptr_t
  5.  
  6. python_string = "foo"
  7.  
  8. cdef void* ptr = <void*>python_string
  9. cdef Py_intptr_t adress_in_c = <Py_intptr_t>ptr
  10. address_from_void = adress_in_c # address_from_void is a python int
  11.  
  12. cdef PyObject* ptr2 = <PyObject*>python_string
  13. cdef Py_intptr_t address_in_c2 = <Py_intptr_t>ptr2
  14. address_from_PyObject = address_in_c2 # address_from_PyObject is a python int
  15.  
  16. assert address_from_void == address_from_PyObject == id(python_string)
  17.  
  18. print(<object>ptr) # Prints "foo"
  19. print(<object>ptr2) # prints "foo"

The precedence of <…> is such that <type>a.b.c is interpreted as <type>(a.b.c).

Casting to <object> creates an owned reference. Cython will automaticallyperform a Py_INCREF and Py_DECREF operation. Casting to<PyObject *> creates a borrowed reference, leaving the refcount unchanged.

Checked Type Casts

A cast like <MyExtensionType>x will cast x to the classMyExtensionType without any checking at all.

To have a cast checked, use the syntax like: <MyExtensionType?>x.In this case, Cython will apply a runtime check that raises a TypeErrorif x is not an instance of MyExtensionType.This tests for the exact class for builtin types,but allows subclasses for Extension Types.

Statements and expressions

Control structures and expressions follow Python syntax for the most part.When applied to Python objects, they have the same semantics as in Python(unless otherwise noted). Most of the Python operators can also be applied toC values, with the obvious semantics.

If Python objects and C values are mixed in an expression, conversions areperformed automatically between Python objects and C numeric or string types.

Reference counts are maintained automatically for all Python objects, and allPython operations are automatically checked for errors, with appropriateaction taken.

Differences between C and Cython expressions

There are some differences in syntax and semantics between C expressions andCython expressions, particularly in the area of C constructs which have nodirect equivalent in Python.

  • An integer literal is treated as a C constant, and willbe truncated to whatever size your C compiler thinks appropriate.To get a Python integer (of arbitrary precision) cast immediately toan object (e.g. <object>100000000000000000000). The L, LL,and U suffixes have the same meaning as in C.

  • There is no -> operator in Cython. Instead of p->x, use p.x

  • There is no unary operator in Cython. Instead of p, use p[0]

  • There is an & operator, with the same semantics as in C.

  • The null C pointer is called NULL, not 0 (and NULL is a reserved word).

  • Type casts are written <type>value , for example,:

  1. cdef char* p, float* q
  2. p = <char*>q

Scope rules

Cython determines whether a variable belongs to a local scope, the modulescope, or the built-in scope completely statically. As with Python, assigningto a variable which is not otherwise declared implicitly declares it to be avariable residing in the scope where it is assigned. The type of the variabledepends on type inference, except for the global module scope, where it isalways a Python object.

Built-in Functions

Cython compiles calls to most built-in functions into direct calls tothe corresponding Python/C API routines, making them particularly fast.

Only direct function calls using these names are optimised. If you dosomething else with one of these names that assumes it’s a Python object,such as assign it to a Python variable, and later call it, the call willbe made as a Python function call.

Function and argumentsReturn typePython/C API Equivalent
abs(obj)object,double, …PyNumber_Absolute, fabs,fabsf, …
callable(obj)bintPyObject_Callable
delattr(obj, name)NonePyObject_DelAttr
exec(code, [glob, [loc]])object

-
dir(obj)listPyObject_Dir
divmod(a, b)tuplePyNumber_Divmod
getattr(obj, name, [default])(Note 1)objectPyObject_GetAttr
hasattr(obj, name)bintPyObject_HasAttr
hash(obj)int / longPyObject_Hash
intern(obj)objectPy*_InternFromString
isinstance(obj, type)bintPyObject_IsInstance
issubclass(obj, type)bintPyObject_IsSubclass
iter(obj, [sentinel])objectPyObject_GetIter
len(obj)Py_ssize_tPyObject_Length
pow(x, y, [z])objectPyNumber_Power
reload(obj)objectPyImport_ReloadModule
repr(obj)objectPyObject_Repr
setattr(obj, name)voidPyObject_SetAttr

Note 1: Pyrex originally provided a function getattr3(obj, name, default)()corresponding to the three-argument form of the Python builtin getattr().Cython still supports this function, but the usage is deprecated in favour ofthe normal builtin, which Cython can optimise in both forms.

Operator Precedence

Keep in mind that there are some differences in operator precedence betweenPython and C, and that Cython uses the Python precedences, not the C ones.

Integer for-loops

Cython recognises the usual Python for-in-range integer loop pattern:

  1. for i in range(n):
  2. ...

If i is declared as a cdef integer type, it willoptimise this into a pure C loop. This restriction is required asotherwise the generated code wouldn’t be correct due to potentialinteger overflows on the target architecture. If you are worried thatthe loop is not being converted correctly, use the annotate feature ofthe cython commandline (-a) to easily see the generated C code.See Automatic range conversion

For backwards compatibility to Pyrex, Cython also supports a more verboseform of for-loop which you might find in legacy code:

  1. for i from 0 <= i < n:
  2. ...

or:

  1. for i from 0 <= i < n by s:
  2. ...

where s is some integer step size.

Note

This syntax is deprecated and should not be used in new code.Use the normal Python for-loop instead.

Some things to note about the for-from loop:

  • The target expression must be a plain variable name.
  • The name between the lower and upper bounds must be the same as the targetname.
  • The direction of iteration is determined by the relations. If they are bothfrom the set {<, <=} then it is upwards; if they are both from the set{>, >=} then it is downwards. (Any other combination is disallowed.)

Like other Python looping statements, break and continue may be used in thebody, and the loop may have an else clause.

Cython file types

There are three file types in Cython:

  • The implementation files, carrying a .py or .pyx suffix.
  • The definition files, carrying a .pxd suffix.
  • The include files, carrying a .pxi suffix.

The implementation file

The implementation file, as the name suggest, contains the implementationof your functions, classes, extension types, etc. Nearly all thepython syntax is supported in this file. Most of the time, a .pyfile can be renamed into a .pyx file without changingany code, and Cython will retain the python behavior.

It is possible for Cython to compile both .py and .pyx files.The name of the file isn’t important if one wants to use only the Python syntax,and Cython won’t change the generated code depending on the suffix used.Though, if one want to use the Cython syntax, using a .pyx file is necessary.

In addition to the Python syntax, the user can alsoleverage Cython syntax (such as cdef) to use C variables, candeclare functions as cdef or cpdef and can import C definitionswith cimport. Many other Cython features usable in implementation filescan be found throughout this page and the rest of the Cython documentation.

There are some restrictions on the implementation part of some Extension Typesif the corresponding definition file also defines that type.

Note

When a .pyx file is compiled, Cython first checks to see if a corresponding.pxd file exists and processes it first. It acts like a header file fora Cython .pyx file. You can put inside functions that will be used byother Cython modules. This allows different Cython modules to use functionsand classes from each other without the Python overhead. To read more aboutwhat how to do that, you can see pxd files.

The definition file

A definition file is used to declare various things.

Any C declaration can be made, and it can be also a declaration of a C variable orfunction implemented in a C/C++ file. This can be done with cdef extern from.Sometimes, .pxd files are used as a translation of C/C++ header filesinto a syntax that Cython can understand. This allows then the C/C++ variable andfunctions to be used directly in implementation files with cimport.You can read more about it in Interfacing with External C Code and Using C++ in Cython.

It can also contain the definition part of an extension type and the declarationsof functions for an external library.

It cannot contain the implementations of any C or Python functions, or anyPython class definitions, or any executable statements. It is needed when onewants to access cdef attributes and methods, or to inherit fromcdef classes defined in this module.

Note

You don’t need to (and shouldn’t) declare anything in a declaration filepublic in order to make it available to other Cython modules; its merepresence in a definition file does that. You only need a publicdeclaration if you want to make something available to external C code.

The include statement and include files

Warning

Historically the include statement was used for sharing declarations.Use Sharing Declarations Between Cython Modules instead.

A Cython source file can include material from other files using the includestatement, for example,:

  1. include "spamstuff.pxi"

The contents of the named file are textually included at that point. Theincluded file can contain any complete statements or declarations that arevalid in the context where the include statement appears, including otherinclude statements. The contents of the included file should begin at anindentation level of zero, and will be treated as though they were indented tothe level of the include statement that is including the file. The includestatement cannot, however, be used outside of the module scope, such as insideof functions or class bodies.

Note

There are other mechanisms available for splitting Cython code intoseparate parts that may be more appropriate in many cases. SeeSharing Declarations Between Cython Modules.

Conditional Compilation

Some features are available for conditional compilation and compile-timeconstants within a Cython source file.

Compile-Time Definitions

A compile-time constant can be defined using the DEF statement:

  1. DEF FavouriteFood = u"spam"
  2. DEF ArraySize = 42
  3. DEF OtherArraySize = 2 * ArraySize + 17

The right-hand side of the DEF must be a valid compile-time expression.Such expressions are made up of literal values and names defined using DEFstatements, combined using any of the Python expression syntax.

The following compile-time names are predefined, corresponding to the valuesreturned by os.uname().


UNAME_SYSNAME, UNAME_NODENAME, UNAME_RELEASE,
UNAME_VERSION, UNAME_MACHINE

The following selection of builtin constants and functions are also available:


None, True, False,
abs, all, any, ascii, bin, bool, bytearray, bytes, chr, cmp, complex, dict,
divmod, enumerate, filter, float, format, frozenset, hash, hex, int, len,
list, long, map, max, min, oct, ord, pow, range, reduce, repr, reversed,
round, set, slice, sorted, str, sum, tuple, xrange, zip

Note that some of these builtins may not be available when compiling underPython 2.x or 3.x, or may behave differently in both.

A name defined using DEF can be used anywhere an identifier can appear,and it is replaced with its compile-time value as though it were written intothe source at that point as a literal. For this to work, the compile-timeexpression must evaluate to a Python value of type int, long,float, bytes or unicode (str in Py3).

  1. from __future__ import print_function
  2.  
  3. DEF FavouriteFood = u"spam"
  4. DEF ArraySize = 42
  5. DEF OtherArraySize = 2 * ArraySize + 17
  6.  
  7. cdef int a1[ArraySize]
  8. cdef int a2[OtherArraySize]
  9. print("I like", FavouriteFood)

Conditional Statements

The IF statement can be used to conditionally include or exclude sectionsof code at compile time. It works in a similar way to the #if preprocessordirective in C.:

  1. IF UNAME_SYSNAME == "Windows":
  2. include "icky_definitions.pxi"
  3. ELIF UNAME_SYSNAME == "Darwin":
  4. include "nice_definitions.pxi"
  5. ELIF UNAME_SYSNAME == "Linux":
  6. include "penguin_definitions.pxi"
  7. ELSE:
  8. include "other_definitions.pxi"

The ELIF and ELSE clauses are optional. An IF statement can appearanywhere that a normal statement or declaration can appear, and it can containany statements or declarations that would be valid in that context, includingDEF statements and other IF statements.

The expressions in the IF and ELIF clauses must be valid compile-timeexpressions as for the DEF statement, although they can evaluate to anyPython value, and the truth of the result is determined in the usual Pythonway.