真实情况如何<2实施? [英] How is True < 2 implemented?

查看:54
本文介绍了真实情况如何<2实施?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它不是直接在bool上实现的.

It's not implemented directly on bool.

>>> True.__lt__(2)
AttributeError: 'bool' object has no attribute '__lt__'

而且显然也没有在 int 上实现:

And it's apparently not implemented on int either:

>>> super(bool, True).__lt__(2)
AttributeError: 'super' object has no attribute '__lt__'

没有用于 2 __ lt __ 反射版本来控制操作,并且由于 int 类型不是的子类bool 永远无法正常工作.

There is no reflected version of __lt__ for 2 to control the operation, and since int type is not a subclass of bool that would never work anyway.

Python 3的行为符合预期:

Python 3 behaves as expected:

>>> True.__lt__(2)
True

所以, True<怎么样?2 是在Python 2中实现的吗?

So, how is True < 2 implemented in Python 2?

推荐答案

您没有找到 super(bool,True).__lt __ ,因为 int 使用旧版 __ cmp __ 方法,而不是在Python 2上进行丰富的比较.它是 int .__ cmp __ .

You didn't find super(bool, True).__lt__ because int uses the legacy __cmp__ method instead of rich comparisons on Python 2. It's int.__cmp__.

这篇关于真实情况如何&lt;2实施?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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