歷代 Python 語法重點隨筆

紀錄 Python 每一代新出值得注意的新語法,持續更新

3.11

文件還在寫,目前看起來主要是效能上的改進

3.10

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
with (CtxManager() as example):
...

with (
CtxManager1(),
CtxManager2()
):
...

with (CtxManager1() as example,
CtxManager2()):
...

with (CtxManager1(),
CtxManager2() as example):
...

with (
CtxManager1() as example1,
CtxManager2() as example2
):

# 變成
with (
CtxManager1() as example1,
CtxManager2() as example2,
CtxManager3() as example3,
):

終於有 switch 了,讚讚

1
2
3
4
5
6
7
8
9
10
11
12
def http_error(status):
match status:
case 400:
return "Bad request"
case 404:
return "Not found"
case 418:
return "I'm a teapot"
case 401 | 403:
return "qqqqq"
case _:
return "Something's wrong with the internet"
1
2
3
4
def square(number: int | float) -> int | float:
return number ** 2

isinstance(1, int | str)

3.9

Type Hinting Generics In Standard Collections

再也不用 import typing

  • before
    1
    2
    3
    from typing import List
    def greet_all(names: List) -> None:
    # your code here
  • after
    1
    2
    def greet_all(names: list[str]) -> None:
    # your code here

3.8

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

支援象牙符號,

也許你也會想看看