小编Byc*_*han的帖子

类型注释:TypeVar:绑定、问题

在Python文档中,我们发现:

T = TypeVar('T')              # Can be anything
S = TypeVar('S', bound=str)   # Can be any subtype of str
A = TypeVar('A', str, bytes)  # Must be exactly str or bytes
Run Code Online (Sandbox Code Playgroud)

我们还发现了这段代码:

def repeat(x: T, n: int) -> Sequence[T]:
    """Return a list containing n references to x."""
    return [x]*n


def print_capitalized(x: S) -> S:
    """Print x capitalized, and return x."""
    print(x.capitalize())
    return x


def concatenate(x: A, y: A) -> A:
    """Add two strings or bytes objects together."""
    return …
Run Code Online (Sandbox Code Playgroud)

python types mypy python-typing

2
推荐指数
1
解决办法
108
查看次数

标签 统计

mypy ×1

python ×1

python-typing ×1

types ×1