python eval 怪异

发布于 2024-08-30 22:08:17 字数 1235 浏览 4 评论 0原文

我的一个类中有以下代码,并在代码未评估时进行检查:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

獨角戲 2024-09-06 22:08:17

最有可能的是,self.recipientIMSIself.recipientMSISDN 的类型是 int,将它们与字符串进行比较会返回 False。添加此行以查看是否是这种情况:

print type(self.recipientIMSI), type(self.recipientMSISDN)

如果不是,请尝试检查相同表达式在没有 eval 的情况下的计算结果。

也就是说,您确定需要使用eval吗?通常有一种无需 evalexec 即可完成任务的方法,这将带来更安全、更易于维护的代码。

Most likely, the type of self.recipientIMSI or self.recipientMSISDN is int, and comparing them with strings returns False. Add this line to see if this is the case:

print type(self.recipientIMSI), type(self.recipientMSISDN)

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 without eval or exec, which will lead to safer, more maintainable code.

夜未央樱花落 2024-09-06 22:08:17

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 an and statement in your code string, presumably one or both of the expressions evaluate to False.

橘和柠 2024-09-06 22:08:17

你为什么要进行评估?为什么不直接在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文