Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Other New Features

00:00 In the previous lesson, I showed you the new strict parameter to the zip() function. In this lesson, I’ll cover a bunch of smaller changes in Python 3.10.

00:10 When you open a file in text mode in Python, it assumes the file’s encoding based on the operating system. Linux and Mac both use UTF-8, but the encoding varies for Windows.

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:38 The best practice is to always be explicit about the file encoding type in the call to the open() function. You can do this with the encoding parameter.

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.

01:01 The -X parameter is how you set all sorts of flags for your Python interpreter. This new one is warn_default_encoding. To see all the optional flags, type python --help on the command line.

01:15 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.

01:41 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.

02:52 Otherwise, your Python installer should take care of it for you.

02:57 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.

03:11 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 typing and zipimport. New in Python 3.10 is the stdlib_module_names (standard library module names) value.

03:27 This contains everything in the standard library. On my system, that’s 302 modules.

03:38 Applying the same sorting treatment, and you see a bunch of zip stuff leading to zlib, a different kind of zipping, and zoneinfo.

03:46 To see how these two concepts interact, let me load the aiofiles module.

03:53 It doesn’t have to be this one, just any old third-party library package will prove my point.

04:01 Looking at sys.modules again, and you see that 76 has grown to 157.

04:10 But stdlib_module_names hasn’t changed. The contents of modules are whatever’s loaded. The contents of stdlib_module_names is only the standard library modules.

04:23 With a bit of creative code, you can use these two concepts to see all the modules that are loaded that are not part of the standard library.

04:40 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.

04:56 The other feature added to sys is called orig_argv. Here, I’ve written a short program that prints two lines, one showing the contents of sys.argv, and the other, orig_argv.

05:08 Let me run this in the lower window.

05:13 sys.argv shows the name of the program and the arguments passed to it. As there were no arguments, argv contains just the name show_args.py, the program that was run.

05:25 orig_argv shows the arguments to the interpreter and the arguments to the program. For this simple case, you don’t see much else except that Python was called.

05:34 Let me try it again with some more arguments, though.

05:43 This time, I passed in a -X and -O argument to the Python interpreter, called show_args.py, and passed myarg and 42 to that program. sys.argv contains the name of the program, 'myarg', and '42'. sys.orig_argv shows all the arguments to the interpreter itself, as well as those to the program.

06:07 You’re not likely to need this very much, but it has one key benefit. If you’re spawning a second Python process, this gives you the ability to spawn it with the same options as the parent.

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.

06:33 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 '3.10' because '10' begins with a '1'.

06:47 You want to use the tuple format to compare instead. This means don’t use sys.version but sys.version_info. The tuple of 3 and 6 is smaller than the tuple of 3 and 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.