I am converting a vanilla python to cython to speed up the performance. I need to define a class PFR
with an attribute gas
. The type of gas is Solution which is a type from an external library called cantera (written in C++ with a python interface created using Cython).
# distutils: language=c++
cimport cantera as ct
cdef class PFR:
cdef public ct.Solution gas;
def __init__(self):
self.gas = None;
def set_gas(self, gas):
self.gas = gas
When I complie the code I get this error:
Error compiling Cython file:
------------------------------------------------------------
...
# distutils: language=c++
cimport cantera as ct
cdef class PFR:
cdef public ct.Solution gas;
^
------------------------------------------------------------
C:\Users\[****]\.ipython\cython\_cython_magic_d7002e8c4a676e8ba0827e69fe89ba2a.pyx:5:16: 'Solution' is not a type identifier
Error compiling Cython file:
------------------------------------------------------------
...
# distutils: language=c++
cimport cantera as ct
cdef class PFR:
cdef public ct.Solution gas;
^
------------------------------------------------------------
C:\Users\[****]\.ipython\cython\_cython_magic_d7002e8c4a676e8ba0827e69fe89ba2a.pyx:5:28: C attribute of type '<error>' cannot be accessed from Python
I found a trick to make the code work. I declare gas
initially as a object
instead of ct.Solution
# distutils: language=c++
import cantera as ct
cdef class PFR:
cdef public object gas;
def __init__(self):
self.gas = None;
def set_gas(self, gas):
self.gas = gas;
def set_TP(self, T, P):
self.gas.TP = T, P
This works fine without any errors. I can create a gas
object, connect it to an instance of PFR
using the gas
attribute and modify it using set.TP(T,P)
method.
But, I am not sure if this approach is correct because I do not understand what is happening under the hood. Could this cause performance errors when I expand the class or create multiple instances of PFR
?