Python“是"声明:发生了什么? [英] Python "is" statement: what is happening?

查看:59
本文介绍了Python“是"声明:发生了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很惊讶

[] 不是 []

评估为 True.

这段代码发生了什么?notis 语句到底在做什么?

解决方案

a is not b 是一个特殊的运算符,它等价于 not a is b.>

运算符a is b 如果a 和b 绑定到同一个对象,则返回True,否则返回False.当您创建两个空列表时,您会得到两个不同的对象,因此 is 返回 False(因此 is not 返回 True).

I was quite surprised when

[] is not []

evaluated to True.

What is happening in this code? What really not and is statements are doing?

解决方案

a is not b is a special operator which is equivalent to not a is b.

The operator a is b returns True if a and b are bound to the same object, otherwise False. When you create two empty lists you get two different objects, so is returns False (and therefore is not returns True).

这篇关于Python“是"声明:发生了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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