Python:为什么不支持列表和元组之间的比较? [英] Python: Why is comparison between lists and tuples not supported?

查看:179
本文介绍了Python:为什么不支持列表和元组之间的比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在比较元组和像...之类的列表时。

When comparing a tuple with a list like ...

>>> [1,2,3] == (1,2,3)
False
>>> [1,2,3].__eq__((1,2,3))
NotImplemented
>>> (1,2,3).__eq__([1,2,3])
NotImplemented

... Python不像使用(1,2,3)==(1,2,3)一样深入比较它们。

... Python does not deep-compare them as done with (1,2,3) == (1,2,3).

这是什么原因呢?是因为可变列表可以随时更改(线程安全问题)或什么?

So what is the reason for this? Is it because the mutable list can be changed at any time (thread-safety issues) or what?

(我知道这是在CPython中实现的,

(I know where this is implemented in CPython, so please don't answer where, but why it is implemented.)

推荐答案

>>> tuple([1, 2]) == (1, 2)
True

Keep考虑到Python,与例如Javascript不同,是强烈的 typed ,and some(most ?)我们喜欢这种方式。

Keep in mind that Python, unlike for example Javascript, is strongly typed, and some (most?) of us prefer it that way.

这篇关于Python:为什么不支持列表和元组之间的比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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