0 == False但是[]!= False? [英] 0 == False but [] != False?

查看:107
本文介绍了0 == False但是[]!= False?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个稍微天真的问题,但我知道0可用于

代表False。所以


>> 0 == False



True


但是,我知道我可以使用[]代表False,如


>> if not []:print''empty''



....




但是接下来的做法令人惊讶(对我来说! )结果


>> [] == False



False


有人能指出为什么会这样吗?


谢谢,

Rajarshi

解决方案

Rajarshi写道:


这是一个稍微天真的问题,但我知道0可用于

代表False。所以


>>> 0 == False



正确


但是,我知道我可以使用[]代表False,如


>>>如果不是[]:打印''空''



...




然后做以下操作会给我一个惊喜(对我来说!)结果


>>> [] ==错误



错误


可能有人指出为什么会这样吗?



" if foo:"不检查foo == True是否或foo == False而是

bool(foo)。对于空列表,字符串,元组,词汇和其他一些东西,

bool(foo)== False,而对于列表等,至少有一个元素,

" bool(foo)== True"。


-

Robert Kern


我已经开始相信整个世界都是一个谜,一个无害的谜团

由于我们自己疯狂地试图解释它而使它变得可怕,好像它已经

一个潜在的事实。

- Umberto Eco


Rajarshi写道:


这是一个稍微天真的问题,但我知道0可用于

代表False。所以


>>>> 0 == False



True


但是,我知道我可以使用[]代表False,如


>>>>如果不是[]:打印''空''



...




但是接下来做了一个令人惊讶的(对我来说!)结果


>>>> [] == False



False


有人能指出为什么会这样吗?


谢谢,

Rajarshi



打坐:


pyisinstance(False,int)



pyisinstance([],int)

False

pybool([])

False


James


> [] == False


False


有人能指出为什么会这样吗?



写作,if x是写作的缩写如果bool(x)。

评估bool(x)检查x .__非零__()

如果该方法不是''定义后,它会检查

x .__ len __()以查看x是否为非空容器。


在您的情况下,写入if []"转换为

" if len([])!= 0",其评估结果为False。


正确和错误的类型为bool,这是一个子类

of int。所以,False确实等于零和

真实等于一。


相反,空列表不是int类型。 />
所以[]!= False eventHough bool([])== False。

Raymond


This is a slightly naive question, but I know that 0 can be used to
represent False. So

>>0 == False

True

But, I know I can use [] to represent False as in

>>if not []: print ''empty''

....
empty

But then doing the following gives a surprising (to me!) result

>>[] == False

False

Could anybody point out why this is the case?

Thanks,
Rajarshi

解决方案

Rajarshi wrote:

This is a slightly naive question, but I know that 0 can be used to
represent False. So

>>>0 == False

True

But, I know I can use [] to represent False as in

>>>if not []: print ''empty''

...
empty

But then doing the following gives a surprising (to me!) result

>>>[] == False

False

Could anybody point out why this is the case?

"if foo:" does not check if "foo == True" or "foo == False" but rather
"bool(foo)". For empty lists, strings, tuples, dicts and some other things,
"bool(foo) == False", while for lists, etc., with at least one element,
"bool(foo) == True".

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco


Rajarshi wrote:

This is a slightly naive question, but I know that 0 can be used to
represent False. So

>>>>0 == False


True

But, I know I can use [] to represent False as in

>>>>if not []: print ''empty''


...
empty

But then doing the following gives a surprising (to me!) result

>>>>[] == False


False

Could anybody point out why this is the case?

Thanks,
Rajarshi

Meditate on:

pyisinstance(False, int)
True
pyisinstance([], int)
False
pybool([])
False

James


>[] == False

False

Could anybody point out why this is the case?

Writing, "if x" is short for writing "if bool(x)".
Evaluating bool(x) checks for a x.__nonzero__()
and if that method isn''t defined, it checks for
x.__len__() to see if x is a non-empty container.

In your case, writing "if []" translates to
"if len([]) != 0", which evaluates to False.

True and False are of type bool which is a subclass
of int. So, False really is equal to zero and
True really is equal to one.

In contrast, the empty list is not of type int.
So [] != False eventhough bool([]) == False.
Raymond


这篇关于0 == False但是[]!= False?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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