Rock Solid Python with Python Typing Transcripts
Chapter: Typing in Python
Lecture: Generics Available in Python 3.12

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now coming in Python 3.12, this section here is part of that 15% when we said not 100% of the stuff is covered.
0:09 Python is going to have PEP 695 is accepted and is coming in 3.12. And basically, the name is type parameter syntax or generics or templates,
0:21 and depending on the language you're familiar with. So we scroll down a little bit, you can see some examples.
0:25 You can do that now with some pretty complicated ways. from typing import generic and type var. You can do this, is it covariant? And what's its bound?
0:35 There's a lot going on here. The new syntax would simplify this. So now your class instead of deriving from generic of T,
0:44 we just say it has bracket T for the type, much like you would say list of int, here we say class of T, and then whatever goes there,
0:54 in this case it's a string that comes out here. Maybe more relevant is a function. So here's how you would do a generic function today.
1:04 Create the variable and then use it. Here you just say it's a func of T, which takes some type and some type and whatever those two types are,
1:12 it gives you another one back. Like it takes a A which is an int and a B is an int, so you get an int or A which is a float
1:19 and a B is a float and you get a float back. Something along those lines, right? So this is a really long, complex, and deep HEP.
1:29 It looks like a lot of work for the folks that put it together. Check it out if you're interested. And it's gonna be nice to have here,
1:35 but at the same time, I don't think a ton of people are gonna be using it, maybe consuming it. It's in the type system in 3.12 coming up soon,
1:43 so put it on your radar.


Talk Python's Mastodon Michael Kennedy's Mastodon