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
3d61a95d
Commit
3d61a95d
authored
Jun 22, 2018
by
scoder
Committed by
GitHub
Jun 22, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2399 from gabrieldemarmiesse/test_extension_types_3
Added tests to "Extension types" part 3
parents
a261155f
2c2d352c
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 @
3d61a95d
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 @
3d61a95d
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 @
3d61a95d
...
...
@@ -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
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
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
Declaring a ``__dict__`` attribute is the second way of enabling dynamic attributes.:
.. literalinclude:: ../../examples/userguide/extension_types/dict_animal.pyx
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