鱼C论坛

 找回密码
 立即注册
查看: 2695|回复: 4

[已解决]元组比较

[复制链接]
发表于 2020-12-9 23:02:42 | 显示全部楼层
1、查阅文档
https://fishc.com.cn/forum.php?m ... peid%26typeid%3D403
__lt__(self, other)        定义小于号的行为:x < y 调用 x.__lt__(y)
__gt__(self, other)        定义大于号的行为:x > y 调用 x.__gt__(y)
2、查看帮助文档
>>> help(tuple)  # 可以发现,元祖也是有上述的比较运算符方法的

具体怎么定义的呢?以下是源代码,有兴趣可以去看看(貌似并没有什么卵用)
class tuple(Sequence[_T_co], Generic[_T_co]):
    def __new__(cls: Type[_T], iterable: Iterable[_T_co] = ...) -> _T: ...
    def __init__(self, iterable: Iterable[_T_co] = ...): ...
    def __len__(self) -> int: ...
    def __contains__(self, x: object) -> bool: ...
    @overload
    def __getitem__(self, x: int) -> _T_co: ...
    @overload
    def __getitem__(self, x: slice) -> Tuple[_T_co, ...]: ...
    def __iter__(self) -> Iterator[_T_co]: ...
    def __lt__(self, x: Tuple[_T_co, ...]) -> bool: ...
    def __le__(self, x: Tuple[_T_co, ...]) -> bool: ...
    def __gt__(self, x: Tuple[_T_co, ...]) -> bool: ...
    def __ge__(self, x: Tuple[_T_co, ...]) -> bool: ...
    @overload
    def __add__(self, x: Tuple[_T_co, ...]) -> Tuple[_T_co, ...]: ...
    @overload
    def __add__(self, x: Tuple[Any, ...]) -> Tuple[Any, ...]: ...
    def __mul__(self, n: int) -> Tuple[_T_co, ...]: ...
    def __rmul__(self, n: int) -> Tuple[_T_co, ...]: ...
    def count(self, x: Any) -> int: ...
    if sys.version_info >= (3, 5):
        def index(self, x: Any, start: int = ..., end: int = ...) -> int: ...
    else:
        def index(self, x: Any) -> int: ...
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-10-29 04:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表