0

Basically I have a base class defined in Cython with basic structure as follows. This is in the baseclass.pyx file.

cdef class BaseClass:
    def __init__(self, fov):
        self.fov = fov

    cdef Vector3 MyMethod(self, parameter):
        cdef Vector3 transformed = Vector3()
        return transformed

I have a python class inheriting that base cython class as follows:

from baseclass import BaseClass

class Child(BaseClass):
    def __init__(self, near=1e-6, far=1e-6):
        self._near = near
        self._far = far

    # more methods here

Finally, I create an instance of the child class and try to call the parent method:

temp = Child()
temp.MyMethod(parameter)

And I get the error:

'Child' has no attribute 'MyMethod'.
  • When you use `__init__` you override the parent class. See https://www.w3schools.com/python/python_inheritance.asp – artemis Dec 16 '19 at 19:26
  • so I added `super().__init__(fov)` to the child `__init__` (it also takes fov as a parameter), and that did not work. However, adding a method to the child in which I called the parent method via super did work. Is there not a better way around this? Also tried adding `BaseClass.__init__(self, fov)` to the child constructor, and that did not work either. – Saucy Dumpling Dec 16 '19 at 19:34

1 Answers1

0

You need to use cpdef as follows:

cdef class BaseClass:
    def __init__(self, fov):
        self.fov = fov

    cpdef Vector3 MyMethod(self, parameter):
        cdef Vector3 transformed = Vector3()
        return transformed

The cpdef keyword causes Cython to generate both a cdef function and a def function. The latter basically just calls the former but allows you to call the function from Python.

Kent Shikama
  • 3,910
  • 3
  • 22
  • 55