推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
littleMaple
V2EX  ›  Python

Python 类型标注如何地道地指明某类型为 comparable/orderable?

  •  
  •   littleMaple ·
    MapleCCC · Jan 2, 2021 · 2251 views
    This topic created in 1997 days ago, the information mentioned may be changed or developed.

    例如如下可复现例子:

    from typing import TypeVar
    
    T = TypeVar("T")
    
    def is_bigger(a: T, b: T)->bool:
        return a > b
    

    类型检查库 Pyright 报错如下:

    Operator ">" not supported for types "T" and "T"
    

    类型检查库 MyPy 报错如下:

    Unsupported left operand type for > ("T")
    
    4 replies    2021-01-02 16:06:18 +08:00
    ericls
        1
    ericls  
       Jan 2, 2021
    给一个 `__lt__`
    Trim21
        2
    Trim21  
       Jan 2, 2021   ❤️ 3
    littleMaple
        3
    littleMaple  
    OP
       Jan 2, 2021
    @Trim21 正解,谢谢大佬!看来使用 Python 类型标注需要额外封装的东西也不少啊 😂
    abersheeran
        4
    abersheeran  
       Jan 2, 2021
    @littleMaple 是的。还需慢慢发展。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3453 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 52ms · UTC 00:47 · PVG 08:47 · LAX 17:47 · JFK 20:47
    ♥ Do have faith in what you're doing.