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