问题描述
虽然我在课前添加了这个,但我注意到当我忽略为 jitclass
声明它时,我将一个字符串传递给我的类无法工作,并且当我尝试使用字符串相同时type 不能用.
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)
@jitclass(spec)
TypeError: 规范值应该是 Numba 类型实例,得到 ……………………………………………………………………………………………………………………………………………………………………....................
TypeError: spec values should be Numba type instances, got <class 'str'>
..........................................................................
推荐答案
这个怎么样?这有点难看,但可能是一个足够好的解决方法:
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
然后:
a = A(np.frombuffer(b'abcdef', dtype='uint8'))
print(a.filename.tostring())
您将无法在 nopython
即时函数中使用 tostring()
,但如果您只是在 numba 之外使用它,它就可以工作.
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.
这篇关于如何在 numba jitclass python 类中传递字符串类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!