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
Boxiang Sun
cython
Commits
269963f8
Commit
269963f8
authored
Apr 10, 2013
by
nbruin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
explain difference between cdef and cpdef
parent
e8bd1789
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
9 additions
and
4 deletions
+9
-4
docs/src/tutorial/cdef_classes.rst
docs/src/tutorial/cdef_classes.rst
+9
-4
No files found.
docs/src/tutorial/cdef_classes.rst
View file @
269963f8
...
...
@@ -32,20 +32,25 @@ Cython code and pure Python code.
So far our integration example has not been very useful as it only
integrates a single hard-coded function. In order to remedy this,
with
out
sacrificing speed, we will use a cdef class to represent a
with
hardly
sacrificing speed, we will use a cdef class to represent a
function on floating point numbers::
cdef class Function:
cpdef double evaluate(self, double x) except *:
return 0
Like before,
cpdef makes two versions of the method available; one
The directive
cpdef makes two versions of the method available; one
fast for use from Cython and one slower for use from Python. Then::
cdef class SinOfSquareFunction(Function):
cpdef double evaluate(self, double x) except *:
return sin(x**2)
This does slightly more than providing a python wrapper for a cdef
method: unlike a cdef method, a cpdef method is fully overrideable by
subclasses and instance attributes. This adds a little calling overhead
compared to a cdef method.
Using this, we can now change our integration example::
def integrate(Function f, double a, double b, int N):
...
...
@@ -62,8 +67,8 @@ Using this, we can now change our integration example::
print(integrate(SinOfSquareFunction(), 0, 1, 10000))
This is almost as fast as the previous code, however it is much more flexible
as the function to integrate can be changed.
It is even possible to pass
in a new
function defined in Python-space::
as the function to integrate can be changed.
We can even pass in a new
function defined in Python-space::
>>> import integrate
>>> class MyPolynomial(integrate.Function):
...
...
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