TranslateProject/sources/tech/20210521 How Python 3.9 fixed decorators and improved dictionaries.md
2021-05-28 08:56:44 +08:00

3.5 KiB

How Python 3.9 fixed decorators and improved dictionaries

Explore some of the useful features of the recent version of Python. Python in a coffee cup.

This is the tenth in a series of articles about features that first appeared in a version of Python 3.x. Some of these versions have been out for a while. Python 3.9 was first released in 2020 with cool new features that are still underused. Here are three of them.

Adding dictionaries

Say you have a dictionary with "defaults," and you want to update it with parameters. Before Python 3.9, the best option was to copy the defaults dictionary and then use the .update() method.

Python 3.9 introduced the union operator to dictionaries:

defaults = dict(who="someone", where="somewhere")
params = dict(where="our town", when="today")
defaults | params

`    {'who': 'someone', 'where': 'our town', 'when': 'today'}`

Note that the order matters. In this case, the where value from params overrides the default, as it should.

Removing prefixes

If you have done ad hoc text parsing or cleanup with Python, you will have written code like:

def process_pricing_line(line):
    if line.startswith("pricing:"):
        return line[len("pricing:"):]
    return line
process_pricing_line("pricing:20")

`    '20'`

This kind of code is prone to errors. For example, if the string is copied incorrectly to the next line, the price will become 0 instead of 20, and it will happen silently.

Since Python 3.9, strings have a .lstrip() method:

`"pricing:20".lstrip("pricing:")`[/code] [code]`    '20'`

Arbitrary decorator expressions

Previously, the rules about which expressions are allowed in a decorator were underdocumented and hard to understand. For example, while:

@item.thing
def foo():
    pass

is valid, and:

@item.thing()
def foo():
    pass

is valid, the similar:

@item().thing
def foo():
    pass

produces a syntax error.

Starting in Python 3.9, any expression is valid as a decorator:

from unittest import mock

item = mock.MagicMock()

@item().thing
def foo():
    pass
print(item.return_value.thing.call_args[0][0])

`    <function foo at 0x7f3733897040>`

While keeping to simple expressions in the decorator line is still a good idea, it is now a human decision, rather than the Python parser's option.

Welcome to 2020

Python 3.9 was released about one year ago, but some of the features that first showed up in this release are cool—and underused. Add them to your toolkit if you haven't already.


via: https://opensource.com/article/21/5/python-39-features

作者:Moshe Zadka 选题:lujun9972 译者:译者ID 校对:校对者ID

本文由 LCTT 原创编译,Linux中国 荣誉推出