Walrus operator! Summary of new features of Python 3.8

(Star Python developers to improve Python skills)

Original finishing: Python developer (id: PythonCoder)

On October 15, the stable version of Python 3.8 was officially released~ 

The official website also posted an article introducing the new features of v3.8. Here is a summary:

1. New assignment operator := Function: Assign a certain part of the larger expression to a variable.

Because it looks like the eyes and teeth of a walrus, the :=  operator has a special name: the walrus operator 640?wx_fmt=png

640?wx_fmt=png

Example 1:

if (n := len(a)) > 10:	
    print(f"List is too long ({n} elements, expected <= 10)")

Used := to avoid calling len() twice.

Example 2:

discount = 0.0	
if (mo := re.search(r'(\d+)% discount', advertisement)):	
    discount = float(mo.group(1)) / 100.0

Judging from the current online comments, most think that although the code is saved, the readability is reduced.

2. Newly added ( Positional-only parameters ) syntax symbols / Positional-only parameters

Role: Indicate that some function parameters must specify the position and cannot be used as keyword parameters.

example

def f(a, b, /, c, d, *, e, f):	
    print(a, b, c, d, e, f)

In this example, the parameters a and b are only positional parameters, and c or d can be only positional parameters or keyword parameters, and e and f must be keyword parameters.

3. New PYTHONPYCACHEPREFIX setting (-X pycache_prefix) function: configure the implicit bytecode cache to use a separate parallel file system tree instead of using the default __pycache__ subdirectory in each source directory.

4. The debug version and the release version share ABI

5. New support for f string = specifier, used for self-recording expressions and debugging

The f string is a formatted string literal, which was introduced in Python 3.6 and has become very popular. 

This time support for the = specifier is added in v3.8.

>>> user = 'eric_idle'	
>>> member_since = date(1975, 7, 31)	
>>> f'{user=} {member_since=}'	
"user='eric_idle' member_since=datetime.date(1975, 7, 31)"

>>> delta = date.today() - member_since	
>>> f'{user=!s}  {delta.days=:,d}'	
'user=eric_idle  delta.days=16,075'

6. PEP 578: Python Runtime Audit Hooks / Runtime Audit Hooks

PEP adds an audit hook and a verification opening hook. Both versions are available in Python and native code, allowing applications and frameworks written in pure Python code to take advantage of additional notifications, while also allowing embedded personnel or system administrators to always enable auditing Deploy the Python build under.

7、PEP 587: Python Initialization Configuration

PEP 587 adds a new C API to configure Python initialization, providing better control over the entire configuration and better error reporting.

640?wx_fmt=png

640?wx_fmt=png

In addition,

  • continue can finally appear legally finally: in the block;

  • In Windows, the default asyncio event loop is changed to ProactorEventLoop

  • On macOS, the spawn start method is added to  multiprocessing by default 

  • Multiprocessing can now use shared memory segments to avoid pickle overhead between processes;

  • typed_ast has been merged back into CPython;

  • LOAD_GLOBAL is now 40% faster;

  • Pickle now uses protocol 4 by default, which improves performance;

For other features and interpretation, please refer to the official introduction:

https://docs.python.org/3/whatsnew/3.8.html

In addition, the next version of Python 3.9 is already under development.

Recommended reading

(Click the title to jump to read)

Think this article is helpful to you? Please share with more people

Pay attention to the "Python Developer" starred to improve Python skills

640?wx_fmt=png

Good article, I am reading ❤️

Guess you like

Origin blog.csdn.net/iodjSVf8U1J7KYc/article/details/102597872