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
Labels
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
nexedi
cython
Commits
2c2d352c
Commit
2c2d352c
authored
Jun 22, 2018
by
gabrieldemarmiesse
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Moved examples from extension_types.rst to the examples directory for testing.
parent
b9f7a142
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
29 additions
and
27 deletions
+29
-27
docs/examples/userguide/extension_types/dict_animal.pyx
docs/examples/userguide/extension_types/dict_animal.pyx
+11
-0
docs/examples/userguide/extension_types/extendable_animal.pyx
.../examples/userguide/extension_types/extendable_animal.pyx
+14
-0
docs/src/userguide/extension_types.rst
docs/src/userguide/extension_types.rst
+4
-27
No files found.
docs/examples/userguide/extension_types/dict_animal.pyx
0 → 100644
View file @
2c2d352c
cdef
class
Animal
:
cdef
int
number_of_legs
cdef
dict
__dict__
def
__cinit__
(
self
,
int
number_of_legs
):
self
.
number_of_legs
=
number_of_legs
dog
=
Animal
(
4
)
dog
.
has_tail
=
True
docs/examples/userguide/extension_types/extendable_animal.pyx
0 → 100644
View file @
2c2d352c
cdef
class
Animal
:
cdef
int
number_of_legs
def
__cinit__
(
self
,
int
number_of_legs
):
self
.
number_of_legs
=
number_of_legs
class
ExtendableAnimal
(
Animal
):
# Note that we use class, not cdef class
pass
dog
=
ExtendableAnimal
(
4
)
dog
.
has_tail
=
True
\ No newline at end of file
docs/src/userguide/extension_types.rst
View file @
2c2d352c
...
@@ -78,36 +78,13 @@ It is not possible to add attributes to an extension type at runtime by default.
...
@@ -78,36 +78,13 @@ It is not possible to add attributes to an extension type at runtime by default.
You have two ways of avoiding this limitation, both add an overhead when
You have two ways of avoiding this limitation, both add an overhead when
a method is called from Python code. Especially when calling ``cpdef`` methods.
a method is called from Python code. Especially when calling ``cpdef`` methods.
The first approach is to create a Python subclass.:
:
The first approach is to create a Python subclass.:
cdef class Animal:
.. literalinclude:: ../../examples/userguide/extension_types/extendable_animal.pyx
cdef int number_of_legs
Declaring a ``__dict__`` attribute is the second way of enabling dynamic attributes.:
def __cinit__(self, int number_of_legs):
self.number_of_legs = number_of_legs
class ExtendableAnimal(Animal): # Note that we use class, not cdef class
pass
dog = ExtendableAnimal(4)
dog.has_tail = True
Declaring a ``__dict__`` attribute is the second way of enabling dynamic attributes.::
cdef class Animal:
cdef int number_of_legs
cdef dict __dict__
def __cinit__(self, int number_of_legs):
self.number_of_legs = number_of_legs
dog = Animal(4)
dog.has_tail = True
.. literalinclude:: ../../examples/userguide/extension_types/dict_animal.pyx
Type declarations
Type declarations
===================
===================
...
...
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