Commit 2d3d7be9 authored by Stefan Behnel's avatar Stefan Behnel

clarification on __cinit__

parent 19a453c4
...@@ -124,18 +124,22 @@ Note that it says ``__cinit__`` rather than ``__init__``. While ...@@ -124,18 +124,22 @@ Note that it says ``__cinit__`` rather than ``__init__``. While
``__init__`` is available as well, it is not guaranteed to be run (for ``__init__`` is available as well, it is not guaranteed to be run (for
instance, one could create a subclass and forget to call the instance, one could create a subclass and forget to call the
ancestor's constructor). Because not initializing C pointers often ancestor's constructor). Because not initializing C pointers often
leads to crashing the Python interpreter without leaving as much as a leads to hard crashes of the Python interpreter, Cython provides
stack trace, Cython provides ``__cinit__`` which is *always* called on ``__cinit__`` which is *always* called immediately on construction,
construction. However, as ``__cinit__`` is called during object before CPython even considers calling ``__init__``, and which
therefore is the right place to initialise ``cdef`` fields of the new
instance. However, as ``__cinit__`` is called during object
construction, ``self`` is not fully constructed yet, and one must construction, ``self`` is not fully constructed yet, and one must
avoid doing anything with ``self`` but assigning to ``cdef`` fields. avoid doing anything with ``self`` but assigning to ``cdef`` fields.
Note also that the above method takes no parameters, although subtypes Note also that the above method takes no parameters, although subtypes
may want to accept some. Although it is guaranteed to get called, the may want to accept some. A no-arguments ``__cinit__()`` method is a
no-arguments ``__cinit__()`` method is a special case here as it does special case here that simply does not receive any parameters that
not prevent subclasses from adding parameters as they see fit. If were passed to a constructor, so it does not prevent subclasses from
parameters are added they must match those of any declared adding parameters. If parameters are used in the signature of
``__init__`` method. ``__cinit__()``, they must match those of any declared ``__init__``
method of classes in the class hierarchy that are used to instantiate
the type.
Before we continue implementing the other methods, it is important to Before we continue implementing the other methods, it is important to
understand that the above implementation is not safe. In case understand that the above implementation is not safe. In case
......
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