python中具有多种可能性的类型提示



对于多种可能的输入,什么是最好的类型提示方式?

例如:

def example(input1):
    return input1

如果input1是,例如,10个可能的类之一,使用Union还是最好的,或者有更好的方法?

恒等函数的类型通常为:

from typing import TypeVar
T = TypeVar('T')

def identity(x:T) -> T:
    return x
    

最新更新