Source From Here
Preface
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:
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:
Output:
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:
Arbitrary decorator expressions
Previously, the rules about which expressions are allowed in a decorator were underdocumented and hard to understand. For example, while:
is valid, and:
is valid, the similar:
produces a syntax error.
Starting in Python 3.9, any expression is valid as a decorator:
Output:
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:
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")
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:
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
- @item.thing()
- def foo():
- pass
- @item().thing
- def foo():
- pass
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])
沒有留言:
張貼留言