python - 在 Python 中, "type annotations"和 "type hints"是一回事吗?

标签 python type-hinting

在 Python 中,“类型注释”和“类型提示”是一回事吗?如果不是,它们之间有什么区别?

最佳答案

是的,它们都可以指同一组事物,即 the language feature of Python 3.5+ :

class Animal:
    #     *
    name: str

    #                  *       *         type annotations
    def foo(self, bar: baz) -> quux:
        ...

有时还有像 Mypy 这样的工具支持的 Python-2 兼容的注释语法。 :
x = {6, 7}  # type: Set[int]

关于python - 在 Python 中, "type annotations"和 "type hints"是一回事吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59023552/

相关文章:

python - 使用非类型对象注释参数

php - 是否可以为一个参数指定多个类型提示?

python - 在 pandas 数据框中加上等于

python - 在numpy记录数组中如何访问python中的某些元素值

python - 对 Dataframe 中的每月缩写列(Jan、Feb、Mar 等)进行排序(目前按字母顺序排序)

python - 正在定义的类型对象的类型提示

python - 使用列表/元组/等。从键入与直接将类型引用为列表/元组/等

python - 将文本转换为列

python - Jupyter: "notebook"不是 jupyter 命令

python - 如何创建包含 threading.Event 和 multiprocessing.Event 的协议(protocol)?