Understanding type annotation in Python

Understanding type annotation in Python

Why do we need type hints in Python?

We can annotate (comment) variables and functions with data types. Being a dynamically typed language, the datatype of a variable in Python is determined at runtime. As a python developer, you no longer need to declare the data type of the variable. Being so flexible also introduces some inconveniences, like errors detected at runtime that could have been avoided at the development time.

So why do we need it?

Type hints allow developers to stick to the data type the variable or the function was expecting.?

The Developer can use a linter or a tool (like mypy, pytype) to perform type check, and provide hints or warnings when these types are used incorrectly, and prevent unwanted errors on later stages. Plus, the code will be slowly documented and more readable.?

So how do I add type hints to variables?

variable_name: type = value

This will declare a variable name of a type and assign it to a value. You can do it the right way like:

name: string= "Lisa", but in case you do it the wrong way

name: int = "Lisa"

python interpreter won’t trigger an error, but a type checker like mypy will flag this as an error.


Can I add type hints to functions?

Yes.

def my_function(language: str, version: float) -> str:

The function now has type hint showing that it receives str and float arguments, and returns str.


Can there be more than one type?

Yes.

def view_type(num: str | int) -> None:

We can add union | to show that the parameter num is either str or int.


Can type hints be added to dictionaries?

Yes.

grades: dict[str, int] = { "Homer": 100, "Marge": 90}

And tuples?

Yes.

student: tuple[str, int] = ("Bart", 18)


Type hinting in python can be very useful, it can give developers a way to make their code readable,? self-described, and can reduce heavy headaches when searching for bugs.?

要查看或添加评论,请登录

Orsan Awawdi的更多文章

  • Docker Disk Space Management

    Docker Disk Space Management

    It is necessary to regularly check the disk space occupied by Docker to ensure efficient resource management and…

  • Code Review

    Code Review

    Code Review is a sensitive matter. It introduces the code you wrote to the eyes of another person, who has their own…

  • re.findall

    re.findall

    When we talk about finding recurrent text in a string, we think about regex (Regular Expressions). Regex has so many…

    1 条评论
  • AI generated code

    AI generated code

    Should we always listen to AI generated code? I asked BLACKBOX.AI to write me a simple code in #python.

  • Environment variables

    Environment variables

    Environment variables are variables that store data in your program but outside your code. For example, key and secret…

    1 条评论
  • Nested Repeaters

    Nested Repeaters

    Let's take an example. We have Categories table in our DB, and each Category has multiple subcategories.

  • Process transcript with Python

    Process transcript with Python

    Let's see what is the most popular word from Donald Trump speech. Transcript of the speech can be found when googling…

  • Three options to filter a list in C#

    Three options to filter a list in C#

    I will show here three different ways to filter text by some string criteria in C#. We have a public class called…

  • Validation using Attributes in C#

    Validation using Attributes in C#

    Validating data entered by user can be done via multiple methods. Attributes is one powerful yet simple way to validate…

社区洞察

其他会员也浏览了