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
Gwenaël Samain
cython
Commits
4771d156
Commit
4771d156
authored
May 30, 2014
by
Julien Delafontaine
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
corrections from scoder
parent
e3402907
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
34 additions
and
26 deletions
+34
-26
docs/src/tutorial/pure.rst
docs/src/tutorial/pure.rst
+33
-25
docs/src/userguide/language_basics.rst
docs/src/userguide/language_basics.rst
+1
-1
No files found.
docs/src/tutorial/pure.rst
View file @
4771d156
...
@@ -34,7 +34,7 @@ Thus if one has a file :file:`A.py`::
...
@@ -34,7 +34,7 @@ Thus if one has a file :file:`A.py`::
def myfunction(x, y=2):
def myfunction(x, y=2):
a = x-y
a = x-y
return a
+(x*y)
return a
+ x * y
class A:
class A:
def __init__(self, b=0):
def __init__(self, b=0):
...
@@ -45,8 +45,7 @@ Thus if one has a file :file:`A.py`::
...
@@ -45,8 +45,7 @@ Thus if one has a file :file:`A.py`::
and adds :file:`A.pxd`::
and adds :file:`A.pxd`::
cpdef inline int myfunction(int x,int y):
cpdef inline int myfunction(int x,int y)
cdef int a
cdef class A:
cdef class A:
cdef public int a,b
cdef public int a,b
...
@@ -54,29 +53,28 @@ and adds :file:`A.pxd`::
...
@@ -54,29 +53,28 @@ and adds :file:`A.pxd`::
then at compilation time :file:`A.py` would be interpreted as::
then at compilation time :file:`A.py` would be interpreted as::
cdef int myfunction(int x,int y):
cpdef inline int myfunction(int x,int y):
cdef int a
a = x-y
a = x-y
return a
+(x*y)
return a
+ x * y
cdef class A:
cdef class A:
cdef int a,b
cdef
public
int a,b
def __init__(self,
int
b=0):
def __init__(self, b=0):
self.a = 3
self.a = 3
self.b = b
self.b = b
cdef foo(self, double x):
c
p
def foo(self, double x):
print x + 1.0
print x + 1.0
while still letting the possibility of running the Python interpreter
while still letting the possibility of running the Python interpreter
as before with `python A.py`.
as before with `python A.py`.
Note that in order to provide the Python wrappers to the
:keyword:`cdef`
Note that in order to provide the Python wrappers to the
definitions
definitions
in the :file:`.pxd`,
in the :file:`.pxd`,
* function
s ar
e declared as `cpdef inline`;
* function
definitions must b
e declared as `cpdef inline`;
* classes are declared as `cdef class`;
*
`cdef`
classes are declared as `cdef class`;
*
class attributes are declared as `cdef public`
;
*
`cdef` class attributes must be declared as `cdef public` to be accessible from Python
;
*
class methods are declared as `cpdef`
.
*
`cdef` class methods must be declared as `cpdef` to be accessible from Python
.
Normal Python (:keyword:`def`) functions cannot be declared in
Normal Python (:keyword:`def`) functions cannot be declared in
:file:`.pxd` files, so it is currently impossible to override the types of
:file:`.pxd` files, so it is currently impossible to override the types of
...
@@ -91,9 +89,8 @@ Special decorators are available using the ``cython`` module that can
...
@@ -91,9 +89,8 @@ Special decorators are available using the ``cython`` module that can
be used to add static typing within the Python file, while being ignored
be used to add static typing within the Python file, while being ignored
by the interpreter.
by the interpreter.
Their use is more limited than :file:`.pxd` extensions or usual Cython code,
This option adds the ``cython`` dependency to the original code, but does
and add the ``cython`` dependency to the original code. However they can
not require to maintain a supplementary file.
come in handy and do not require to maintain a supplementary file.
"Compiled" switch
"Compiled" switch
^^^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^^^
...
@@ -102,9 +99,9 @@ come in handy and do not require to maintain a supplementary file.
...
@@ -102,9 +99,9 @@ come in handy and do not require to maintain a supplementary file.
runs, and ``False`` in the interpreter. Thus the code::
runs, and ``False`` in the interpreter. Thus the code::
if cython.compiled:
if cython.compiled:
print
"Yep, I'm compiled."
print
("Yep, I'm compiled.")
else:
else:
print
"Just a lowly interpreted script."
print
("Just a lowly interpreted script.")
will behave differently depending on whether or not the code is loaded as a
will behave differently depending on whether or not the code is loaded as a
compiled :file:`.so` file or a plain :file:`.py` file.
compiled :file:`.so` file or a plain :file:`.py` file.
...
@@ -124,6 +121,14 @@ Static typing
...
@@ -124,6 +121,14 @@ Static typing
cython.declare(x=cython.int, y=cython.double) # cdef int x; cdef double y
cython.declare(x=cython.int, y=cython.double) # cdef int x; cdef double y
It can also be used to type class constructors::
class A:
cython.declare(a=cython.int, b=cython.int)
def __init__(self, b=0):
self.a = 3
self.b = b
* ``@cython.locals`` is a decorator that is used to specify the types of local variables
* ``@cython.locals`` is a decorator that is used to specify the types of local variables
in the function body (including any or all of the argument types)::
in the function body (including any or all of the argument types)::
...
@@ -131,7 +136,8 @@ Static typing
...
@@ -131,7 +136,8 @@ Static typing
def foo(a, b, x, y):
def foo(a, b, x, y):
...
...
It cannot be used to type class constructor attributes.
It cannot be used to type class constructor attributes. See ``cython.declare``
instead to do so.
* ``@cython.returns(<type>)`` specifies the function's return type.
* ``@cython.returns(<type>)`` specifies the function's return type.
...
@@ -150,8 +156,10 @@ Extension types and cdef functions
...
@@ -150,8 +156,10 @@ Extension types and cdef functions
* ``@cython.cclass`` creates a ``cdef class``.
* ``@cython.cclass`` creates a ``cdef class``.
* ``@cython.cfunc`` creates a :keyword:`cdef` function.
* ``@cython.cfunc`` creates a :keyword:`cdef` function.
* ``@cython.ccall`` creates a :keyword:`cpdef` function.
* ``@cython.ccall`` creates a :keyword:`cpdef` function, i.e. one that Cython code
* ``@cython.locals`` declares the argument types (see above).
can call at the C level.
* ``@cython.locals`` declares local variables (see above). It can also be used to
declare types for the local variables that are used in the signature.
* ``@cython.inline`` is the equivalent of the C ``inline`` modifier.
* ``@cython.inline`` is the equivalent of the C ``inline`` modifier.
Here is an example of a :keyword:`cdef` function::
Here is an example of a :keyword:`cdef` function::
...
@@ -162,8 +170,8 @@ Here is an example of a :keyword:`cdef` function::
...
@@ -162,8 +170,8 @@ Here is an example of a :keyword:`cdef` function::
def c_compare(a,b):
def c_compare(a,b):
return a == b
return a == b
Others attribute
s
Further Cython functions and declaration
s
^^^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^^^^^^^^^^
* ``address`` is used in place of the ``&`` operator::
* ``address`` is used in place of the ``&`` operator::
...
...
docs/src/userguide/language_basics.rst
View file @
4771d156
...
@@ -68,7 +68,7 @@ an anonymous :keyword:`enum` declaration for this purpose, for example,::
...
@@ -68,7 +68,7 @@ an anonymous :keyword:`enum` declaration for this purpose, for example,::
Types
Types
-----
-----
There are numerous types built in to the Cython module.
One ha
s all the
There are numerous types built in to the Cython module.
It provide
s all the
standard C types, namely ``char``, ``short``, ``int``, ``long``, ``longlong``
standard C types, namely ``char``, ``short``, ``int``, ``long``, ``longlong``
as well as their unsigned versions ``uchar``, ``ushort``, ``uint``, ``ulong``,
as well as their unsigned versions ``uchar``, ``ushort``, ``uint``, ``ulong``,
``ulonglong``. One also has ``bint`` and ``Py_ssize_t``.
``ulonglong``. One also has ``bint`` and ``Py_ssize_t``.
...
...
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