What’s new in Python 3.8?

Python 3.8 will be released in few months and you want to know the new features of this version, I will explain you the new points, the new audit hook or the f-string {=} and the other changes of this new version.

This session took place in track PyConDE and was classified suitable for some domain / basic python by the speaker.

Transcript (auto)

Auto-generated from the recording utilizing Open-Source AI. Speaker labels (Speaker 1, Speaker 2) reflect diarization, not identity. Timestamps refer to the recording.

Speaker 1 [00:04]

Just 3.8. No, 3.5 or whatever. I'm going to put my timer. Just 25 minutes, yes? Yeah, sure. Okay, it's fine. Firstly, do you want to work? No? Yes. I'm Stéphane. In German, Stéphane, maybe. In Italian, Stefano, as you want. Firstly, I'm a Python lover since 2001. A freelancer, an expert of Python. I'm a CPython co-developer since April. Thank you to the other co-developers. A member of the PSF, a fellow of the PSF. I work for the marketing workgroup and for the fellowship workgroup. If you know one person, and if this person could be a fellow member of the PSF, I'm just here. Tell me, and we can discuss about this person. I'm a board member of EuroPython Society. and the dates for the next year of Python is just there, in July 2020, from the 20th to the 26th of July. And I'm a former co-organizer of Python FOSDEM. That will be in 1st February in Brussels. Okay. How about the schedule for this presentation? Firstly, I don't want to explain the backend stuff of Python, the C parts of Python, because we don't have time for that, okay? Just for the useful feature for the end user of Python, okay? Firstly, the Python positional only parameters. Secondly, the Assegment expression. I don't want to explain with Guido, okay? Because there was a problem with this pep and I'm not here for that. Maybe in the hall after that. I'm going to explain the Python runtime audit hooks, how to use them, how to declare them, et cetera, et cetera. And some other points, like the f-string, how to debug your f-string. Normally, for this version of Python, there is the Pickle Protocol 5, the Python Initial Region Configuration, and the Victor Call. But I don't want to explain that. So, for the release 3.8, Firstly, we started in January of the last year. We had some alpha version, beta version, and now we have a candidate. Thank you to Lukasz. Just there. But it's a very big job because with the candidate, maybe there will be a second candidate or not. Just the final version. Maybe for the next Monday. Okay? It's time to update your servers. And for you, it's your first baby. Yes? Congratulations. So, about the PEPs. The first one is just PEP 570, the Python positional only parameters. How can I explain that? For example, you define a function. You have some positional arguments. and sometimes you want to use this function with just positional arguments not with the name arguments but we can have some problems for example with the backward compatibility we can break the backward compatibility for that we have introduced the slash operator just inside the naming of the function in this example here oh, it does not work sorry, it's my fault we have the slash in the name in the function name and now yes, just there, you see it? this operator this sign is just to explain from the left the parameter on the left is just for the positional arguments just for the positional only arguments if you want to call them just by position, not by name For the rest, between the slash and the asterisk, that will be for the positional argument or just for the keyword argument. And for the rest, on the right, just for the keywords only. Example. For the Po function, I have x and i slash mod is an equal norm. In my example, I'm going to show with Po 2 and 10. Okay. I'm going to use the positional argument just there. I can do the same thing for the third element. And for the rest, I can use the name of the argument. Because I'm going to use a positional argument. But if I try to call my function with the argument, my name, x equal to and i equal to 10, I'm going to get a syntax error. Just a type error here. and the message is, sorry, you need to use the positional argument and not the keyword argument. So, next step. Do you know the Assegment expression? The Waldus operator? Yes. That was a very... How can I say that? That was a very big problem with the community and Guido because everybody was for the disperse or against the disperse. For me, it's the same thing, but accepted for some points. For example, who likes to use a dictionary and get a key from the dictionary? Me. Okay. Do you know this pattern? Yes, sure. Sometimes there are too many lines. And I like to reduce to this example. just in two lines I can get the value of my key and I can execute some test on it, for example with the world use operator, just there so when you will define, when you will use the world use operator this variable will be evaluated just in the block, in this block of the if Not elsewhere. Another example. Do you like to use the regex? Yes. But we can reduce the code with this example. Just. It's just an introduction to the Waldorf operator. Because if you like, you can search on Twitter, and there is a lot of examples from Raymond Ettinger. Another example. With the NLN, in this case, that was before. No, it's just after, with the Walrus operator. I think there is an error. I'm not sure about the name. Sorry. Yep. This. Yes. I fixed one thing in my talk. Sorry. Okay. For the next point, it's another example. We can use the wireless operator just with the regex. Usually, we have this example, and now we can execute, we can get the result of the regex just in the if, without a temporary variable. And we can start to use it in the source code of CPython. That was just during the CPython sprint in London, and we try to reduce the source code. Instead of a one-loop with a true condition, if we don't receive an information from, in this case, just the connection, we stop the reading. With the new line, with the one-loop separator, we have only one line. Okay? For the rest, it's just a small example from Raymond Ettinger. You see there, we can, just for Hangul, we can use Hangul just here, and define theta, and use theta there. Do you like this syntax? For me, no. But it's useful sometimes. Sometimes. For me, I prefer this example, because sometimes we need to compute one value, and use this value somewhere for a second test, for example. Okay? So, next point. About the audit. The audit in Python was introduced in this version, but already discussed in the past. And in fact, they allow to generate some event when you call some function. C function or Python function. Usually, when you develop something with Python, you like to have a print function or just define a class or just a login class, and you will emit some events, like the signal. But with the hooks, we can use them for a low-level access. For example, we can monitor an access to the file, to a resource, a network resource, or from other stuff. For example, when you want to import a library, you can have an event where you try to import the library. And of course, we can watch the events with some hooks. Example. I'm going to define a new hook with this new function. This add audit hook. This function will take a callable where, in this example, we have audit hook. It's just an event and an arc. And I'm going to just hit with add audit hook. And when I'm going to execute an external call to a web service, for example, I'm going to get a truss. From that, I can check if I have a problem with my software. And I can avoid to modify this software. Another example. When you want to define, when you want to emit a new event. We can define them. We can define a hook. We can emit an event. For that, we need to have a string just here. Make request. and we can try to imagine that when I'm going to execute a request or I think IOHTTP and I'm going to execute my request, automatically I'm going to emit an event and I can catch this event just here. And you can see the result there. Okay? Of course, with the events, we have a list of events. It's not just for URL lib, but you can have an event when you try to compile a source code, when you try to execute, when you try to import, when you set a value on attribute, delete an attribute, get an attribute, and in fact you have a very big list of events, and you can develop your events. Example. In my case, I wanted just to show the import. when I try to import just from collection import default dict. And here's the result of my hook. From that, I can see all the imports in my call. How about the performance? In fact, because it's just in the C part, it's not a real problem. Firstly, secondly, we have a benchmark. And when we see the result with the benchmark, we have, in some case, we can be faster or we can be slower. It's not very different. Just 1%. It's fine. Okay. Next point. I just explained about the PEPS. Now, the funny part. We have implemented some stuff, and that stuff are not in the PEPS because there was a problem with a griddle and a steering console. Okay. And some peps were just in the backlog. Now, firstly, who has started to play with async-ko in Python 3.4? Yes, fight everybody. Thank you. Do you remember this thing? Async-ko.coroutine and try to fetch to get the event loop and just execute your coroutine. That was with Python 3.4. With Python 3.5, we have introduced async and await. Okay? With Python 3.7, we start to reduce. Instead of call get even loop, we have a new function, asyncko.run. And we can pass the coroutine. And now, since two or three months, we have a new function. In fact, you can execute asyncko on the command line and just get an interpreter where you can execute your coroutine just with asyncdef say, for example, and you can call your coroutine with await directly in the command line of Python. You don't need to do asyncko.run or just asyncko get even loop. I'm a lazy developer. I prefer sometimes this solution. Okay? So, do you remember the string? Who used Python 2? Oh, sorry for you. I like to use Python 3.7. Maybe on Monday, I'm going to start to use Python 3.8. Before the f-string, there were three methods for the string, for the string formatting. Hello with the person s, the LO with the parentheses the what is in English the bracket thank you or we can use the format ok with the string Marietta is the promoter of string do you have some stickers if you want to have stickers you can have to Marietta so for example with the string just you have to use the prefix, the f-prefix in front of your string and from that Python will use the local or the global variable from the scope as the case here. Okay? But when you want to use the f-string sometimes because you want to develop your software because you are a very good developer, an expert developer you are in this case x equal x. Okay? But I don't like to be, I don't like the duplication. We have just f string x equal, just like that. And now, the result will show this thing. User with equal will prompt on the common line just user is equal to something. In my case, it's just Stefan. And for the delta.is, it will be the same thing. Just with the curl bracket and the equal parameter operator, you will show the variables and the value of this variable. Okay? Just the funny stuff. Like in Python 3.7, we have the breakpoint function. I use it every day. Okay. Do you like sometimes I use JSON tools because I like to format my JSON when I play with web services, I like to use this command. And now, there is a new argument, JSON lines. I think that the use case is very limited because usually when you interact with a web service, you get a very big JSON with a lot of lines. This case will only be executed when you have only a JSON on one line. you see the backslash n. From that, you can have a stream, a JSON stream, with a lot of one-line JSON, and you can use JSON tool, JSON-lineage. And from that, you will start to format the JSON correctly. Okay? It's just for the end user. I think there is one use case. So, do you know TAR? Do you know CPIO? But we have PAX. PAX is another file format for the compression. MakerChief from SHHTL module will use it, and TAR file also. It's just a new file format. Usually it's just a TAR file format. Now we will start to use the PAX. so we are with python 3.7 and we don't have a library for the test with asynchronous code unit test comes with async mock from that here in just in this example we can start to mock an asynchronous code just here i have my function my coroutine and I'm going to iterate over my instance. But instance of what? No, it's not a new word. Just this instance of my hashing mock. And I'm going to return just 1, 2, 3. When I'm going to run my coroutine, I get the result. There was another library for that, PyTest async. Yes, PyTest async test. Async test, sorry. so of course we can override I enter, I exit and we can check if we use them with just an async with ok if you want to test that it's fine, we have that in the standard library and here is just a small example with the unit test where we can execute some tests for an asynchronous code In this example, for example, we have a call for a connection in a relationship. Okay, thank you. Self-connection, I'm going to await for a connection, an asynchronous connection. And in my test, I can execute it. And for the rest, I need to just include isolated as in KO test case. Okay? You don't need to use an external library. We have that in the standard library of Python. Who uses the threads? Hey, sometimes they don't use that. So, when you have an exception in a thread, in fact, there is a problem because, in this case, the main thread will be will crash ok not the not the not the current thread just the main thread and we can get this exception but in fact there is a problem because normally when I try to use this except hook and I define another normally I wanted to get got exception ok but there is a problem because sys.except is just for the main thread. And, yes, for the main thread. And we don't have my message. I don't have gotException here in my message. For that, we have introduced a new function, tradingExceptHook. TradingExceptHook has the same behavior of sys.exceptHook, except that it's just for the trading module. from that when I'm going to get an exception from my thread here I will get my exception the right exception that was not the case here ok this feature unresizable hook is when you can use when you can have that maybe in the sub-interpreters for Python 3.9 but for us for the hand user, never ok, but we don't know, sometime we can get this result, in fact sometime we can have an exception by Python, but we don't know how to catch it ok you can use this unresolvable hook and just define a new function and from that But this function, when you will delete an instance, and for example in my instance, my demo, I'm going to raise an exception. Oh, sorry, I don't have an example. I'm going to get the result of my call. Just, it's not, for us it's not very useful, but this function already exists. Oh, yeah. when we are in the world where we are in the Unicode world and with the emoji world you know that and sometimes because I love Python I wanted to parse my Unicode ok and that will be the case for Chinese, for Vietnamese for for Mayata because she loves the emojis and in my case I wanted to get the version of my string and there is a new syntax for the unicode part and matching we can use slash uppercase N and from that we will parse the snake ok example the snake that you can see here is just the name of the Unicode character of snake or instead of the Unicode code or of this character. And of course, with the new version of Python 3.8, we support Unicode 12.1. Ah, yeah. Because we like to use unpacking of Python, sometimes, we would like to use this syntax, just this one, and return the tuple. But with the current version of Python, we get an exception. It's just an invalid syntax. With the new version, it's just a load. Okay? And because I like the Simpson, in fact, it's just an example from internet. But now we can use a tuple without the parentheses just here with the return. And because we use unpacking here. So, some syntax warning when there is a missing comma. Example, when you use some tuples here, you see there is a missing comma. Normally, we will get an exception in syntax warning. Okay? This is the case. We have type error. Maybe your tuple is not a collable. That's true. It's not a collable. With the new version of Python 3.8, we have another message. Hey, please, could you add the comma? Maybe it's a collable. Or maybe not. We have a new model. It's five minutes. Thank you. We have a new model. The last year, that was import lib resources, Where you can load some resources. Thank you. I might remember. We can load the resources from a package. For example, the email, the test data from packages. Here, within metadata, we can get the version of the package, the requirements of this package, or just the files of this package. Instead of package resource, you can use metadata input label. For the process, when you use the multiprocessing model of Python, usually in the past, if you want to share something, you have to use a queue from the multiprocessing process, a multiprocessing module, a queue. And this queue will be shared between your process. But there was a problem because you need to serialize, deserialize, etc. It's not very interesting for the speed, for the performance. With this new version, you can say, welcome to the shared memory of multiprocessing. From that, we will share a memory from different process, and we can discuss together to share this memory. Example, here, I'm going to declare a shared memory, and I need to get this name. And I'm going to print the value of this shared memory element. Here is just a list of five elements. Because I create this list just there. Okay? That was my first process. I have another process. Now, if I give the name of my shared element, it's here and there, I can get the result. I can get the element. Okay? And, of course, I can change the value of my element. And when I get the result in my first process, I can see that I have 10 instead of 4. Okay? So, about the mathematics stuff, we have a new function, mat.dist for the Euclidean distance between two points. We have the product for a list of numbers. We have the square just for the integer. And, for example, we have some change for the hypotenuse, for the multidimension, and for the factorial, we only support the integer, not the float. For the mathematician, they like that. Oh, yeah. In the socket, now we can create a server. You know that with Python. but before that was just with for example if we want to reuse the address reuse the port and because we would like to support IPv4, IPv6 we have this configuration now we have just one line just this one I can create a server in one line ok it's a new function so for the deprecation and the removals we remove bdist wininst from distutives. Please use bdist will. The will format will be the main format. We remove isalive from thread because there is another function for that. And we started to remove macpass but from popen instead of you can use ospopen or subprocess. TimeClock the replacement is process time and to avoid the confusion we removed pyvanv instead of you can use python mvanv ok how about the performance subprocess will start to use a new function osposix this function will be used only if we don't specify the pre-exec path find CWD insert new session when we call the function if the executable path contains a directory and if we don't set close FDS. From that, we can gain a lot of performance. For some functions, when we try to copy some files, copy file, copy, copy 2, copy 3, and move, we can use the fast copy syscall from Linux or from Mac OS. It's just a call from the kernel. By default, PQL will use the protocol 4 and not the protocol 3, because about performance, a small size. UID will reduce the memory footprint, and item getter will be improved by 33%. And for the rest, in green, it's just Python 3, and yellow, sorry, in green, Python 3, and yellow, Python 7, And in some cases, it's lower, but for the rest, it's just faster. And for the rest, if you have any questions, you can ask me. Thank you, Stefan. Is the next speaker here already? Then we can probably set up, and maybe you have one question. Do you have any questions about Python 3.9? for him no and in fact about the challenge log of Python 3.8 thank you to Lukasz the other developer other code developer because it's just a file of two or three thousand lines of code of documentation and when you have to read everything that's really difficult thank you thank you

Stéphane Wirtel

CPython Core Dev

Social card for talk: What’s new in Python 3.8?