问题
while I added this before my class but I noticed that I passed a string to my class when I neglected to declare it for jitclass
can't work and when i trying using string to be the same type can't use it.
spec = [
('filename', str),
('rows', int32),
('cls', int32),
('L', int32),
('H', int32),
('checking', int32[:]),
('enum_file', int32[:]),
('step', int32),
('slices', int32),
]
@jitclass(spec)
TypeError: spec values should be Numba type instances, got <class 'str'>
..........................................................................
回答1:
How about this? It's a little ugly, but might be a good enough workaround:
import numba as nb
spec = [
('filename', nb.uint8[:]),
('rows', nb.int32),
('cls', nb.int32),
('L', nb.int32),
('H', nb.int32),
('checking', nb.int32[:]),
('enum_file', nb.int32[:]),
('step', nb.int32),
('slices', nb.int32),
]
@nb.jitclass(spec)
class A:
def __init__(self, fname):
self.filename = fname
and then:
a = A(np.frombuffer(b'abcdef', dtype='uint8'))
print(a.filename.tostring())
You won't be able to use tostring()
in a nopython
jitted function, but if you're just using it outside of numba, it works.
来源:https://stackoverflow.com/questions/48987368/how-can-i-pass-string-type-in-class-in-numba-jitclass-python