Python 3.10's new type hinting features

Поделиться
HTML-код
  • Опубликовано: 3 янв 2025

Комментарии • 121

  • @Mutual_Information
    @Mutual_Information 3 года назад +148

    This channel is my only hope of staying up to date with recent Python updates. Fortunately it’s working.

    • @mCoding
      @mCoding  3 года назад +48

      *Me, preparing new in 3.11 videos already*

    • @godfather7339
      @godfather7339 3 года назад +5

      @@mCoding please keep up this awesome, good work.

    • @dane2565
      @dane2565 2 года назад

      @@mCoding you da man James Murphy

  • @thefelixgan
    @thefelixgan 3 года назад +88

    Structural Pattern Matching is definitely the highlight of this patch. Awaiting eagerly.

    • @mCoding
      @mCoding  3 года назад +10

      Can't wait!

    • @vinno97
      @vinno97 3 года назад +2

      It look very cool and fun to use, but I've always been of the persuasion of "if you think you should use a switch-case, you should probably refractor your code". I'm thus very in the fence

    • @ianliu88
      @ianliu88 3 года назад +10

      @@vinno97 But it isn't a switch-case statement. It is a pattern matching statement.

    • @arturgasparyan2523
      @arturgasparyan2523 3 года назад

      @@vinno97 why

    • @vinno97
      @vinno97 3 года назад +2

      @@arturgasparyan2523 switch-cases mean your function decides between many different code paths, already something you'd preferably minimize per function. More so, those paths are not extensible without changing the function and it's difficult to test many paths. For the last reason (and readability), it's often already cleaner to move the code per case into a separate handler functions.
      An even cleaner method is to use a factory(-like) pattern where you have a map of functions/handlers where the key is the switch-case variable. Instead of a long switch-case statement, you now have a single line of "map[key](args, ...)" and some error-checking. The handlers can then be registered somewhere else.
      @Ian I know, but the reasoning kind of remains the same. Only for pattern matching, you'd have the guard in your handler itself and that guard would probably be uglier code than the elegant match-case

  • @john.dough.
    @john.dough. 3 года назад +86

    I really appreciate your new audio. Your audio and video are great.

    • @mCoding
      @mCoding  3 года назад +9

      Yay, thank you!

  • @SwissTHX11384EB
    @SwissTHX11384EB 3 года назад +21

    The quality of your videos is truly something. I could watch this all day.

    • @mCoding
      @mCoding  3 года назад +6

      Wow, thanks!

    • @SwissTHX11384EB
      @SwissTHX11384EB 3 года назад +1

      @@mCoding I followed Martin Odersky's Functional Programming class during my BSc, and your style kinda reminds me his MOOC videos which I greatly appreciated back then (we had both classroom and video teaching with him).

  • @GodlikeGER
    @GodlikeGER 3 года назад +24

    I love these short and concise videos.

    • @mCoding
      @mCoding  3 года назад +6

      Glad to hear!

  • @user-hk3ej4hk7m
    @user-hk3ej4hk7m 3 года назад +12

    Being able to type higher order functions is definitely going to make my life much easier. *typed partial application here we go*

    • @egor.okhterov
      @egor.okhterov 3 года назад

      Do you have some references to examples?

    • @TechSY730
      @TechSY730 3 года назад +1

      Sadly, type checking will still fail on functools dot partial, as typing dot ParamSpec and typing dot Concatenate* in Python 3.10 does not support variable number of replacements (like from a varargs argument to a wrapper function). Check PEP 612
      You'll have to wait longer for that. ☹️
      * have to use "dot" instead of "." as otherwise RUclips thinks I am trying to make a link and thus the anti-spam bot zaps it.

  • @robertbrummayer4908
    @robertbrummayer4908 3 года назад +1

    The new typing features are indeed interesting. Thanks for the great video, James!

    • @mCoding
      @mCoding  3 года назад +1

      Thanks 😊

  • @MagnusAnand
    @MagnusAnand 3 года назад +3

    I’m loving this channel so much

  • @TheParkitny
    @TheParkitny 3 года назад +1

    Learn new things every video. Keep it coming

    • @mCoding
      @mCoding  3 года назад

      That's the plan!

  • @Mediners
    @Mediners 3 года назад +3

    I really love your content! thank you for making more!

    • @mCoding
      @mCoding  3 года назад +1

      Glad you like them!

  • @9SMTM6
    @9SMTM6 3 года назад +3

    Most of these type features are taken straight from typescript. Not that I mind, it's a good example and does a lot of things the right way.

    • @Zizaco
      @Zizaco 3 года назад

      Yes. I was thinking the same

  • @krtirtho
    @krtirtho 3 года назад +5

    Language Creators in the 90's: "Dynamic is awesome & so easy. Lets make everything dynamic"
    Programmers nowadays: "who made this language sh*t dynamic. *Static types to the rescue"

    • @ringyring
      @ringyring 3 года назад

      I am relatively new to programming, but I think unless the code author names the variables in an obvious manner like number_of_chairs or interest_rate (which as I've noticed most people often don't), having static types is better than having dynamic types, because it makes it easier for the reader of the code to understand exactly what kinds of objects they are dealing with, which helps them avoid type-specific problems, such as the result of divisions being inaccurate or expecting one behavior from a container but getting a different one.
      Edit: Though I might just be more used to ALGOL-like C and Java, since in Python, you can often simply go to the declarations of arguments or variables to determine their type.

  • @yoavcarmel1245
    @yoavcarmel1245 3 года назад

    4:35 can also use:
    from __ future __ import annotations
    edit: pretend that the underscores are not separated from the "future"

  • @BrianJorgensenAbides
    @BrianJorgensenAbides 3 года назад +1

    Great summary. I hate when I miss a good PEP. I almost missed the rise of pydantic for that reason, even though I had been trying to solve the same problem for years!
    In this case, I’m most excited for language features that improve our communication to the reader of our code. It gives confidence to us all when things feel the most deterministic.
    Even dynamic stack/pipeline analysis is deterministic, but it doesn’t FEEL that way because the rules are somewhat magick spells

    • @markcuello5
      @markcuello5 2 года назад

      Thanks; I never heard of PEP or pydantic.

  • @re.liable
    @re.liable 3 года назад

    It's going to be a joy learning TypeScript alongside Python with this feature

  • @hikingpete
    @hikingpete 3 года назад

    Another release like this one and you'll be able to unironically describe python as a member of the ML family. I love it!

  • @aditya95sriram
    @aditya95sriram 3 года назад

    My favorite kinda content from this channel 😌

  • @RamkrishanYT
    @RamkrishanYT 3 года назад

    Do you have any video on args and kwargs?

  • @Jacob011
    @Jacob011 3 года назад +10

    Oh Man, it's getting convoluted as hell.

    • @Kitulous
      @Kitulous 3 года назад +1

      but types are better than no types, hopefully even Python developers understand that

    • @EvenTheDogAgrees
      @EvenTheDogAgrees 3 года назад

      @@Kitulous No they are not. There are advantages to strongly and statically typed languages, just as there are advantages to weakly and dynamically typed ones. And there are plenty of languages to pick from if you prefer the former. Just because you don't understand duck typing doesn't mean you suddenly get to declare the diametrical opposite of that as The One True Way (TM). It just means you don't understand duck typing.

  • @eyemotif
    @eyemotif 3 года назад +3

    wow this ability to manipulate the stuff going on under the hood is really convincing me to start using python

    • @NabekenProG87
      @NabekenProG87 3 года назад +1

      Wait till you hear about decorators. That's were I entered the matrix

    • @vinno97
      @vinno97 3 года назад

      @@NabekenProG87 do python decorators do special things compared to how they work in other languages?

  • @ebux9885
    @ebux9885 3 года назад +1

    i no longer code python yet this is VERY entertaining for me
    edit: ursina really got me back to python

  • @henry-db
    @henry-db 3 года назад

    Wonderful video, I love the new Python 3.10 release 😍, it's AWESOME!!!

    • @mCoding
      @mCoding  3 года назад

      It really is!

    • @henry-db
      @henry-db 3 года назад

      @@mCoding Yes, I love the new match statement and the new typing improvements 😍

  • @Yotanido
    @Yotanido 3 года назад +1

    Pattern matching in python? What's next, sum types?
    Those two are the features I miss the most when using python, though better scoping is very high on that list too

    • @mCoding
      @mCoding  3 года назад +1

      One day maybe...

  • @willjohnson4579
    @willjohnson4579 3 года назад

    This is brilliant, I wish I understood how to effectively use any of this

    • @mCoding
      @mCoding  3 года назад +2

      Try it out!

  • @Zhuclam
    @Zhuclam 3 года назад +6

    It's interesting how weakly typed languages like python, php and javscript are evolving more and more to be more strongly typed in the recent years. For javascript at the very least, typescript has become a game challenger, and no one wants to go back now

    • @Kitulous
      @Kitulous 3 года назад +1

      while dynamic typing is good for prototyping, you really don't want to create big apps with it, because it's very prone to bugs

    • @son_guhun
      @son_guhun 3 года назад +2

      Python was always a strongly typed language. Strong typing is not the same as dynamic typing. But yeah, it's interesting how these languages are adding static typing features

    • @deimuader
      @deimuader 3 года назад +1

      Python is not a weakly typed language!

  • @aqworldsmaster88
    @aqworldsmaster88 3 года назад

    Thank you James

    • @mCoding
      @mCoding  3 года назад

      Thank you too!

  • @DebashishGhoshOfficial
    @DebashishGhoshOfficial 3 года назад +6

    As a C++ developer, I heartily welcome the typing improvements.

  • @yugiohsc
    @yugiohsc 3 года назад +1

    Why are we using type hints in Python in the first place?? These changes are throwing me for a loop

  • @oliverwooding2066
    @oliverwooding2066 3 года назад

    i had an issue where i couldn’t use an object as a return type inside it’s class definition, does anyone know if that’s intended behaviour?

    • @mCoding
      @mCoding  3 года назад +1

      This is what I'm referring to at 4:10 through 4:40. Python is dynamically typed, so it's impossible to refer to a type within the code that runs when you define type itself (this does not include the bodies of methods because methods they do not run at definition time of the class). Python allows you to use the string representation of the type name in place of the actual type name for this reason.

  • @sagnikbhattacharya1202
    @sagnikbhattacharya1202 3 года назад +4

    I'm convinced that Python 3.20 will be Rust.

  • @joeeeee8738
    @joeeeee8738 3 года назад +12

    This is not another April's fools right? 😂

    • @mCoding
      @mCoding  3 года назад +6

      Haha it's real this time!

  • @Danielagostinho21
    @Danielagostinho21 3 года назад +7

    I'm still using 3.7 waiting for a time to go through updating everything

    • @EvenTheDogAgrees
      @EvenTheDogAgrees 3 года назад +1

      3.8 here, but yeah, I get what you mean. Not to mention all the already existing projects that now run on older versions of the language, framework, libraries, ... Just keeping up with the changes and keeping everything updated is becoming harder and harder.
      I'm starting to feel like languages and frameworks nowadays are targeted towards an audience that writes small applications that will only run for a short while, before being decommissioned or replaced with something new. Because by the time you're done building a somewhat large'ish project, your framework and libraries will be out of date, and you're looking at a couple days work to bring everything up to date and make it work again.

    • @flex9663
      @flex9663 3 года назад

      @@EvenTheDogAgrees program in C... program in C.. program in C manage your memory with malloc and freeeeeeeee don't sink your app with runtime bloat software in C will stay afloat no hot libraries everyday do all your work there close to the hardware program in C

  • @MindlessTurtle
    @MindlessTurtle 3 года назад

    How do you people like Python's new typing? Still seems weird to me, but most of my scripts / projects are pretty small.

    • @deimuader
      @deimuader 3 года назад +4

      I think it's great and really helps working on large codebases where you simply cannot know every single piece of code

  • @AerotaleYT
    @AerotaleYT 3 года назад +3

    Great!

  • @alexh7849
    @alexh7849 3 года назад +4

    py3.10s new typing hints look great
    too bad LTS I code for is still on 3.8 ;-;

    • @mCoding
      @mCoding  3 года назад +1

      the struggle

    • @pogclippers4098
      @pogclippers4098 3 года назад +1

      meanwhile me and half the libs I use are 3.4-3.6

    • @AntoineViallonDevelloper
      @AntoineViallonDevelloper 3 года назад +1

      @@pogclippers4098 Wow, that's ancient. Even CentOS moved since then.

  • @marekglowacki2607
    @marekglowacki2607 3 года назад

    New syntax for union looks like Haskell ;-)

  • @cedric1731
    @cedric1731 3 года назад +1

    And here I was thinking I knew most of current Python by now...

  • @gillnabiel8385
    @gillnabiel8385 3 года назад

    Wait isn't 3.10 < 3.9 ?

    • @mCoding
      @mCoding  3 года назад +1

      en.wikipedia.org/wiki/Software_versioning CTRL+F for "Semantic versioning"

  • @FalseDev
    @FalseDev 3 года назад +4

    I'm waiting for the day py3.10 becomes common so I can use the new typehints for my projects, I hate having to type # type: ignore comments

  • @branislavvujnovic9489
    @branislavvujnovic9489 3 года назад

    Your videos are great, topics are great, but sound quality can be improved!

  • @lordtony8276
    @lordtony8276 3 года назад +2

    Still no ++.

  • @xxlarrytfvwxx9531
    @xxlarrytfvwxx9531 3 года назад

    0:48 *My brain screeches to a halt*
    Explicit type Aliases?

  • @IndellableHatesHandles
    @IndellableHatesHandles 3 года назад

    I think devs should wait until Debian updates to use a Python version before releasing a version of a program that needs it. Debian is the slowest updating mainstream Linux distro, as far as I know.

    • @mCoding
      @mCoding  3 года назад +1

      What better way to entice people to upgrade sooner than to have a cool library that actually uses new features? 😀

  • @me_jd_solanki
    @me_jd_solanki 3 года назад

    As typing will be mandatory from python 4 and it is always a huge benifits, Can you please create a detailed video series on python typing, which also covers advanced typing as well beginner level typing?
    Your videos are so much informative.
    Awaiting your video series on typing.
    Regards.

    • @goganii
      @goganii 3 года назад

      what ? python 4 isn't confirmed, the devs said it might not even happen

  • @motbus3
    @motbus3 3 года назад

    this param types and typesvars are dope.
    I understand the need to get better type checking but people are turning python into cpp

    • @mCoding
      @mCoding  3 года назад

      As long as it's not required!

  • @bluemonk9480
    @bluemonk9480 3 года назад

    god damn it i just got used to 3.9

  • @jakub7321
    @jakub7321 3 года назад +4

    4:29 yawn :D

    • @mCoding
      @mCoding  3 года назад +1

      :O

    • @jakub7321
      @jakub7321 3 года назад +1

      @@mCoding cool video btw, thank you!!

  • @BlackHermit
    @BlackHermit 3 года назад +2

    I never liked type hints... :\

    • @fdwr
      @fdwr 3 года назад +2

      Conversely, I never liked coming across a new function Foo(..) with unknown parameter types, only for my program to die at runtime because I passed in a dog, but Foo called Meow() on it, and there was sadly no hint of what was allowed.

    • @BlackHermit
      @BlackHermit 3 года назад +1

      @@fdwr Fair point, thank you so much! Greetings from Sweden!

  • @zackjames2409
    @zackjames2409 3 года назад +3

    I’m just learning programming and when I see Python's built-in asynchronous functionality (I had to google what a sync means) it makes me cry. I will never be able to learn all of this. I am self taught 😢

    • @mateusz7590
      @mateusz7590 3 года назад +3

      It just takes time and practice. Don't bother about asynchronous programming for now. Focus on fundamentals and you will be surprised how much can be done without diving into complicated stuff. I work with programmers who have been successfully using Python for years and yet they don't know about and/or use async or other Python nuances.

    • @sortof3337
      @sortof3337 3 года назад +1

      Every programmer is self taught, in fact everyone who has learned something is self taught. Take things slow, I have been doing programming since I was 8 or something and I am still learning. Don't believe people on stack overflow, nobody knows shit we are all experimenting.
      Async stuff is honestly really useful and easy. I don't know if this will helpful but I understand this stuff like this --> Think of async as having multiple people in at burger stand where they order go somewhere and when their order is ready you call them, you can sell to more customers with less resources and they will have to wait less. Now, think of people as functions, their requests are asynchronous and the when you call them don't forget to await to make sure their orders are ready. You might hear "event loop" and its complicated, but event loop is just the process in which you are serving -- like burger stand here could be a event loop. And, yes if you wanted you could have multiple burger stands, or sandwich stands or whatever.
      This will be useful when you will try to make your sync code, (( the old system where you took an order and made people wait there)) into new one without changing much.

    • @sortof3337
      @sortof3337 3 года назад

      ​@@mateusz7590 Yup definitely very true. I literally didn't know if some stuff could be done in Python until few years back. Runtime type and data validations.. and so much more I am oblivious about.

  • @arthuralexandersson
    @arthuralexandersson 3 года назад

    nice

  • @EvenTheDogAgrees
    @EvenTheDogAgrees 3 года назад +1

    Am I the only one who's got the impression the Python steering committee (or whatever they're called) is desperately trying to catch up with other languages by adding useless and syntactically complex features, and in the process forgetting one of the core goals of Python: to be a language that is easy for humans to learn and read?

    • @mCoding
      @mCoding  3 года назад +1

      Much of the typing related stuff is due to a lot of pressure from companies. Duck typing without any static analysis becomes a nightmare once a codebase hits a certain size in terms of code size as well as number of developers (who all have varied skill and background). At that size, type hints become a huge win, and that's why we see large companies, who have a lot of influence on the council, pushing for these changes. The good news is that it's all optional, so developers can work perfectly contently without ever dealing with typing if they don't want to.

    • @EvenTheDogAgrees
      @EvenTheDogAgrees 3 года назад

      @@mCoding Thanks for the clarification. Hope what follows doesn't come across as combative, it's not my intention to shoot one of the various messengers or anything, just expressing my feelings...
      To me, though, that makes about as much sense as outfitting your crew with hammers. But not before forcing the manufacturer to change the blueprints so that it has a bitholder on the side. Because the projects you work on require the use of screws rather than nails. Why not just get some decent screwdrivers instead? They'll be a lot more ergonomical and reliable than a hammer with a bit holder welded on sideways.
      Don't even mind the type hinting, really, but this latest iteration seems to turn it into an unreadable syntactically charged mess a cat couldn't find her young in.
      And on the subject of "it's purely optional"... Yes, it is. In your own projects. But in team projects? As soon as one yokel on the team introduces this junk into the codebase, the rest of the team better learn how the hell it works if they want to make sense of the codebase.
      Anyway, as I said, thanks for the clarification. Gives me some insight into what goes into these decisions, and I appreciate you taking the time to elaborate on the subject. Take care, and thanks for all the work you put into these videos.

  • @markcuello5
    @markcuello5 2 года назад

    Help me

  • @michalbotor
    @michalbotor 3 года назад +8

    am i the only one who doesn't like python's type hints? in my opinion all they do in practice is make the code lengthy, ugly, and unreadable, oh and confusing as well. why did they introduce type hints to python? it used to be such a beautiful and concise language...
    don't make python into java

    • @EdwardCashin
      @EdwardCashin 3 года назад +1

      Compared to languages with strict type checking in the language itself and type inference to help keep the code succinct and readable (F#, SML, Ocaml, Rust, ...), Python gets it backward. In Python the human specifies all the types explicitly, and the Python interpreter doesn't enforce anything without extra tools.

    • @EdwardCashin
      @EdwardCashin 3 года назад

      Good video, though. I was hoping the news would be that the Python interpreter itself would start enforcing type consistency, so my disappointment is with the message, not the messenger!

    • @dsnein
      @dsnein 3 года назад +5

      I feel like they’re building to a strongly typed python 4 and I’m not here for it.
      I also wonder wtf this is. Debugger and docstrings were all I ever needed for typing python. It feels like they’re going “you can type it, but we won’t require it to artificially lower the barrier to entry”

    • @fdwr
      @fdwr 3 года назад

      How does clarifying the contract to a reader of your function on what kind of parameter is expected add confusion? If anything, it reduces confusion.

    • @dsnein
      @dsnein 3 года назад +3

      @@fdwr because the markdown syntaxes used to do it are goofy looking yet still verbose, and have weird issues complying with linter and checkers, which are supposedly part of the value. Such clarification and typing notes were already being done in docstrings, which already had an ecosystem of goofy support. I don't see the upgrade over it besides arbitrarily declaring it to be the standard via PEP.

  • @echoptic775
    @echoptic775 3 года назад

    Why such focus on types in python? If im not wrong python wont correct types

  • @kokop1107
    @kokop1107 3 года назад

    What an awful idea

  • @Mrjulle3
    @Mrjulle3 3 года назад +1

    IM FIRSt