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
78f4d7b8
Commit
78f4d7b8
authored
May 20, 2018
by
scoder
Committed by
GitHub
May 20, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2277 from gabrieldemarmiesse/dynamic_attributes
Transfer of the dynamic attributes into the userguide.
parents
6d44fd22
966d84c5
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
47 additions
and
4 deletions
+47
-4
docs/src/userguide/extension_types.rst
docs/src/userguide/extension_types.rst
+47
-4
No files found.
docs/src/userguide/extension_types.rst
View file @
78f4d7b8
...
...
@@ -39,14 +39,16 @@ interface to them.
.. _readonly:
Attributes
============
Static
Attributes
============
=====
Attributes of an extension type are stored directly in the object's C struct.
The set of attributes is fixed at compile time; you can't add attributes to an
extension type instance at run time simply by assigning to them, as you could
with a Python class instance. (You can subclass the extension type in Python
and add attributes to instances of the subclass, however.)
with a Python class instance. However, you can explicitly enable support
for dynamically assigned attributes, or subclass the extension type with a normal
Python class, which then supports arbitrary attribute assignments.
See :ref:`dynamic_attributes`.
There are two ways that attributes of an extension type can be accessed: by
Python attribute lookup, or by direct access to the C struct from Cython code.
...
...
@@ -76,6 +78,47 @@ and the depth attribute readable but not writable.
Python access, not direct access. All the attributes of an extension type
are always readable and writable by C-level access.
.. _dynamic_attributes:
Dynamic Attributes
==================
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.::
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
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
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