繁体   English   中英

使用 Python 3.10 键入注释

[英]Type annotations with Python 3.10

我试图了解 Python 3.10 的类型注释。 请考虑以下功能:

def A(n: int):
    return [k for k in range(n+1)]   
    
def B(n: int):
    return B(n - 1) + [n] if n > 0 else [0]

VSCode 中显示的类型提示有:

(function) A: (n: int) -> list[int]   
(function) B: (n: int) -> (Any | list[int])

为什么在这两种情况下返回类型都不是 list[int] ?

更具体地说:我如何编写递归变体 B 以仅获取类型 list[int] 作为返回类型?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM