paint-brush
Type Annotation In Pythonby@h3avren
8,276 reads
8,276 reads

Type Annotation In Python

by Ajay Singh RanaFebruary 1st, 2022
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Type annotations are used to indicate the types of variables and inputs/outputs of functions and methods in Python. Type annotations have a straight-forward syntax i.e. `variable_name : data_type` These are associated with variables or function/method arguments to make the code more readable for ourselves and the user**. These expressions do not have any meaning attached with them and these are **ignored by the interpreter**** These are never gonna raise an error if the given variable or argument doesn't have the correct type cause these are just ***hinters*** def sum(num_1 : int,num_2 : int = 2) -> int: return num_1 + num_2

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Type Annotation In Python
Ajay Singh Rana HackerNoon profile picture

What is Type Annotation..?

Type Annotations or type hints in Python (or any other language) are used to indicate the data types of variables and inputs/outputs of functions and methods.

These are expressions that are associated with variables or function/method arguments to make the code more readable for ourselves and the user, as these expressions do not have any meaning attached to them and are ignored by the interpreter.

What is the syntax of Type Annotations..?

Type annotations have a straight-forward syntax i.e. variable_name : data_type. For example:

    name : str = 'Rohan'`
    age : int = 19


And for functions the syntax is:

    def function_name(argument_1 : data_type,argument_2 : data_type) -> return_type:
        function body


For example:

    def sum(num_1 : int,num_2 : int = 3) -> int:    # here 3 is the default value for the argument num_2
        return num_1 + num_2

How is this useful..?

First of all type annotations make the code more readable, as, even though these don't enforce the type on the variables and functions these are a quick way to validate the actual type of the variables or arguments that are being passed to the functions that cause a wrong type may lead to an error.


Type annotations are never gonna raise an error if the given variable or argument doesn't have the correct type cause these are just hinters and aren't enforced types.


Type annotations are used by code linters and if using an IDE will always highlight the arguments if these aren't of the correct type.


There are third-party libraries like mypy which can be used for static type checking.


Annotations can also be accessed using function_name.__annotations__ i.e.:

    def sum(num_1 : int,num_2 : int = 2) -> int:
        return num_1 + num_2
   
    sum.__annotations__    # accessing the __annotations__ attribute of the function


Output:

    {'num_1': <class 'int'>, 'num_2': <class 'int'>, 'return': <class 'int'>}


Or taking a help on the function would yield a similar result too:

    def sum(num_1 : int,num_2 : int = 2) -> int:
        '''
        A function that takes two integers as input and returns their sum
        '''
        return num_1 + num_2


Now, if we do help(sum) it will result in:

    sum(num_1: int, num_2: int = 2) -> int
        A function that takes two integers as input and returns their sum


So, we could also say that type annotations are useful in documentation too.


For more information visit:


Conclusion

We learned about type annotations or now that we know what these are it would be better to call these types of hints. Alongside all other benefits, these can also come in handy when debugging functions working based on the input arguments passed to it. Moreover, the fact that these are also used for documentation purposes it is a good habit to use these as it will not only be good for us to read through or get a help documentation but also for others to all those who are gonna use our code in the future.