Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
What's New In Python 3.5
************************
Editors:
Elvis Pranskevichus <elvis@magic.io>, Yury Selivanov
<yury@magic.io>
This article explains the new features in Python 3.5, compared to 3.4.
Python 3.5 was released on September 13, 2015. See the changelog for
a full list of changes.
See also: **PEP 478** - Python 3.5 Release Schedule
Summary -- Release highlights
=============================
New syntax features:
* PEP 492, coroutines with async and await syntax.
* PEP 465, a new matrix multiplication operator: "a @ b".
* PEP 448, additional unpacking generalizations.
New library modules:
* "typing": PEP 484 -- Type Hints.
* "zipapp": PEP 441 Improving Python ZIP Application Support.
New built-in features:
* "bytes % args", "bytearray % args": PEP 461 -- Adding "%" formatting
to bytes and bytearray.
* New "bytes.hex()", "bytearray.hex()" and "memoryview.hex()" methods.
(Contributed by Arnon Yaari in bpo-9951.)
* "memoryview" now supports tuple indexing (including multi-
dimensional). (Contributed by Antoine Pitrou in bpo-23632.)
* Generators have a new "gi_yieldfrom" attribute, which returns the
object being iterated by "yield from" expressions. (Contributed by
Benno Leslie and Yury Selivanov in bpo-24450.)
* A new "RecursionError" exception is now raised when maximum
recursion depth is reached. (Contributed by Georg Brandl in
bpo-19235.)
CPython implementation improvements:
* When the "LC_TYPE" locale is the POSIX locale ("C" locale),
"sys.stdin" and "sys.stdout" now use the "surrogateescape" error
handler, instead of the "strict" error handler. (Contributed by
Victor Stinner in bpo-19977.)
* ".pyo" files are no longer used and have been replaced by a more
flexible scheme that includes the optimization level explicitly in
".pyc" name. (See PEP 488 overview.)
* Builtin and extension modules are now initialized in a multi-phase
process, which is similar to how Python modules are loaded. (See PEP
489 overview.)
Significant improvements in the standard library:
* "collections.OrderedDict" is now implemented in C, which makes it 4
to 100 times faster.
* The "ssl" module gained support for Memory BIO, which decouples SSL
protocol handling from network IO.
* The new "os.scandir()" function provides a better and significantly
faster way of directory traversal.
* "functools.lru_cache()" has been mostly reimplemented in C, yielding
much better performance.
* The new "subprocess.run()" function provides a streamlined way to
run subprocesses.
* The "traceback" module has been significantly enhanced for improved
performance and developer convenience.
Security improvements:
* SSLv3 is now disabled throughout the standard library. It can still
be enabled by instantiating a "ssl.SSLContext" manually. (See
bpo-22638 for more details; this change was backported to CPython
3.4 and 2.7.)
* HTTP cookie parsing is now stricter, in order to protect against
potential injection attacks. (Contributed by Antoine Pitrou in
bpo-22796.)
Windows improvements:
* A new installer for Windows has replaced the old MSI. See Using
Python on Windows for more information.
* Windows builds now use Microsoft Visual C++ 14.0, and extension
modules should use the same.
Please read on for a comprehensive list of user-facing changes,
including many other smaller improvements, CPython optimizations,
deprecations, and potential porting issues.
New Features
============
PEP 492 - Coroutines with async and await syntax
------------------------------------------------
**PEP 492** greatly improves support for asynchronous programming in
Python by adding *awaitable objects*, *coroutine functions*,
*asynchronous iteration*, and *asynchronous context managers*.
Coroutine functions are declared using the new "async def" syntax:
>>> async def coro():
... return 'spam'
Inside a coroutine function, the new "await" expression can be used to
suspend coroutine execution until the result is available. Any object
can be *awaited*, as long as it implements the *awaitable* protocol by
defining the "__await__()" method.
PEP 492 also adds "async for" statement for convenient iteration over
asynchronous iterables.
An example of a rudimentary HTTP client written using the new syntax:
import asyncio
async def http_get(domain):
reader, writer = await asyncio.open_connection(domain, 80)
writer.write(b'\r\n'.join([
b'GET / HTTP/1.1',
b'Host: %b' % domain.encode('latin-1'),
b'Connection: close',
b'', b''
]))
async for line in reader:
print('>>>', line)
writer.close()
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(http_get('example.com'))
finally:
loop.close()
Similarly to asynchronous iteration, there is a new syntax for
asynchronous context managers. The following script:
import asyncio
async def coro(name, lock):
print('coro {}: waiting for lock'.format(name))
async with lock:
print('coro {}: holding the lock'.format(name))
await asyncio.sleep(1)
print('coro {}: releasing the lock'.format(name))
loop = asyncio.get_event_loop()
lock = asyncio.Lock()
coros = asyncio.gather(coro(1, lock), coro(2, lock))
try:
loop.run_until_complete(coros)
finally:
loop.close()
will output:
coro 2: waiting for lock
coro 2: holding the lock
coro 1: waiting for lock
coro 2: releasing the lock
coro 1: holding the lock
coro 1: releasing the lock
Note that both "async for" and "async with" can only be used inside a
coroutine function declared with "async def".
Coroutine functions are intended to be run inside a compatible event
loop, such as the asyncio loop.
Note:
Changed in version 3.5.2: Starting with CPython 3.5.2, "__aiter__"
can directly return *asynchronous iterators*. Returning an
*awaitable* object will result in a "PendingDeprecationWarning".See
more details in the Asynchronous Iterators documentation section.
Loading
Loading full blame…