That's correct! In Python 3.9, the union operators | and |= were introduced for dictionaries, making it easier to merge and update them. Here's a brief overview of these new features:
- Union Operator
|: This operator allows you to merge two dictionaries into a new one without affecting the original dictionaries. The resulting dictionary contains all unique keys from both dictionaries, and if a key is present in both, the value from the second dictionary is used.
dict1 = {'a': 1, 'b': 2}
dict2 = {'b': 3, 'c': 4}
merged_dict = dict1 | dict2
# Output: {'a': 1, 'b': 3, 'c': 4}
- In-place Union Operator
|=: This operator updates the first dictionary with the contents of the second dictionary. If a key is present in both, the value from the second dictionary is used.
dict1 = {'a': 1, 'b': 2}
dict2 = {'b': 3, 'c': 4}
dict1 |= dict2
# Output: dict1 = {'a': 1, 'b': 3, 'c': 4}
- Relaxed Grammar Restrictions on Decorators: Python 3.9 allows the use of multiple decorators in a more visually appealing way, without the need for parentheses or backslashes when placing decorators one below the other.
@decorator1
@decorator2
def some_function():
pass
- Type Hinting Generics in Standard Collections: Python 3.9 allows the use of built-in collection types (e.g.,
list, tuple, dict, and set) as generic types for type hinting, which makes the code more readable and concise.
from typing import List, Tuple
# Before Python 3.9
def process_data(data: List[Tuple[int, str]]) -> None:
pass
# With Python 3.9
def process_data(data: list[tuple[int, str]]) -> None:
pass
These new features in Python 3.9 help make the code more readable and easier to maintain.