Why You Should Consider Using Python Type Hints

Improve your code without impact your performance.

Fernando Souza
Vacatronics

--

Photo by Sangga Rima Roman Selia on Unsplash.

When I started to learn programming, I used C as the main language. Declaring the actual type of the variable you will use was mandatory, and the compiler would complain a lot if you forget. Changing to C++ and Java later didn’t change anything. I still needed to know which type I was going to use, and never change again.

These languages are static-typed.

But then I discovered Python. Now I don’t have to worry about typing anymore. The interpreter knows what type you’re trying to create and will allocate the correct amount of memory to it. And then just reference this new object to your variable.

It’s a dynamically-typed language. Python does not care about the type of your variable until the moment it has to use it.

Note: Python is also strongly typed, which means an object cannot change type. What we are changing is what reference we are holding with our variable.

Another concept of in typing for Python is what it is called duck typing. It means that it doesn’t actually matter what type the data is, only if we can do something with it.

If it walks like a duck and it quacks like a duck, then it must…

--

--

Fernando Souza
Vacatronics

Enthusiast of programming, electronics, technology and beer, not necessarily in that order. BuyMeACoffee: buymeacoffee.com/ustropo