Python 3.x 如何在类python中传递字符串类型?
虽然我在我的类之前添加了这个,但我注意到,当我忽略为Python 3.x 如何在类python中传递字符串类型?,python-3.x,numba,Python 3.x,Numba,虽然我在我的类之前添加了这个,但我注意到,当我忽略为jitclass声明字符串时,我向类传递了一个字符串,而当我尝试使用字符串作为同一类型时,我无法使用它 spec = [ ('filename', str), ('rows', int32), ('cls', int32), ('L', int32), ('H', int32), ('checking', int32[:]), ('enum_file', int32[:]), ('
jitclass
声明字符串时,我向类传递了一个字符串,而当我尝试使用字符串作为同一类型时,我无法使用它
spec = [
('filename', str),
('rows', int32),
('cls', int32),
('L', int32),
('H', int32),
('checking', int32[:]),
('enum_file', int32[:]),
('step', int32),
('slices', int32),
]
@JIT等级(规范)
TypeError:规范值应为Numba类型实例,获得
这个怎么样?这有点难看,但可能是一个足够好的解决办法:
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())
您将无法在nopyton
jitted函数中使用tostring()
,但如果您只是在numba之外使用它,它就可以工作