I am trying to abstract one class (like shown here).
But when trying to call a method implemented only in the child, I get an error (pretty much the same as in this other question).
Here is an example:
# compile with:
# python setup.py build_ext --inplace
cdef class MetaB:
cdef meta_method(self):
print("called c_meta_method")
cdef class B(MetaB):
cdef method(self):
print("Called method")
cdef class A:
cdef:
MetaB b
def __init__(self):
self.b = B()
cdef c_call_b_method(self):
self.b.method()
def call_method(self):
return self.c_call_b_method()
cdef c_call_b_meta_method(self):
self.b.meta_method()
def call_meta_method(self):
return self.c_call_b_meta_method()
When I try it, the parent class method works fine:
$ python -c "import A; A.A().call_meta_method()"
[out]: called c_meta_method
But the child method does not work:
$ python -c "import A; A.A().call_method()"
[out]: Traceback (most recent call last):
File "<string>", line 1, in <module>
File "A.pyx", line 24, in A.A.call_method
return self.c_call_b_method()
File "A.pyx", line 21, in A.A.c_call_b_method
self.b.method()
AttributeError: 'A.B' object has no attribute 'method'
How can I abstract a class, but still allow the child to expose different methods?