Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Xavier Thompson
cython
Commits
35ecaccb
Commit
35ecaccb
authored
Apr 08, 2020
by
will-ca
Committed by
GitHub
Apr 09, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Mention __init__.pxd in tutorial documentation. (GH-3439)
parent
b5b314cf
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
39 additions
and
9 deletions
+39
-9
docs/src/tutorial/pxd_files.rst
docs/src/tutorial/pxd_files.rst
+39
-9
No files found.
docs/src/tutorial/pxd_files.rst
View file @
35ecaccb
...
...
@@ -11,27 +11,25 @@ using the ``cimport`` keyword.
``pxd`` files have many use-cases:
1. They can be used for sharing external C declarations.
2. They can contain functions which are well suited for inlining by
the C compiler. Such functions should be marked ``inline``, example:
::
1. They can be used for sharing external C declarations.
2. They can contain functions which are well suited for inlining by
the C compiler. Such functions should be marked ``inline``, example::
cdef inline int int_min(int a, int b):
return b if b < a else a
3.
When accompanying an equally named ``pyx`` file, they
3.
When accompanying an equally named ``pyx`` file, they
provide a Cython interface to the Cython module so that other
Cython modules can communicate with it using a more efficient
protocol than the Python one.
In our integration example, we might break it up into ``pxd`` files like this:
1.
Add a ``cmath.pxd`` function which defines the C functions available from
1.
Add a ``cmath.pxd`` function which defines the C functions available from
the C ``math.h`` header file, like ``sin``. Then one would simply do
``from cmath cimport sin`` in ``integrate.pyx``.
2. Add a ``integrate.pxd`` so that other modules written in Cython
can define fast custom functions to integrate.
::
2. Add a ``integrate.pxd`` so that other modules written in Cython
can define fast custom functions to integrate::
cdef class Function:
cpdef evaluate(self, double x)
...
...
@@ -41,3 +39,35 @@ In our integration example, we might break it up into ``pxd`` files like this:
Note that if you have a cdef class with attributes, the attributes must
be declared in the class declaration ``pxd`` file (if you use one), not
the ``pyx`` file. The compiler will tell you about this.
__init__.pxd
^^^^^^^^^^^^
Cython also supports ``__init__.pxd`` files for declarations in package's
namespaces, similar to ``__init__.py`` files in Python.
Continuing the integration example, we could package the module as follows:
1. Place the module files in a directory tree as one usually would for
Python::
CyIntegration/
├── __init__.pyx
├── __init__.pxd
├── integrate.pyx
└── integrate.pxd
2. In ``__init__.pxd``, use ``cimport`` for any declarations that one
would want to be available from the package's main namespace::
from CyIntegration cimport integrate
Other modules would then be able to use ``cimport`` on the package in
order to recursively gain faster, Cython access to the entire package
and the data declared in its modules::
cimport CyIntegration
cpdef do_integration(CyIntegration.integrate.Function f):
return CyIntegration.integrate.integrate(f, 0., 2., 1)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment