I think having paragraphs like this should be somewhere else which we can link to from here
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.
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.
...
@@ -111,6 +110,10 @@ This can make Python a very relaxed and comfortable language for rapid developme
...
@@ -111,6 +110,10 @@ This can make Python a very relaxed and comfortable language for rapid developme
However with Cython it is possible to gain significant speed-ups through the use of ‘early binding’ programming techniques.
However with Cython it is possible to gain significant speed-ups through the use of ‘early binding’ programming techniques.
.. note:: Typing is not a necessity
Providing static typing to parameters and variables is convenience to speed up your code, but it is not a necessity. Optimize where and when needed.
The cdef Statement
The cdef Statement
==================
==================
...
@@ -168,7 +171,8 @@ The ``cdef`` statement is used to make C level declarations for:
...
@@ -168,7 +171,8 @@ The ``cdef`` statement is used to make C level declarations for:
...
...
.. note::
.. note:: Constants
Constants can be defined by using an anonymous enum::
Constants can be defined by using an anonymous enum::
cdef enum:
cdef enum:
...
@@ -191,6 +195,16 @@ A series of declarations can grouped into a ``cdef`` block::
...
@@ -191,6 +195,16 @@ A series of declarations can grouped into a ``cdef`` block::
void f(Spam *s):
void f(Spam *s):
print s.tons, "Tons of spam"
print s.tons, "Tons of spam"
.. note:: ctypedef statement
The ``ctypedef`` statement is provided for naming types::
ctypedef unsigned long ULong
ctypedef int *IntPtr
Parameters
Parameters
==========
==========
...
@@ -214,7 +228,8 @@ Parameters
...
@@ -214,7 +228,8 @@ Parameters
cdef spamobjs(x, y):
cdef spamobjs(x, y):
...
...
.. note::
.. note:: --
This is different then C language behavior, where it is an int by default.
This is different then C language behavior, where it is an int by default.
...
@@ -447,19 +462,82 @@ Scope Rules
...
@@ -447,19 +462,82 @@ Scope Rules
True = 1
True = 1
Built-in Constants
==================
Pre-defined Python built-in constants:
* None
* True
* False
Operator Precedence
===================
* Cython uses Python precedence order, not C
For-loops
==========
* ``range()`` is C optimized when the index value has been declared by ``cdef``::
cdef i
for i in range(n):
...
* The other form available in C is the for-from style
* The target expression must be a variable name.
* The name between the lower and upper bounds must be the same as the target name.
for i from 0 <= i < n:
...
* Or when using a step size::
for i from 0 <= i < n by s:
...
* To reverse the direction, reverse the conditional operation::
for i from 0 >= i > n:
...
* The ``break`` and ``continue`` are permissible.
* Can contain an if-else clause.
=====================
=====================
Functions and Methods
Functions and Methods
=====================
=====================
* There are three types of function declarations in Cython as the sub-sections show below.
* Only "Python" functions can be called outside a Cython module from *Python interpretted code*.
*
Callable from Python
Callable from Python
=====================
=====================
* Are decalared with the ``def`` statement
* Are called with Python objects
* Return Python objects
* See **Parameters** for special consideration
Callable from C
Callable from C
================
================
* Are declared with the ``cdef`` statement.
* Are called with either Python objects or C values.
* Can return either Python objects or C values.
Callable from both Python and C
Callable from both Python and C
================================
================================
* Are declared with the ``cpdef`` statement.
* Can be called from anywhere, because it uses a little Cython magic.
* Uses the faster C calling conventions when being called from other Cython code.
Overriding
Overriding
==========
==========
...
@@ -482,6 +560,51 @@ Function Pointers
...
@@ -482,6 +560,51 @@ Function Pointers
* Functions declared in a ``struct`` are automatically converted to function pointers.
* Functions declared in a ``struct`` are automatically converted to function pointers.