Python 3 type check not works with use typing module?

后端 未结 5 1116
时光取名叫无心
时光取名叫无心 2021-01-13 11:19

Why does type checking not work in Python 3?

I have done the following code with type checks or hints:

import typing

def hello(message: str):
    pr         


        
5条回答
  •  暖寄归人
    2021-01-13 11:48

    Type hints are just hints, they should tell users what the function expects not what it requires. This is explicitly mentioned in the PEP that introduced them: PEP 3107:

    Fundamentals of Function Annotations

    Before launching into a discussion of the precise ins and outs of Python 3.0's function annotations, let's first talk broadly about what annotations are and are not:

    • Function annotations, both for parameters and return values, are completely optional.

    • Function annotations are nothing more than a way of associating arbitrary Python expressions with various parts of a function at compile-time.

      By itself, Python does not attach any particular meaning or significance to annotations. [...]

提交回复
热议问题