关于@overload装饰器的几点理解

关于@overload装饰器的几点理解 以下代码来自typing.py中的官方注释代码 overload def utf8(value: None) - None: ... overload def utf8(value: bytes) - bytes: ... overload def utf8(value: str) - bytes: ... def utf8(value): ... # implementation goes here 1针对上面这段代码出现utf-8这个名称其主要表示的是函数名称。 2这段代码中utf-8这个函数被多次定义表明是使用到了函数重载这个语法python中利用overload装饰器来进行函数重载。 3函数重载的类型说明通常写在.pyi文件中也可以和函数定义一起写在.py文件中。