What's new in Python 3.7
This is a modal window.
The media could not be loaded, either because the server or network failed or because the format is not supported.
Formal Metadata
Title |
| |
Title of Series | ||
Number of Parts | 132 | |
Author | ||
License | CC Attribution - NonCommercial - ShareAlike 3.0 Unported: You are free to use, adapt and copy, distribute and transmit the work or content in adapted or unchanged form for any legal and non-commercial purpose as long as the work is attributed to the author in the manner specified by the author or licensor and the work or content is shared also in adapted form only under the conditions of this | |
Identifiers | 10.5446/44880 (DOI) | |
Publisher | ||
Release Date | ||
Language |
Content Metadata
Subject Area | ||
Genre | ||
Abstract |
| |
Keywords |
EuroPython 2018125 / 132
2
3
7
8
10
14
15
19
22
27
29
30
31
34
35
41
44
54
55
56
58
59
61
66
74
77
78
80
81
85
87
91
93
96
98
103
104
105
109
110
111
113
115
116
118
120
121
122
123
125
127
128
129
130
131
132
00:00
SoftwarePauli exclusion principleSocial classModule (mathematics)Performance appraisalImage resolutionFunction (mathematics)Context awarenessVariable (mathematics)Asynchronous Transfer ModePoint (geometry)Java appletSelf-organizationControl flowPauli exclusion principleBitDesign by contractRevision controlComputer animation
01:14
Pauli exclusion principleSocial classModule (mathematics)Performance appraisalFunction (mathematics)Image resolutionVariable (mathematics)Context awarenessAsynchronous Transfer ModeDebuggerFatou-MengeDefault (computer science)Point (geometry)Interior (topology)Software bugPoint (geometry)CASE <Informatik>Social classControl flowWave packetDebuggerKey (cryptography)Letterpress printingProjective planeSoftware developerError messageFunctional (mathematics)Interface (computing)SoftwareVideo game consoleCodeGraph coloringScripting languageLine (geometry)Integrated development environment
05:38
Pauli exclusion principleSocial classTupleSubject indexingTupleTable (information)Social classComputer animation
06:27
TupleSocial classPauli exclusion principleDefault (computer science)Configuration spaceInheritance (object-oriented programming)Module (mathematics)Attribute grammarFunction (mathematics)Fatou-MengePiPerformance appraisalData typeVariable (mathematics)Line (geometry)Execution unitElectric currentFile formatSystem callBenchmarkImage resolutionSocial classString (computer science)Ocean currentBenchmarkEqualiser (mathematics)Representation (politics)Type theoryDefault (computer science)Uniform boundedness principleTable (information)Right angleInstance (computer science)Functional (mathematics)Positional notationInterpreter (computing)ResultantCodeImplementationParameter (computer programming)Pairwise comparisonComputer programmingPauli exclusion principleCASE <Informatik>Category of beingLine (geometry)Software developerAttribute grammarMultiplication signJava appletDeclarative programmingInheritance (object-oriented programming)Point (geometry)Electronic mailing listMixed realityFluidModule (mathematics)Object (grammar)Form (programming)Physical systemRow (database)Endliche ModelltheorieDescriptive statisticsSet (mathematics)Connected spaceOrder (biology)DampingFilm editingAxiom of choiceEnterprise architecture2 (number)Normal (geometry)WritingNumberRevision controlException handlingWater vaporConstructor (object-oriented programming)Computer configurationLimit (category theory)Software bugRead-only memoryTupleSource codeProcess (computing)Variable (mathematics)Library (computing)Goodness of fitReading (process)Function (mathematics)Data dictionaryAsynchronous Transfer ModeComputer animation
15:13
Electric currentFunction (mathematics)System callImage resolutionPauli exclusion principleMalwareDefault (computer science)Software testingSoftware developerTrailTransport Layer SecuritySimilarity (geometry)Variable (mathematics)Context awarenessIntegrated development environmentThread (computing)Task (computing)Object (grammar)Task (computing)Context awarenessPoint (geometry)Thread (computing)Pauli exclusion principleVariable (mathematics)InformationSoftware developerDampingObject (grammar)BenchmarkSoftware testingLimit (category theory)Functional (mathematics)NamespaceLevel (video gaming)Default (computer science)System callMultiplication signRevision controlWebsiteMusical ensembleVarianceDemosceneIntegerWordException handlingImage resolutionIntegrated development environmentModule (mathematics)Field (computer science)Computer programmingSoftware frameworkComputer animation
18:17
Context awarenessVariable (mathematics)Pauli exclusion principleDefault (computer science)Context awarenessWeb 2.0Web serviceRevision controlLetterpress printingThread (computing)Default (computer science)SoftwareOcean currentRenewal theoryCASE <Informatik>Reduction of orderFunctional (mathematics)Resultant
20:02
Insertion lossRule of inferenceSynchronizationPauli exclusion principleOrder (biology)Insertion lossCASE <Informatik>Functional (mathematics)Goodness of fitCodeData dictionary
20:52
SynchronizationLoop (music)Function (mathematics)Context awarenessKeyboard shortcutTransport Layer SecurityTask (computing)Electric currentData managementSoftware developerAsynchronous Transfer ModeDefault (computer science)Error messageComputing platformRun time (program lifecycle phase)Read-only memoryResource allocationModule (mathematics)Core dumpBinary fileSimilarity (geometry)Installable File SystemOverhead (computing)Line (geometry)CodeFunctional (mathematics)Error messageConnected spaceCore dumpServer (computing)Memory managementTask (computing)Loop (music)Revision controlEndliche ModelltheorieCASE <Informatik>Event horizonWeb 2.0Parameter (computer programming)Software developerMultiplication signLocal ringGoodness of fitComputer fileKeyboard shortcutCoroutineProfil (magazine)Digital photographyWindowTransport Layer SecurityEmailPhysical systemDirected graphKernel (computing)Module (mathematics)Computer animation
24:47
Binary fileSimilarity (geometry)Function (mathematics)Installable File SystemOverhead (computing)Context awarenessProjective planeRevision controlFile systemComputer fileLibrary (computing)Data managementContext awarenessModule (mathematics)Message passingDirectory serviceLine (geometry)String (computer science)SoftwareForm (programming)Binary fileFunctional (mathematics)Endliche ModelltheorieGoodness of fitComputer animation
27:19
CASE <Informatik>Social classMedical imagingPresentation of a groupTupleComputer animation
27:54
CASE <Informatik>Correspondence (mathematics)TuplePosition operatorPositional notationTable (information)Read-only memorySocial classMeeting/Interview
28:25
Java appletElement (mathematics)ResultantField (computer science)Social classFreewarePosition operatorComputer animation
29:09
Java appletMeeting/Interview
Transcript: English(auto-generated)
00:06
In fact, it's just a joke, it's not about Python, it's just Java. I'm a little bit stressed and I like to play with the jokes. In French that's better, in English that would be harder. We are here just about the new feature about Python 3.7, just that.
00:26
Not about 2.7 and not about 2.8. Okay, we are sure that we will use the last version of Python. What can you say? Oh, an empty slide, sorry. About me. I'm just Stefan.
00:41
I'm a small contributor of Python, of the Python ecosystem. I am a small contributor of EuroPython because I have the badge with organizer. Thank you to be here. And I'm a fellow member of the PSF, member of EuroPython Society, and you can see there's a blah, blah, blah, blah. If you know a movie with Dracula, that's fine.
01:04
So with Python 3.7, we have some pep to respect, of course, because it's a contract with the new version of Python. The first one is just the awesome break point. I'm going to explain that because sometimes when you give a technical training to some
01:23
newbie, newbies, sorry, they want to know how to debug with Python. Usually they try to with PyCharm or maybe with Atom or whatever. In this case, we have a new built-in function for that. I'm going to explain about the data classes. I don't want to go into details about this project, but I'm going to give you an overview.
01:47
Mainly, how to define the getter and the dir method for a new model, and we will see some other stuff, and the most important is maybe the break point for the first point. So the break point.
02:02
When you want to start to debug your software, usually, you have this kind of code. I know that the code will give this kind of error. Just divide by zero is just forbidden, okay? If I would like to debug it, I have two solutions. Firstly, I'm going to use the print function, yes, why not, yes, and we could use the
02:25
debugger keyword from JavaScript, no, no, no, no, I'm not with JavaScript. Just with Python, and with Python, we can have a lot of debuggers, okay? We have PDB, I'm going to start with the first one, PDB, PUDB, IPDB, PDPP, etc.
02:50
Okay? Do you understand? When you start to learn Python, there's a problem. Which debugger can I use? In this case, that's really difficult. For that, there is a new built-in function.
03:02
Just, no, not PDB set trace, but this one, or not this one, just breakpoint, okay? So when you will start to use the debugger, when you start Python for the kids, you have this one, PDB, PDB is really ugly but functional.
03:23
We don't have any problem with this one. We can find a bug really easily with this one, okay? But if you like to have an NQRS console, an NQRS interface, you can use PUDB, okay? With the new keyword, new function, breakpoint, you can define your debugger on the command
03:44
line just after. Firstly, with the breakpoint, you have an environment variable, sorry, from English, but where you can specify your debugger on the command line.
04:01
The first one, if you don't specify something, it's just PDB, okay? We have an example just here. If you want to specify PUDB, you could use this command line, okay? And if you want to use IPython with your project, because you like IPython and the
04:25
magic stuff of IPython, you can use that, okay? So there is an interesting thing with the breakpoint with this new function. If you don't specify the breakpoint on the command line, just with breakpoint is
04:40
equal to zero, we will disable everything, all the beginning session, okay? In this case, you could execute your script without a debugger. Yes? I'm not sure, okay. So, and of course, sometimes, you can use the print function if you prefer,
05:02
because we like the print function, okay? So, it's just the first new feature of Python 3.7. Not really funny, but I like to use it just with the newbies because sometimes... I'm sorry, sir, but which debugger can we use? Well, this one, okay?
05:21
Next point, about the data classes. Who knows the data classes? Hey, great. Yes, you are a code developer, of course. So, the data classes. What is a data class? Who wants to represent a user or just a person or an account?
05:45
Usually, we can use the name tuple, we could use the tuple or just a dictionary or maybe just a class, okay? But sometimes, that's really difficult to explain to a new person.
06:00
Why I need to have a very big class? In my example, I'm going to explain, I'm going to give this example just with a tuple. I'm going to represent a person with this tuple. Welcome is my first name and last name and my age. Sorry, I'm not young. And the problem with a tuple, we don't know if this index is the first name,
06:24
the last name or the age, okay? We could use the dict. Okay, we change just the form. We use a dict, first name, last name and age. We can use for that the attribute, not the attribute but the name with the dictionary.
06:42
But for me, I prefer to use just the dot notation, person dot first name, okay? We could use the collection name tuple. The same thing, we can use it with a tuple, we can define the parameter
07:02
and we have the dot notation, okay? I have a mix between the tuple and maybe a class. But it's not very perfect. Because in this case, we don't specify the tip of the variable. We cannot override it. There is no default values for this one. So in this case, maybe we could use a simple class, okay?
07:25
There is a Java developer here, yes. Just one? I'm sorry for you, ah, two. Okay. Now that when you want to develop one class for an object, you have to define the properties and the method,
07:43
the getter, et cetera and the rest, okay? And we can have this kind of code. That's very boring and really verbose. And for that, Python can be a better choice for that, okay? So in this case, we have the person with the constructor.
08:02
I'm going to give some parameters. I'm going to give this parameter to the instance. I'll have a representation with a string, just a method for the equality, where I'm going to test the first name, last name and the age.
08:21
And maybe if I would like to make a comparison, the LT. So, but I would like to replace that with another thing, just the data classes, in this case. Before, after, before, after.
08:43
For the next Java developer, you can come, we have a lot of jobs for you. That's really easy with Python because in fact, we don't need to improve the interpreter. It's just a new class, a new function in the standard library. And from that, we will create a new function
09:02
for this class, okay? So with this code, I have the same result. But I can define the kind, the tip of the name, or latitude or longitude, the default value. With the name tuple, with a tuple, with a dict, I cannot do that.
09:21
Here, in five lines, I have the result, okay? So, of course, with the data classes, I could define a new method or just a new property. In this case, I'm going to define the full name, my full name, with a representation, and I can use it like a normal class, okay?
09:42
Of course, with the data classes, we can define some, we have some properties, options, when we use the decorator. For example, we have init, just here. This one will create the init function, the constructor of the class, if this one does not exist. It will be the same for wrap, equal, order, and frozen.
10:04
If you know the frozen set, you will have the same result. In fact, frozen is just for the name tuple, because a name tuple is just in read-only mode. So one example, where I'm going to specify keyword, parameter just here, and I'm going to have an exception
10:22
when I try to assign a new value to my age, okay? And of course, with the data classes, I can try to work with the inheritance. I can create a person, and I can iterate from this person and create a new user class.
10:41
Can we do that with the name tuple? No? With the tuple? No, with the dictionary? No. Why come this new feature is just the data classes? Okay? The next point is just, for example,
11:01
you are developing a new feature, or a new function, and in your source code, you have some deprecated functions, okay? With this kind of code, I'm going to check if I have a function in the deprecated list, this one, our function,
11:20
and if my function is deprecated, I'm going to execute a warning at the execution, at the read time, just with that. The system, when we will load the library and try to import this function, we will get just a warning, a deprecation warning, okay?
11:42
It's a good reason if you want to clear your code, a good tool for that, okay? So, for the rest, we have the dir function. This one can just change the list of the attributes of a model. It's just sympathetic for the developer, okay?
12:03
And it's just an example. Okay. Another thing, sorry. When you are stressed, you have to drink a lot of water. Who is using the annotation for the type itings?
12:24
Okay, we have the current version of Python. This example, is correct or not? No, in French, no. No, signore, si, qu'est pas.
12:41
In fact, no, it's not the case, because we will have a problem. Here, we try to define the node class, and this node class will be used just here when we define the type of left and right. And in this, we have a problem, because the interpreter has not finished to read the description of the node class.
13:01
For that, if we try to execute the code, we will get an exception. Just the node, this node is not defined. Yes, okay, but how can I use my node? Because type itings is really useful, we can use mypy, and I can detect a lot of bugs with the static analyzer.
13:22
There is a pep for that. Oh, sorry. But if you want to solve the problem, just use the string annotation, okay? There is a pep. This pep is the annotation.
13:42
You can import it from future, and with this import, we can define left as a node, right as a node. In this case, we don't have to define a forward reference, okay? Because this notation with the string is a forward reference.
14:03
There is this kind of stuff with C++ and the forward declaration, okay? And if we mix the annotation and the data classes, we can have this result, just that, okay? We have an optional node, the default value is just none,
14:22
and does the same for the right attribute. Okay, another point. Who likes or love to make some benchmark with Python? Is there a scientist here? Yeah, no, no, yes, of course.
14:43
I'm going to present you the master of the performance of Python. It's just here, Victor. And in fact, it has found a problem with Python with the current implementation of the time module. We have a problem because the current time module uses the float system, the float type,
15:02
just for the representation of the number when we need some seconds or millisecond. And because we use the float, we have a limitation. This limitation is just for one and two and four days. After that, you will have a problem with the precision.
15:21
Okay? In this example, the first example came from Victor. It's just about the benchmark of Python. When you call a function, sometimes this call can take less than 100 nanoseconds. And for that, I started to write this new PEP and introduce new function, where you can work
15:43
at the nanosecond level. Okay? This example is just with the normal monotonic function, where we can see the float. And with the nanosecond resolution, we have a Python integer,
16:01
and you have all the information about that. Okay? So the next point is just about the deprecation warning. Just before, when we have an exception, we can raise a deprecation warning. There was a problem since the last, not the last version, but since the version 3.2.
16:24
In fact, by default, the deprecation warning was hidden by default. With the new version of Python, 3.7, we will show the deprecation warning in the main namespace. Just that. And for the rest, for the future warning,
16:42
and the pending deprecation warning, we will show them in the test. Just that. So, the next point. Sorry, there is a lot of next point. Sorry. That's very difficult.
17:01
The PEP is very long, and I have to read a lot of PEPs and try to make a small syntax of that is very difficult. For example, the context variable is really useful for us, for the developer using the asynchronous development, because, for example, when we use a website,
17:20
when we try to develop a website with Django or Flask or another framework, we will use a thread, and this thread will receive the request. But there is a problem with the asynchronous environment, because we use only one thread, and we have a lot of asynchronous tasks
17:41
at the same time. For that, we have a new model, the context bar. This context bar will provide the context object and the context bar object. The context will be associated to the OS thread, and the context bar will contain the value of the context for the context, for the current context.
18:02
In this example, I create just a context bar with a default value. I'm going to set the value and just read before, and I'm going to execute the main function with a new context, okay? And if I see the result,
18:22
the first print will be 42, the second one will be inside spam, just here. We can see that we have the original value. We can change it with set arm, just here, and when we go outside of the context of the function,
18:42
we will get the first version of the value. So, when, okay, thank you. When can we use that? Maybe when you want to develop an asynchronous web service. Okay, for example. Because with the asynchronous web service,
19:01
we don't use, we have a lot, we only have one thread and not a lot of thread. And in this case, we will define a new context bar, set just the default value, and we can use it just with the reference, current user in this case, okay? So, with the context variable,
19:24
you can start to develop an asynchronous software without any problem, because you don't have the problem with the TLS. For the next thing, it's just about the documentation. With Python 3.7, we have, of course, the documentation in English, maybe,
19:43
and now in French, in Japan, and in Korean. With Spanish or Italian, if you want to contribute Chinese, okay, you can become the new contributor of Chinese, for the Chinese. Yeah, just that.
20:04
But, in fact, if we follow the PEP of Python 3.7, it's not a very funny story for you, sometimes we develop some new feature without a PEP, is the case with the dictionary. For example, the new dict in Python 3.7,
20:23
we guarantee the insertion order, just that. And it's a leitmotif of Guido. PEP insertion order, yeah, good. Async and await are keywords.
20:41
That was not the case before. There was a duplication warning. No, it's a keyword. If you want to migrate your code to Python 3.7, don't forget to change the name of your variable or function, just that. Stop, you cannot do that. Sorry, for you. If you have one million in the line of code,
21:02
that's for you, not for me, sorry. No, I am in the same case. And we cannot do that, def await. Sorry. So, yes. With Python 3.7, we have some improvements about asyncio.
21:22
Who likes to use asyncio? One, two, three, four, yes, 10. Yes, good. We see that we can continue to work on asyncio. Okay, when you want to use asyncio,
21:40
you have to define an event loop, okay? With this event loop, you will run until the coroutine is just finished. And after that, you will close the loop. Since the new version of Python 3.7, instead of that, just that, asyncio run.
22:02
Sorry, two years for this function, but it's two years. Yeah, with asyncio, we have some new function, create task, how to create a task is just a shortcut to the create task of the loop. We can start a TLS connection.
22:22
For example, with an unsecure connection, you can start a TLS connection. You can get the current task. You can have all the tasks from the loop. And if you like to send some files from the web server to your customer,
22:41
you can use the sock send file. This function will use the kernel, Linux in this case. About the performance, we can see that the get event loop has been completely rewritten in C, just for the performance,
23:01
more or less 15 times faster. That's the case for the get error function, the sleep function, and the future, because future has been improved. We have some new function on the command line. For example, if you want to know if your import is just slow and wide,
23:22
you can use x import time. You will have a profiling of your import. This new parameter is just, if you want to use Python in UTF-8,
23:42
because in some case, on Windows, Linux, Python will use the locale of the current system, and sometimes we can have some problem. With this parameter on the command line, we will force Python to use UTF-8.
24:01
Another thing, if you're a developer of Python, and if you want to have a very big dump of your stack trace, thank you, you can use Python X and dev. This one will enable the debug model of asyncio.
24:20
We'll use the photo handler module, just for the dump, because we like to have a very big dump of a crash, and install some debug hooks for the memory allocation. So, for the rest, and after that I'm going to finish in five minutes, maybe. Okay.
24:40
Do you like to load your data from your packages? We have a package resource? Just one. Yes, good. Okay, there is a new function, a new model, resources. Resources, it just is the same behavior of the previous,
25:03
but with the performance, and without the bullshit. Okay? In fact, I would like to be clean, but PKG resource like to do a lot of useless stuff. Okay? We have resources, in fact,
25:21
there is a very big improvement about the performances, and now we can read a file from the file system, a zip file from the network, or whatever. Okay? A small example, I have my package. I would like to read this file, message.em.
25:42
With the standard library, I could do that. Try to find the directory of the project, and the directory of the data. Try to make the path for the message, and read the file. Or, with the pathlib path, we can do that.
26:04
We've imported resources in two lines, just one line. The second one is just for the import, but with one line, you can load a binary file from your package, or from your zip file, or whatever.
26:21
You can do that with a context manager, if you want to play with the reference to the file, and if you don't like this form with the string, you can use just your model. Import your model, and you want to get a file from your model. You can do. So, of course, we have many improvements in bugface
26:42
in Python 3.7, and Python is faster than the last version, of course. Conclusion. I think that Python 3.7 is a great vintage, and yeah, just that.
27:01
Thank you. Do you have any question? No. Yeah, if there's any question, please stand down and come here. No? And what about Python?
27:21
Sir, please come up. Victor. Hi, thanks for your presentation. A question.
27:41
When is the case to use a named tuple, or a data class? For example, are the performance the same in the case of read-only,
28:04
in the case of read-only, thanks. In fact, we don't use, usually, we don't like to use a class, and we prefer to use just a tuple, because a tuple is just more efficient, okay? But then there is a problem.
28:20
You don't have the notation, the dot notation. You cannot know which position corresponds which element. You don't know that. But with the data classes, you have this result. And now, if you have a problem about the performance, you can use a slot with the data classes.
28:41
Just specify, underscore, underscore, slot, and you give your fields, just that. No more, no less. And you will get the same result. Maybe that will be slower, but it's not very significant. Okay, welcome.
29:02
Another question about Python 3, or 4.0, no? No, or Java, no? Okay. I confirm it's just the EuroPython, not EuroJava. Okay. Thank you. Thank you again.