Python中的真正不一致 [英] True inconsistency in Python

查看:87
本文介绍了Python中的真正不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里似乎存在不一致:


Python 2.3.2(#1,2003年10月3日,19:04:58)

[ GCC 3.2 20020903(Red Hat Linux 8.0 3.2-7)] on linux2

1 == True
True 3 = = True
如果1:printtrue,则返回false
....

如果3:print" true"
....

true

0 == False
True -1 == False
False -1 == True
假如果-1:print" true"
....

如果-2:print" true"
....

true

if None:
.... print" hello"

。 ... x =无
x ==无
True x == True
False x == False
False x<>真
真x<> False
True None == True
False None == False



False

历史上Python允许<> 0在评估中等于true。现在

<> 0在评估中仍然评估为真。但它并不等于

True。它们不可互换。 (与空列表等相同)

假设需要旧的行为,程序员需要小心

不要将变量与True进行比较,如下所示:


如果var == True:#仅当var为1时才有效

blah


''必须使用:


如果var:#的工作如果var不是0

blah


这种不一致是否会在未来?


怎么样? (将<> 0 == True或将<> 0<> true或将解决一些

其他方式,我太不透明了?: - )


似乎Python应该抛出一个警告(也许如果一个标志是

设置)任何时候碰到代码比较一个变量为True或者这是假的。

它非常微妙,很容易引发新手。


当然,根据上面的逻辑,如果1 == true和2 == true,

然后1 == 2!值得庆幸的是,这在Python中不起作用。 Python是神奇的。

我喜欢魔法,但它会产生一些有趣的不一致。


Scott

解决方案

Scott Chapman写道:

历史上Python允许<> 0在评估中等于true。
现在
<> 0在评估中仍然评估为真。但它并不等于真实。它们不可互换。 (与空列表等相同)




这是因为测试真相的正确方法不使用True

价值。这是:


如果x:

...


不是这样的:


如果x ==真:

...


-

Erik Max Francis& ;&安培; ma*@alcyone.com && http://www.alcyone.com/max/

__美国加利福尼亚州圣何塞市&& 37 20 N 121 53 W&& & tSftDotIotE

/ \生命是一种在死亡之前的困境。

\ __ / Henry James


Scott Chapman写道:


如果var == True:#只有当var为1时才有效
blah

''必须使用:

如果var:#的工作如果var不是0
blah




只是因为某些东西不是真的没有这不是真的。


David


2003年11月12日星期三19 :42:27 -0800,Scott Chapman写道:

这里似乎有一个不一致的地方:




是的。不一致是期望所有布尔真值都是

相同的值。


对象True将评估为布尔真值。对象False将

不作为布尔真值进行评估。这并不意味着没有

其他值将会或不会被评估为布尔真值。


你们很多人都希望阅读PEP导致了''bool''

类型(以及真假对象)的创建:


< http:// www。 python.org/peps/pep-0285.html>


简而言之:用''if''来测试表达式的布尔真值,

不是价值比较运营商。


-

\bash awk grep perl sed,df du,du-du du-du, vi troff su fsck |

` \ rm * halt LART LART LART! - 瑞典BOFH,|

_o__)alt.sysadmin.recovery |

Ben Finney< http://bignose.squidly.org/>


There seems to be an inconsistency here:

Python 2.3.2 (#1, Oct 3 2003, 19:04:58)
[GCC 3.2 20020903 (Red Hat Linux 8.0 3.2-7)] on linux2

1 == True True 3 == True False if 1: print "true" ....
true if 3: print "true" ....
true
0 == False True -1 == False False -1 == True False if -1: print "true" ....
true if -2: print "true" ....
true
if None: .... print "hello"
.... x = None
x == None True x == True False x == False False x <> True True x <> False True None == True False None == False


False
Historically Python has allowed <> 0 to equal true in evaluations. Now
<> 0 still evaluates to true in evaluations. However it doesn''t equal
True. They are not interchangable. (Same with empty lists, etc.)
Assuming the old behavior is desired, programmers need to be careful
not to compare a variable with True as in:

if var == True: # only works if var is 1
blah

'' Must use:

if var: # works if var is not 0
blah

Is this inconsistency going to be resolved in the future?

How? (Will <>0 == True or will <>0 <> true or will it be resolved some
other way that I''m too opaque to see? :-)

It seems that maybe Python should throw a warning (perhaps if a flag is
set) any time it bumps into code comparing a variable to True or False.
It''s pretty subtle and would easily throw a newbie.

Of course, according to the above logic, if 1 == true and 2 == true,
then 1 == 2! Thankfully this doesn''t work in Python. Python is magic.
I love the magic but it creates some interesting inconsistencies.

Scott

解决方案

Scott Chapman wrote:

Historically Python has allowed <> 0 to equal true in evaluations.
Now
<> 0 still evaluates to true in evaluations. However it doesn''t equal
True. They are not interchangable. (Same with empty lists, etc.)



That''s because the proper way to test for truth does not use the True
value at all. It is this:

if x:
...

not this:

if x == True:
...

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ Life is a predicament which precedes death.
\__/ Henry James


Scott Chapman wrote:


if var == True: # only works if var is 1
blah

'' Must use:

if var: # works if var is not 0
blah



Just because something isn''t True doesn''t mean it isn''t true.

David


On Wed, 12 Nov 2003 19:42:27 -0800, Scott Chapman wrote:

There seems to be an inconsistency here:



Yes. The inconsistency is in expecting all Boolean truths to be the
same value.

The object True will evaluate as a Boolean truth. The object False will
not evaluate as a Boolean truth. This doesn''t mean that there are no
other values that will or won''t evaluate as Boolean truth.

You many want to read the PEP that led to the creation of the ''bool''
type (and True and False objects):

<http://www.python.org/peps/pep-0285.html>

In short: Testing the Boolean truth of an expression is done with ''if'',
not with value-comparison operators.

--
\ "bash awk grep perl sed, df du, du-du du-du, vi troff su fsck |
`\ rm * halt LART LART LART!" -- The Swedish BOFH, |
_o__) alt.sysadmin.recovery |
Ben Finney <http://bignose.squidly.org/>


这篇关于Python中的真正不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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