Type annotation for A strongly and dynamically typed Python.

Type annotation for A strongly and dynamically typed Python.

Python is a dynamically-typed language. That means that variable types are dynamically set at run-time, upon assignment of a value to a variable.

For example, in

def fn(a, b):
    return a + b        

The types of?a?and?b?are not known at build-time, only when?a?and?b?are assigned values at run-time.

Hence, calling

fn("a", 1)        

somewhere in your code will not raise an exception until the code is actually executed and the function is called:

>>> fn("a", 1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str        

In Python 3, type annotations do not change this. Python is still a dynamically-typed language. Type annotations serve the following purpose:

  • Code documentation: thanks to them, a developer reading type-annotated code (his own or someone else’s) will know exactly what type each variables is supposed to be. This helps reduce bugs and exceptions and accelerate the development cycle.
  • Linting and validation: code editors and continuous integration (CI) pipelines can be configured to automatically validate type-annotated code at build-time and catch bugs before they make it to production.

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

Hassan Juma的更多文章

  • REST API

    REST API

    REST API is a software architectural style for Backend. REST = “REpresentational State Transfer”.

  • What is a child process?

    What is a child process?

    Although it may sound like something out of a parenting handbook or a psychological journal, the term child process…

  • Data Profiling with its Benefits, Best Practices & Tools

    Data Profiling with its Benefits, Best Practices & Tools

    What is the importance of data to a business? Good data is the core of most effective business decisions and…

  • Containers and Containerization

    Containers and Containerization

    What containers really are and why do we need them? Containers are a solution to the problem of how to get the software…

  • Server Monitoring

    Server Monitoring

    What do we really mean by Server Monitoring? Cloud service providers like Amazon Web Service (AWS), Google Cloud and…

  • Monitoring

    Monitoring

    Just as the heart monitor in a hospital that is making sure that a patient’s heart is beating and at the right beat…

  • Web stack debugging

    Web stack debugging

    Intro Debugging usually takes a big chunk of a software engineer’s time. The art of debugging is tough and it takes…

社区洞察

其他会员也浏览了