Function Annotation

There are many uses cases for using function annotation in Python and one of it is to let IDEs show what types a function expects and returns which generally used in online coding test, so it is necessary to understand this simple syntax.

More on this are available on the Python Official documentation.

Example 1:

def func(a: int, b: int) -> int:
  return a+b

The above example shows an a function func that expect 2 input arguments of a and b with a class datatype of int which will give an return an output with class datatype int.

Example 2:

class ListNode:
  def __init__(self, x):
    self.val = x
    self.next = None

class Solution:
  def reverseList(self, head: ListNode) -> ListNode:

The above example shows an a function reverseList that expect 1 input argument with a class datatype of ListNode which will give an return an output with class datatype ListNode.

Previous
Next