Python 3.12 Generic Types Explained

Python 3.12 Generic Types Explained

ArjanCodes

11 месяцев назад

62,729 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@jakobullmann7586
@jakobullmann7586 - 18.11.2023 19:22

Is that a Durgod Taurus?

Ответить
@dankprole7884
@dankprole7884 - 19.11.2023 00:57

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.

Ответить
@sebastian8436
@sebastian8436 - 19.11.2023 11:29

Python is becoming more and more like Java😂

Ответить
@thomaseb97
@thomaseb97 - 19.11.2023 15:51

dynamic typing was a mistake imo, it causes more issues than it solves, i prefer enforced static typing, so going from that generics is atleast a step up for me

Ответить
@tedlazar
@tedlazar - 19.11.2023 17:08

You can achieve the same by using list instead of list[T]

Ответить
@PippyPappyPatterson
@PippyPappyPatterson - 19.11.2023 23:18

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

Ответить
@QuintinMassey
@QuintinMassey - 20.11.2023 13:20

I wonder what exactly is happening underneath to accommodate generic types. If int and strings are different sizes in memory, then what is the interpreter doing to the memory to make that work.

Ответить
@liquiddddd
@liquiddddd - 20.11.2023 14:25

Here comes a big rant about this craze that is type-hints in python:

This video made it clear to me that we absolutely do not need Generic Types. Every single thing Arjan tells when comparing 'Any' with 'TypeVar' sounds like: Any is absolutely fine and 'TypeVar' is yet another an extremely NON-pythonic way of formalizing something that simply doesn't need to be formalized like this.

The wave of bulshitting that has been hitting python since type hints were introduced is mind-blowing. Protocols, by the way, goes along the same lines.

It almost feel like too many core Java developers joined the core development of python and are now breaking everything in name of typing. And to be honest: fair enough. That wouldn't be a big deal if not every single major 'python influencer' out there (like this one) wouldn't jumping on this bandwagon, and pushing their viewers to do the same.

From day one python says that typing is totally optional, and indeed there's nothing RIGHT NOW that makes it mandatory. Except it is not true: when every major media (trainings, YT videos, books, podcasts, articles, etc) start presenting type-hints as the ultimate wonder in python, the collective understanding artificially changes to: "ok, this is the thing now and we gotta do it, or else...", and then it becomes a 'de facto' rule and 100% expected best practice.

Try going to any Big Tech interview today and explicitly saying that you don't use type-hints and think they are silly in python. It will be an instant drop from the process.

To me it is very clear that sometime in the next 10 years (maybe even before we get to 2030), python will flip to a dumb-hard-coded-non-optional-typed language.

Ответить
@pawematysiak3641
@pawematysiak3641 - 20.11.2023 14:28

These are long overdue.

Ответить
@liquiddddd
@liquiddddd - 20.11.2023 15:00

Why in the name of God, does a class named 'MyClass' need a Generic type? Objects of that class would be of type 'MyClass'. End of story. What am I missing here?

Ответить
@AspartameBoy
@AspartameBoy - 20.11.2023 15:50

A…. Ah…Why worry about type at all? Just process the input as a list? Unless using cython, typing is not a good thing.. and by making the type generic you surrender the whole premise

Ответить
@bramble-east
@bramble-east - 20.11.2023 17:45

Box[T] is one of the ways to solve type variance issues when working with generics, when T is used as both input and output of a generic function. So I would not call the Box[T] useless, more like "niche".

Ответить
@danielschmider5069
@danielschmider5069 - 20.11.2023 18:06

I feel like Pycharm does most of this automatically, without having to use typehints. Why are you using vscode with pylance if it is buggy?

Ответить
@MateHegyhati
@MateHegyhati - 21.11.2023 15:23

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.

Ответить
@VincentSaelzler
@VincentSaelzler - 21.11.2023 16:21

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.

Ответить
@grzegorzbajson3897
@grzegorzbajson3897 - 21.11.2023 16:38

@ArjanCodes Could you tell which plugin do you use for AI assistance, please?

Ответить
@tigidou3344
@tigidou3344 - 23.11.2023 20:21

I don't understand people who want put type in python code.

Ответить
@georgikostov6982
@georgikostov6982 - 23.11.2023 22:08

Very good content - thank you!

Ответить
@couldntfindafreename
@couldntfindafreename - 25.11.2023 17:18

C# without braces and without the performance advantages.

Ответить
@dinartd
@dinartd - 25.11.2023 23:06

