Python 3.12 Generics in a Nutshell

preview_player
Показать описание
Generics in Python 3.12 can transform your code by allowing classes to be used with different data types, enhancing flexibility, and catching bugs early. In this video, I'll dive into generic classes and subclasses and show you how they can streamline your development process.

🎓 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
- Kit Hygh
- Alexander Milden
- Bean

🔖 Chapters:
0:00 Intro
Chapters:
1:09 Generic classes
3:25 Subclassing Generic Classes
5:34 Conclusion
5:54 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!
Рекомендации по теме
Комментарии
Автор

after many years working as python developer, I must say that dynamic typing was a mistake. We wen't full circle and now type annotating our code to make it workable. I wish we just added static typing to python in the end, could make python faster.

ponysmallhorse
Автор

I just have an impression that implementing all those type-features will make Python even more complex than C++ with far less performance, and still it won't be type-safe. But anyway, this is interesting. Maybe there are benefits I can't see now. I started to use type hints quite long time ago, which makes the code easier to understand, especially with support with IDEs which supports that. Anyway, very cool video.

ravm
Автор

Hey, arjan. Can you make a tutorial on how to use logging in production, what are dos' and dont's when logging in work environment, what function to log. how will it go with async, a tutorial on a real world example? I have seen the logging video on your channel, it is good, but some serious knowledge and real world use case will be helpful to understand the actual use.
Make a tutorial when you are free and willing . Thank you.

tikendraw
Автор

Generics is great, I use them a lot in typescript 🎉

rockNbrain
Автор

Good video! It's a shame that this syntax hasn't been there since previous versions, it's much more readable.

diegol_
Автор

Looks like Arjan is the new Knuth...good stuff.

daviddelaney
Автор

I think we are getting to the point where statically-compiled languages with type-inferencing are starting to look more GvR Pythonic than modern Python. Anybody remember the days when Django was the villain because they were writing Python as if it were Java?

herberttlbd
Автор

After your first video about Generics i started to use it in my project

IvanIvanov-dksm
Автор

What a Great video! new Python's Generics are so similar to golang's Generics

heinereniscaicedo
Автор

I think Python's dynamic type system is its selling point. It allows for pretty elegant code, ex. when programming dynamic plugin systems. But the type system is extremely limited for anything beyond standard linting. Coming from Scala and Rust, I was even surprised to see how the syntax knows all about covariance and contravariance types.

TJ-hsqm
Автор

Interesting items in python i encounter

pydantic - awesome for json validation
Langgraph, langchain - do pretty well for api abstraction their pydantic parser is pretty good
Sqlglot - pretty interesting capabilities for transpiling and inspecting abstract syntax trees
NamedTuple - simple class to clean up access
Dotenv - easy way to get variables
Sqlite - built in DB which obeys the api pattern
Sqlalchemy - an ORM which can be nice
Mypy - static type checker

Then i would like to point that a module with functions is exactly equivalent in access and calls for a class without any data

I think finally, TDD (test driven development) will play a MASSIVE role in Artificial Intelligence and Machine Learning based features. Theres absolutely no way around it... i think😅

Great vid as always!

ravenecho
Автор

I used it (in the old py10 syntax) to create a Pydantic collection type, that puts the return list of values into an attribute called "data"
Why? I found that a lot of json parsers don't like to get a list as a reply - they expect a json objet. This solves the need to create a custom object for every time I want to return a list of multiple object

guruyaya
Автор

Well I come from the Java world and adding a bit more type checking always feels better for me so this feature is important.

It's always a bit wierd that type errors in python are only actually warnings for your IDE.

berdeter
Автор

yeah, another python type hinting which we will use only for ide :D so cool

evgeniy_toropchin
Автор

The best case of using generics for me was my own implementation of a B+Tree. I think the next step in python must be an implementation kind of a standart library like C++ has.

tulliolevichivita
Автор

This is ok when one uses a basic type like int, float, etc. But what if I want to build, let's say a Stack[myCustomObject]? What do I have to implement in the class? I assume __eq_, at least. Probably iterator as well?

orlanino
Автор

I prefer to use SingleDispatch. However, Python still doesn't support MultipleDispatch natively (have to pip install another package), it'll be great if they could include that in the next version.

ButchCassidyAndSundanceKid
Автор

Are there series about functional programming ?

wziskti
Автор

The Python community is giving low priority to supporting generic variables a.k.a. Higher Kinded Types. It is currently impossible to annotate an operation that keeps a parametric container as is, but changes the type it contains (the package "returns" offers this functionality but you need to add a plugin to the mypy checker). They (Guido Van Rossum himself!) say that it is a lot of work and it is not worth it. I was surprised to read that because even simple operations cannot be fully specified in the annotations. What is your take? Should operations like that be avoided in the first place? Will they ever support them?

kiraleskirales
Автор

Unfortunately, I cannot use generics like this at the moment. I would love to, though. My company supports only Python 3.9 and it'll be five years - I guess - before I can use Python 3.12 😅

twelvethis