
typing 是在 python 3.5 才有的模块
Python 类型提示:https://cloud.tencent.com/developer/article/1864619
https://cloud.tencent.com/developer/article/1866298
https://www.cnblogs.com/poloyy/p/15153883.html
可以自定义创一个新类型
# NewType
from typing import NewType
UserId = NewType('UserId', int)
def name_by_id(user_id: UserId) -> str:
print(user_id)
UserId('user') # Fails type check
num = UserId(5) # type: int
name_by_id(42) # Fails type check
name_by_id(UserId(42)) # OK
print(type(UserId(5)))
# 输出结果
42
42
<class 'int'>可以看到 UserId 其实也是 int 类型

# 'output' is of type 'int', not 'UserId'
output = UserId(23413) + UserId(54341)
print(output)
print(type(output))
# 输出结果
77754
<class 'int'>https://cloud.tencent.com/developer/article/1866297
https://cloud.tencent.com/developer/article/1866293