hi, all:
I'm reading <Cython, A Guide for Python Programmers>, and in the chapter 5, section `C-Level Initialization and Finalization`, it says "Cython guarantees that __cinit__ is called exactly once and that it is called before __init, __new__ or alternative Python-level constructors...". But, as far as I know
from libc.stdlib cimport malloc, free
cdef class Matrix:
cdef:
unsigned int nrows, ncols
double *_matrix
def __cinit__(self, nr, nc):
print("[__cinit__]id of self: %s" % id(self))
self.nrows = nr
self.ncols = nc
self._matrix = <double *>malloc(nr * nc * sizeof(double))
if self._matrix == NULL:
raise MemoryError()
def __dealloc__(self):
if self._matrix != NULL:
free(self._matrix)
# @classmethod
# def __new__(cls, nr, nc):
# print("id of cls: %s" % id(cls))
# return super().__new__(cls, nr, nc)
def __init__(self, nr, nc):
print("[__init__]id of self: %s" % id(self))
the `self` object in `__init__` is the return value of `__new__`. and I've run it:
In [1]: import matrix
In [2]: matrix.Matrix(1, 2)
[__cinit__]id of self: 140461907621872
[__init__]id of self: 140461907621872
Out[2]: <matrix.Matrix at 0x7fbfd61bb7f0>
yes, `__cinit__` is called before `__init__`, but what about `__new__`? we can only get `self` object through `__new__`, right? so is it("Cython guarantees that __cinit__ is called exactly once and that it is called before __init, __new__ or alternative Python-level constructors...") wrong?