Commit 2c2d352c authored by gabrieldemarmiesse's avatar gabrieldemarmiesse

Moved examples from extension_types.rst to the examples directory for testing.

parent b9f7a142
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
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
......@@ -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
===================
......
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