Neel
Neel

Reputation: 10143

Different formats of return type in Python 3

Suppose we have user defined datatype class Node. There are two functions funcA and funcB. Both the functions accept a single parameter of type Node. After some processing, they return a single value of type Node. However, the return type of funcA is Node which is obvious but funcB also returns a value of type Node that is expressed as "Node".

How does type hinting in Python3 understand the difference between type Node and string "Node"?

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

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

Upvotes: 0

Views: 27

Answers (1)

Mike Slinn
Mike Slinn

Reputation: 8403

They both work. Sometimes the type is a forward reference, which would not compile. In that case, enclosing the type in quotes allows the forward reference.

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

Upvotes: 1

Related Questions