这是我想出的解决办法。
def get_Variable_Names():
return ['A','B','C']
get_Variable_Names
def update_val(self, i, var):
if i == 0:
self.A = var
elif i == 1:
self.B = var
elif i == 2:
self.C = var
为了使用这个函数,我在由返回的列表中搜索变量名的索引
获取变量名
然后打电话给
update_val
jitclass的函数。
listnames = get_Variable_Names()
val = 1.
name = 'A'
index = listnames.index(name)
C1.update_val(index,val)
这是所有的密码。
来自numba import jitclass,int32,float64
def get_Variable_Names():
return ['A','B','C']
spec = [('A' ,float64),('B' ,float64),('C' ,float64)]
@jitclass(spec)
class maClass():
def __init__(self,):
self.A = 0.
self.B = 0.
self.C = 0.
def update_val(self, i, var):
if i == 0:
self.A = var
elif i == 1:
self.B = var
elif i == 2:
self.C = var
C1 = maClass()
listnames = get_Variable_Names()
val = 1.
name = 'A'
index = listnames.index(name)
C1.update_val(index,val)
val = 2.
name = 'B'
index = listnames.index(name)
C1.update_val(index,val)
val = 3.
name = 'C'
index = listnames.index(name)
C1.update_val(index,val)
print('A',C1.A)
print('B',C1.B)
print('C',C1.C)
它不是我一直在找的,但现在已经找到了。
listnames= ['A','B','C']
for i, n in enumerate(listnames):
if i == 0:
print('if i == ' + str(i)+ ' :\n\tself.' + n + ' = var')
else:
print('elif i == ' + str(i) + ' :\n\tself.' + n + ' = var')