Commit 0f6795d0 authored by gabrieldemarmiesse's avatar gabrieldemarmiesse

Gave a shrubbery to the knights.

parent 084a25f5
cdef class Shrubbery:
cdef int width, height
from my_module cimport Shrubbery
cdef Shrubbery another_shrubbery(Shrubbery sh1):
cdef Shrubbery sh2
sh2 = Shrubbery()
sh2.width = sh1.width
sh2.height = sh1.height
return sh2
from my_module cimport Shrubbery
cdef widen_shrubbery(Shrubbery sh, extra_width):
sh.width = sh.width + extra_width
...@@ -105,21 +105,15 @@ will be very inefficient. If the attribute is private, it will not work at all ...@@ -105,21 +105,15 @@ will be very inefficient. If the attribute is private, it will not work at all
-- the code will compile, but an attribute error will be raised at run time. -- the code will compile, but an attribute error will be raised at run time.
The solution is to declare ``sh`` as being of type :class:`Shrubbery`, as The solution is to declare ``sh`` as being of type :class:`Shrubbery`, as
follows:: follows:
cdef widen_shrubbery(Shrubbery sh, extra_width): .. literalinclude:: ../../examples/userguide/extension_types/widen_shrubbery.pyx
sh.width = sh.width + extra_width
Now the Cython compiler knows that ``sh`` has a C attribute called Now the Cython compiler knows that ``sh`` has a C attribute called
:attr:`width` and will generate code to access it directly and efficiently. :attr:`width` and will generate code to access it directly and efficiently.
The same consideration applies to local variables, for example,:: The same consideration applies to local variables, for example:
cdef Shrubbery another_shrubbery(Shrubbery sh1): .. literalinclude:: ../../examples/userguide/extension_types/shrubbery_2.pyx
cdef Shrubbery sh2
sh2 = Shrubbery()
sh2.width = sh1.width
sh2.height = sh1.height
return sh2
Type Testing and Casting Type Testing and Casting
......
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