Commit 5d7bb4c8 authored by gabrieldemarmiesse's avatar gabrieldemarmiesse

removed the recommendation of subclassing.

parent 32322030
...@@ -45,8 +45,10 @@ Static Attributes ...@@ -45,8 +45,10 @@ Static Attributes
Attributes of an extension type are stored directly in the object's C struct. 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 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 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 with a Python class instance. However, you can explicitly enable support
and add attributes to instances of the subclass, see :ref:`dynamic_attributes`.) 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 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. Python attribute lookup, or by direct access to the C struct from Cython code.
...@@ -84,11 +86,9 @@ Dynamic Attributes ...@@ -84,11 +86,9 @@ Dynamic Attributes
It is not possible to add attributes to an extension type at runtime by default. 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. a method is called from Python code. Especially when calling ``cpdef`` methods.
The first workaround is making a child Python class and is preferred way of The first approach is to create a Python subclass.::
keeping the static attributes of an extension
type while enabling the use of dynamic attributes::
cdef class Animal: cdef class Animal:
...@@ -105,9 +105,7 @@ type while enabling the use of dynamic attributes:: ...@@ -105,9 +105,7 @@ type while enabling the use of dynamic attributes::
dog.has_tail = True dog.has_tail = True
Declaring a ``__dict__`` attribute is the second way of enabling dynamic attributes Declaring a ``__dict__`` attribute is the second way of enabling dynamic attributes.::
and can have a significant performance penalty compared to subclassing,
especially when using ``cpdef`` class methods::
cdef class Animal: cdef class Animal:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment