Python 3.12 Generic Types Explained

preview_player
Показать описание
✅ Hostinger code for 10% off: ARJANCODES

In this video, I’m going to explore how generic types in Python 3.12 work, and what the advantage is over just using the Any types.

🎓 Courses:

👍 If you enjoyed this content, give this video a like. If you want to watch more of my upcoming videos, consider subscribing to my channel!

Social channels:

👀 Code reviewers:
- Yoriz
- Ryan Laursen
- Dale Hagglund

🔖 Chapters:
0:00 Intro
0:22 Generic Types
5:41 The Any Type
7:57 Old vs new generic type syntax
12:16 Upper bounds
16:02 Advantages of Using Generic Types over Any
17:58 Outro

#arjancodes #softwaredesign #python

DISCLAIMER - The links in this description might be affiliate links. If you purchase a product or service through one of those links, I may receive a small commission. There is no additional charge to you. Thanks for supporting my channel so I can continue to provide you with free content each week!
Рекомендации по теме
Комментарии
Автор

'Generic type' was my nickname in highschool

benizakson
Автор

Where the generic type variables shine is, for example, when defining higher-order functions, or functions that return "higher-order" types, such as generators; programming languages that allow you to specify the relations between types in these situations are few. In larger code bases, generic types are a kind of documentation of what to expect from an interface. I love them.

Padancyo
Автор

Python team following Go typing system and I AM LOVING IT, we can get GO and Python to work the same way LETS GOO

marouanebenmoussa
Автор

I was googling 3 days to understand how to create properly custom types. How to type hint my function/class for using particular class or its subclasses (ancestors). Because type[Baseclass] is not what i really need. And thanks to the video, now i understand that i have to create my custom type with TypeVar and bounding arg to limit the ancestors. Thanks !

IvanIvanov-dksm
Автор

This is a very clear and useful introduction to Python 3.12 typing. It has primed me to dive into the docs. Thank you!

wizardfix
Автор

Thanks for the update, this change is welcome. Also, Python starts to look more and more like C++ 😀One note: in the video you often say "you can" or "you can't", or something along those lines. If I'm not mistaken, you always "can", as annotations are still just for the IDE + SCA not for runtime. But that is maybe not as clear for people learning the language. (And I assume your channel rightfully attracts many beginners) So maybe clarifying that in each typing related video independently from precious ones could be beneficial. Just a humble suggestion, and thank you for the update again.

MateHegyhati
Автор

Thank you for the excellent presentation and professionalism. You are the best IT blogger on YouTube!

uszr
Автор

you've finally clearly and easily explained to me what generic is, God bless you

illiaavdiienko
Автор

This is great. If possible, I would like to see more advanced concepts introduced in videos like this, eg, connect it to type theory if possible :)

lukekurlandski
Автор

Can you make a continuation of this video about TypeVarTuples and paramspec? I had a little more trouble understanding these.

munzutai
Автор

Great video, thanks! It looks like inheritance in this case means "uses an X" instead of "is an X"? Can't wait to use the new syntax once all my dependencies catch up to 3.12.

dankprole
Автор

Fun to see how similar scala 3 and python syntax are getting. 😂 Thnxs for the clear explanation

prinsniels
Автор

Interesting that they didn't use [V: Car | Boat] syntax, since it's more similar to the way you specify multiple types that are valid for function parameters.

quillaja
Автор

I started using type annotations less than a year ago (despite using python since the last millennium), and I LOVE IT. To be honest, one of the reasons I love it is that type annotations do not force me to behave, but it makes it so useful to behave. And generics are phantastics: It is simply fun to carry a concept to the meta-level.
For an educational plotting tool which I've written in the last days (called schulplots), I first forgot to turn on type checks in my IDE. Bad idea. I had so many stupid errors -- I've almost forgotten how cumbersome it is to write non-trivial code without type checking turned on.
Just today, I stumbled into a situation where I was almost forced to turn off type checks in my IDE: I derived a class from a dataclass, and pylance did not understand that the derived class had all the stuff which the parent also had. But there came the @dataclass_transform decorator (which is available in Python 3.11, but you can quite easily mock it for older versions on import errors). And voila: Nothing red in my IDE.

hansaschauer
Автор

The trap I fall into with generic functions is when two object types need to be processed SLIGHTLY differently. That results in small, hard-to-find sections of code within the generic function having to perform runtime type checking of "if type is A then do X, if type is B then do Y". At that point I start wondering if two separate functions would be more clear, even if the code of them is mostly identical.

VincentSaelzler
Автор

Does anyone know what is the shortcut to trigger the type import hint at 3:01 ? Thanks in advance! 🙏

Edit: Closest I can get is `Ctrl/Cmd + .`, but that still looks different 😅

macong
Автор

I finally understood genetics in python after I started learning Rust

blanky_nap
Автор

That generic type upper bound pattern is so cumbersome. Protocols/interfaces/traits all do that in a way cleaner way.

PippyPappyPatterson
Автор

The real upshot of generics in python: if you are stuck with using python, then they are useful. But if you can choose any other programming language, then choose a properly statically typed programming language, like Haskell say.

goncalorodrigues
Автор

Watching this I bounce back and forth between "I am a bad dev because I don't write such clean code" and "All this to make some squiggly lines go away..."

d_b_