我正在尝试下一个代码:
x = 'asd'
y = 'asd'
z = input() #write here string 'asd'. For Python 2.x use raw_input()
x == y # True.
x is y # True.
x == z # True.
x is z # False.
Run Code Online (Sandbox Code Playgroud)
为什么我们在最后一个表达中有错?
我在Python 3.6.5中遇到了一个奇怪的现象:虽然它们彼此相等但float('50.0')不一样.float(50.0)
我运行了一些代码来找到差异.除了python说他们不一样,我找不到区别.我很困惑.如果有人能解释这里发生的事情,我会喜欢它.
这是我的测试:
if float('50.0') is float(50.0):
print("float('50.0') is float(50.0)")
else:
print("float('50.0') is not float(50.0)")
if float('50.0') == float(50.0):
print("float('50.0') == float(50.0)")
else:
print("float('50.0') != float(50.0)")
if float('50.0') is 50.0:
print("float('50.0') is 50.0")
else:
print("float('50.0') is not 50.0")
if float(50.0) is 50.0:
print('float(50.0) is 50.0')
else:
print('float(50.0) is not 50.0')
if float(50.0) is float(50.0):
print('float(50.0) is float(50.0)')
else:
print('float(50.0) is not float(50.0)')
xstr_float = float('50.0')
norm_float = float(50.0)
print ('xstr_float: {0:.100f}'.format(xstr_float))
print ('xstr_float is …Run Code Online (Sandbox Code Playgroud) 当我使用 Pytest 进行 Python 格式化时,它抱怨执行以下操作:
>>> assert some_function_ret_val() == True
E712 comparison to True should be 'if cond is True:' or 'if cond:'
Run Code Online (Sandbox Code Playgroud)
并想要:
assert some_function_ret_val() is True
Run Code Online (Sandbox Code Playgroud)
我知道 True/False/None 只能有一份副本,但我认为所有原语都是不可变类型。
在什么情况下,原始类型的“==”和“is”比较会不同?
不然为什么“==”会成为比较任务的常态呢?
我发现这篇 stackoverflow 帖子讨论了与非原始类型的比较,但我似乎找不到为什么“is”比较对于原始类型可能是危险的原因。 与布尔 numpy 数组 VS PEP8 E712 的比较
如果只是约定,我认为“is”比“==”更易读,但我觉得可能存在一些疯狂的边缘情况,其中可能有多个原始类型的副本。
风格指南如下:
# Correct:
if greeting:
# Wrong:
if greeting == True:
# Worse:
if greeting is True:
Run Code Online (Sandbox Code Playgroud)
请参阅PEP 8,搜索单词“更糟”
为什么是这样?我习惯于尽可能明确地检查条件,以使代码更具可读性并捕获异常。
考虑这个函数:
def f(do_it):
if do_it : print("doit")
else : print("no don't")
Run Code Online (Sandbox Code Playgroud)
很容易被滥用/监督,出现意想不到的行为
>>> f("False")
doit
>>> f([False])
doit
Run Code Online (Sandbox Code Playgroud)
例如,当您检查可能无意中传递 if 子句的返回值时,这是一个真正的问题。通过使用该构造可以避免这种情况is。
显然,PEP 建议有充分的理由,但它是什么?
在评论者的推动下,进一步的研究使我得出以下发现:
if x:
Run Code Online (Sandbox Code Playgroud)
调用 x 类的 __bool 方法。该方法应返回 True 或 False,具体取决于对象认为自己是两者中的哪一个。
if x==True:
Run Code Online (Sandbox Code Playgroud)
调用 x 类的 __eq 方法。该方法应该能够将自身与 True(或 False)进行比较,并根据具体情况返回 True 或 False。
if x is True
Run Code Online (Sandbox Code Playgroud)
两者都不调用。这测试 x 是否是“True”对象。它完全绕过了 __eq 和 __bool …
我接受了 Python 测试,其中一个问题是:应该将什么传递给以下函数,以便它返回True?
def fun(x):
if x + 1 is 1 + x:
return False
if x + 2 is not 2 + x:
return False
return True
Run Code Online (Sandbox Code Playgroud)
在我看来,这没有多大意义,但我只想知道正确的答案(如果存在这样的答案)。
有人可以向我解释这种行为吗?
a = 'Test'
b = 'Test'
print(a is b) # True
Run Code Online (Sandbox Code Playgroud)
我预计结果为 False,因为 a 和 b 是不同的引用。当使用相等运算符 (==) 来比较值相等时,我所期望的显示结果。
is-operator 是为了比较引用相等?还是我弄错了?
显示行为的原因是什么?
我正在探索 python is vs ==,当我探索它时,我发现我是否写了以下内容;
>>> a = 10.24
>>> b = 10.24
Run Code Online (Sandbox Code Playgroud)
在 python shell 中输入>> a is b,它给我输出为假。但是当我在 python 编辑器中编写以下代码并运行它时,我得到了正确的结果。
a = 10.24
b = 10.24
print(a is b)
Run Code Online (Sandbox Code Playgroud)
谁能解释为什么我得到相同变量和表达式的两个不同结果?
我正在尝试使用 python 的unittest库来编写一些单元测试。我有一个返回对象的无序列表的函数。我想验证对象是否相同,并且我尝试使用assertCountEqual来执行此操作。
==然而,尽管各个对象彼此相等 ( ),但这似乎失败了。这是断言失败的“diff”输出:
First has 1, Second has 0: Intersection(time=8.033252939677466e-08, del_time=8.033252939677466e-08, pos=Vector(10.00, 0.00), line=Line(Vector(500.00, 0.00), Vector(0.00, 0.00)), ent=None, oth=None, invalid=False)
First has 1, Second has 0: Intersection(time=-9.918729244820295e-16, del_time=-9.918729244820295e-16, pos=Vector(10.00, 0.00), line=Line(Vector(500.00, 0.00), Vector(0.00, 0.00)), ent=None, oth=None, invalid=False)
First has 0, Second has 1: Intersection(time=8.033252939677466e-08, del_time=8.033252939677466e-08, pos=Vector(10.00, 0.00), line=Line(Vector(500.00, 0.00), Vector(0.00, 0.00)), ent=None, oth=None, invalid=False)
First has 0, Second has 1: Intersection(time=-9.918729244820295e-16, del_time=-9.918729244820295e-16, pos=Vector(10.00, 0.00), line=Line(Vector(500.00, 0.00), Vector(0.00, 0.00)), ent=None, oth=None, invalid=False)
Run Code Online (Sandbox Code Playgroud)
验证它们是否相等: …
我希望打印的值是"200!" 和"404!" 在Python3.4中.但我得到的结果是"200!" 和"别的!".
我做了一个奇怪的面孔 - 并在C中再次尝试.C中的结果与Python3中的结果不同.为什么?
Python3.4代码
def chk(s):
if s is 200:
print('200!')
elif s is 404:
print('404!')
else:
print('else!')
chk(200)
chk(404)
Run Code Online (Sandbox Code Playgroud)
Python3.4代码结果
200!
else!
Run Code Online (Sandbox Code Playgroud)
C代码
#include <stdio.h>
void chk(int s) {
if (s == 200)
printf("200!");
else if (s == 404)
printf("404!");
else
printf("else!");
}
int main(void) {
chk(200);
chk(404);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
C代码结果
200!404!
Run Code Online (Sandbox Code Playgroud) 如果numpy.any()返回True比较is True失败但是== True有效.有谁知道为什么?
一个最小的例子
from __future__ import print_function
import numpy
a = numpy.array([True])
if a.any() == True:
print('== works')
if a.any() is True:
print('is works')
Run Code Online (Sandbox Code Playgroud)
这段代码的输出就是== works.
python ×9
boolean ×1
c ×1
comparison ×1
immutability ×1
numpy ×1
python-3.4 ×1
python-3.x ×1
python-idle ×1
unit-testing ×1