Code block 1 using __init__
%%cython -3
cdef class c:
cdef:
int a
str s
def __init__(self):
self.a=1
self.s="abc"
def get_vals(self):
return self.a,self.s
m=c()
print(m.get_vals())
Code block 2 using __cinit__
%%cython -3
cdef class c:
cdef:
int a
str s
def __cinit__(self): # cinit here
self.a=1
self.s="abc"
def get_vals(self):
return self.a,self.s
m=c()
print(m.get_vals())
I tested both of these codes, and both run without error. In this case, what's the point of using
__cinit__
instead of__init__
?I've read the official article, I got confused by one sentence:
If you need to pass a modified argument list to the base type, you will have to do the relevant part of the initialization in the
__init__()
method instead, where the normal rules for calling inherited methods apply.
What does "modified argument" mean? Here, why should I use init rather than cinit?