The Python team has released the first beta of Python 3.8. New features include an improved C API to configure initialization. In addition, the programming language Assignment gets expressions and a syntax for purely position-dependent parameters. The upcoming version of the programming language applies with the release of the first beta as a feature complete, even if changes or adjustments to the new features are still possible or they can be omitted in exceptional cases.
The PEP 587 (Python Enhancement Proposal) provides a new C API to configure Python before initializing. The previous configuration options are inconsistent and can be reached via different routes. As examples, the PEP names global configuration variables such as Py_IsolatedFlag , environment variables like PYTHONPATH , command line parameters like -b, and function calls like Py_setProgramName () . Some parameters are difficult or impossible to configure using the existing C API.
Python 3.8 now provides the structures PyConfig , PyPreConfig , PyStatus, and PyWideStringList in the new API , which have common functions for setting individual parameters and reading and clearing the configuration. Developers can read and overwrite calculated parameters before applying the configuration.
Also new is the option to create a system isolated configuration using PyPreConfig_InitIsolatedConfig () and PyConfig_InitIsolatedConfig () that ignores global configuration and environment variables as well as command line parameters .
Assignment in expressions
PEP 572 introduces the not previously provided in Python Assignment Expressions, with which the Python Expressions as calculations assign a variable and can be used in the following code. This is to make the code more compact and avoid repeated calculations. The syntax is NAME: = expr , where the latter is an almost arbitrary expression – exceptions are non-bracketed tuples. As examples, the PEP implements, among other things, the processing of a regular expression and a loop for reading from a file:
# Handle a matched regex if (match: = pattern.search (data)) is not None: # Do something with match # A loop that can not be trivially rewritten using 2-arg iter () while chunk: = file.read (8192): process (chunk)
For clarity, the assignment statements must be in parenthesis in some places, with the PEP discouraging the use of these examples as it may not be meaningful or even confusing, as in the following example:
foo (x = y: = f (x)) # INVALID foo (x = (y: = f (x))) # Valid, but probably confusing
Assignment expressions are not allowed anywhere in places where assignment statements are allowed. This way you can not assign multiple destinations in short form, but the assignment is possible by setting brackets:
x = y = z = 0 # Equivalent: (z: = (y: = (x: = 0)))
For assignments such as a [i] = x, there are no corresponding assignment expressions, and operators such as + = can not be used.
A question of position
The PEP 570 introduces a new syntax for functions with purely position-specific parameters (positional-only parameters). As the name suggests, the position determines the assignment to the respective parameter, and its naming is not allowed.
Even if the proposal defines a new syntax, the positional-only parameters already exist in CPython’s builtin functions as in pow (x, y [, z]). A call with parameter names results in a TypeError, as the example from the PEP shows:
>>> pow (x = 5, y = 3) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: pow () takes no keyword arguments
The syntax proposed in PEP 570 extends the previous one and is thus fully backward compatible. The purely position-determined parameters can be at the beginning of a parameter list and followed by an / for marking. Then you can first follow position or keyword-specific parameters, and after the dividing * at the end, purely keyword-specific parameters:
def name (positional_only_parameters, /, positional_or_keyword_parameters, *, keyword_only_parameters):
The parameters defined by keyword may still be optional. If a position-only parameter gets a default value, the subsequent positions or keywords must also have default values, which may be defined purely by keyword, but can not do without:
def name (p1, p2 = None, /, p_or_kw = None, *, kw):
Other proposals that have been incorporated into the Python 3.8 Beta are the PEP 590 with a so-called vectorcall protocol, a C API that aims to optimize the invocation of objects, PEP 578 with two APIs, the test and logging frameworks, and Security tools should provide insight into a running Python application as well as PEP 574 for a new pickle protocol for serializing objects. There are also some additions to the syntax. Among other things, continue in finally : blocks is allowed. Also under the hood there are some additions and optimizations.
And now for something completely different
For developers who already rely on Python 3.x, the new features of Python 3.8 are nice additions. For those still using Python 2.x, time is slowly running out: support ends at the end of the year, bringing Python 2 to End of Life. An article on Revyuh Developer gives an overview of the necessary cleanup work as well as the additions and changes in Python 3.x over Python 2.x.
Further details about the beta of Python 3.8 can be found in the official announcement. The release of the final variant is scheduled for October 21, according to the PEP 569 Release Schedule. The first release candidate will be released on September 30th.