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
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,ignoreandreplace(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,replaceandsurrogateescape(Contributed by Victor Stinner in issue 25301). bytes % argsis now up to 2 times faster. (Contributed by Victor Stinner in issue 25349).bytearray % argsis now between 2.5 and 5 times faster. (Contributed by Victor Stinner in issue 25399).- Optimize
bytes.fromhex()andbytearray.fromhex(): they are now between 2x and 3.5x faster. (Contributed by Victor Stinner in issue 25401).
Build and C API Changes¶
- New
Py_FinalizeEx()API which indicates if flushing buffered data failed (issue 5319).
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¶
importlib.machinery.SourceFileLoader()andimportlib.machinery.SourcelessFileLoader()are now deprecated. They were the only remaining implementations ofimportlib.abc.Loader.load_module()inimportlibthat had not been deprecated in previous versions of Python in favour ofimportlib.abc.Loader.exec_module().
Deprecated functions and types of the C API¶
- None yet.
Deprecated features¶
- The
pyvenvscript has been deprecated in favour ofpython3 -m venv. This prevents confusion as to what Python interpreterpyvenvis 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 anImportWarning. (Contributed by Rose Ames in issue 25791.)
Deprecated Python behavior¶
- Raising the
StopIterationexception inside a generator will now generate aDeprecationWarning, and will trigger aRuntimeErrorin Python 3.7. See PEP 479: Change StopIteration handling inside generators for details.
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.Ignoreclass andtraceback.usage,traceback.modname,traceback.fullmodname,traceback.find_lines_from_code,traceback.find_lines,traceback.find_strings,traceback.find_executable_linesmethods were removed from thetracebackmodule. 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_lnotabattribute of code objects changed to support negative line number delta. By default, Python does not emit bytecode with negative line number delta. Functions usingframe.f_lineno,PyFrame_GetLineNumber()orPyCode_Addr2Line()are not affected. Functions decoding directlyco_lnotabshould 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. SeeObjects/lnotab_notes.txtfor theco_lnotabformat and how to decode it, and see the PEP 511 for the rationale. - The functions in the
compileallmodule now return booleans instead of1or0to 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 for1or0. See issue 25768. - Reading the
portattribute ofurllib.parse.urlsplit()andurlparse()results now raisesValueErrorfor out-of-range values, rather than returningNone. See issue 20059. - The
impmodule now raises aDeprecationWarninginstead ofPendingDeprecationWarning. - 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,threadingandwave. This means they will export new symbols whenimport *is used. See issue 23883. - When performing a relative import, if
__package__does not compare equal to__spec__.parentthenImportWarningis raised. (Contributed by Brett Cannon in issue 25791.) - When a relative import is performed and no parent package is known, then
ImportErrorwill be raised. Previously,SystemErrorcould 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.