By default, extension type attributes are only accessible by direct access,
not Python access, which means that they are not accessible from Python code.
To make them accessible from Python code, you need to declare them as
-:keyword:`public` or :keyword:`readonly`. For example,::
+:keyword:`public` or :keyword:`readonly`. For example::
cdef class Shrubbery:
cdef public int width, height
parameter of the methods of that type, but in other cases you will have to use
a type declaration.
-For example, in the following function,::
+For example, in the following function::
cdef widen_shrubbery(sh, extra_width): # BAD
sh.width = sh.width + extra_width
Now the Cython compiler knows that ``sh`` has a C attribute called
:attr:`width` and will generate code to access it directly and efficiently.
-The same consideration applies to local variables, for example,::
+The same consideration applies to local variables, for example::
cdef Shrubbery another_shrubbery(Shrubbery sh1):
cdef Shrubbery sh2
If you are forward-declaring an extension type that has a base class, you must
specify the base class in both the forward declaration and its subsequent
-definition, for example,::
+definition, for example::
cdef class A(B)
By default, extension types do not support having weak references made to
them. You can enable weak referencing by declaring a C attribute of type
-object called :attr:`__weakref__`. For example,::
+object called :attr:`__weakref__`. For example::
cdef class ExplodingAnimal:
"""This animal will self-destruct when it is
------------------
Cython requires you to include a module name in an extern extension class
-declaration, for example,::
+declaration, for example::
cdef extern class MyModule.Spam:
...
statement will be executed at module load time.
The module name can be a dotted name to refer to a module inside a package
-hierarchy, for example,::
+hierarchy, for example::
cdef extern class My.Nested.Package.Spam:
...
You can also specify an alternative name under which to import the type using
-an as clause, for example,::
+an as clause, for example::
cdef extern class My.Nested.Package.Spam as Yummy:
...
to platform-dependent flavours of numeric types, you will need a
corresponding :keyword:`ctypedef` statement, but you don't need to match
the type exactly, just use something of the right general kind (int, float,
- etc). For example,::
+ etc). For example::
ctypedef int word
---------------------------------
One particular use of the ``cdef extern from`` statement is for gaining access to
-routines in the Python/C API. For example,::
+routines in the Python/C API. For example::
cdef extern from "Python.h":
----------------------------
The ``__stdcall`` and ``__cdecl`` calling convention specifiers can be used in
-Cython, with the same syntax as used by C compilers on Windows, for example,::
+Cython, with the same syntax as used by C compilers on Windows, for example::
cdef extern int __stdcall FrobnicateWindow(long handle)
print, you can rename it to something else in your Cython module.
As well as functions, C names can be specified for variables, structs, unions,
-enums, struct and union members, and enum values. For example,::
+enums, struct and union members, and enum values. For example::
cdef extern int one "ein", two "zwei"
cdef extern float three "drei"
You can declare a whole group of items as :keyword:`public` and/or
:keyword:`api` all at once by enclosing them in a :keyword:`cdef` block, for
-example,::
+example::
cdef public api:
void order_spam(int tons)
See also :ref:`struct-union-enum-styles`
There is currently no special syntax for defining a constant, but you can use
-an anonymous :keyword:`enum` declaration for this purpose, for example,::
+an anonymous :keyword:`enum` declaration for this purpose, for example::
cdef enum:
tons_of_spam = 3
when being called from other Cython code.
Parameters of either type of function can be declared to have C data types,
-using normal C declaration syntax. For example,::
+using normal C declaration syntax. For example::
def spam(int i, char *s):
...
The name object can also be used to explicitly declare something as a Python
object. This can be useful if the name being declared would otherwise be taken
-as the name of a type, for example,::
+as the name of a type, for example::
cdef ftang(object int):
...
for propagating Python exceptions that have already been raised, either by a Cython
function or a C function that calls Python/C API routines. To get an exception
from a non-Python-aware function such as :func:`fopen`, you will have to check the
-return value and raise it yourself, for example,::
+return value and raise it yourself, for example::
cdef FILE *p
p = fopen("spam.txt", "r")
C functions defined at the top level of a module can be made available via
:keyword:`cimport` by putting headers for them in the ``.pxd`` file, for
-example,:
+example::
:file:`volume.pxd`::