What’s New In Python 3.6

Release:3.6.0a0
Date:February 15, 2016

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

For full details, see the Misc/NEWS file.

Note

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

Summary – Release highlights

PEP 498: Formatted string literals

Formatted string literals are a new kind of string literal, prefixed with 'f'. They are similar to the format strings accepted by str.format(). They contain replacement fields surrounded by curly braces. The replacement fields are expressions, which are evaluated at run time, and then formatted using the format() protocol.

>>> name = "Fred"
>>> f"He said his name is {name}."
'He said his name is Fred.'

See PEP 498 and the main documentation at Formatted string literals.

Other Language Changes

  • None yet.

New Modules

  • None yet.

Improved Modules

datetime

The datetime.strftime() and date.strftime() methods now support ISO 8601 date directives %G, %u and %V. (Contributed by Ashley Anderson in issue 12006.)

os

A new close() method allows explicitly closing a scandir() iterator. The scandir() iterator now supports the context manager protocol. If a scandir() iterator is neither exhausted nor explicitly closed a ResourceWarning will be emitted in its destructor. (Contributed by Serhiy Storchaka in issue 25994.)

pickle

Objects that need calling __new__ with keyword arguments can now be pickled using pickle protocols older than protocol version 4. Protocol version 4 already supports this case. (Contributed by Serhiy Storchaka in issue 24164.)

rlcompleter

Private and special attribute names now are omitted unless the prefix starts with underscores. A space or a colon is added after some completed keywords. (Contributed by Serhiy Storchaka in issue 25011 and issue 25209.)

Names of most attributes listed by dir() are now completed. Previously, names of properties and slots which were not yet created on an instance were excluded. (Contributed by Martin Panter in issue 25590.)

telnetlib

Telnet is now a context manager (contributed by Stéphane Wirtel in issue 25485).

urllib.robotparser

RobotFileParser now supports the Crawl-delay and Request-rate extensions. (Contributed by Nikolay Bogoychev in issue 16099.)

zipfile

A new ZipInfo.from_file() class method allows making a ZipInfo instance from a filesystem file. A new ZipInfo.is_dir() method can be used to check if the ZipInfo instance represents a directory. (Contributed by Thomas Kluyver in issue 26039.)

zlib

The compress() function now accepts keyword arguments. (Contributed by Aviv Palivoda in issue 26243.)

Optimizations

  • The ASCII decoder is now up to 60 times as fast for error handlers surrogateescape, ignore and replace (Contributed by Victor Stinner in issue 24870).
  • The ASCII and the Latin1 encoders are now up to 3 times as fast for the error handler surrogateescape (Contributed by Victor Stinner in issue 25227).
  • The UTF-8 encoder is now up to 75 times as fast for error handlers ignore, replace, surrogateescape, surrogatepass (Contributed by Victor Stinner in issue 25267).
  • The UTF-8 decoder is now up to 15 times as fast for error handlers ignore, replace and surrogateescape (Contributed by Victor Stinner in issue 25301).
  • bytes % args is now up to 2 times faster. (Contributed by Victor Stinner in issue 25349).
  • bytearray % args is now between 2.5 and 5 times faster. (Contributed by Victor Stinner in issue 25399).
  • Optimize bytes.fromhex() and bytearray.fromhex(): they are now between 2x and 3.5x faster. (Contributed by Victor Stinner in issue 25401).

Build and C API Changes

Deprecated

New Keywords

async and await are not recommended to be used as variable, class, function or module names. Introduced by PEP 492 in Python 3.5, they will become proper keywords in Python 3.7.

Deprecated Python modules, functions and methods

Deprecated functions and types of the C API

  • None yet.

Deprecated features

  • The pyvenv script has been deprecated in favour of python3 -m venv. This prevents confusion as to what Python interpreter pyvenv is connected to and thus what Python interpreter will be used by the virtual environment. (Contributed by Brett Cannon in issue 25154.)
  • When performing a relative import, falling back on __name__ and __path__ from the calling module when __spec__ or __package__ are not defined now raises an ImportWarning. (Contributed by Rose Ames in issue 25791.)

Deprecated Python behavior

Removed

API and Feature Removals

  • inspect.getmoduleinfo() was removed (was deprecated since CPython 3.3). inspect.getmodulename() should be used for obtaining the module name for a given path.
  • traceback.Ignore class and traceback.usage, traceback.modname, traceback.fullmodname, traceback.find_lines_from_code, traceback.find_lines, traceback.find_strings, traceback.find_executable_lines methods were removed from the traceback module. They were undocumented methods deprecated since Python 3.2 and equivalent functionality is available from private methods.

Porting to Python 3.6

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

Changes in the Python API

  • The format of the co_lnotab attribute of code objects changed to support negative line number delta. By default, Python does not emit bytecode with negative line number delta. Functions using frame.f_lineno, PyFrame_GetLineNumber() or PyCode_Addr2Line() are not affected. Functions decoding directly co_lnotab should be updated to use a signed 8-bit integer type for the line number delta, but it’s only required to support applications using negative line number delta. See Objects/lnotab_notes.txt for the co_lnotab format and how to decode it, and see the PEP 511 for the rationale.
  • The functions in the compileall module now return booleans instead of 1 or 0 to represent success or failure, respectively. Thanks to booleans being a subclass of integers, this should only be an issue if you were doing identity checks for 1 or 0. See issue 25768.
  • Reading the port attribute of urllib.parse.urlsplit() and urlparse() results now raises ValueError for out-of-range values, rather than returning None. See issue 20059.
  • The imp module now raises a DeprecationWarning instead of PendingDeprecationWarning.
  • The following modules have had missing APIs added to their __all__ attributes to match the documented APIs: calendar, csv, enum, fileinput, ftplib, logging, optparse, tarfile, threading and wave. This means they will export new symbols when import * is used. See issue 23883.
  • When performing a relative import, if __package__ does not compare equal to __spec__.parent then ImportWarning is raised. (Contributed by Brett Cannon in issue 25791.)
  • When a relative import is performed and no parent package is known, then ImportError will be raised. Previously, SystemError could be raised. (Contribute by Brett Cannon in issue 18018.)

Changes in the C API

  • Py_Exit() (and the main interpreter) now override the exit status with 120 if flushing buffered data failed. See issue 5319.