What is the New features in Python 3.8
New Features
Assignment expressions
There is new syntax :=
that assigns values to variables as part of a larger expression. It is affectionately known as “the walrus operator” due to its resemblance to the eyes and tusks of a walrus.
In this example, the assignment expression helps avoid calling len()
twice:
A similar benefit arises during regular expression matching where match objects are needed twice, once to test whether a match occurred and another to extract a subgroup:
The operator is also useful with while-loops that compute a value to test loop termination and then need that same value again in the body of the loop:
Another motivating use case arises in list comprehensions where a value computed in a filtering condition is also needed in the expression body:
Try to limit use of the walrus operator to clean cases that reduce complexity.
This is The feature #2 .
Reversible dictionary — order
Dictionaries are now iterable in reversed insertion order using reversed()
.
This is The feature #3 .
New modules — metadata
There is a new importlib.metadata
module that allows you to read metadata from third-party packages. You can extract version numbers of packages in your scripts.
This is The feature #4 .
Continue — finally
It used to be illegal to use continue
statement in a finally
clause due to a problem with the implementation. Not anymore.
This is The feature #5 .
Positional-only parameters
There is a new function parameter syntax /
to indicate that some function parameters must be specified positionally and cannot be used as keyword arguments. This is the same notation shown by help()
for C functions annotated with Larry Hastings’ Argument Clinic tool.
In the following example, parameters a and b are positional-only, while c or d can be positional or keyword, and e or f are required to be keywords:
The following is a valid call:
However, these are invalid calls:
One use case for this notation is that it allows pure Python functions to fully emulate behaviors of existing C coded functions. For example, the built-in divmod()
function does not accept keyword arguments:
Another use case is to preclude keyword arguments when the parameter name is not helpful. For example, the builtin len()
function has the signature len(obj, /)
. This precludes awkward calls such as:
A further benefit of marking a parameter as positional-only is that it allows the parameter name to be changed in the future without risk of breaking client code. For example, in the statistics
module, the parameter name dist may be changed in the future. This was made possible with the following function specification:
Since the parameters to the left of /
are not exposed as possible keywords, the parameters names remain available for use in **kwargs
:
This greatly simplifies the implementation of functions and methods that need to accept arbitrary keyword arguments. For example, here is an excerpt from code in the collections
module:
Comments
Post a Comment
Thanks for Your time . we appreciate your feedback. Comment here!