[英]Get parameter to dictionary function
嗨,我正在编写程序,用于从某些设备的串行消息中分离电流和电压值。 我使用字典消除了很多 if|elif。我从 main 调用 function getValues 而 function 并从 msg 中给出这个 function 参数。 我在 function def V1中有错误,该变量不存在。 我是 python 的新手,还在学习。 是否有一些选项可以在字典功能或类似的东西中传递 msg 参数?
def getValues(msg,typeVal,corI3,corI2,corI1):
busValues = {'V1':V1,
'V2':V2,
'V3':V3,
'I1':I1,
'I2':I2,
'I3':I3,
'Vb':Vb,
'Vl':Vl,
'a':a,
'Va':Va,
'Ia':Ia,
'Ib':Ib}
busValues.get(typeVal)(typeVal)
def V1(value):
global index
global strEnd1
global strEnd2
global strEnd3
index = msg.find('V1:')
strEnd1 = 30+corI3+corI2+corI1
strEnd2 = 31+corI3+corI2+corI1
strEnd3 = 32+corI3+corI2+corI1
看起来您只需要将msg
添加到您的函数 arguments 中。 这同样适用于您似乎需要的cor
参数。 那会给你:
def V1(value, msg, corI3, corI2, corI1):
从那里,您应该可以将其称为:
busValues.get(typeVal)(typeVal, msg, corI3, corI2, corI1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.