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
Kirill Smelkov
cython
Commits
0366b382
Commit
0366b382
authored
Jul 06, 2018
by
scoder
Committed by
GitHub
Jul 06, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2437 from gabrieldemarmiesse/test_extension_types_4
Added tests for "Extension types" part 4
parents
55850ac6
28a0408f
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
39 additions
and
11 deletions
+39
-11
docs/examples/userguide/extension_types/my_module.pxd
docs/examples/userguide/extension_types/my_module.pxd
+2
-0
docs/examples/userguide/extension_types/my_module.pyx
docs/examples/userguide/extension_types/my_module.pyx
+11
-0
docs/examples/userguide/extension_types/shrubbery_2.pyx
docs/examples/userguide/extension_types/shrubbery_2.pyx
+8
-0
docs/examples/userguide/extension_types/widen_shrubbery.pyx
docs/examples/userguide/extension_types/widen_shrubbery.pyx
+4
-0
docs/src/userguide/extension_types.rst
docs/src/userguide/extension_types.rst
+13
-11
docs/src/userguide/sharing_declarations.rst
docs/src/userguide/sharing_declarations.rst
+1
-0
No files found.
docs/examples/userguide/extension_types/my_module.pxd
0 → 100644
View file @
0366b382
cdef
class
Shrubbery
:
cdef
int
width
,
height
docs/examples/userguide/extension_types/my_module.pyx
0 → 100644
View file @
0366b382
from
__future__
import
print_function
cdef
class
Shrubbery
:
def
__init__
(
self
,
w
,
h
):
self
.
width
=
w
self
.
height
=
h
def
describe
(
self
):
print
(
"This shrubbery is"
,
self
.
width
,
"by"
,
self
.
height
,
"cubits."
)
docs/examples/userguide/extension_types/shrubbery_2.pyx
0 → 100644
View file @
0366b382
from
my_module
cimport
Shrubbery
cdef
Shrubbery
another_shrubbery
(
Shrubbery
sh1
):
cdef
Shrubbery
sh2
sh2
=
Shrubbery
()
sh2
.
width
=
sh1
.
width
sh2
.
height
=
sh1
.
height
return
sh2
docs/examples/userguide/extension_types/widen_shrubbery.pyx
0 → 100644
View file @
0366b382
from
my_module
cimport
Shrubbery
cdef
widen_shrubbery
(
Shrubbery
sh
,
extra_width
):
sh
.
width
=
sh
.
width
+
extra_width
docs/src/userguide/extension_types.rst
View file @
0366b382
...
...
@@ -105,21 +105,23 @@ will be very inefficient. If the attribute is private, it will not work at all
-- the code will compile, but an attribute error will be raised at run time.
The solution is to declare ``sh`` as being of type :class:`Shrubbery`, as
follows:
:
follows:
cdef widen_shrubbery(Shrubbery sh, extra_width):
sh.width = sh.width + extra_width
.. literalinclude:: ../../examples/userguide/extension_types/widen_shrubbery.pyx
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,::
cdef Shrubbery another_shrubbery(Shrubbery sh1):
cdef Shrubbery sh2
sh2 = Shrubbery()
sh2.width = sh1.width
sh2.height = sh1.height
return sh2
The same consideration applies to local variables, for example:
.. literalinclude:: ../../examples/userguide/extension_types/shrubbery_2.pyx
.. note::
We here ``cimport`` the class :class:`Shrubbery`, and this is necessary
to declare the type at compile time. To be able to ``cimport`` an extension type,
we split the class definition into two parts, one in a definition file and
the other in the corresponding implementation file. You should read
:ref:`sharing_extension_types` to learn to do that.
Type Testing and Casting
...
...
docs/src/userguide/sharing_declarations.rst
View file @
0366b382
...
...
@@ -199,6 +199,7 @@ example:
this object from Python, nor can you use it from Cython using a normal import
statement; you have to use :keyword:`cimport`.
.. _sharing_extension_types:
Sharing Extension Types
=======================
...
...
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