Just as for function annotations, the Python interpreter does not attach any particular meaning to variable annotations and only stores them in the
__annotations__
attribute of a class or module.
In contrast to variable declarations in statically typed languages, the goal of annotation syntax is to provide an easy way to specify structured type metadata for third party tools and libraries via the abstract syntax tree and the
__annotations__
属性。
改进模块
¶
array
¶
Exhausted iterators of
array.array
will now stay exhausted even if the iterated array is extended. This is consistent with the behavior of other mutable sequences.
Contributed by Serhiy Storchaka in
bpo-26492
.
ast
¶
新的
ast.Constant
AST node has been added. It can be used by external AST optimizers for the purposes of constant folding.
Contributed by Victor Stinner in
bpo-26146
.
asyncio
¶
Starting with Python 3.6 the
asyncio
module is no longer provisional and its API is considered stable.
Notable changes in the
asyncio
module since Python 3.5.0 (all backported to 3.5.x due to the provisional status):
-
The
get_event_loop()
function has been changed to always return the currently running loop when called from coroutines and callbacks. (Contributed by Yury Selivanov in
bpo-28613
)。
-
The
ensure_future()
function and all functions that use it, such as
loop.run_until_complete()
, now accept all kinds of
可期待对象
. (Contributed by Yury Selivanov.)
-
New
run_coroutine_threadsafe()
function to submit coroutines to event loops from other threads. (Contributed by Vincent Michel.)
-
New
Transport.is_closing()
method to check if the transport is closing or closed. (Contributed by Yury Selivanov.)
-
The
loop.create_server()
method can now accept a list of hosts. (Contributed by Yann Sionneau.)
-
New
loop.create_future()
method to create Future objects. This allows alternative event loop implementations, such as
uvloop
, to provide a faster
asyncio.Future
implementation. (Contributed by Yury Selivanov in
bpo-27041
)。
-
New
loop.get_exception_handler()
method to get the current exception handler. (Contributed by Yury Selivanov in
bpo-27040
)。
-
New
StreamReader.readuntil()
method to read data from the stream until a separator bytes sequence appears. (Contributed by Mark Korenberg.)
-
The performance of
StreamReader.readexactly()
has been improved. (Contributed by Mark Korenberg in
bpo-28370
)。
-
The
loop.getaddrinfo()
method is optimized to avoid calling the system
getaddrinfo
function if the address is already resolved. (Contributed by A. Jesse Jiryu Davis.)
-
The
loop.stop()
method has been changed to stop the loop immediately after the current iteration. Any new callbacks scheduled as a result of the last iteration will be discarded. (Contributed by Guido van Rossum in
bpo-25593
)。
-
Future.set_exception
will now raise
TypeError
when passed an instance of the
StopIteration
exception. (Contributed by Chris Angelico in
bpo-26221
)。
-
New
loop.connect_accepted_socket()
method to be used by servers that accept connections outside of asyncio, but that use asyncio to handle them. (Contributed by Jim Fulton in
bpo-27392
)。
-
TCP_NODELAY
flag is now set for all TCP transports by default. (Contributed by Yury Selivanov in
bpo-27456
)。
-
New
loop.shutdown_asyncgens()
to properly close pending asynchronous generators before closing the loop. (Contributed by Yury Selivanov in
bpo-28003
)。
-
Future
and
Task
classes now have an optimized C implementation which makes asyncio code up to 30% faster. (Contributed by Yury Selivanov and INADA Naoki in
bpo-26081
and
bpo-28544
)。
binascii
¶
The
b2a_base64()
function now accepts an optional
newline
keyword argument to control whether the newline character is appended to the return value. (Contributed by Victor Stinner in
bpo-25357
)。
collections
¶
新的
Collection
abstract base class has been added to represent sized iterable container classes. (Contributed by Ivan Levkivskyi, docs by Neil Girdhar in
bpo-27598
)。
新的
Reversible
abstract base class represents iterable classes that also provide the
__reversed__()
method. (Contributed by Ivan Levkivskyi in
bpo-25987
)。
新的
AsyncGenerator
abstract base class represents asynchronous generators. (Contributed by Yury Selivanov in
bpo-28720
)。
The
namedtuple()
function now accepts an optional keyword argument
模块
, which, when specified, is used for the
__module__
attribute of the returned named tuple class. (Contributed by Raymond Hettinger in
bpo-17941
)。
The
verbose
and
rename
arguments for
namedtuple()
are now keyword-only. (Contributed by Raymond Hettinger in
bpo-25628
)。
Recursive
collections.deque
instances can now be pickled. (Contributed by Serhiy Storchaka in
bpo-26482
)。
concurrent.futures
¶
The
ThreadPoolExecutor
class constructor now accepts an optional
thread_name_prefix
argument to make it possible to customize the names of the threads created by the pool. (Contributed by Gregory P. Smith in
bpo-27664
)。
contextlib
¶
The
contextlib.AbstractContextManager
class has been added to provide an abstract base class for context managers. It provides a sensible default implementation for
__enter__()
which returns
self
and leaves
__exit__()
an abstract method. A matching class has been added to the
typing
module as
typing.ContextManager
. (Contributed by Brett Cannon in
bpo-25609
)。
datetime
¶
The
datetime
and
time
classes have the new
fold
attribute used to disambiguate local time when necessary. Many functions in the
datetime
have been updated to support local time disambiguation. See
Local Time Disambiguation
section for more information. (Contributed by Alexander Belopolsky in
bpo-24773
)。
The
datetime.strftime()
and
date.strftime()
methods now support ISO 8601 date directives
%G
,
%u
and
%V
. (Contributed by Ashley Anderson in
bpo-12006
)。
The
datetime.isoformat()
function now accepts an optional
timespec
argument that specifies the number of additional components of the time value to include. (Contributed by Alessandro Cucci and Alexander Belopolsky in
bpo-19475
)。
The
datetime.combine()
now accepts an optional
tzinfo
argument. (Contributed by Alexander Belopolsky in
bpo-27661
)。
decimal
¶
New
Decimal.as_integer_ratio()
method that returns a pair
(n, d)
of integers that represent the given
Decimal
instance as a fraction, in lowest terms and with a positive denominator:
>>> Decimal('-3.14').as_integer_ratio()
(-157, 50)
(Contributed by Stefan Krah amd Mark Dickinson in
bpo-25928
)。
distutils
¶
The
default_format
attribute has been removed from
distutils.command.sdist.sdist
和
formats
attribute defaults to
['gztar']
. Although not anticipated, any code relying on the presence of
default_format
may need to be adapted. See
bpo-27819
了解更多细节。
email
¶
The new email API, enabled via the
policy
keyword to various constructors, is no longer provisional. The
email
documentation has been reorganized and rewritten to focus on the new API, while retaining the old documentation for the legacy API. (Contributed by R. David Murray in
bpo-24277
)。
The
email.mime
classes now all accept an optional
policy
keyword. (Contributed by Berker Peksag in
bpo-27331
)。
The
DecodedGenerator
now supports the
policy
关键词。
There is a new
policy
属性,
message_factory
, that controls what class is used by default when the parser creates new message objects. For the
email.policy.compat32
policy this is
Message
, for the new policies it is
EmailMessage
. (Contributed by R. David Murray in
bpo-20476
)。
encodings
¶
On Windows, added the
'oem'
encoding to use
CP_OEMCP
,和
'ansi'
alias for the existing
'mbcs'
encoding, which uses the
CP_ACP
code page. (Contributed by Steve Dower in
bpo-27959
)。
enum
¶
Two new enumeration base classes have been added to the
enum
模块:
Flag
and
IntFlags
. Both are used to define constants that can be combined using the bitwise operators. (Contributed by Ethan Furman in
bpo-23591
)。
Many standard library modules have been updated to use the
IntFlags
class for their constants.
新的
enum.auto
value can be used to assign values to enum members automatically:
>>> from enum import Enum, auto
>>> class Color(Enum):
... red = auto()
... blue = auto()
... green = auto()
...
>>> list(Color)
[<Color.red: 1>, <Color.blue: 2>, <Color.green: 3>]
hashlib
¶
hashlib
supports OpenSSL 1.1.0. The minimum recommend version is 1.0.2. (Contributed by Christian Heimes in
bpo-26470
)。
BLAKE2 hash functions were added to the module.
blake2b()
and
blake2s()
are always available and support the full feature set of BLAKE2. (Contributed by Christian Heimes in
bpo-26798
based on code by Dmitry Chestnykh and Samuel Neves. Documentation written by Dmitry Chestnykh.)
The SHA-3 hash functions
sha3_224()
,
sha3_256()
,
sha3_384()
,
sha3_512()
, and SHAKE hash functions
shake_128()
and
shake_256()
were added. (Contributed by Christian Heimes in
bpo-16113
. Keccak Code Package by Guido Bertoni, Joan Daemen, Michaël Peeters, Gilles Van Assche, and Ronny Van Keer.)
The password-based key derivation function
scrypt()
is now available with OpenSSL 1.1.0 and newer. (Contributed by Christian Heimes in
bpo-27928
)。
idlelib and IDLE
¶
The idlelib package is being modernized and refactored to make IDLE look and work better and to make the code easier to understand, test, and improve. Part of making IDLE look better, especially on Linux and Mac, is using ttk widgets, mostly in the dialogs. As a result, IDLE no longer runs with tcl/tk 8.4. It now requires tcl/tk 8.5 or 8.6. We recommend running the latest release of either.
‘Modernizing’ includes renaming and consolidation of idlelib modules. The renaming of files with partial uppercase names is similar to the renaming of, for instance, Tkinter and TkFont to tkinter and tkinter.font in 3.0. As a result, imports of idlelib files that worked in 3.5 will usually not work in 3.6. At least a module name change will be needed (see idlelib/README.txt), sometimes more. (Name changes contributed by Al Swiegart and Terry Reedy in
bpo-24225
. Most idlelib patches since have been and will be part of the process.)
In compensation, the eventual result with be that some idlelib classes will be easier to use, with better APIs and docstrings explaining them. Additional useful information will be added to idlelib when available.
New in 3.6.2:
Multiple fixes for autocompletion. (Contributed by Louie Lu in
bpo-15786
)。
New in 3.6.3:
Module Browser (on the File menu, formerly called Class Browser), now displays nested functions and classes in addition to top-level functions and classes. (Contributed by Guilherme Polo, Cheryl Sabella, and Terry Jan Reedy in
bpo-1612262
)。
The IDLE features formerly implemented as extensions have been reimplemented as normal features. Their settings have been moved from the Extensions tab to other dialog tabs. (Contributed by Charles Wohlganger and Terry Jan Reedy in
bpo-27099
)。
The Settings dialog (Options, Configure IDLE) has been partly rewritten to improve both appearance and function. (Contributed by Cheryl Sabella and Terry Jan Reedy in multiple issues.)
New in 3.6.4:
The font sample now includes a selection of non-Latin characters so that users can better see the effect of selecting a particular font. (Contributed by Terry Jan Reedy in
bpo-13802
.) The sample can be edited to include other characters. (Contributed by Serhiy Storchaka in
bpo-31860
)。
New in 3.6.6:
Editor code context option revised. Box displays all context lines up to maxlines. Clicking on a context line jumps the editor to that line. Context colors for custom themes is added to Highlights tab of Settings dialog. (Contributed by Cheryl Sabella and Terry Jan Reedy in
bpo-33642
,
bpo-33768
,和
bpo-33679
)。
On Windows, a new API call tells Windows that tk scales for DPI. On Windows 8.1+ or 10, with DPI compatibility properties of the Python binary unchanged, and a monitor resolution greater than 96 DPI, this should make text and lines sharper. It should otherwise have no effect. (Contributed by Terry Jan Reedy in
bpo-33656
)。
New in 3.6.7:
Output over N lines (50 by default) is squeezed down to a button. N can be changed in the PyShell section of the General page of the Settings dialog. Fewer, but possibly extra long, lines can be squeezed by right clicking on the output. Squeezed output can be expanded in place by double-clicking the button or into the clipboard or a separate window by right-clicking the button. (Contributed by Tal Einat in
bpo-1529353
)。
inspect
¶
The
inspect.signature()
function now reports the implicit
.0
parameters generated by the compiler for comprehension and generator expression scopes as if they were positional-only parameters called
implicit0
. (Contributed by Jelle Zijlstra in
bpo-19611
)。
To reduce code churn when upgrading from Python 2.7 and the legacy
inspect.getargspec()
API, the previously documented deprecation of
inspect.getfullargspec()
has been reversed. While this function is convenient for single/source Python 2/3 code bases, the richer
inspect.signature()
interface remains the recommended approach for new code. (Contributed by Nick Coghlan in
bpo-27172
)
json
¶
json.load()
and
json.loads()
now support binary input. Encoded JSON should be represented using either UTF-8, UTF-16, or UTF-32. (Contributed by Serhiy Storchaka in
bpo-17909
)。
math
¶
The tau (
τ
) constant has been added to the
math
and
cmath
modules. (Contributed by Lisa Roach in
bpo-12345
,见
PEP 628
了解细节。)
os
¶
See the summary of
PEP 519
for details on how the
os
and
os.path
modules now support
像路径对象
.
scandir()
现在支持
bytes
路径在 Windows。
新的
close()
method allows explicitly closing a
scandir()
iterator. The
scandir()
iterator now supports the
上下文管理器
protocol. If a
scandir()
迭代器既未耗尽,也未明确关闭
ResourceWarning
will be emitted in its destructor. (Contributed by Serhiy Storchaka in
bpo-25994
)。
在 Linux,
os.urandom()
now blocks until the system urandom entropy pool is initialized to increase the security. See the
PEP 524
for the rationale.
Linux
getrandom()
syscall (get random bytes) is now exposed as the new
os.getrandom()
function. (Contributed by Victor Stinner, part of the
PEP 524
)
pdb
¶
The
Pdb
class constructor has a new optional
readrc
argument to control whether
.pdbrc
files should be read.
pickle
¶
Objects that need
__new__
called 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
bpo-24164
)。
pydoc
¶
The
pydoc
module has learned to respect the
MANPAGER
environment variable. (Contributed by Matthias Klose in
bpo-8637
)。
help()
and
pydoc
can now list named tuple fields in the order they were defined rather than alphabetically. (Contributed by Raymond Hettinger in
bpo-24879
)。
random
¶
新的
choices()
function returns a list of elements of specified size from the given population with optional weights. (Contributed by Raymond Hettinger in
bpo-18844
)。
re
¶
Added support of modifier spans in regular expressions. Examples:
'(?i:p)ython'
匹配
'python'
and
'Python'
, but not
'PYTHON'
;
'(?i)g(?-i:v)r'
匹配
'GvR'
and
'gvr'
, but not
'GVR'
. (Contributed by Serhiy Storchaka in
bpo-433028
)。
Match object groups can be accessed by
__getitem__
, which is equivalent to
group()
. So
mo['name']
现在相当于
mo.group('name')
. (Contributed by Eric Smith in
bpo-24454
)。
Match
objects now support
index-like objects
as group indices. (Contributed by Jeroen Demeyer and Xiang Zhang in
bpo-27177
)。
readline
¶
添加
set_auto_history()
to enable or disable automatic addition of input to the history list. (Contributed by Tyler Crompton in
bpo-26870
)。
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
bpo-25011
and
bpo-25209
)。
site
¶
When specifying paths to add to
sys.path
在
.pth
file, you may now specify file paths on top of directories (e.g. zip files). (Contributed by Wolfgang Langner in
bpo-26587
).
socket
¶
The
ioctl()
function now supports the
SIO_LOOPBACK_FAST_PATH
control code. (Contributed by Daniel Stokes in
bpo-26536
)。
The
getsockopt()
constants
SO_DOMAIN
,
SO_PROTOCOL
,
SO_PEERSEC
,和
SO_PASSSEC
are now supported. (Contributed by Christian Heimes in
bpo-26907
)。
The
setsockopt()
now supports the
setsockopt(level, optname, None, optlen: int)
form. (Contributed by Christian Heimes in
bpo-27744
)。
The socket module now supports the address family
AF_ALG
to interface with Linux Kernel crypto API.
ALG_*
,
SOL_ALG
and
sendmsg_afalg()
were added. (Contributed by Christian Heimes in
bpo-27744
with support from Victor Stinner.)
New Linux constants
TCP_USER_TIMEOUT
and
TCP_CONGESTION
were added. (Contributed by Omar Sandoval,
bpo-26273
).
ssl
¶
ssl
supports OpenSSL 1.1.0. The minimum recommend version is 1.0.2. (Contributed by Christian Heimes in
bpo-26470
)。
3DES has been removed from the default cipher suites and ChaCha20 Poly1305 cipher suites have been added. (Contributed by Christian Heimes in
bpo-27850
and
bpo-27766
)。
SSLContext
has better default configuration for options and ciphers. (Contributed by Christian Heimes in
bpo-28043
)。
SSL session can be copied from one client-side connection to another with the new
SSLSession
class. TLS session resumption can speed up the initial handshake, reduce latency and improve performance (Contributed by Christian Heimes in
bpo-19500
based on a draft by Alex Warhawk.)
新的
get_ciphers()
method can be used to get a list of enabled ciphers in order of cipher priority.
All constants and flags have been converted to
IntEnum
and
IntFlags
. (Contributed by Christian Heimes in
bpo-28025
)。
Server and client-side specific TLS protocols for
SSLContext
were added. (Contributed by Christian Heimes in
bpo-28085
)。
添加
ssl.SSLContext.post_handshake_auth
to enable and
ssl.SSLSocket.verify_client_post_handshake()
to initiate TLS 1.3 post-handshake authentication. (Contributed by Christian Heimes in
gh-78851
)。
struct
¶
struct
now supports IEEE 754 half-precision floats via the
'e'
format specifier. (Contributed by Eli Stevens, Mark Dickinson in
bpo-11734
)。
subprocess
¶
subprocess.Popen
destructor now emits a
ResourceWarning
warning if the child process is still running. Use the context manager protocol (
with
proc: ...
) or explicitly call the
wait()
method to read the exit status of the child process. (Contributed by Victor Stinner in
bpo-26741
)。
The
subprocess.Popen
constructor and all functions that pass arguments through to it now accept
encoding
and
errors
arguments. Specifying either of these will enable text mode for the
stdin
,
stdout
and
stderr
streams. (Contributed by Steve Dower in
bpo-6135
)。
sys
¶
新的
getfilesystemencodeerrors()
function returns the name of the error mode used to convert between Unicode filenames and bytes filenames. (Contributed by Steve Dower in
bpo-27781
)。
On Windows the return value of the
getwindowsversion()
function now includes the
platform_version
field which contains the accurate major version, minor version and build number of the current operating system, rather than the version that is being emulated for the process (Contributed by Steve Dower in
bpo-27932
)。
telnetlib
¶
telnetlib.Telnet
is now a context manager (contributed by Stéphane Wirtel in
bpo-25485
).
timeit
¶
新的
Timer.autorange()
convenience method has been added to call
Timer.timeit()
repeatedly so that the total run time is greater or equal to 200 milliseconds. (Contributed by Steven D’Aprano in
bpo-6422
)。
timeit
now warns when there is substantial (4x) variance between best and worst times. (Contributed by Serhiy Storchaka in
bpo-23552
)。
tkinter
¶
Added methods
trace_add()
,
trace_remove()
and
trace_info()
在
tkinter.Variable
class. They replace old methods
trace_variable()
,
trace()
,
trace_vdelete()
and
trace_vinfo()
that use obsolete Tcl commands and might not work in future versions of Tcl. (Contributed by Serhiy Storchaka in
bpo-22115
).
traceback
¶
Both the traceback module and the interpreter’s builtin exception display now abbreviate long sequences of repeated lines in tracebacks as shown in the following example:
>>> def f(): f()
...
>>> f()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in f
File "<stdin>", line 1, in f
File "<stdin>", line 1, in f
[Previous line repeated 995 more times]
RecursionError: maximum recursion depth exceeded
(Contributed by Emanuel Barry in
bpo-26823
)。
tracemalloc
¶
The
tracemalloc
module now supports tracing memory allocations in multiple different address spaces.
新的
DomainFilter
filter class has been added to filter block traces by their address space (domain).
(Contributed by Victor Stinner in
bpo-26588
)。
typing
¶
由于
typing
module is
provisional
, all changes introduced in Python 3.6 have also been backported to Python 3.5.x.
The
typing
module has a much improved support for generic type aliases. For example
Dict[str, Tuple[S, T]]
is now a valid type annotation. (Contributed by Guido van Rossum in
Github #195
)。
The
typing.ContextManager
class has been added for representing
contextlib.AbstractContextManager
. (Contributed by Brett Cannon in
bpo-25609
)。
The
typing.Collection
class has been added for representing
collections.abc.Collection
. (Contributed by Ivan Levkivskyi in
bpo-27598
)。
The
typing.ClassVar
type construct has been added to mark class variables. As introduced in
PEP 526
, a variable annotation wrapped in ClassVar indicates that a given attribute is intended to be used as a class variable and should not be set on instances of that class. (Contributed by Ivan Levkivskyi in
Github #280
)。
新的
TYPE_CHECKING
constant that is assumed to be
True
by the static type checkers, but is
False
at runtime. (Contributed by Guido van Rossum in
Github #230
)。
新的
NewType()
helper function has been added to create lightweight distinct types for annotations:
from typing import NewType
UserId = NewType('UserId', int)
some_id = UserId(524313)
The static type checker will treat the new type as if it were a subclass of the original type. (Contributed by Ivan Levkivskyi in
Github #189
)。
unittest.mock
¶
The
Mock
class has the following improvements:
urllib.request
¶
If a HTTP request has a file or iterable body (other than a bytes object) but no
Content-Length
header, rather than throwing an error,
AbstractHTTPHandler
now falls back to use chunked transfer encoding. (Contributed by Demian Brecht and Rolf Krahl in
bpo-12319
)。
urllib.robotparser
¶
RobotFileParser
now supports the
Crawl-delay
and
Request-rate
extensions. (Contributed by Nikolay Bogoychev in
bpo-16099
)。
venv
¶
venv
accepts a new parameter
--prompt
. This parameter provides an alternative prefix for the virtual environment. (Proposed by Łukasz Balcerzak and ported to 3.6 by Stéphane Wirtel in
bpo-22829
)。
warnings
¶
A new optional
source
parameter has been added to the
warnings.warn_explicit()
function: the destroyed object which emitted a
ResourceWarning
。
source
attribute has also been added to
warnings.WarningMessage
(contributed by Victor Stinner in
bpo-26568
and
bpo-26567
).
当
ResourceWarning
warning is logged, the
tracemalloc
module is now used to try to retrieve the traceback where the destroyed object was allocated.
Example with the script
example.py
:
import warnings
def func():
return open(__file__)
f = func()
f = None
Output of the command
python3.6 -Wd -X tracemalloc=5 example.py
:
example.py:7: ResourceWarning: unclosed file <_io.TextIOWrapper name='example.py' mode='r' encoding='UTF-8'>
f = None
Object allocated at (most recent call first):
File "example.py", lineno 4
return open(__file__)
File "example.py", lineno 6
f = func()
The “Object allocated at” traceback is new and is only displayed if
tracemalloc
is tracing Python memory allocations and if the
warnings
module was already imported.
winreg
¶
Added the 64-bit integer type
REG_QWORD
. (Contributed by Clement Rouault in
bpo-23026
)。
xmlrpc.client
¶
The
xmlrpc.client
module now supports unmarshalling additional data types used by the Apache XML-RPC implementation for numerics and
None
. (Contributed by Serhiy Storchaka in
bpo-26885
)。