我打算用Python function annotations来指定静态工厂方法返回值的类型.我知道这是注释的one of the desired use cases.
class Trie:
@staticmethod
def from_mapping(mapping) -> Trie:
# docstrings and initialization ommitted
trie = Trie()
return trie
PEP 3107个国家表示:
函数注释只是在编译时将任意Python表达式与函数的各个部分关联起来的一种方式.
Trie
在Python中是一个有效的表达式,不是吗?Python不同意,或者更确切地说,找不到名称:
def from_mapping(mapping) -> Trie:
NameError: name 'Trie' is not defined
值得注意的是,如果指定了基本类型(如object
或int
)或标准库类型(如collections.deque
),则不会发生此错误.
What is causing this error and how can I fix it?