Other New Features
00:21 If you stuck with the entire lesson on asynchronous I/O, you saw me open the files in read-binary mode even though they were going to be text files. That’s to get around this exact problem. If you open a file in text mode and Python assumes the wrong encoding, you will get an error.
00:48 Python 3.10 has added a command-line option to help with the encodings problem. If you set the flag when you execute Python, it will warn you if you attempt to open a file without an encoding explicitly set.
A context manager declares a block of code that automatically calls pre- and post-conditions on that code. The most common use of this is to manage resources such as file handles, sockets, or connections to databases. Prior to Python 3.10, if you wanted the
with block to interact with multiple context managers, you did this by separating them with commas and using a continuation character.
To make the syntax more consistent with things like imports, Python 3.10 has added the ability to use parentheses around the context managers. This works with or without the
as portion of the statement and with or without trailing commas.
01:55 Why would you need two context managers in a block? Well, the most common use case is reading from one file and writing to another. As a quick aside, strictly speaking, this isn’t a Python 3.10 thing.
02:07 This feature is part of the new PEG parser introduced in Python 3.9, but you didn’t have to use the PEG parser in 3.9. It was optional. In Python 3.10, the PEG parser is the only choice, so parentheses-wrapped multiple context managers is now fully supported.
02:25 Python uses the OpenSSL library to implement many of its cryptography features. Depending on how and where you get your Python, OpenSSL may ship with it or be a package dependency. As of Python 3.10, the minimum supported version of OpenSSL has been bumped up for security reasons. If you’re on Ubuntu 18.04, Red Hat 7, or CentOS 7, you’ll need to upgrade your OpenSSL library separately.
Two new values have been added to the
sys module to give you more information about your Python interpreter. Let’s see them in action. Built into
sys is the
modules value, a listing of all the modules that are loaded.
On my system, that’s 76 modules when I first start up Python. If I sort them and show the last five, you’ll see things like
zipimport. New in Python 3.10 is the
stdlib_module_names (standard library module names) value.
Modules that start with a dot are the top-level system modules. By comparing the first set with the
stdlib_module_names value, you get a complete picture of all the third-party libraries that you currently have loaded.
This time, I passed in a
-O argument to the Python interpreter, called
show_args.py, and passed
42 to that program.
sys.argv contains the name of the program,
sys.orig_argv shows all the arguments to the interpreter itself, as well as those to the program.
06:19 This last bit isn’t a feature, but a warning. Up until now, the minor version number of the Python interpreter has always been a single digit. Now that Python 3.10 is on the scene, you have to be careful how you detect versions with your code.
If your code was using the string version of comparison before 3.10, it would work, but now, it will fail. Unfortunately, the string
'3.6' is bigger than the string
'10' begins with a
You want to use the tuple format to compare instead. This means don’t use
sys.version_info. The tuple of
6 is smaller than the tuple of
10, so it works as advertised. If you happen to be using the flake8 linter, it automatically detects this problem.
07:07 Yet another good reason to use a linter. That’s it for the new features. The last lesson is next. I’ll quickly cover whether you should upgrade to 3.10 and then give you a summary of the course.
Become a Member to join the conversation.