为什么我是0.0,当我是等于0.0的列表中的元素时,此函数返回False(Python)

问题描述 投票:1回答:2

为什么下面的代码产生:假假False

而不是:假真正False

def foo(el):
    return (el is 0.0)

seq=[0,0.0,False]
for el in seq:
    print( foo(el) )

python function equals
2个回答
1
投票
例如,考虑将class命名为A

class A: pass

案例1:

x = A() #--> create instance of class A y = A() #--> create instance of class A >>> x is y >>> False

案例2]]

x = A() #--> create instance of class A y = x #--> here y refers to the instance of x >>> x is y >>> True

基本上,如果两个变量引用相同的存储位置,则它们引用相同的对象。您可以使用python中称为id()的内置函数来检查变量的身份,该函数返回对象的身份(内存中对象的地址)。

在情况1中id(x)不等于id(y),因此x is y返回False

    在情况2中id(x)等于id(y),因此x is y返回True

0
投票
© www.soinside.com 2019 - 2024. All rights reserved.