gpt4 book ai didi

Python 类型提示联合

转载 作者:行者123 更新时间:2023-12-05 01:38:10 24 4
gpt4 key购买 nike

我有一个带有两个参数的函数:

def same_type_params(param1: Union[str, int], param2: Union[str, int]):
pass

如何限制 param1param2 的类型相等?即 strint

最佳答案

使用 type variable :

from typing import TypeVar
T = TypeVar('T', str, int)
def same_type_params(param1: T, param2: T) -> None:
pass

关于Python 类型提示联合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60011709/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com