Commit 3d61a95d authored by scoder's avatar scoder Committed by GitHub

Merge pull request #2399 from gabrieldemarmiesse/test_extension_types_3

Added tests to "Extension types" part 3
parents a261155f 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
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