The BIGGEST Misconception About Type Hints In Python Explained

preview_player
Показать описание
Throughout the web I've noticed a lot of people that have been using type hints thinking that it will achieve what "compiled languages" get in terms of static type checking and performance boosts. This video aims to try to address that misconception, and also gives a light introduction to how to actually use type hints in Python.

▶ Become job-ready with Python:

▶ Follow me on Instagram:
Рекомендации по теме
Комментарии
Автор

For those of us coming from strongly typed languages, type hinting is a nice little security blanket.

luckypjn
Автор

Personally I only use type hints in function definitions because it helps with documentation. Not having to do that for every variable is like half the reason I’m using python in the first place 😅

rd
Автор

It really helps with auto-completion when dealing with custom modules and classes. I have been leaning heavily on this of late.

JSDudeca
Автор

One thing that I do since type hints are ment to improve readability and IDE workflow is only using them if the type is not explicit, either for the user or the IDE, like return types after calling a function. This means that, for me, creating variables using the primitive data types or using the class constructor don't really need type hinting because it's obvious.

For example:
banana = Fruit()

In this case, both the IDE and the user know that banana is a Fruit, so adding annotations wouldn't do anything or make your code just more verbose.

That's my opinion though, what you guys think about it?

caiolaytynher
Автор

I sometimes wish there was a strict python debug mode that automatically asserted that your objects in fact conformed to your type hints at runtime. In my opinion, debugging python code in larger projects can be hard at times.

KetilK
Автор

All developers that have been writing dynamic code for years have been snorting that copium, Python couldn't last too long without type hints, proving once again that dynamic programming is not to be worshipped. It's nice to have every now and then, but definitely not for full systems, without any sort of type information. Same reason why we also got TS on top of JS

AlFasGD
Автор

And it's a good habit to have for when you have to learn\switch to strongly typed language. Learning something like TypeScript or Dart is so much easier if you practice sound typing in Python.

GeorgeNoiseless
Автор

Thanks for the video! In my case, I am using type annotations super often (at least in all function definitions) + mark type hint warning as error in the IDE ; it saved a terrific number of hours (not to mention forcing myself to improve my interfaces).

thomasricatte
Автор

I like using type hints and return type in function definition. It makes reading the code, writing documentation and debugging easier.

bozok
Автор

Your voice is relaxing man, love to hear you teach forever, please give some advanced topics as well, not all of us are beginners

DaryaIbrahim
Автор

Worth mentioning that I've found type hinting plays very nicely with tools such as GitHub Copilot. This massively speeds up development.
I do wonder if cPython or pypy interpreters, for example, have any optimisations, or might get some in the future, where type hints improve JIT compilation speeds.

DrGreenGiant
Автор

It's also great adapting this habit if you are learning non-dynamic programming languages!

adiveler
Автор

Type hints are wonderful for documentation, absolutely love this.

JT-mrdb
Автор

2:30 this will cause an error in python 3.8, and not just for typings, it will break the whole code, and the fix is: from typing import List and use it with capital L. Same with Dict and Tuple. Backwards compatibility makes life easier for people with low storage space / slow internet speed that already have dependencies installed with a lower python version.

fluffy-cat
Автор

I also like those typehints, and I think you missed that they're helping also for overloading functions if I'm correct.
Coming from C++, I appreciate types anyway 😉

nilshamacher
Автор

3:11 Guido has hinted that there are no current plans to use type hints to speed up the interpreter, but that it’s something they’re going to look into down the road (5, 10 years time). So it’s not crazy to think that the hint could speed up the code when it’s running one day

allo
Автор

The "typing" module includes broader and slightly better options for "typehints".

Andrumen
Автор

had a good laugh at your int initialiser reaction :D:D

porzo
Автор

Very interesting. Almost as interesting as Copenhagen, where I was over the weekend. Thank you. ☺

castlecodersltd
Автор

I hope the type hints will be used to facilitate the just-in-time compilation. It looks so reasonable.

MingwaiTam