(This is mostly for debugging and experimentation.)
- Use the [Sage]_ notebook which allows Cython code inline.
-Currently, distutils is the most common way Cython files are built and distributed.
+Currently, distutils is the most common way Cython files are built and distributed. The other methods are described in more detail in the :doc:`../reference/compilation` section of the reference manual.
Building a Cython module using distutils
----------------------------------------
start a Python session and do ``from hello import say_hello_to`` and
use the imported function as you see fit.
+
+
.. figure:: sage.png
The Sage notebook allows transparently editing and compiling Cython
There are three file types in cython:
-* Definition files carry a ``.pxd`` suffix
* Implementation files carry a ``.pyx`` suffix
+* Definition files carry a ``.pxd`` suffix
* Include files which carry a ``.pxi`` suffix
+Implementation File
+===================
+
+What can it contain?
+--------------------
+
+* Basically anything Cythonic, but see below.
+
+What can't it contain?
+----------------------
+
+* There are some restrictions when it comes to **extension types**, if the extension type is
+ already defined else where... **more on this later**
+
+
Definition File
===============
* Use the **cimport** statement, as you would Python's import statement, to access these files
from other definition or implementation files.
* **cimport** does not need to be called in ``.pyx`` file for for ``.pxd`` file that has the
- same name. This is automatic.
+ same name, as they are already in the same namespace.
* For cimport to find the stated definition file, the path to the file must be appended to the
``-I`` option of the **cython compile command**.
-Implementation File
-===================
-
-What can it contain?
---------------------
-
-* Basically anything Cythonic, but see below.
-
-What can't it contain?
-----------------------
-
-* There are some restrictions when it comes to **extension types**, if the extension type is
- already defined else where... **more on this later**
-
-
Include File
============
Declaring Data Types
====================
-.. note::
- .. todo:: Remove paragragh
As a dynamic language, Python encourages a programming style of considering classes and objects in terms of their methods and attributes, more than where they fit into the class hierarchy.
-This can make Python a very relaxed and comfortable language for rapid development, but with a price - the ‘red tape’ of managing data types is dumped onto the interpreter. At run time, the interpreter does a lot of work searching namespaces, fetching attributes and parsing argument and keyword tuples. This run-time ‘late binding’ is a major cause of Python’s relative slowness compared to ‘early binding’ languages such as C++.
+This can make Python a very relaxed and comfortable language for rapid development, but with a price - the 'red tape' of managing data types is dumped onto the interpreter. At run time, the interpreter does a lot of work searching namespaces, fetching attributes and parsing argument and keyword tuples. This run-time ‘late binding’ is a major cause of Python’s relative slowness compared to ‘early binding’ languages such as C++.
However with Cython it is possible to gain significant speed-ups through the use of ‘early binding’ programming techniques.
cpdef foo(self, x=*, int k=*)
- * The number of arguments may increase when subclassing, buty the arg types and order must be the same.
+ * The number of arguments may increase when subclassing, but the arg types and order must be the same.
* There may be a slight performance penalty when the optional arg is overridden with one that does not have default values.
Keyword-only Arguments
=======================
-* ``def`` functions can have keyword-only argurments listed after a ``"*"`` parameter and before a ``"**"`` parameter if any::
+* As in Python 3, ``def`` functions can have keyword-only argurments listed after a ``"*"`` parameter and before a ``"**"`` parameter if any::
def f(a, b, *args, c, d = 42, e, **kwds):
...
* For basic numeric and string types, in most situations, when a Python object is used in the context of a C value and vice versa.
-* The following table summarises the conversion possibilities:
+* The following table summarises the conversion possibilities, assuming ``sizeof(int) == sizeof(long)``:
+----------------------------+--------------------+------------------+
| C types | From Python types | To Python types |
...
* The ``break`` and ``continue`` are permissible.
-* Can contain an if-else clause.
+* Can contain an else clause.
=====================
Functions and Methods