I am trying to add a default value for parameter d
of the FunctionA.evaluate
method, but I am getting either a Signature not compatible with previous declaration
or 'Function' is not a type name
compilation errors. How to do that correctly? The whole project is in my Github.
As I am pretty new with Python and Cython I would also appreciate any other comments regarding the code below (even syntax related).
The goal is to have this simple test pass:
test_ti.py
from ti import SubclassA, SubclassB
import pytest
def test_run_me():
this_A = SubclassA(a=1, b=2)
ret_A = this_A.do_that(c=3) # Throws: TypeError: do_that() takes exactly 2 positional arguments (1 given)
assert(ret_A == 6)
this_B = SubclassB(a=1, b=2)
ret_B = this_B.do_that(c=3, d=4)
assert(ret_B == 10)
The rest of the project 'skeleton':
superclass.pxd
cdef class Superclass:
cdef int a
cdef int b
cdef int c
cdef int d
cdef Function f
cpdef int do_that(self, int c, int d) except? -1
cdef class Function:
cpdef int evaluate(self, int a, int b, int c, int d) except *
superclass.pyx
cdef class Superclass:
def __init__(self, a, b):
self.a = a
self.b = b
self.c = 0
cpdef int do_that(self, int c, int d) except? -1:
self.c = c
self.d = d
return(self.f.evaluate(self.a, self.b, self.c, self.d))
cdef class Function:
cpdef int evaluate(self, int a, int b, int c, int d) except *:
return 0
subclas_a.pyx
cimport superclass
from superclass cimport Superclass, Function
cdef class SubclassA(Superclass):
def __init__(self, a=0, b=0):
super(SubclassA, self).__init__(a=a, b=b)
self.f = FunctionA()
cdef class FunctionA(Function):
cpdef int evaluate(self, int a, int b, int c, int d) except *:
# This function needs only 3 parameters, a, b and c and I would
# love to have a d=0 default value here.
return(a + b + c)
subclass_b.pyx
cimport superclass
from superclass cimport Superclass, Function
cdef class SubclassB(Superclass):
def __init__(self, a=0, b=0):
super(SubclassB, self).__init__(a=a, b=b)
self.f = FunctionB()
cdef class FunctionB(Function):
cpdef int evaluate(self, int a, int b, int c, int d) except *:
return(a + b + c + d)