|
发表于 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: ...
|
|