[0 in []的奇怪行为是假的“ [英] weird behaviour of "0 in [] is False"
问题描述
你好!
有人可以解释我以下行为吗?
Hi there !
Can someone explain me the following behaviour ?
l = []
0 in(l为False)
Traceback(最近一次调用最后一次):
文件"< stdin>",line 1,in?
TypeError:需要迭代参数(0 in l)为False
l中的真0为假
l = []
0 in (l is False) Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: iterable argument required (0 in l) is False True 0 in l is False
False
这对我来说非常不利......
-
SylvainThénaultLOGILAB,巴黎(法国)。
http://www.logilab.com http://www.logilab.fr http://www.logilab.org
False
This is really obscur to me...
--
Sylvain Thénault LOGILAB, Paris (France).
http://www.logilab.com http://www.logilab.fr http://www.logilab.org
推荐答案
> ;>>> l = []
>>>> l = []
0 in(l is False)Traceback(最近一次调用最后一次):
文件" < stdin>",第1行,在?
TypeError:需要迭代参数
0 in (l is False) Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: iterable argument required
应该清楚 - 0表示False不能正常工作。
that should be clear - 0 in False can''t work.
(0 in l)为False True 0 in l is False
(0 in l) is False True 0 in l is False
False
False
>
它似乎源于行为python展示在这样的表达式:
3> 2> 1
这产生真实,而
(3> 2)> 1
产生错误。运营商的链接翻译如下:
3> 2和2> 1
查看语言参考的第5.9节。
然后你的表达式被翻译成:
$ b $ l 0 in l和l是假
当然产生假。
-
问候,
Diez B. Roggisch
It seems to stem from a behaviour python exhibits in expressions like this:
3 > 2 > 1
This yields True, while
(3 > 2 ) > 1
yields false. Chaining of operators gets translated like this:
3 > 2 and 2 > 1
Look in section 5.9 of the language reference.
Then your expression gets translated to:
0 in l and l is False
which yields False of course.
--
Regards,
Diez B. Roggisch
Sylvain Thenault写道:
Sylvain Thenault wrote:
你好!
有人可以解释一下我的行为吗?
Hi there !
Can someone explain me the following behaviour ?
l = []
0 in(l is False)Traceback (最近一次调用最后一次):
文件"< stdin>",第1行,在?
TypeError:需要迭代的参数(0 in l)是False真的0在l中是假的
l = []
0 in (l is False) Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: iterable argument required (0 in l) is False True 0 in l is False
False
这真的让我感到不安...
False
This is really obscur to me...
来自语言参考(5.9比较):
comparison :: = or_expr(comp_operator or_expr)*
comp_operator :: ="<" | ">" | " ==" | "> =" | "< =" | "<>" | !="
| "是" [" not] | [" not"]" in"
...剪...
正式地说,如果a,b,c,...,y,z是表达式和opa,opb,...,opy
是比较运算符,然后是opa b opb c ... y opy z等同于opa b和b opb c和... y opy z,除了每个表达式最多被评估一次。
From the language reference (5.9 Comparisons):
comparison ::= or_expr ( comp_operator or_expr )*
comp_operator ::= "<" | ">" | "==" | ">=" | "<=" | "<>" | "!="
| "is" ["not"] | ["not"] "in"
...snip...
Formally, if a, b, c, ..., y, z are expressions and opa, opb, ..., opy
are comparison operators, then a opa b opb c ...y opy z is equivalent
to a opa b and b opb c and ... y opy z, except that each expression is
evaluated at most once.
换句话说''中的0是假的''相当于'' 0 in l和l is False''。
In other words ''0 in l is False'' is equivalent to ''0 in l and l is False''.
2004年11月30日星期二14:18:30 +0100,Diez B. Roggisch写道:< br>
On Tue, 30 Nov 2004 14:18:30 +0100, Diez B. Roggisch wrote:
> l = []
> 0 in(l为False)
> l = []
> 0 in (l is False)
Traceback(最近一次调用最后一次):
文件"< stdin>",第1行,在?
TypeError:iterable参数必须
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: iterable argument required
应该清楚 - 0表示False不能正常工作。
that should be clear - 0 in False can''t work.
是的,我忘记提及这是第三个表达式,它是b / b
烦我。
yes, I forget to mention that it was the third expression which was
bugging me.
> (0 in l)为False
> (0 in l) is False
True
True
> 0中的0是假的
> 0 in l is False
错误
这似乎源于行为蟒蛇在表达式中展示如下:
3> 2> 1
这产生真实,而
(3> 2)> 1
产生错误。运营商的链接被翻译成如下:
3> 2和2> 1
请参阅语言参考的第5.9节。
然后你的表达式被翻译为:
0 in l and l is False
当然会产生错误。
It seems to stem from a behaviour python exhibits in expressions like
this:
3 > 2 > 1
This yields True, while
(3 > 2 ) > 1
yields false. Chaining of operators gets translated like this:
3 > 2 and 2 > 1
Look in section 5.9 of the language reference.
Then your expression gets translated to:
0 in l and l is False
which yields False of course.
谢谢,我错过了这部分语言参考!
还没有在python中发现了一个不当行为......;)
-
SylvainThénaultLOGILAB,巴黎(法国)。
< a rel =nofollowhref =http://www.logilab.comtarget =_ blank> http://www.logilab.com http://www.logilab.fr http://www.logilab.org
thanks, I had missed this part of the language reference !
Not yet found an misbehaviour in python... ;)
--
Sylvain Thénault LOGILAB, Paris (France).
http://www.logilab.com http://www.logilab.fr http://www.logilab.org
这篇关于[0 in []的奇怪行为是假的“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!