
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
https://cloud.tencent.com/developer/article/1866296
https://cloud.tencent.com/developer/article/1866297

# 可以是任意类型
T = TypeVar('T')
def test(name: T) -> T:
print(name)
return name
test(11)
test("aa")
# 输出结果
11
aa# 可以是 int,也可以是 str 类型
AA = TypeVar('AA', int, str)
num1: AA = 1
num2: AA = "123"
print(num1, num2)
num3: AA = []
# 输出结果
1 123
暂时没搞懂这个有什么用,先不管了
# 自定义泛型
from typing import Generic
T = TypeVar('T')
class UserInfo(Generic[T]): # 继承Generic[T],UserInfo[T]也就是有效类型
def __init__(self, v: T):
self.v = v
def get(self):
return self.v
l = UserInfo("小菠萝")
print(l.get())
# 输出结果
小菠萝