在 Python 中输入不同格式的 return 3

Different formats of return type in Python 3

假设我们有用户定义的数据类型class Node。有两个函数 funcAfuncB。 这两个函数都接受类型为 Node 的单个参数。经过一些处理后,它们 return 类型为 Node 的单个值。但是,funcA 的 return 类型是 Node,这是显而易见的,但 funcB 也是 return 类型 Node 的值,表示为 "Node".

Python3 中的类型提示如何理解类型 Node 和字符串 "Node" 之间的区别?

def funcA (input_node:Node) -> Node
 ..

def funcA (input_node:Node) -> "Node"
 ..

它们都有效。有时类型是前向引用,不会编译。在这种情况下,将类型括在引号中允许向前引用。

https://www.python.org/dev/peps/pep-0484/#forward-references