“真,假”中的“0”返回True [英] "0 in [True,False]" returns True

查看:88
本文介绍了“真,假”中的“0”返回True的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


在某些程序中,我测试变量是否为布尔值,使用此

测试:如果v in [True,False ]


我的脚本在某些情况下不起作用,我最终发现,对于v =

0,测试返回True


所以我改变了我的测试显而易见的如果类型(v)是bool,但我仍然

发现它混淆了0,真,假 ;返回True


顺便说一句,我在文档中搜索了obj in list中的内容。意味着和

无法找到一个精确的定义(它是否测试了相等性或

与列表中的一个值的身份?相等,似乎);我没想错吗?


问候,

Pierre

Hi all,

In some program I was testing if a variable was a boolean, with this
test : if v in [True,False]

My script didn''t work in some cases and I eventually found that for v =
0 the test returned True

So I changed my test for the obvious "if type(v) is bool", but I still
find it confusing that "0 in [True,False]" returns True

By the way, I searched in the documentation what "obj in list" meant and
couldn''t find a precise definition (does it test for equality or
identity with one of the values in list ? equality, it seems) ; did I
miss something ?

Regards,
Pierre

推荐答案

Pierre Quentel写道:
Pierre Quentel wrote:
在某些程序中,我测试变量是否为布尔值,使用此测试:如果v in [True,错误]

我的脚本在某些情况下无法工作,我最终发现,对于v =
0测试返回True

所以我改变了我的测试对于明显的如果类型(v)是bool,但我仍然发现令人困惑的是0,在[真,假]中。返回True

顺便说一下,我在文档中搜索了obj in list中的内容。意味着并且
找不到一个精确的定义(它是否测试了与列表中的一个值之间的平等或
身份?似乎是相等的);我是否错过了什么?
In some program I was testing if a variable was a boolean, with this
test : if v in [True,False]

My script didn''t work in some cases and I eventually found that for v =
0 the test returned True

So I changed my test for the obvious "if type(v) is bool", but I still
find it confusing that "0 in [True,False]" returns True

By the way, I searched in the documentation what "obj in list" meant and
couldn''t find a precise definition (does it test for equality or
identity with one of the values in list ? equality, it seems) ; did I
miss something ?


issubclass(bool,int)
True isinstance(False,int)
True False == 0
True int(False)
issubclass(bool, int) True isinstance(False, int) True False == 0 True int(False)



0

但是说真的,除非你正在编写一个内省工具,测试

bool是非常愚蠢的。只需使用if v即可或如果不是v,并将其余部分留给

Python。


< / F>


0

but seriously, unless you''re writing an introspection tool, testing for
bool is pretty silly. just use "if v" or "if not v", and leave the rest to
Python.

</F>


在星期一,2005-12-12 16:26,Pierre Quentel写道:
On Mon, 2005-12-12 at 16:26, Pierre Quentel wrote:
大家好,

在某些程序中我是测试一个变量是否是一个布尔值,用这个
测试:如果v in [True,False]

我的脚本在某些情况下不起作用,我最终发现对于v =
0测试返回True

所以我改变了我的测试显而易见的if type(v)bool,但我仍然觉得混淆了0在[真,假]中返回True

顺便说一下,我在文档中搜索了obj in list中的内容。意味着并且
找不到一个精确的定义(它是否测试了与列表中的一个值之间的平等或
身份?似乎是相等的);我是否错过了什么?
Hi all,

In some program I was testing if a variable was a boolean, with this
test : if v in [True,False]

My script didn''t work in some cases and I eventually found that for v =
0 the test returned True

So I changed my test for the obvious "if type(v) is bool", but I still
find it confusing that "0 in [True,False]" returns True

By the way, I searched in the documentation what "obj in list" meant and
couldn''t find a precise definition (does it test for equality or
identity with one of the values in list ? equality, it seems) ; did I
miss something ?




您在哪里/如何搜索? http://docs.python.org/lib/typesseq.html

毫不含糊地指出x in s返回如果s的项目是

等于x,则返回True,否则返回False


HTH,


Carsten。



Where/how did you search? http://docs.python.org/lib/typesseq.html
states unambiguously that "x in s" returns "True if an item of s is
equal to x, else False"

HTH,

Carsten.


Pierre Quentel写道:
Pierre Quentel wrote:
在某些程序中,我测试变量是否为布尔值,使用此
测试:如果v in [True,False]

我的脚本在某些情况下不起作用,我最终发现,对于v =
0,测试返回True
In some program I was testing if a variable was a boolean, with this
test : if v in [True,False]

My script didn''t work in some cases and I eventually found that for v =
0 the test returned True




这似乎是一个奇怪的测试。这段代码试着做什么?


如果你确定你必须这样做,我会做到:


如果isinstance(v,bool):

...





如果v为True或v为错误:

...


但看起来这段代码似乎正在尝试用Python编写其他语言代码
。 ..


STeVe



This seems like a strange test. What is this code trying to do?

If you''re sure you have to do this, I would do either:

if isinstance(v, bool):
...

or

if v is True or v is False:
...

But it really seems like this code is trying to code some other language
in Python...

STeVe


这篇关于“真,假”中的“0”返回True的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