py3.5
print(*[1], *[2], 3, *[4, 5])
fn(**{'a': 1, 'c': 3}, **{'b': 2, 'd': 4})
"""************************************************"""
{*range(4), 4, *(5, 6, 7)}
- 函数注解 (1. 方便程序员查看,2. IDE的设计者可以用来做代码检查。)
py3.6
str_ = f"He said his name is {name}."
"""添加变量注释后会添加到__annotations__中"""
stats: Dict[str, int] = {}
py3.7
from __future__ import annotations
py3.8
"""海象运算符, :返回值、= 赋值 ,赋值后返回"""
print(f"List is too long ({n} elements, expected <= 10)")
def f(a, b, /, c, d, *, e, f):
f(10, 20, 30, d=40, e=50, f=60)
print(f'{theta=} {cos(radians(theta))=:.3f}')
>>> delta = date.today() - member_since
>>> f'{user=!s} {delta.days=:,d}'
'user=eric_idle delta.days=16,075'
>>> print(f'{theta=} {cos(radians(theta))=:.3f}')
theta=30 cos(radians(theta))=0.866
py3.9
>>> x = {"key1": "value1 from x", "key2": "value2 from x"}
>>> y = {"key2": "value2 from y", "key3": "value3 from y"}
{'key1': 'value1 from x', 'key2': 'value2 from y', 'key3': 'value3 from y'}
{'key2': 'value2 from x', 'key3': 'value3 from y', 'key1': 'value1 from x'}
str.removeprefix(prefix, /)
>>> 'TestHook'.removeprefix('Test')
>>> 'BaseTestCase'.removeprefix('Test')
str.removesuffix(suffix, /)
>>> 'MiscTests'.removesuffix('Tests')
>>> 'TmpDirMixin'.removesuffix('Tests')
def greet_all(names: list[str]):