39
39
module.
40
40
(Contributed by P.Y. Developer in :issue:`12345`.)
41
41
42
- This saves the maintainer the effort of going through the Mercurial log
42
+ This saves the maintainer the effort of going through the Git log
43
43
when researching a change.
44
44
45
45
:Editor: Raymond Hettinger
@@ -59,6 +59,7 @@ notable items not yet covered are:
59
59
60
60
from datetime import date
61
61
from math import cos, radians
62
+ from unicodedata import normalize
62
63
import re
63
64
import math
64
65
@@ -383,9 +384,13 @@ Other Language Changes
383
384
was lifted.
384
385
(Contributed by Serhiy Storchaka in :issue: `32489 `.)
385
386
386
- * The :class: `int ` type now has a new :meth: `~int.as_integer_ratio ` method
387
- compatible with the existing :meth: `float.as_integer_ratio ` method.
388
- (Contributed by Lisa Roach in :issue: `33073 `.)
387
+ * The :class: `bool `, :class: `int `, and :class: `fractions.Fraction ` types
388
+ now have an :meth: `~int.as_integer_ratio ` method like that found in
389
+ :class: `float ` and :class: `decimal.Decimal `. This minor API extension
390
+ makes it possible to write ``numerator, denominator =
391
+ x.as_integer_ratio() `` and have it work across multiple numeric types.
392
+ (Contributed by Lisa Roach in :issue: `33073 ` and Raymond Hettinger in
393
+ :issue: `37819 `.)
389
394
390
395
* Constructors of :class: `int `, :class: `float ` and :class: `complex ` will now
391
396
use the :meth: `~object.__index__ ` special method, if available and the
@@ -410,19 +415,26 @@ Other Language Changes
410
415
never intended to permit more than a bare name on the left-hand side of a
411
416
keyword argument assignment term. See :issue: `34641 `.
412
417
413
- * Iterable unpacking is now allowed without parentheses in :keyword: `yield `
414
- and :keyword: `return ` statements.
415
- (Contributed by David Cuthbert and Jordan Chapman in :issue: `32117 `.)
418
+ * Generalized iterable unpacking in :keyword: `yield ` and
419
+ :keyword: `return ` statements no longer requires enclosing parentheses.
420
+ This brings the *yield * and *return * syntax into better agreement with
421
+ normal assignment syntax::
422
+
423
+ >>> def parse(family):
424
+ lastname, *members = family.split()
425
+ return lastname.upper(), *members
416
426
417
- * The compiler now produces a :exc: ` SyntaxWarning ` in some cases when a comma
418
- is missed before tuple or list. For example::
427
+ >>> parse('simpsons homer marge bart lisa sally')
428
+ ('SIMPSONS', 'homer', 'marge', 'bart', 'lisa', 'sally')
419
429
420
- data = [
421
- (1, 2, 3) # oops, missing comma!
422
- (4, 5, 6)
423
- ]
424
430
425
- (Contributed by Serhiy Storchaka in :issue: `15248 `.)
431
+ (Contributed by David Cuthbert and Jordan Chapman in :issue: `32117 `.)
432
+
433
+ * When a comma is missed in code such as ``[(10, 20) (30, 40)] ``, the
434
+ compiler displays a :exc: `SyntaxWarning ` with a helpful suggestion.
435
+ This improves on just having a :exc: `TypeError ` indicating that the
436
+ first tuple was not callable. (Contributed by Serhiy Storchaka in
437
+ :issue: `15248 `.)
426
438
427
439
* Arithmetic operations between subclasses of :class: `datetime.date ` or
428
440
:class: `datetime.datetime ` and :class: `datetime.timedelta ` objects now return
@@ -439,7 +451,25 @@ Other Language Changes
439
451
and Windows use this to properly terminate scripts in interactive sessions.
440
452
(Contributed by Google via Gregory P. Smith in :issue: `1054041 `.)
441
453
442
- * Added new ``replace() `` method to the code type (:class: `types.CodeType `).
454
+ * Some advanced styles of programming require updating the
455
+ :class: `types.CodeType ` object for an existing function. Since code
456
+ objects are immutable, a new code object needs to be created, one
457
+ that is modeled on the existing code object. With 19 parameters,
458
+ this was somewhat tedious. Now, the new ``replace() `` method makes
459
+ it possible to create a clone with a few altered parameters.
460
+
461
+ Here's an example that alters the :func: `statistics.mean ` function to
462
+ prevent the *data * parameter from being used as a keyword argument::
463
+
464
+ >>> from statistics import mean
465
+ >>> mean(data=[10, 20, 90])
466
+ 40
467
+ >>> mean.__code__ = mean.__code__.replace(co_posonlyargcount=1)
468
+ >>> mean(data=[10, 20, 90])
469
+ Traceback (most recent call last):
470
+ ...
471
+ TypeError: mean() got some positional-only arguments passed as keyword arguments: 'data'
472
+
443
473
(Contributed by Victor Stinner in :issue: `37032 `.)
444
474
445
475
* For integers, the three-argument form of the :func: `pow ` function now
@@ -468,17 +498,55 @@ Other Language Changes
468
498
469
499
(Contributed by Mark Dickinson in :issue: `36027 `.)
470
500
471
- * When dictionary comprehensions are evaluated, the key is now evaluated before
472
- the value, as proposed by :pep: `572 `.
501
+ * Dict comprehensions have been synced-up with dict literals so that the
502
+ key is computed first and the value second::
503
+
504
+ >>> # Dict comprehension
505
+ >>> cast = {input('role? '): input('actor? ') for i in range(2)}
506
+ role? King Arthur
507
+ actor? Chapman
508
+ role? Black Knight
509
+ actor? Cleese
510
+
511
+ >>> # Dict literal
512
+ >>> cast = {input('role? '): input('actor? ')}
513
+ role? Sir Robin
514
+ actor? Eric Idle
515
+
516
+ The guaranteed execution order is helpful with assignment expressions
517
+ because variables assigned in the key expression will be available in
518
+ the value expression::
519
+
520
+ >>> names = ['Martin von Löwis', 'Łukasz Langa', 'Walter Dörwald']
521
+ >>> {(n := normalize('NFC', name)).casefold() : n for name in names}
522
+ {'martin von löwis': 'Martin von Löwis',
523
+ 'łukasz langa': 'Łukasz Langa',
524
+ 'walter dörwald': 'Walter Dörwald'}
473
525
474
526
475
527
New Modules
476
528
===========
477
529
478
530
* The new :mod: `importlib.metadata ` module provides (provisional) support for
479
- reading metadata from third-party packages. For example, you can extract an
480
- installed package's version number, list of entry points, and more. See
481
- :issue: `34632 ` for additional details.
531
+ reading metadata from third-party packages. For example, it can extract an
532
+ installed package's version number, list of entry points, and more::
533
+
534
+ >>> # Note following example requires that the popular "requests"
535
+ >>> # package has been installed.
536
+ >>>
537
+ >>> from importlib.metadata import version, requires, files
538
+ >>> version('requests')
539
+ '2.22.0'
540
+ >>> list(requires('requests'))
541
+ ['chardet (<3.1.0,>=3.0.2)']
542
+ >>> list(files('requests'))[:5]
543
+ [PackagePath('requests-2.22.0.dist-info/INSTALLER'),
544
+ PackagePath('requests-2.22.0.dist-info/LICENSE'),
545
+ PackagePath('requests-2.22.0.dist-info/METADATA'),
546
+ PackagePath('requests-2.22.0.dist-info/RECORD'),
547
+ PackagePath('requests-2.22.0.dist-info/WHEEL')]
548
+
549
+ (Contributed in :issue: `34632 ` by Barry Warsaw and Jason R. Coombs.)
482
550
483
551
484
552
Improved Modules
0 commit comments