Is this autocompletion of the code done by CoPilot or is it another auto complete extension?

Ответить
@mahanaim134
@mahanaim134 - 26.11.2023 15:17

downvoted due to ho$tinger recommendation

Ответить
@jan.kowalski
@jan.kowalski - 27.11.2023 03:33

It's nice that in Python types are not a religious beings but just another form of communication. I give as an example usage of types in Pydantic and FastAPI where using them, is just a natural way of solution creation process and in return you will have a free validation, value coercion and many more niceties. I do agree with you, that in Python types are opportunistic. Opportunistic is good in programming.

Ответить
@0xomara482
@0xomara482 - 03.12.2023 10:22

Shame I can't use this in any library code for at least a year or two.

Ответить
@anton-r
@anton-r - 04.12.2023 09:32

first of all tnx. for your effort !
Why use such complicated structures ? with this generic etc ? i do understand and i do use the small type annotation str int etc. but this ?! why ?! this is python !!! if you like to use strong leng use kotlin or ++ what ever ? but this is python is dynamic leng , this is point of this leng.

Ответить
@georgehammond867
@georgehammond867 - 05.12.2023 18:59

python3 is looking more alike C/CPP.!

Ответить
@aztecGin
@aztecGin - 09.12.2023 13:44

Thank you Arjan, I wanted to spend my Saturday with clean the house and do some sports. Now I'll code instead. You always make me willing to work on my pet projekt :D

Ответить
@MrAlex1601
@MrAlex1601 - 10.12.2023 11:40

Nice video as always @arjan.
Since you‘re a type junkie - how do you think about mypy? Maybe it‘s worth making a video about it? :)

Ответить
@chiemerieokoro3038
@chiemerieokoro3038 - 18.12.2023 17:51

Please, can you cover descriptors?

Ответить
@IvanIvanov-dk6sm
@IvanIvanov-dk6sm - 24.01.2024 00:04

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 !

Ответить
@Geza_Molnar_
@Geza_Molnar_ - 30.01.2024 23:39

Please, be a type junkie! (and doc junkie, too) Good explanation, that's the reason I come back regularly. Although, this time, it was tooo muuuch aaad.

Ответить
@mrvincefox
@mrvincefox - 17.02.2024 03:51

All I heard is click clack clack, bash bash, clack clack. Mech keyboard are dumb

Ответить
@marouanebenmoussa1289
@marouanebenmoussa1289 - 20.02.2024 14:27

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

Ответить
@andrews8733
@andrews8733 - 22.02.2024 23:09

So generic classes are covariant by default? Can you define invariance/contravariance with the new syntax?

Ответить
@oida10000
@oida10000 - 23.02.2024 14:13

Wouldn't Any not be a more usefull anonation for something like elements at even/odd indicies? Because for me list[T] would indicated, it does not matter what the type is but it must be consistent, but what if it isn't, f.e. you have ints, floats and strings. I know Python allows piping up list[Any] seems much cleaner then list[int|float|str|...]?

Ответить
@HappySlapperKid
@HappySlapperKid - 23.02.2024 19:16

Omg the sound of your keyboard I hate it

Ответить
@wizardfix
@wizardfix - 06.03.2024 16:27

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

Ответить
@goncalorodrigues1964
@goncalorodrigues1964 - 06.04.2024 18:22

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.

Ответить
@vxsniffer
@vxsniffer - 21.04.2024 01:38

no doubt, your Python vids rises up YT's average level ;-)

Ответить
@blanky_nap
@blanky_nap - 07.06.2024 11:56

I finally understood genetics in python after I started learning Rust

Ответить
@ApprendreSansNecessite
@ApprendreSansNecessite - 23.06.2024 14:50

I'm not conviced by the tuple notation when adding a union of type bounds to a generic. Why a different syntax when you already have unions? It feels like types in Python are still an afterthought.

I am also surprised by the comments which are against typing altogether. I hope the most important community packages support types

Ответить
@alexanderoransky7601
@alexanderoransky7601 - 30.06.2024 01:37

It blows my mind how many years it is taking to get the type hinting right and only for what? To keep linters happy. Not to mention that it goes against the core idea- make a dynamic language that is easy to learn and use. If you want types- use any other modern language that was designed for it from the beginning.

Ответить
@viddeshk8020
@viddeshk8020 - 05.08.2024 20:41

I see generic type every where. Strong type settings are indeed important.

Ответить
@Padancyo
@Padancyo - 15.08.2024 13:16

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.

Ответить