I'm trying to return a string when I call a function in a class that uses jitclass
but I get an error:
numba.errors.InternalError: Failed at nopython (nopython mode backend)
cannot convert native const('Something') to Python object
[1] During: resolving callee type: BoundFunction((<class 'numba.types.misc.ClassInstanceType'>, 'get_Names') for instance.jitclass.myclass#3f2d488<A:float64,B:float64>)
[2] During: typing of call at <string> (3)
I'm using this code to test the function:
from numba import jitclass
from numba import boolean, int32, float64,uint8
spec = [('A' ,float64),
('B' ,float64)]
@jitclass(spec)
class myclass:
def __init__(self,):
self.A = 3.25
self.B = 22.5
def get_Names(self):
return "Something"
mC = myclass()
print(mC.get_Names())
Does somebody know How I could return a string?
You can sort of work around this by using an array of bytes to represent the string, as shown below.
That said, I think this is pretty ugly/hard to maintain. Assuming it fits the problem, I think you are better off using a plain python class with jit
functions for speedups as necessary, or dropping into something like cython
which has richer support for extension types.
from numba import jitclass, float64
SOMETHING = np.frombuffer(b"Something", dtype='uint8')
spec = [('A' ,float64),
('B' ,float64)]
def get_jitclass_str(val):
return bytes(val).decode('utf-8')
@jitclass(spec)
class myclass:
def __init__(self,):
self.A = 3.25
self.B = 22.5
def get_Names(self):
return SOMETHING
Usage
In [16]: mc = myclass()
In [17]: get_jitclass_str(mc.get_Names())
Out[17]: 'Something'