55:Release: |release |
66:Date: |today |
77
8+ :Author: Elvis Pranskevichus <
[email protected] > (Editor)
9+
810.. Rules for maintenance:
911
1012 * Anyone can add text to this document. Do not spend very much time
@@ -149,106 +151,206 @@ Please read on for a comprehensive list of user-facing changes.
149151 PEP written by Carl Meyer
150152
151153
152- PEP 492 - Coroutines with async and await syntax
153- ------------------------------------------------
154-
155- The PEP added dedicated syntax for declaring :term: `coroutines <coroutine> `,
156- :keyword: `await ` expressions, new asynchronous :keyword: `async for `
157- and :keyword: `async with ` statements.
154+ New Features
155+ ============
158156
159- Example: :
157+ .. _ whatsnew-pep-492 :
160158
161- async def read_data(db):
162- async with db.transaction():
163- data = await db.fetch('SELECT ...')
159+ PEP 492 - Coroutines with async and await syntax
160+ ------------------------------------------------
164161
165- PEP written and implemented by Yury Selivanov.
162+ :pep: `492 ` greatly improves support for asynchronous programming in Python
163+ by adding :term: `awaitable objects <awaitable> `,
164+ :term: `coroutine functions <coroutine function> `,
165+ :term: `asynchronous iteration <asynchronous iterable> `,
166+ and :term: `asynchronous context managers <asynchronous context manager> `.
167+
168+ Coroutine functions are declared using the new :keyword: `async def ` syntax::
169+
170+ >>> async def coro():
171+ ... return 'spam'
172+
173+ Inside a coroutine function, a new :keyword: `await ` expression can be used
174+ to suspend coroutine execution until the result is available. Any object
175+ can be *awaited *, as long as it implements the :term: `awaitable ` protocol by
176+ defining the :meth: `__await__ ` method.
177+
178+ PEP 492 also adds :keyword: `async for ` statement for convenient iteration
179+ over asynchronous iterables.
180+
181+ An example of a simple HTTP client written using the new syntax::
182+
183+ import asyncio
184+
185+ async def http_get(domain):
186+ reader, writer = await asyncio.open_connection(domain, 80)
187+
188+ writer.write(b'\r\n'.join([
189+ b'GET / HTTP/1.1',
190+ b'Host: %b' % domain.encode('latin-1'),
191+ b'Connection: close',
192+ b'', b''
193+ ]))
194+
195+ async for line in reader:
196+ print('>>>', line)
197+
198+ writer.close()
199+
200+ loop = asyncio.get_event_loop()
201+ try:
202+ loop.run_until_complete(http_get('example.com'))
203+ finally:
204+ loop.close()
205+
206+
207+ Similarly to asynchronous iteration, there is a new syntax for asynchronous
208+ context managers::
209+
210+ >>> import asyncio
211+ >>> async def coro1(lock):
212+ ... print('coro1: waiting for lock')
213+ ... async with lock:
214+ ... print('coro1: holding the lock')
215+ ... await asyncio.sleep(1)
216+ ... print('coro1: releasing the lock')
217+ ...
218+ >>> async def coro2(lock):
219+ ... print('coro2: waiting for lock')
220+ ... async with lock:
221+ ... print('coro2: holding the lock')
222+ ... await asyncio.sleep(1)
223+ ... print('coro2: releasing the lock')
224+ ...
225+ >>> loop = asyncio.get_event_loop()
226+ >>> lock = asyncio.Lock()
227+ >>> coros = asyncio.gather(coro1(lock), coro2(lock), loop=loop)
228+ >>> loop.run_until_complete(coros)
229+ coro1: waiting for lock
230+ coro1: holding the lock
231+ coro2: waiting for lock
232+ coro1: releasing the lock
233+ coro2: holding the lock
234+ coro2: releasing the lock
235+ >>> loop.close()
236+
237+ Note that both :keyword: `async for ` and :keyword: `async with ` can only
238+ be used inside a coroutine function declared with :keyword: `async def `.
239+
240+ Coroutine functions are intended to be ran inside a compatible event loop,
241+ such as :class: `asyncio.Loop `.
166242
167243.. seealso ::
168244
169245 :pep: `492 ` -- Coroutines with async and await syntax
246+ PEP written and implemented by Yury Selivanov.
170247
171248
172- PEP 461 - Formatting support for bytes and bytearray
173- ----------------------------------------------------
249+ PEP 465 - A dedicated infix operator for matrix multiplication
250+ --------------------------------------------------------------
174251
175- This PEP proposes adding % formatting operations similar to Python 2's ``str ``
176- type to :class: `bytes ` and :class: `bytearray `.
252+ :pep: `465 ` adds the ``@ `` infix operator for matrix multiplication.
253+ Currently, no builtin Python types implement the new operator, however, it
254+ can be implemented by defining :meth: `__matmul__ `, :meth: `__rmatmul__ `,
255+ and :meth: `__imatmul__ ` for regular, reflected, and in-place matrix
256+ multiplication. The semantics of these methods is similar to that of
257+ methods defining other infix arithmetic operators.
177258
178- Examples::
259+ Matrix multiplication is a notably common operation in many fields of
260+ mathematics, science, engineering, and the addition of ``@ `` allows writing
261+ cleaner code::
179262
180- >>> b'Hello %s!' % b'World'
181- b'Hello World!'
182- >>> b'x=%i y=%f' % (1, 2.5)
183- b'x=1 y=2.500000'
263+ >>> S = (H @ beta - r).T @ inv(H @ V @ H.T) @ (H @ beta - r)
184264
185- Unicode is not allowed for ``%s ``, but it is accepted by ``%a `` (equivalent of
186- ``repr(obj).encode('ascii', 'backslashreplace') ``)::
187-
188- >>> b'Hello %s!' % 'World'
189- Traceback (most recent call last):
190- File "<stdin>", line 1, in <module>
191- TypeError: %b requires bytes, or an object that implements __bytes__, not 'str'
192- >>> b'price: %a' % '10€'
193- b"price: '10\\u20ac'"
265+ An upcoming release of NumPy 1.10 will add support for the new operator::
194266
195- .. seealso ::
267+ >>> import numpy
196268
197- :pep: `461 ` -- Adding % formatting to bytes and bytearray
269+ >>> x = numpy.ones(3)
270+ >>> x
271+ array([ 1., 1., 1.])
198272
273+ >>> m = numpy.eye(3)
274+ >>> m
275+ array([[ 1., 0., 0.],
276+ [ 0., 1., 0.],
277+ [ 0., 0., 1.]])
199278
200- PEP 465 - A dedicated infix operator for matrix multiplication
201- --------------------------------------------------------------
279+ >>> x @ m
280+ array([ 1., 1., 1.])
202281
203- This PEP proposes a new binary operator to be used for matrix multiplication,
204- called ``@ ``. (Mnemonic: ``@ `` is ``* `` for mATrices.)
205282
206283.. seealso ::
207284
208285 :pep: `465 ` -- A dedicated infix operator for matrix multiplication
286+ PEP written by Nathaniel J. Smith; implemented by Benjamin Peterson.
209287
210288
211289PEP 448 - Additional Unpacking Generalizations
212290----------------------------------------------
213291
214- This PEP proposes extended usages of the ``* `` iterable unpacking
215- operator and ``** `` dictionary unpacking operators
216- to allow unpacking in more positions, an arbitrary number of
217- times, and in additional circumstances. Specifically,
218- in function calls, in comprehensions and generator expressions, and
219- in displays.
292+ :pep: ` 448 ` extends the allowed uses of the ``* `` iterable unpacking
293+ operator and ``** `` dictionary unpacking operator. It is now possible
294+ to use an arbitrary number of unpackings in function calls::
295+
296+ >>> print(*[1], *[2], 3, *[4, 5])
297+ 1 2 3 4 5
220298
221- Function calls are proposed to support an arbitrary number of
222- unpackings rather than just one::
299+ >>> def fn(a, b, c, d):
300+ ... print(a, b, c, d)
301+ ...
223302
224- >>> print(*[1], *[2], 3)
225- 1 2 3
226- >>> dict(**{'x': 1}, y=2, **{'z': 3})
227- {'x': 1, 'y': 2, 'z': 3}
303+ >>> fn(**{'a': 1, 'c': 3}, **{'b': 2, 'd': 4})
304+ 1 2 3 4
228305
229- Unpacking is proposed to be allowed inside tuple, list, set,
230- and dictionary displays ::
306+ Similarly, tuple, list, set, and dictionary displays allow multiple
307+ unpackings ::
231308
232309 >>> *range(4), 4
233310 (0, 1, 2, 3, 4)
234311 >>> [*range(4), 4]
235312 [0, 1, 2, 3, 4]
236- >>> {*range(4), 4}
237- {0, 1, 2, 3, 4}
313+ >>> {*range(4), 4, *(5, 6, 7) }
314+ {0, 1, 2, 3, 4, 5, 6, 7 }
238315 >>> {'x': 1, **{'y': 2}}
239316 {'x': 1, 'y': 2}
240317
241- In dictionaries, later values will always override earlier ones::
318+ .. seealso ::
319+
320+ :pep: `448 ` -- Additional Unpacking Generalizations
321+ PEP written by Joshua Landau; implemented by Neil Girdhar,
322+ Thomas Wouters, and Joshua Landau.
323+
324+
325+ PEP 461 - % formatting support for bytes and bytearray
326+ ------------------------------------------------------
242327
243- >>> {'x': 1, **{'x': 2}}
244- {'x': 2}
328+ PEP 461 adds % formatting to :class: `bytes ` and :class: `bytearray `, aiding in
329+ handling data that is a mixture of binary and ASCII compatible text. This
330+ feature also eases porting such code from Python 2.
245331
246- >>> {**{'x': 2}, 'x': 1}
247- {'x': 1}
332+ Examples::
333+
334+ >>> b'Hello %s!' % b'World'
335+ b'Hello World!'
336+ >>> b'x=%i y=%f' % (1, 2.5)
337+ b'x=1 y=2.500000'
338+
339+ Unicode is not allowed for ``%s ``, but it is accepted by ``%a `` (equivalent of
340+ ``repr(obj).encode('ascii', 'backslashreplace') ``)::
341+
342+ >>> b'Hello %s!' % 'World'
343+ Traceback (most recent call last):
344+ File "<stdin>", line 1, in <module>
345+ TypeError: %b requires bytes, or an object that implements __bytes__, not 'str'
346+ >>> b'price: %a' % '10€'
347+ b"price: '10\\u20ac'"
248348
249349.. seealso ::
250350
251- :pep: `448 ` -- Additional Unpacking Generalizations
351+ :pep: `461 ` -- Adding % formatting to bytes and bytearray
352+ PEP written by Ethan Furman; implemented by Neil Schemenauer and
353+ Ethan Furman.
252354
253355
254356PEP 484 - Type Hints
@@ -261,17 +363,19 @@ where annotations are not available.
261363For example, here is a simple function whose argument and return type
262364are declared in the annotations::
263365
264- def greeting(name: str) -> str:
265- return 'Hello ' + name
366+ def greeting(name: str) -> str:
367+ return 'Hello ' + name
266368
267369The type system supports unions, generic types, and a special type
268370named ``Any `` which is consistent with (i.e. assignable to and from) all
269371types.
270372
271373.. seealso ::
272374
273- * :pep: `484 ` -- Type Hints
274375 * :mod: `typing ` module documentation
376+ * :pep: `484 ` -- Type Hints
377+ PEP written by Guido van Rossum, Jukka Lehtosalo, and Łukasz Langa;
378+ implemented by Guido van Rossum.
275379
276380
277381PEP 471 - os.scandir() function -- a better and faster directory iterator
@@ -282,12 +386,10 @@ to the standard library. Additionally, :func:`os.walk` is now
282386implemented using :func: `os.scandir `, which speeds it up by 3-5 times
283387on POSIX systems and by 7-20 times on Windows systems.
284388
285- PEP and implementation written by Ben Hoyt with the help of Victor Stinner.
286-
287389.. seealso ::
288390
289- :pep: `471 ` -- os.scandir() function -- a better and faster directory
290- iterator
391+ :pep: `471 ` -- os.scandir() function -- a better and faster directory iterator
392+ PEP written and implemented by Ben Hoyt with the help of Victor Stinner.
291393
292394
293395PEP 475: Retry system calls failing with EINTR
@@ -357,12 +459,11 @@ not raise an exception:
357459* :func: `signal.sigtimedwait `, :func: `signal.sigwaitinfo `
358460* :func: `time.sleep `
359461
360- PEP and implementation written by Charles-François Natali and Victor Stinner,
361- with the help of Antoine Pitrou (the french connection).
362-
363462.. seealso ::
364463
365464 :pep: `475 ` -- Retry system calls failing with EINTR
465+ PEP and implementation written by Charles-François Natali and
466+ Victor Stinner, with the help of Antoine Pitrou (the french connection).
366467
367468
368469PEP 479: Change StopIteration handling inside generators
@@ -378,12 +479,11 @@ be used::
378479Without a ``__future__ `` import, a :exc: `PendingDeprecationWarning ` will be
379480raised.
380481
381- PEP written by Chris Angelico and Guido van Rossum. Implemented by
382- Chris Angelico, Yury Selivanov and Nick Coghlan.
383-
384482.. seealso ::
385483
386484 :pep: `479 ` -- Change StopIteration handling inside generators
485+ PEP written by Chris Angelico and Guido van Rossum. Implemented by
486+ Chris Angelico, Yury Selivanov and Nick Coghlan.
387487
388488
389489PEP 486: Make the Python Launcher aware of virtual environments
@@ -397,6 +497,7 @@ environment will be used.
397497.. seealso ::
398498
399499 :pep: `486 ` -- Make the Python Launcher aware of virtual environments
500+ PEP written and implemented by Paul Moore.
400501
401502
402503PEP 488: Elimination of PYO files
@@ -414,6 +515,7 @@ has an updated API to help with this change.
414515.. seealso ::
415516
416517 :pep: `488 ` -- Elimination of PYO files
518+ PEP written and implemented by Brett Cannon.
417519
418520
419521PEP 489: Multi-phase extension module initialization
@@ -429,7 +531,10 @@ rather than being restricted to ASCII.
429531
430532.. seealso ::
431533
432- :pep: `488 ` -- Multi-phase extension module initialization
534+ :pep: `489 ` -- Multi-phase extension module initialization
535+ PEP written by Petr Viktorin, Stefan Behnel, and Nick Coghlan;
536+ implementation by Petr Viktorin.
537+
433538
434539PEP 485: A function for testing approximate equality
435540----------------------------------------------------
@@ -442,6 +547,9 @@ close is determined according to given absolute and relative tolerances.
442547.. seealso ::
443548
444549 :pep: `485 ` -- A function for testing approximate equality
550+ PEP written by Christopher Barker; implemented by Chris Barker and
551+ Tal Einat.
552+
445553
446554Other Language Changes
447555======================
0 commit comments