Type-aware programming in Python! Let's use type annotations

Python

Hello. This is Tatsuno Information System Tokyo Team.
In this article."Type annotations introduced in Python 3.6Here is some information about
Suddenly, when you write a program in Python, are you aware of the "type"? (There may be a few people who say "I'm not aware of it")

In the case of dynamically typed languages such as Python, variables and other types are "sort of" made nice by the language.
It's a good thing that you can write without being aware of it, but as the size of the source code grows
"What? Is the return value of this function a list or a dictionary? Or is it a dictionary?
"Is this property of this library a string?
You'll spend a lot of time in non-coding areas like

This is where the "type annotations" introduced in Python 3.6 come in.
Although this feature is already implemented in other dynamically typed languages (e.g. PHP), Python also allows variables to be commented about their type.

  • 1. How to use the code
  • 2. Notes on using annotations
  • 3. At the end.

 

How to use the code

Let's see how to use it with actual code.

variable annotation

Variable annotations are a way to explicitly write the type of a variable in Python.
First, here is a simple Hello world code.

str1 = 'Hello world!
print(str1)

Variable str1 is a string type (str type). At this level, type annotation is not necessary, but we dare to use type annotation.

str1 :str = 'Hello world!
print(str1)

in this way Variable name :Variable typeYou can specify "which type the variable is" by writing
 

function annotation

We just added type annotations to variables, but you can also annotate function return values and arguments. (Function Annotations)

You can use it to improve your development efficiency. Especially, if you add return type annotation, you can easily check the return type on the editor such as Visual Studio Code.

▼ How to write function annotations
1. The type expected for the argument of a function is indicated by appending a colon: after the argument.
2. The type expected for the return value is indicated by an arrow-> after the closing parenthesis of the argument.

Now let's look at some examples that might be useful in practice.

import datetime
def get_todays_date() -> str:
  dt_now = datetime.datetime.now()
  date :str = dt_now.strftime("%m month %d day")
  return date

today = get_todays_date()
print("Today is", today, "is")

In the above program, it is not possible to distinguish whether the function get_todays_date returns as a datetime object or as a str type.
So, we can use function annotations and -> str you can see at a glance that the return value of get_todays_date is of type str. I think you can see it much better now.
 

Points to note when using annotations

Type annotations are useful, but there are caveats.
First, you must have Python 3.6 or higher in use. If you are unable to upgrade your system or package due to dependency issues, you cannot use this feature.
Also, unlike other languages, type annotations are implemented as comments, so even if the actual type and the type of the annotation are different, the program will still work if the syntax is correct. Therefore, there is a possibility that the misalignment will occur when refactoring. Be careful not to forget to fix the annotations.
This is a brief explanation of how to use type annotations in Python.
 

at the end

A program is like a piece of writing, it needs to be "easy to read".

Properly adding explanations to your code will not only make it easier to read, but will also make it more valuable.
Comments are the most generic method, but in this article, we introduced the Variable Annotation and Function Annotation, which are useful for explaining functions.

en_USEnglish