New Features You Need To Know In Python 3.12

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
when I realized that python 3.12 is just around the corner I got pretty excited noise the thing that I was most excited about is that with just two versions away from python 3.14 also called pie Thon there are definitely some new things coming to be excited about but next to that you should also be aware of a couple of things that are going to be removed now before we dive into python 3.12 I have something for you it's a free guide to help you learn how to design a piece of software from scratch you can get this at ironwork Golds design guide contains the seven steps that I take whenever I design new piece of software and hopefully it helps you avoid some of the mistakes that I made in the past iron dot goals slash design out the link is also in the description of this video now let's dive into python 3.12. the first thing that's improved in Python 3.12 is the error messages now over the last version updates of python already error messages were improved a lot but there are even more things in this particular release so for example one thing is that modules from the standard Library allow suggested as part of the error messages you can actually see that here so if you try to use assist dot something then it's going to mention hey did you forget to import sis it's a minor thing but it's pretty useful another thing is that the error messages are also Smarter with common errors that people make for example you quite often see somebody typing input X from y instead of from X import Y and the error message is more explicit and recommending to do it the correct way another way in which error messages have become smarter is that if you're referring to something that belongs to self that belongs to an object but you forget these self dots then also the name error message actually refers to that and finally if you have an import error and you get some exception like for example try to import something from module but that doesn't work well then it automatically suggests Corrections like these now these are relatively small improvements in error messaging but overall they do make the coding experience in Python a lot better the second thing that I want to talk about is performance improvements now python 3.11 was a huge leap forward in performance improvements with respect to python 3.10 improving speed by up to 60 percent sometimes now from python 3.11 to python 3.12 it's not that big of a difference but there are a couple of three here and there in the code that do help a lot one of those improvements is comprehension inlining and that was suggested by pep 709 comprehension is a really nice feature of python that allows you to create list a dictionary or a set using just a few Simple Rules here's an example of what a comprehension looks like now what happened in the past is that these comprehensions were internally represented by functions that each had a separate frame for the context of that particular function and that meant in terms of performance that comprehensions were not the greatest so what they've done in Python 3.12 is that they now create these comprehensions in line and that means you don't need these nested functions anymore that leads to a big Speedo and some isolated Benchmark that speeds up the execution of comprehension up to two times and in a real world code Benchmark using comprehensions there was a overall performance Improvement of 11 not too shabby there are a few other things that may also help improve performance for example the W string and W string length members have been removed from objects and that reduces the object size so that just saves memory and potentially avoids cache misses and things like that another thing that was added in Python 3.12 is Immortal objects now in Python in principle every object has a reference count to keep track of whether the object is still being used but of course that's only relevant if that's an object that the user is actually creating and then doesn't need it anymore in the future there are some objects that are simply always needed by the system for example and this is exactly what an immortable object is so the reference count is never updates though it stays the same now that might seem like a pretty insignificant minor thing but it could actually have huge implications for python in the future because now with Immortal objects we can indicate that an object is basically never going to change and you're going to avoid again cash and validations you're going to avoid data races where multiple pieces of code want to modify the same data if it's Immortal we know okay that's not going to change so it makes things much simpler and this actually helps with the next feature that's also new in Python 3.12 although you can't redo a lot with it at the moment which is deeper interpreter Guild Global interpreter lock and that's actually a pretty big change in how python works you can now have sub interpreters that each have their own Global interpreter long and this allows python to take better advantage of multiple CPU cores which is a huge deal now unfortunately by 3.12 doesn't really expose this capability yet it's merely the C python internals that allow for this here you see a piece of C code that actually does this you're gonna have to wait for three points 13 in order to have this behavior and there's going to be a new module called interpreters that is going to allow for you to do this from python itself here's an example of what that's probably going to look like in Python 3.13 so you simply create an interpreter and then you run some python code that you supply as a string and if you want you can also run this in a thread now I'm sure this is probably going to change before they release this and python 3.13 but at least it gives you an idea of what's coming another thing that's really nice in Python 3.12 is that F strings are going to become less finicky like you can now have nested double quotes which is really cool so this is what that looks like and again that may seem like a minor difference in the way that you write your python code but actually because this wasn't possible in earlier versions you always had to resort to different types of quotes and that means there was a limit to how far these nested strings with variables with other strings can go now practically speaking I don't think you should have deeply nested strings like this in your code anyway but it it's good that this at least is something that now we have to worry less about we don't have to think about hey we need to switch to different types of quotes because this is simply not going to work so mine effects but it's nice that it's there there are a couple of changes that are more related with types and type annotations one thing is that if you have keyword arguments that you can use the on back mechanism and provide it with the type to define the type of keyword arguments in earlier versions of python it didn't work like this so if you wanted to specify a type for a keyword arguments you have to do something like this for example and now this means that each keyword argument is going to be a string there was no easy way to define that keyword argument would for example be a name and a year and now you can do that using unpack and then recently Supply the type that defines the type of the keyword arguments by the way I'm not really a big fan of keyword arguments I try to be just as explicit as possible with the arguments that I provide to my function so I'm just wondering are you using keyword arguments like this a lot what are you using them for any tips recommendations let me know in the comments another thing that's also coming in Python 3.12 is the override keyword and what this helps you do is indicate explicitly that one method overrides another method let's say you have this Base Class with a gets color methods and we have a child that overrides get color and return something else but we also have a batchel that also overrides it but misspells the name and now what python 3.12 will add unfortunately this doesn't work yet as I'm running this at the moment but this is supposed to give a type error because now of course you're overriding the methods wrongly because you're mistyping it and it's nice that the type Checker now gives an error for this because running this code means that we would expect to get red but actually this prints blue because it calls the original method instead of this over written wrong methods but in my opinion these two things are not the biggest changes with respect to types and type annotations the biggest one is a new syntax for type parameters or generic classes and functions this has been proposed in pep 695 and what's nice about this is that it's gonna really simplify the way you deal with generics now before we have to do some complicated stuff in Python with using type vars you can actually see that here and then we could use a class and actually Define it like this but with the new syntax we don't need any of those things anymore we can simply specify that hey we have a class that is a generic class that relies on a type T that inherits from a string and then we have a method that then returns something of that type T so instead of doing this saying that the type is covariant and it's bound to the string type we don't need any of that anymore it's way way simpler and I like that a lot same for functions so up until now this is how we Define a generic function so we have a type c using type far and then we have a b in this case and it also returns something of type T the new syntax doesn't need typeforce anymore it simply specifies the like this which is much close to how other programming languages are doing that as well another example is if you define a type Alias so this is how you would do that up until now using a typefar but now you can simply specify it as part of the Alias itself and there's also a new syntax for specifying type aliases that looks like this so really nice improvements to the way that you deal with types in Python it's just one of the many steps forwards I'm still waiting for an improved callable syntax I would love to have the arrow syntax style in Python like we have in languages such as typescript by the way if you enjoy these types of videos you might also like my free Weekly Newsletter where I cover python news and all kinds of other interesting things that I run across you can subscribe by going to ironcodes.com few other minor changes in by 3.12 that I think are noteworthy first pathlib now has a walk method that allow you to walk the directory tweeze and generate files and directory name similar to what we had with OS dot walk it's also nice that this is now part of pathlip because I use this a lot if I want to explore A3 of directories not a minor thing thing is that c python 3.12 supports the ability to monitor calls returns lines exceptions and all other types of events using instrumentation that means that it's going to be way faster if you need very fast debugging or coverage tools because they just can access exactly what they need and finally a few things that you need to know there are a couple of modules that have been removed in Python 3.12 such as async ads and asyncore which have been superseded by async IO which is the all-encompassing package for asynchronous concurrent operations also this details has been removed because it was already deprecated in Python 3.10 and now typically you will use setup tools instead also good to know that a lot of old stuff has been removed from the unit test package such as these deprecated alliances for these method names that you should use instead from now on so I hope this gives you an idea of what to expect with python 3.12 I'm very excited about it can't wait for it to be released so I can actually start using the stable version overall I'm pretty happy with this in particular I'm quite happy about the improvements in type implementations now if you'll learn more about how these type annotations actually work and why you should definitely use them in your python code watch this video next thanks for watching and take care
Info
Channel: ArjanCodes
Views: 178,628
Rating: undefined out of 5
Keywords: new python features, new python, python 3, python 3.12, python 3.12 new features, python 3.12 performance, python 3.12 performance vs 3.11, python 3.12 changes, python programming, learn python, what is new in python 3.12, new features of python 3.12, python tutorial 2023, python 2023, programming in 2023, new python framework, python programming basics, pep 684, immortal objects python, fstrings, python f-strings, python performance, python performance improvements
Id: udHmeAmOlbI
Channel Id: undefined
Length: 12min 3sec (723 seconds)
Published: Fri Sep 29 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.