我们可以通过咨询找到这些信息
typeshed
具体来说,如果我们看一下
weakref
module
ref
从
_weakref
module
. 从那里,我们看到了
定义为等同于
ReferenceType
威克雷夫
).
my_list
变量类型提示如下所示:
from __future__ import annotations
from typing import List
from weakref import ref, ReferenceType
# ...snip...
my_list: List[ReferenceType[MyObject]] = [...]
from __future__ import annotations
from typing import List
from weakref import ref
# ...snip...
my_list: List[ref[MyObject]] = [...]
基本上,
裁判
所以我们可以互换使用这两种类型。
,但这主要是因为我太习惯用大写字母开头的打字了(或者,如果类型提示开始变得太冗长,我可能会定义一个自定义类型别名
Ref = ReferenceType
).
请注意
from __future__ import annotations
from typing import List
from weakref import ref
# ...snip...
my_list: "List[ReferenceType[MyObject]]" = [...]
# Or:
my_list: List["ReferenceType[MyObject]"] = [...]