Python`s myths
I often hear from PHP or C++ developers that python hasn`t types. It is not correct.
The wikipedia says:
“Smalltalk, Ruby, Python, and Self are all "strongly typed" in the sense that typing errors are prevented at runtime and they do little implicit type conversion, but these languages make no use of static type checking: the compiler does not check or enforce type constraint rules. The term duck typing is now used to describe the dynamic typing paradigm used by the languages in this group.”
So, you can't use 'str' + 1, you will give “TypeError: can only concatenate str (not "int") to str”.
Python is flexible and if we WANT we can add type casts using dunder methods (if you don't know what and why you are doing don`t ever use this).
领英推荐
Other things that you may want to know are type annotations. Since python3.9 it works fine.
The code editor shows warnings, but annotations are not checked at runtime.? Is this a problem?
No. We can use linters to prevent type misusing, as well as many other errors. This behavior is similar to compiling code. You can add `pylint my_progect` to the CI/CD and fail to deploy if code contains problems. Of course you can disable some warnings. For example Django requires you to disable a couple checks because Django can't check code correctly in some cases (please avoid Django if you can).
Python has many reasons to use duck typing. This makes python easy to write fast, short code. For small and medium projects and prototypes maybe python is the best choice. But if you want to write enterprise code - you still can use python with lints and type annotations.