Master the type in Python? (When should type check be done)

Originally I wrote it in the comment of the following article, but I thought that it was too aggressive to write in the comment, so I moved it here. Mastering types with Python [Python 3.9 support]

It is a well-known article that it will only cause religious controversy.

Recently, the advantages of static typing (or the disadvantages of dynamic typing?) Have become popular, and TypeScript has become popular, and Python can also perform static type checking. However, personally (especially in dynamically typed languages, with the static type checking feature added later), I do "unnecessary typing" (in some cases, rather "types that even seem harmful"). I feel like the code is increasing.

Mastering types with Python [Python 3.9 support] In the article

def add(a, b):
    """Returns the result of adding arguments"""
    return a + b

With a mold

def add_type(a: int, b: int) -> int:
    """Returns the result of adding arguments"""
    return a + b

An example will appear.

However, in this example, I think the type should not be written ***. It is not good to specify the type unnecessarily. In this case, any type that makes sense for the operation ʻa + b` is sufficient, and I think it is a "bad" programming style to specify int. Of course, I understand that the above is just an example, but I still think this example is "inappropriate as an example".

In what cases is it inconvenient to specify> int?

After understanding that reality is not enough with ideal theory, if you talk about ideal theory,

*** Should be programmed for interfaces, not for concrete types ***

That's why.

In this example, the ʻadd function is applicable to any object that has the property (interface) that it can be added with the + operator. Of course, the specific processing performed by the + operator differs depending on each specific type (polymorphism), but the ʻadd function is programmed for the" interface "called the+operator. Therefore, I am not aware of the specific processing content of the + operator (or rather, it should not depend on the specific processing content of the + operator ***). That's why.

In this example, you shouldn't be tied to the concrete type ʻint. Then, if you want to change it to float instead of ʻint later, you have to replace all the keywords ʻint in the program with float`. Will be.

For that matter, I don't think it is necessary to narrow down the arguments of the ʻadd function to numbers. The argument of the ʻadd function only needs to define an interface called the + operator, so the ʻadd function defines the str, list, and even the add special method. There is nothing wrong with entering your own class. To be more precise, the ʻadd function should be *** should be written to work with any object that has the+operator in the first place.

In fact, these days, in the world of statically typed languages (especially in C ++ and Rust), I don't type as much as possible (let the compiler automatically type infer), and even if I dare to type, it's as generic as possible. The programming style of typing is widespread.

On the other hand, recently, I've come to see code that is typed in Python, but I often wonder why such "unnecessary typed" is done. To be honest, it feels like you're looking at "30 years old C ++ code". Are Python programmers going to start over from scratch with the last 30 years of trial and error (and the resulting paradigm shift) in the world of statically typed languages? I thought.

Writing this can quite lead to religious warfare, but in my "bias", people who want to type in Python are people who are new to Python (or C ++ 20-30 years ago). Those who are stuck with Java knowledge), but rather those who have recently moved from a statically typed language to Python, have the impression that they are reluctant to type in Python.

Recommended Posts

Master the type in Python? (When should type check be done)
Check the behavior when assigning Python
Master the type with Python [Python 3.9 compatible]
Master the weakref module in Python
Check the argument type annotation when executing a function in Python and make an error
Be careful when specifying the default argument value in Python3 series
What to do when the value type is ambiguous in Python?
Check the behavior of destructor in Python
Check if the URL exists in Python
Check if the characters are similar in Python
Investigation when import cannot be done with python
Check Python # type identity
Check if the string is a number in python
Python note: When the pip command cannot be used
Initial settings when using the foursquare API in python
When import matplotlib.pyplot cannot be done in pyenv-virtualenv environment
Check the asymptotic nature of the probability distribution in Python
I checked the reference speed when using python list, dictionary, and set type in.
When will the default arguments be bound in python? When variables are bound in closure lazy evaluation.
[python] How to check if the Key exists in the dictionary
Download the file in Python
Check the operation of Python for .NET in each environment
Find the difference in Python
Solution when the image cannot be displayed with tkinter [python]
Python --Check type of values
Check in advance what happens when you execute the command
Attention when os.mkdir in Python
Solve the Japanese problem when using the CSV module in Python.
Scripts that can be used when using bottle in Python
Check the Check button in Tkinter to allow Entry to be edited
[Python] Check the installed libraries
A simple reason why the return value of round (2.675,2) is 2.67 in python (it should be 2.68 in reality ...)
Check if the password hash generated by PHP matches in Python
I got an AttributeError when mocking the open method in python
Mode line when you open the appropriate Python code in Vim
How to check the memory size of a variable in Python
A record that GAMEBOY could not be done in Python. (PYBOY)
33 strings that should not be used as variable names in python
Timezone specification when converting a string to datetime type in python
How to check the memory size of a dictionary in Python
Jupyter Notebook 6.0.2 cannot be installed in the Python 2.7 environment created in Anaconda
Have python check if the string can be converted / converted to int
Don't forget shebang when writing Check! Ansible's Dynamic Inventory in python!
Check for memory leaks in Python
Getting the arXiv API in Python
Check for external commands in python
Precautions when using pit in Python
Function argument type definition in python
Save the binary file in Python
Hit the Sesami API in Python
Behavior when listing in Python heapq
Get the desktop path in Python
Dynamically load json type in python
Get the script path in Python
In the python command python points to python3.8
Implement the Singleton pattern in Python
Type specified in python. Throw exceptions
Check and move directories in Python
[Python] Be careful when using print
Hit the web API in Python
Check the data summary in CASTable