Python 3.10 will be here later this year and I wanted to go over some of the new features that will make our lives a little easier.
Multi line context managers.
This allows you to have multiple context managers in one with
statement. Instead of nesting context managers and the associated complexity, you can open everything at once in one expression. One use case would be having multiple files open. Now all the needed files can be opened and closed in the same context without nesting.
with (open('source_a.txt') as source_a,
open('source_b.txt') as source_b,
open('out.txt', 'w') as out):
<perform some logic to use the contents of all the files>
Error messages point to the fault and not to where the interpreter failed
Error messages have been improved so that if you are missing a bracket, the error handler will highlight the portion of code it thinks is missing the bracket instead of the first assignment that failed due to a missing bracket.
Previous implementations would report where the interpreter quit working, which was after the part of code that was missing the bracket. This should make troubleshooting code much easier.
Structural pattern matching, a.k.a. switch statements
Python is getting a feature that seems like a switch statement and can be used that way, but is actually more flexible that simple equality checks.
From the python docs "Patterns consist of sequences, mappings, primitive data types as well as class instances." This means that you can match on a broad array of criteria and can compare class instances against one another based on how the instances were created. You can also use array methods, like map
and match on the output from that.
This will be a massive improvement over using imperative if
clauses. This more declarative approach is easier to read and can be implemented in complex cases easier.
array
The index()
method of the builtin array is getting start and stop parameters. This will make it work more like Javascript.
Top comments (0)