在python3中,我可以使用参数和返回类型注释。示例:
class Graph:
def __init__(self, V: int, E: int, edges: list):
pass
@classmethod
def fromfile(cls, readobj: type(sys.stdin)):
pass
def V(self) -> int:
pass
def E(self) -> int:
pass
问题是我不能用当前类(Graph)的返回类型进行注释,它还没有定义。示例:
class Graph:
def reverse(self) -> Graph:
pass
这段代码会出错
def reverse(self) -> Graph:
NameError: name 'Graph' is not defined
这些批注对于记录和允许集成开发环境识别参数和返回类型=> enable autocomplete都非常有用
UPD:所以我想到的是,这要么是不可能的,要么需要一些我不喜欢的技巧,所以我决定只使用def reverse (self) -> 'Graph':
,它对于文档是可以理解的,尽管违反了规则。缺点是它不适用于IDE自动完成。
https://stackoverflow.com/questions/15853469
复制相似问题