|
49 | 49 | This article explains the new features in Python 3.3, compared to 3.2. |
50 | 50 |
|
51 | 51 |
|
| 52 | +.. pep-3118-update: |
| 53 | +
|
52 | 54 | PEP 3118: New memoryview implementation and buffer protocol documentation |
53 | 55 | ========================================================================= |
54 | 56 |
|
@@ -85,7 +87,9 @@ Features |
85 | 87 | * Multi-dimensional comparisons are supported for any array type. |
86 | 88 |
|
87 | 89 | * All array types are hashable if the exporting object is hashable |
88 | | - and the view is read-only. |
| 90 | + and the view is read-only. (Contributed by Antoine Pitrou in |
| 91 | + :issue:`13411`) |
| 92 | + |
89 | 93 |
|
90 | 94 | * Arbitrary slicing of any 1-D arrays type is supported. For example, it |
91 | 95 | is now possible to reverse a memoryview in O(1) by using a negative step. |
@@ -258,9 +262,56 @@ part of its operations to another generator. This allows a section of code |
258 | 262 | containing 'yield' to be factored out and placed in another generator. |
259 | 263 | Additionally, the subgenerator is allowed to return with a value, and the |
260 | 264 | value is made available to the delegating generator. |
| 265 | + |
261 | 266 | While designed primarily for use in delegating to a subgenerator, the ``yield |
262 | 267 | from`` expression actually allows delegation to arbitrary subiterators. |
263 | 268 |
|
| 269 | +For simple iterators, ``yield from iterable`` is essentially just a shortened |
| 270 | +form of ``for item in iterable: yield item``:: |
| 271 | + |
| 272 | + >>> def g(x): |
| 273 | + ... yield from range(x, 0, -1) |
| 274 | + ... yield from range(x) |
| 275 | + ... |
| 276 | + >>> list(g(5)) |
| 277 | + [5, 4, 3, 2, 1, 0, 1, 2, 3, 4] |
| 278 | + |
| 279 | +However, unlike an ordinary loop, ``yield from`` allows subgenerators to |
| 280 | +receive sent and thrown values directly from the calling scope, and |
| 281 | +return a final value to the outer generator:: |
| 282 | + |
| 283 | + >>> def accumulate(start=0): |
| 284 | + ... tally = start |
| 285 | + ... while 1: |
| 286 | + ... next = yield |
| 287 | + ... if next is None: |
| 288 | + ... return tally |
| 289 | + ... tally += next |
| 290 | + ... |
| 291 | + >>> def gather_tallies(tallies, start=0): |
| 292 | + ... while 1: |
| 293 | + ... tally = yield from accumulate() |
| 294 | + ... tallies.append(tally) |
| 295 | + ... |
| 296 | + >>> tallies = [] |
| 297 | + >>> acc = gather_tallies(tallies) |
| 298 | + >>> next(acc) # Ensure the accumulator is ready to accept values |
| 299 | + >>> for i in range(10): |
| 300 | + ... acc.send(i) |
| 301 | + ... |
| 302 | + >>> acc.send(None) # Finish the first tally |
| 303 | + >>> for i in range(5): |
| 304 | + ... acc.send(i) |
| 305 | + ... |
| 306 | + >>> acc.send(None) # Finish the second tally |
| 307 | + >>> tallies |
| 308 | + [45, 10] |
| 309 | + |
| 310 | +The main principle driving this change is to allow even generators that are |
| 311 | +designed to be used with the ``send`` and ``throw`` methods to be split into |
| 312 | +multiple subgenerators as easily as a single large function can be split into |
| 313 | +multiple subfunctions. |
| 314 | + |
264 | 315 | (Implementation by Greg Ewing, integrated into 3.3 by Renaud Blanch, Ryan |
265 | 316 | Kelly and Nick Coghlan, documentation by Zbigniew Jędrzejewski-Szmek and |
266 | 317 | Nick Coghlan) |
@@ -327,6 +378,21 @@ suppressed valuable underlying details):: |
327 | 378 | KeyError('x',) |
328 | 379 |
|
329 | 380 |
|
| 381 | +PEP 414: Explicit Unicode literals |
| 382 | +====================================== |
| 383 | + |
| 384 | +:pep:`414` - Explicit Unicode literals |
| 385 | + PEP written by Armin Ronacher. |
| 386 | + |
| 387 | +To ease the transition from Python 2 for Unicode aware Python applications |
| 388 | +that make heavy use of Unicode literals, Python 3.3 once again supports the |
| 389 | +"``u``" prefix for string literals. This prefix has no semantic significance |
| 390 | +in Python 3, it is provided solely to reduce the number of purely mechanical |
| 391 | +changes in migrating to Python 3, making it easier for developers to focus on |
| 392 | +the more significant semantic changes (such as the stricter default |
| 393 | +separation of binary and text data). |
| 394 | + |
| 395 | + |
330 | 396 | PEP 3155: Qualified name for classes and functions |
331 | 397 | ================================================== |
332 | 398 |
|
@@ -408,10 +474,6 @@ Some smaller changes made to the core Python language are: |
408 | 474 |
|
409 | 475 | (:issue:`12170`) |
410 | 476 |
|
411 | | -* Memoryview objects are now hashable when the underlying object is hashable. |
412 | | - |
413 | | - (Contributed by Antoine Pitrou in :issue:`13411`) |
414 | | - |
415 | 477 |
|
416 | 478 | New and Improved Modules |
417 | 479 | ======================== |
|
0 commit comments