What's new in Python 3.7?

Scheduled for release in mid-June after the conference, Python 3.7 is shaping up to be a feature-packed release! This talk will cover all the new features of Python 3.7, including the Data Classes and the Context Variables for the asynchronous programming with asyncio.

This session was classified suitable for expert domain / expert 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:03]

Thank you for the organizer for PyCon DA. It's my first time here, and we try to make some words in German, but no. Welcome to Java 1.0. I'm going to start. I just dropped Python, and I'm going to start with Java. No, just kidding. I'm Stéphane. This picture was taken in Pentecost last week. I was there in Spain. And what can I say about me? You said I'm a fellow member of the PSF, a member of your Python society, a co-organizer of your Python, just in the website workgroup. I made the Python for them in Belgium. 800 people. Everything is just free. You can go in February of the next year. And I'd like to contribute to CPython, just CPython and Greenicron also. So what's new with Python 3.7? Firstly, we changed the syntax. We drop everything, we start with a new syntax. So no, just kidding, just a joke. Firstly I'm going to explain all the paths for the new version of Python 3.7. We can see we have a new function breakpoint, the data classes, some new attributes for module, the postpone evolution for the annotation, the time function with nanosecond resolution. Hello, come on, there's no problem, come on, there's a lot of place. I'm going to explain you the deprecation warning, that's very useful, the context variables, very useful with AsyncIO and of course the ATF8 mode. Firstly, the breakpoint function. When you want to develop a software with Python, you start with this very small function for newbie newcomer you will start with this one and sometimes you can have a crash okay but for me for the debugging normal people just like that on the left and experimented programmer does the same thing except that with the product okay so in Python 3.7 we have a new function, debugger. Debugger is inspired from JavaScript. Whoa, no, in fact it's not the debugger because this debugger keyword is just specific to Java but in the case of Python we have a problem because sometimes we would like to use a debugger but we have a problem with the debuggers with an S because we have PDB, IPDB, PUDB, PDBPP, etc. etc. etc. We will start with Python with a newcomer, a newbie. Nobody? Okay, so go out. In fact, you can try to debug with your favorite IDE, Visual Studio Code, PyCharm, whatever, Emacs, Vim, Head, why not. And when you want to use your debugger, you can use PDB for for example or PUDB of course but we don't know which one we want to use okay and we we have introduced a new function breakpoint okay this button function will execute by default the PDB set trace function when you have a crash just PDB set trace you don't need to execute PDB set trace just add breakpoint. So breakpoint has a configuration. You can configure it with this variable, this environment variable. Python breakpoint is equal to zero. I'm going to disable all the breakpoints in Python, in the code. Breakpoint is equal empty. I'm going to execute the default debugger breakpoint PUDB set trace I'm going to use PUDB because I love PUDB and if you like a Python you can use it okay that's fine for that and of course if you like to debug with your web interface your favorite browser you can use web PDB and it's a result okay that's fine any question it's a good feature? I know. Okay, yes, I think it's a good feature. Okay, next point. The data classes. Who use Python 3.7? Okay, I'm not alone. Yes, about the introduction of the data classes, it's just a way to create a structure for some data. Usually, we have some implementation, we can use the NEM tuple, DATTRS project from INAC, or with the Python classes. We can do that. My case, when I try to learn the NEM tuple, the class or the data class to some trainees in a Python training, I'm going to use the tuple. First example is just with the tuple. In the tuple, I'm going to define the tuple here, and I can use in this there is a problem with that because I don't know if I'm going to use first name or last name okay just that for the dict I'm going to have a solution I can specify just the first name or the last name but there is another problem I don't have the dot notation so I could use the name tuple for that I'm going to define my person it's a new class new tuple with my attributes and just instantiate my class with foo and bar and I'm going to print first name. I have my dot notation but I don't like that. There's another example I could use a class just for one person. I have my person, I have the constructor, I have the representation, I have some operator for the equality or just for the less than or greater than operator okay and I can use them just here but with the data classes I can reduce the code in four lines just import the data class use this decorator define your class and use the typewriting of Python 3.5 and just specify the type. Instead of that, we have this solution. Do you prefer? I agree with you, I prefer this one. So I can instantiate it, I can use some attribute, and of course I can execute the equality operator. But I can use another function, makeDataClasses, like the name tuple. I could use some default values. I have my position, latitude and longitude, and by default the value will be zero. If I don't specify the latitude and longitude directly, the default value will be zero. And in some cases, for example, I'm an attendee, I would like to have a conference. In this conference I have a list of attendees and I can use a new function just filled where I'm going to define default factory in fact when I'm going to instantiate conference automatically the system will load the default factory default function and I'm going to create a default for you after the default attendee like that okay so about the method and the properties. I have the data classes with data class. I have my first name, last name and age, the attributes and I can define the property. I can do that. Of course, so the data classes have some configuration flags. You can use init, wrap, equal, order and frozen. By default, init is true, wrap is the same and equal is true also. But for the other, and frozen, just false. Example. With frozen, I cannot try to change the attribute of my person. It's false. I don't have 27. Maybe 38, since September. Okay, we have the frozen. We can use that if you don't want to have a mutable instance. We have the other. With the other, I'm going to get this operator. Just that, okay? So, for the inheritance, we can have two data classes. The first one is Person, the second one User. And, for example, for Person, I'm going to have two attributes, two parameters for my constructor. And for user I'm going to have four parameters. The two first are just for first name and last name and the second, username and secret, that's just for the user. We can have the inheritance. Of course because we are using the type hiding from Python we can use mypy. Yesterday there was a great talk about mypy. I think that can be really useful for us. Less bugs, less code, too maintenance that's that's good so and of course we have some helper has date and has tuple if you want to save your data class to file for example of course we have there is a conclusion for that we can replace with the data classes we can replace the tuple the name tuple the simple class just for a small object a stupid object and we can use the type annotation that's really useful because because we can use MyPy to detect some bugs. We can use the default values. I have about the default values in Python 3.7. Now, you have a default attribute on the name tuple, and you define the name tuple. And you can have the business logic with methods and properties. You can use the inheritance from the object programming, and we have some appers. And for that, I just bump. Success. Okay. I love this kid. I think that we can find it everywhere. So PEP. This PEP is just how to define some new attributes on the module because a module can have this kind of attribute to get for example in this case it's just for the get attr. For example I have my library, my main function. I'm going to import my library and my old function. And I would like to have a deprecation warning, okay? If I try to execute it, I'm going to get this deprecation warning message. Old function is deprecated. How can I do that? Just, for example, with... I'm going to define two functions, two deprecated functions. Old function, warn, I'm going to emit a a deprecation warning and for the second one I'm going to do the same thing I could create a descriptor for that or just use a new feature with get-attr I can check if the name the call it name is just in the deprecated names for example in this list and automatically generate a deprecation warning and just return the parameter okay so new version previous. For two lines, it's not really useful. For 100 lines, yeah, I prefer. 100 functions. Okay. Now, for the dir parameter, the attribute, the function, this one can be used for example when you want to change the sort of a module with the dir function. And for example here, I'm going to use just dir. Normally, I will get all the list of the functions. And here, with the sorted functions, this one, I'm going to sort all my functions from my parameter. Just for example, if you have some deprecated function, you can use the dir, underscore dir, and just remove the deprecated function. Okay? What time is it? Oh, it's good. Okay, for the postponed evaluation of annotations, sometimes when you want to write that with a former version of Python, a former 3.6, for example, you will have a problem. Why? Who can explain that? just because node is not yet defined. And I'm going to get this message. For that, there is a new solution. Thank you. For that, there is a solution. We can just specify the type in a single quote. But it's not a real solution because we cannot compile it and there is no check verification at the compile time. For that, we can use fromFutureImportAnnotation. And with this solution, we can define just node, like that. Okay? So, it's another example with my tree, where I have the left and the right. I use the data classes, the optional, and of course the annotation. And with that, I have a small code. Time function with nanosecond resolution. Who works with the monotonic time, monotonic clock? Ah, yes, there is a solution for you with Python 3.7. So, you know, when we use time, the time, we have a problem. It's just limited to the representation of the float in the memory of the computer. The limit is just 104 days, which is sometimes that can be really useless. And there is another problem. When we want to make a benchmark on some functions, we will have a problem with the precision. For that, we have introduced the nanosecond resolution. Example with the monotonic. Time monotonic is just in float. And with the nanosecond resolution, I'm going to get a big integer. Ok? That's very difficult. So, the deprecation warning is just shown in the main function. In Python 3.0.2, we just removed the deprecation warning when we executed the script. A script, just a simple script. But there were some side effects. For example, who developed with Django? Yeah, sometimes when you execute the test, do you have some deprecation warnings? Yeah, yeah, yeah, yeah, yeah, there is a yes, yes, yes, I know that. So only visible when there is a fault, a mistake, when running test, okay? And when I try with Python 3.7, the deprecation warning will be automatically shown. Example is the documentation of Python, just to prove my concept. Python version 3.6.7 and I'm going to execute just the make file for the documentation. Here I don't have any problem. I can install things and blurb, sorry, and I'm going to start just the download of my dependency. With Python 3.7 I'm going to get this warnings automatically okay I think it's very useful because sometimes when you don't work on your dependency you can have a problem with that example with a sink it's a new keyword in Python 3.7 so yeah sorry for Victor I like to move it move it Victor is a friend of mine so and ask me if you show that you have to do I like move it move it so just kidding okay context variables yes we like to use a flask or Django okay we'll use Hasink.io with IHTTP you have any problem with the TLS with the thread local storage when you have some variables from a request and you want to use them somewhere in the code no in fact when you use TLS that's great for the synchronous programming just because a TLS is a thread storage just for this thread you will receive a request and you can store the result of your request inside in this TLS okay but for the asynchronous development we will have a problem we have only one thread and a lot of requests. How can we be sure that we can assign a variable to the thread? We cannot because there is a problem with the conception. For that we have the context var. In the context var we can define current user just here for example with a default value if you prefer and I'm going to have my handler just for my endpoint when I specify the name I get the name and I'm going to store it in the current user if I receive another request I do the same thing and in this inner function and can use it do you like that yeah yes you can take a picture so it is eight mode yeah ten minutes By default, ETF8 mode is just disabled. If you specify LCO is POSIX or C, automatically we will use ETF8 mode. Why do we want to use that? Because on some computers, the locale is not correct, and we want to override that. For example, with Docker. If you execute your Python script in docker, sometimes you can get an error. If you override the default behavior of Python 3.7 with this flag, automatically everything will be in UTF-8. Of course, we will override everything. The GetFileSystem will be in UTF-8. For the local, it will be the same thing. And for stdin and stdout, we will use the SurrogateEscape algorithm, just to avoid the Unicode, Unicode Decode Error. Ah, yeah, you can take another picture. Sorry, I'm a little bit stressed. So, for PEP 545, it's my favorite PEP, because we started the translation of the documentation. And actually, we have a French translation, 37%, Japanese, 75%, Korean 10 or 12%. But if you prefer, you can start the translation in your favorite language, in German, in Polish, Spanish, Ukrainian. So, and we are waiting for your contribution. So why I show this to Pep, just because when you start to learn Python for a kid, or for For my grandpa, he does not speak in English, but he can speak in French. And the French documentation will be really useful for him. Okay? For a starter, that will be the same thing. So, what's not in the PEP? Input lib resource. Do you know input lib resource? No. Do you know package resources? Yes. Okay, this package resource is dead for me. Okay? because there is a lot of bugs, and about the performance it's just crappy, I don't know the right term in English, but it's not very good. But with ImportLibResources you can load your binary artifacts, binary files from your package. Of course, we keep the compatibility with PKGResource in terms of features, and in terms of performance, we are just greater than. And with ImportLibResource, you can read everything from the file system, Amazon, S3, a zip file, whatever, etc. I have my package, and I would like to read just a message.eml file. Without ImportLib, I need to use that. I'm going to constrict my path, and after that, read the bytes of my file. But with importlib, I have one line. I can use context manager with. There's a problem, I'm going to close the file. And of course, if you don't like to use a string, you can use just your model. For what do you need a context manager here? Oh, you should do a lot of stuff in there. It's just an example. But do you need to clean up something? Yeah, normally, pass it, we clean everything. But just for the example. We have this possibility. Just an example. So, with Python 3.6, we keep the insertion order of the dict. Okay, example. I have my dict. name, location, year, and I'm going to have name in the first position, location in the second, and year in the third. Okay? I'm going to keep this order. For Python 3.6, that was just unofficial. With Python.7, it's just official. Okay? Just, it's not really difficult. From, import. Sometimes when you debug, or you execute, in this example, For example, I would like to import demo from EtherTools and I can get this message. Not very useful. With 3.7, I have, oh, from EtherTools there is a problem. Async and await are keywords and you cannot use them like a variable name. Example. For async, async is equal to. I'm going to get a syntax error. That was the case with IOPK when you use RabbitMQ. In the code, you can find a thing or just a weight. And it's the same thing for a weight. Just a syntax error. So in this case, don't use a thing or a weight for a variable. I think I have an improvement. Before, for example, I like to go to unpython.org and I would like to read the text of the main page I can use this code and in the past before 3.7 I need to create to get the event loop run until complete my function and just close it with a new version, just one line ok yeah because just for that, usually I try to go, Google is my best friend But now I think IORun is fine. We have a lot of view functions. I think I support the context var. You can use it with IOHTTP and the other IOLibs. We have create task. It's just a shortcut to create task of the loop. We can start a new TLS connection. We can get the current task, because before that was not the case. We can get all the tasks and of course for the performance you can use SOC send file. This one will use SOC as send file and this function will use the send file from the kernel Linux. I don't know for Windows. And about the performance we have some improvements for the getEventLoop just because this function has been written with C. Improvement with the future, same thing with getter and sleep. and we have some gift for the developers if you want to know when you debug your import cyclic import or whatever you can use import time and with import time you will get just a timing for the load of your libraries if you want to debug okay we have the dev parameter just with this function this this line I'm going to add the debugging for the debugging mode for us in ko I'm going to have the foot on there when you have a crash I'm going to to find the capability cutty What do you say in English? In French, it's just coupable. The guilty. Alpha. Okay. And we will enable the debug hooks for the memory allocation, for the trust malloc, et cetera, et cetera. About the performance. That's the performance of today, on this morning. Okay? It's not a graph from the last year. When it's just lower, it's better. And we have a lot of improvements, except for this one, for XML3 parts, but I know there is a bug fix for that. And of course we have a lot of improvements, bug fixes and whatever, and I think Python 3.7 will be a great advantage, and I'm going to wait for Python 3.8, maybe in one year. Okay, do you have any questions? Yeah, if you have any questions, we can. Yes, you. I just asked myself if it's really desirable to have a dictionary that keeps some sort of order, and I wanted to ask you if it's really guaranteed now that this order persists and that it is something that will not break with Python 3.8? Okay. We keep the order. Great, thanks. You're welcome. And in fact, there is another side effect. When you pass your parameter with the care works, care works will keep the same order. Just a... You can launch it. You mentioned shortly the asyncio-run function. I'm sorry? You mentioned the asyncio-run function shortly. It's still in the provisional API. Do you think it's going to be in the main API in 3.8? I know one thing. Python 3.7 was great because we have this function. At EuroPython 2018, there was Yuri Selivanov. He's a main contributor of Async.io, and he has a lot of very good ideas from Trio. He would like to insert them in Python 3.8, and yeah, that would be great. But about Async.io, I don't use it, except for some script, because I'm a consultant and I work only with Django or Odoo or some other stuff. But I would like to implement a very big software without in KO, but I don't have the use case for the moment. Okay, so I'm going to ask about this insertion order in dicts as well. The thing is, there are other implementations of Python than this CPython. Do you know about how they are going to support it if they have a completely different implementation of dict? Normally, yes. Normally, yes. I'm not sure. For Pai Pai, you can ask. It's time, sorry. I'm not sure, but we can discuss just outside. Okay. But I think, yeah. Okay. Okay, time's over. Thanks for the great talk. Thank you.

Stephane Wirtel

Contributor of CPython, Stephane is a member of the EuroPython team since 2015, he likes to improve his skills with best practices, TDD, CI, etc.... Python developer since 2001 and member of the PSF, EuroPython and AFPy, he is also a freelancer in the wonderful Python world.

Social card for talk: What's new in Python 3.7?