python eval 怪异
我的一个类中有以下代码,并在代码未评估时进行检查:
filterParam="self.recipientMSISDN==tmpBPSS.split('_')[3].split('#')[0] and self.recipientIMSI==tmpBPSS.split('_')[3].split('#')[1]"
if eval(filterParam):
print "Evalled"
else:
print "Not Evalled\nfilterParam\n'%s'\ntmpBPSS\n'%s'\nself.recipientMSISDN\n'%s'\nself.recipientIMSI\n'%s'" % (filterParam, tmpBPSS, self.recipientMSISDN, self.recipientIMSI)
我没有得到任何“评估”。结果如下:
Not Evalled
filterParam
'self.recipientMSISDN==tmpBPSS.split('_')[3].split('#')[0] and self.recipientIMSI==tmpBPSS.split('_')[3].split('#')[1]'
tmpBPSS
'bprm_DAILY_MO_919844000039#892000000'
self.recipientMSISDN
'919844000039'
self.recipientIMSI
'892000000'
因此,我使用上面的输出来检查 python shell 中的代码,正如您所看到的,代码评估正确:
>>> filterParam="recipientMSISDN==tmpBPSS.split('_')[3].split('#')[0] and recipientIMSI==tmpBPSS.split('_')[3].split('#')[1]"
>>> tmpBPSS='bprm_DAILY_MO_919844000039#892000000'
>>> recipientMSISDN='919844000039'
>>> recipientIMSI='892000000'
>>> if eval(filterParam):
... print "Evalled"
... else:
... print "Not Evalled"
...
Evalled
我是不是疯了,或者我错过了什么?
一个
I have the following code in one of my classes along with checks when the code does not eval:
filterParam="self.recipientMSISDN==tmpBPSS.split('_')[3].split('#')[0] and self.recipientIMSI==tmpBPSS.split('_')[3].split('#')[1]"
if eval(filterParam):
print "Evalled"
else:
print "Not Evalled\nfilterParam\n'%s'\ntmpBPSS\n'%s'\nself.recipientMSISDN\n'%s'\nself.recipientIMSI\n'%s'" % (filterParam, tmpBPSS, self.recipientMSISDN, self.recipientIMSI)
I am not getting anything to 'eval'. Here are the results:
Not Evalled
filterParam
'self.recipientMSISDN==tmpBPSS.split('_')[3].split('#')[0] and self.recipientIMSI==tmpBPSS.split('_')[3].split('#')[1]'
tmpBPSS
'bprm_DAILY_MO_919844000039#892000000'
self.recipientMSISDN
'919844000039'
self.recipientIMSI
'892000000'
So I used the outputs from the above to check the code in a python shell and as you can see the code evalled correctly:
>>> filterParam="recipientMSISDN==tmpBPSS.split('_')[3].split('#')[0] and recipientIMSI==tmpBPSS.split('_')[3].split('#')[1]"
>>> tmpBPSS='bprm_DAILY_MO_919844000039#892000000'
>>> recipientMSISDN='919844000039'
>>> recipientIMSI='892000000'
>>> if eval(filterParam):
... print "Evalled"
... else:
... print "Not Evalled"
...
Evalled
Am I off my rocker or what am I missing?
A
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最有可能的是,
self.recipientIMSI
或self.recipientMSISDN
的类型是int
,将它们与字符串进行比较会返回 False。添加此行以查看是否是这种情况:如果不是,请尝试检查相同表达式在没有
eval
的情况下的计算结果。也就是说,您确定需要使用
eval
吗?通常有一种无需eval
或exec
即可完成任务的方法,这将带来更安全、更易于维护的代码。Most likely, the type of
self.recipientIMSI
orself.recipientMSISDN
isint
, and comparing them with strings returns False. Add this line to see if this is the case:If not, try checking what the same expression evaluates to without
eval
.That said, Are you sure you need to use
eval
? Usually there's a way of doing things withouteval
orexec
, which will lead to safer, more maintainable code.eval
的返回值不是代码是否被计算,而是计算结果返回的实际值。由于代码字符串中有一个and
语句,因此可能其中一个或两个表达式的计算结果为 False。The return value from
eval
is not whether or the code was evaluated, but the actual value returned by doing so. Since you have anand
statement in your code string, presumably one or both of the expressions evaluate to False.你为什么要进行评估?为什么不直接在 if 语句中进行比较呢?
可能存在类型不匹配。您指定的值之一可以是 unicode 或某种其他类型的类似字符串的对象。当你打印它时,你将它转换为字符串,因此它们看起来相同,但它们可能是不同的类型,因此评估结果为 False。
Why are you even doing the eval at all? Why not just make the comparison directly in the if statement?
It's possible there is a type mismatch. One of those values you specify could be unicode or some other type of string-like object. Whey you print it, you're casting it to a string and so they look equal, but they may be different types, and so evaluate to False.