What’s New In Python 3.7

Release:3.7.0a0
Date:mai 23, 2017

This article explains the new features in Python 3.7, compared to 3.6.

For full details, see the changelog.

Note

Prerelease users should be aware that this document is currently in draft form. It will be updated substantially as Python 3.7 moves towards release, so it’s worth checking back even after reading earlier versions.

Summary – Release highlights

New Features

Other Language Changes

  • More than 255 arguments can now be passed to a function, and a function can now have more than 255 parameters. (Contributed by Serhiy Storchaka in bpo-12844 and bpo-18896.)
  • bytes.fromhex() and bytearray.fromhex() now ignore all ASCII whitespace, not only spaces. (Contributed by Robert Xiao in bpo-28927.)
  • ImportError now displays module name and module __file__ path when from ... import ... fails. (Contributed by Matthias Bussonnier in bpo-29546.)
  • Circular imports involving absolute imports with binding a submodule to a name are now supported. (Contributed by Serhiy Storchaka in bpo-30024.)
  • object.__format__(x, '') is now equivalent to str(x) rather than format(str(self), ''). (Contributed by Serhiy Storchaka in bpo-28974.)

New Modules

  • None yet.

Improved Modules

cgi

parse_multipart() returns the same results as FieldStorage : for non-file fields, the value associated to a key is a list of strings, not bytes. (Contributed by Pierre Quentel in bpo-29979.)

binascii

The b2a_uu() function now accepts an optional backtick keyword argument. When it’s true, zeros are represented by '`' instead of spaces. (Contributed by Xiang Zhang in bpo-30103.)

contextlib

contextlib.asynccontextmanager() has been added. (Contributed by Jelle Zijlstra in bpo-29679.)

distutils

README.rst is now included in the list of distutils standard READMEs and therefore included in source distributions. (Contributed by Ryan Gonzalez in bpo-11913.)

http.server

SimpleHTTPRequestHandler supports the HTTP If-Modified-Since header. The server returns the 304 response status if the target file was not modified after the time specified in the header. (Contributed by Pierre Quentel in bpo-29654.)

locale

Added another argument monetary in format_string() of locale. If monetary is true, the conversion uses monetary thousands separator and grouping strings. (Contributed by Garvit in bpo-10379.)

math

New remainder() function, implementing the IEEE 754-style remainder operation. (Contributed by Mark Dickinson in bpo-29962.)

os

Added support for bytes paths in fwalk(). (Contributed by Serhiy Storchaka in bpo-28682.)

Added support for file descriptors in scandir() on Unix. (Contributed by Serhiy Storchaka in bpo-25996.)

unittest.mock

The sentinel attributes now preserve their identity when they are copied or pickled. (Contributed by Serhiy Storchaka in bpo-20804.)

xmlrpc.server

register_function() of xmlrpc.server.SimpleXMLRPCDispatcher and its subclasses can be used as a decorator. (Contributed by Xiang Zhang in bpo-7769.)

urllib.parse

urllib.parse.quote() has been updated to from RFC 2396 to RFC 3986, adding ~ to the set of characters that is never quoted by default. (Contributed by Christian Theune and Ratnadeep Debnath in bpo-16285.)

uu

Function encode() now accepts an optional backtick keyword argument. When it’s true, zeros are represented by '`' instead of spaces. (Contributed by Xiang Zhang in bpo-30103.)

Optimizations

  • Added two new opcodes: LOAD_METHOD and CALL_METHOD to avoid instantiation of bound method objects for method calls, which results in method calls being faster up to 20%. (Contributed by Yury Selivanov and INADA Naoki in bpo-26110.)
  • Searching some unlucky Unicode characters (like Ukrainian capital “Є”) in a string was to 25 times slower than searching other characters. Now it is slower only by 3 times in worst case. (Contributed by Serhiy Storchaka in bpo-24821.)
  • Fast implementation from standard C library is now used for functions tgamma(), lgamma(), erf() and erfc() in the math module. (Contributed by Serhiy Storchaka in bpo-26121.)
  • The os.fwalk() function has been sped up by 2 times. This was done using the os.scandir() function. (Contributed by Serhiy Storchaka in bpo-25996.)
  • Optimized case-insensitive matching and searching of regular expressions. Searching some patterns can now be up to 20 times faster. (Contributed by Serhiy Storchaka in bpo-30285.)

Build and C API Changes

Deprecated

Changes in the C API

  • The type of results of PyThread_start_new_thread() and PyThread_get_thread_ident(), and the id parameter of PyThreadState_SetAsyncExc() changed from long to unsigned long. (Contributed by Serhiy Storchaka in bpo-6532.)

Removed

API and Feature Removals

  • Unknown escapes consisting of '\' and an ASCII letter in replacement templates for re.sub() were deprecated in Python 3.5, and will now cause an error.
  • Removed support of the exclude argument in tarfile.TarFile.add(). It was deprecated in Python 2.7 and 3.2. Use the filter argument instead.
  • The splitunc() function in the ntpath module was deprecated in Python 3.1, and has now been removed. Use the splitdrive() function instead.
  • Functions bool(), float(), list() and tuple() no longer take keyword arguments. The first argument of int() can now be passes only as positional argument.
  • Removed previously deprecated in Python 2.4 classes Plist, Dict and _InternalDict in the plistlib module. Dict values in the result of functions readPlist() and readPlistFromBytes() are now normal dicts. You no longer can use attribute access to access items of these dictionaries.

Porting to Python 3.7

This section lists previously described changes and other bugfixes that may require changes to your code.

Changes in the Python API

  • A format string argument for string.Formatter.format() is now positional-only. Passing it as a keyword argument was deprecated in Python 3.5. (Contributed by Serhiy Storchaka in bpo-29193.)
  • Attributes key, value and coded_value of class http.cookies.Morsel are now read-only. Assigning to them was deprecated in Python 3.5. Use the set() method for setting them. (Contributed by Serhiy Storchaka in bpo-29192.)
  • Module, FunctionDef, AsyncFunctionDef, and ClassDef AST nodes now have a new docstring field. The first statement in their body is not considered as a docstring anymore. co_firstlineno and co_lnotab of code object for class and module are affected by this change. (Contributed by INADA Naoki and Eugene Toder in bpo-29463.)
  • The mode argument of os.makedirs() no longer affects the file permission bits of newly-created intermediate-level directories. To set their file permission bits you can set the umask before invoking makedirs(). (Contributed by Serhiy Storchaka in bpo-19930.)

CPython bytecode changes