Python 3.9 có khớp không?

Tóm lược. Trăn 3. 10, dự kiến ​​ra mắt vào đầu tháng 10 năm 2021, sẽ bao gồm một tính năng ngôn ngữ mới lớn được gọi là khớp mẫu cấu trúc. Bài viết này là một bài trình bày quan trọng nhưng [hy vọng] mang tính thông tin về tính năng này, với các ví dụ dựa trên mã trong thế giới thực

Đi đến. . . . . .

Tại một buổi họp mặt Python địa phương gần đây, một người bạn đã trình bày một số tính năng mới trong Python 3. 8 và 3. 9, và sau đó chúng ta nói về tính năng khớp mẫu có trong Python 3. 10. Tôi đã nói một cách nhẹ nhàng về việc tôi nghĩ Python đã làm mất cốt truyện như thế nào. biểu thức gán đầu tiên bằng cách sử dụng

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
1, và bây giờ là tính năng khá dài này

Bạn tôi diễn giải câu nói của tôi khá hào phóng, và ngay sau đó nói, "có vẻ như bạn muốn nói về nó trong buổi gặp mặt tiếp theo của chúng ta". Được rồi… tốt, tại sao không

Trong thời gian chờ đợi, tôi nghĩ mình sẽ hiểu rõ hơn về tính năng này bằng cách viết ra những suy nghĩ của mình và một số ví dụ về mã ở dạng bài viết. Như bạn có thể hiểu, tôi khá thiên vị, nhưng tôi sẽ cố gắng trình bày những mặt tích cực cũng như những lời chỉ trích

Tính năng khớp mẫu có không ít hơn ba PEP [Đề xuất cải tiến Python] để mô tả tính năng này

Đặc biệt, hướng dẫn cung cấp một cái nhìn tổng quan tốt về tính năng này, vì vậy nếu bạn chỉ muốn đọc một trong các PEP, hãy đọc cái đó. Tôi cũng sẽ giới thiệu các tính năng bên dưới

Mặt hoài nghi của tôi nhận thấy rằng cơ sở lý luận là dài nhất cho đến nay [với tốc độ 8500 từ]. Tôi nghĩ rằng bạn phản đối quá nhiều?

Nhưng tôi nghĩ điều còn thiếu trong PEP là đánh giá chi phí so với lợi ích. Chi phí là ngữ nghĩa ngôn ngữ mới đáng kể để các nhà phát triển tìm hiểu, cũng như chi phí triển khai [đối với CPython và các triển khai Python khác]. Những lợi ích cần được thảo luận dưới ánh sáng của mã trong thế giới thực. loại mã mà mọi người sử dụng Python hàng ngày, không chỉ là các ví dụ khá giả tạo trong phần “Động lực” của PEP

Một phần của những gì tôi muốn làm ở đây là đánh giá một số mã thực và xem việc khớp mẫu cải thiện được bao nhiêu [hoặc ít]. Nhưng trước tiên, chúng ta hãy xem sơ qua về cách khớp mẫu cấu trúc trong Python trông như thế nào

nó là gì

Thật hấp dẫn khi nghĩ về khớp mẫu như một câu lệnh

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
2 trên steroid. Tuy nhiên, như lý do PEP chỉ ra, nó tốt hơn nên được coi là một “khái niệm tổng quát về việc giải nén lặp đi lặp lại”. Nhiều người đã yêu cầu một
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
2 bằng Python trong những năm qua, mặc dù tôi có thể hiểu tại sao điều đó chưa bao giờ được thêm vào. Nó chỉ không cung cấp đủ giá trị qua một loạt các câu lệnh
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
4 để trả cho chính nó. Tính năng mới của
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
5 cung cấp những điều cơ bản của
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
2, cộng với phần đối sánh “cấu trúc” – và một số

Cú pháp cơ bản được hiển thị trong ví dụ giống như công tắc sau [hãy tưởng tượng chúng tôi đang triển khai Git CLI của riêng mình]

parser = argparse.ArgumentParser[]
parser.add_argument['command', choices=['push', 'pull', 'commit']]
args = parser.parse_args[]

match args.command:
    case 'push':
        print['pushing']
    case 'pull':
        print['pulling']
    case _:
        parser.error[f'{args.command!r} not yet implemented']

Python đánh giá biểu thức

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
7, sau đó thử từng
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
8 từ trên cùng, thực thi trường hợp đầu tiên khớp hoặc trường hợp mặc định
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
9 nếu không có trường hợp nào khác khớp

Nhưng đây là nơi phần cấu trúc xuất hiện. các mẫu

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
8 không nhất thiết phải theo nghĩa đen. Các mẫu cũng có thể

  • Sử dụng các tên biến được đặt nếu một
    match event.get[]:
        case Click[[x, y], button=Button.LEFT]:  # This is a left click
            handle_click_at[x, y]
        case Click[]:
            pass  # ignore other clicks
        case KeyPress[key_name="Q"] | Quit[]:
            game.quit[]
        case KeyPress[key_name="up arrow"]:
            game.go_north[]
        ...
        case KeyPress[]:
            pass # Ignore other keystrokes
        case other_event:
            raise ValueError[f"Unrecognized event: {other_event}"]
    
    8 phù hợp
  • Khớp các chuỗi bằng cách sử dụng cú pháp danh sách hoặc bộ dữ liệu [như tính năng giải nén lặp lại hiện có của Python]
  • Khớp ánh xạ bằng cú pháp dict
  • Sử dụng
    match event.get[]:
        case Click[[x, y], button=Button.LEFT]:  # This is a left click
            handle_click_at[x, y]
        case Click[]:
            pass  # ignore other clicks
        case KeyPress[key_name="Q"] | Quit[]:
            game.quit[]
        case KeyPress[key_name="up arrow"]:
            game.go_north[]
        ...
        case KeyPress[]:
            pass # Ignore other keystrokes
        case other_event:
            raise ValueError[f"Unrecognized event: {other_event}"]
    
    22 để khớp với phần còn lại của danh sách
  • Sử dụng
    match event.get[]:
        case Click[[x, y], button=Button.LEFT]:  # This is a left click
            handle_click_at[x, y]
        case Click[]:
            pass  # ignore other clicks
        case KeyPress[key_name="Q"] | Quit[]:
            game.quit[]
        case KeyPress[key_name="up arrow"]:
            game.go_north[]
        ...
        case KeyPress[]:
            pass # Ignore other keystrokes
        case other_event:
            raise ValueError[f"Unrecognized event: {other_event}"]
    
    23 để khớp với các phím khác trong lệnh
  • Khớp các đối tượng và thuộc tính của chúng bằng cú pháp lớp
  • Bao gồm các mẫu “hoặc” với
    match event.get[]:
        case Click[[x, y], button=Button.LEFT]:  # This is a left click
            handle_click_at[x, y]
        case Click[]:
            pass  # ignore other clicks
        case KeyPress[key_name="Q"] | Quit[]:
            game.quit[]
        case KeyPress[key_name="up arrow"]:
            game.go_north[]
        ...
        case KeyPress[]:
            pass # Ignore other keystrokes
        case other_event:
            raise ValueError[f"Unrecognized event: {other_event}"]
    
    24
  • Chụp các mẫu phụ với
    match event.get[]:
        case Click[[x, y], button=Button.LEFT]:  # This is a left click
            handle_click_at[x, y]
        case Click[]:
            pass  # ignore other clicks
        case KeyPress[key_name="Q"] | Quit[]:
            game.quit[]
        case KeyPress[key_name="up arrow"]:
            game.go_north[]
        ...
        case KeyPress[]:
            pass # Ignore other keystrokes
        case other_event:
            raise ValueError[f"Unrecognized event: {other_event}"]
    
    25
  • Bao gồm một mệnh đề "bảo vệ"
    match event.get[]:
        case Click[[x, y], button=Button.LEFT]:  # This is a left click
            handle_click_at[x, y]
        case Click[]:
            pass  # ignore other clicks
        case KeyPress[key_name="Q"] | Quit[]:
            game.quit[]
        case KeyPress[key_name="up arrow"]:
            game.go_north[]
        ...
        case KeyPress[]:
            pass # Ignore other keystrokes
        case other_event:
            raise ValueError[f"Unrecognized event: {other_event}"]
    
    26

Ồ. Đó là rất nhiều tính năng. Hãy xem liệu chúng ta có thể sử dụng tất cả chúng trong một lần hay không, để xem chúng trông như thế nào trong một ví dụ rất giả tạo [để có phần giới thiệu dần dần, hãy đọc hướng dẫn]

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
7

Như bạn có thể thấy, nó phức tạp nhưng cũng mạnh mẽ. Các chi tiết chính xác về cách khớp được thực hiện được cung cấp trong thông số kỹ thuật. Rất may, phần lớn những điều trên khá dễ hiểu, mặc dù thuộc tính

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
27 yêu cầu giải thích. nếu các đối số vị trí được sử dụng trong một mẫu lớp, các mục trong bộ dữ liệu
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
27 của lớp sẽ cung cấp tên của các thuộc tính. Đây là cách viết tắt để tránh chỉ định tên thuộc tính trong mẫu lớp

Một điều cần lưu ý là

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
7 và
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
8 không phải là từ khóa thực mà là “từ khóa mềm”, nghĩa là chúng chỉ hoạt động dưới dạng từ khóa trong khối
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
5. Điều này là do thiết kế, bởi vì mọi người luôn sử dụng
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
7 làm tên biến – Tôi hầu như luôn sử dụng biến có tên
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
7 là kết quả của phép khớp biểu thức chính quy

Nơi nó tỏa sáng

Như tôi đã đề cập, tôi không nghĩ rằng

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
7 sẽ thành công nếu bạn chỉ sử dụng nó như một
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
2 được tôn vinh. Vì vậy, nó trả hết ở đâu?

Trong hướng dẫn PEP có một số ví dụ làm cho nó tỏa sáng. kết hợp các lệnh và đối số của chúng trong một trò chơi dựa trên văn bản đơn giản. Tôi đã hợp nhất một số ví dụ đó lại với nhau và sao chép chúng bên dưới

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
7

Để so sánh, hãy viết lại nhanh đoạn trích đó mà không khớp mẫu, kiểu cũ. Bạn gần như chắc chắn sẽ sử dụng một loạt các khối

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
4. Tôi sẽ đặt một biến mới
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
97 cho các trường được chia trước và
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
98 cho số lượng trường, để làm cho điều kiện đơn giản hơn

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
1

Ngoài việc ngắn hơn một chút, tôi nghĩ thật công bằng khi nói rằng phiên bản đối sánh cấu trúc dễ đọc hơn và liên kết biến tránh được việc lập chỉ mục thủ công như

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
99. Về khả năng đọc thuần túy, ví dụ này có vẻ như là một chiến thắng rõ ràng cho việc so khớp mẫu

Hướng dẫn cũng cung cấp các ví dụ về kết hợp dựa trên lớp, có lẽ là một phần của vòng lặp sự kiện của trò chơi

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]

Hãy thử viết lại cái này bằng cách sử dụng

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
4 đơn giản. Tương tự như cách tôi xử lý lệnh “go” trong phần viết lại ở trên, tôi sẽ hợp nhất các trường hợp lại với nhau khi hợp lý [các sự kiện của cùng một lớp]

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
2

Đối với tôi cái này có vẻ nhiều ranh giới hơn. Nó chắc chắn đẹp hơn một chút với khớp mẫu, nhưng không nhiều.

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
7 có lợi thế là tất cả các
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
8 đều xếp hàng;

Bất chấp sự hoài nghi của tôi, tôi đang cố gắng công bằng. những ví dụ này trông rất đẹp và ngay cả khi bạn chưa đọc thông số khớp mẫu, thì cũng khá rõ ràng về những gì chúng làm – có thể ngoại trừ phép thuật

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
27

Ngoài ra còn có một trình phân tích và đánh giá biểu thức mà Guido van Rossum đã viết để giới thiệu tính năng này. Nó sử dụng rất nhiều

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
5 [11 lần trong một tệp khá nhỏ]. Đây là một ví dụ

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
9

Điều đó sẽ như thế nào với

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
4? . Lưu ý rằng các khối
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
26 lồng nhau không thực sự làm tăng mức độ thụt đầu dòng, do yêu cầu lồng kép cho các mệnh đề
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
8 trong một
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
7

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
3

Đó là hai dòng nữa do giải nén thuộc tính thủ công cho các trường

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
37 và
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
92. Có lẽ đó chỉ là tôi, nhưng tôi thấy cái này cũng dễ đọc như phiên bản
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
7, và rõ ràng hơn

Một nơi khác mà

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
7 có thể hữu ích là khi xác thực cấu trúc của JSON từ một yêu cầu HTTP [đây là ví dụ do tôi tự tạo]

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
9

cái này khá hay. Tuy nhiên, một nhược điểm là nó không cung cấp cho bạn các lỗi xác thực tốt. lý tưởng nhất là một API sẽ cho người gọi biết trường nào bị thiếu hoặc loại nào không chính xác

Sử dụng nó trong mã của tôi

Hãy xem xét chuyển đổi một số mã hiện có để sử dụng tính năng mới. Về cơ bản, tôi đang quét các khối

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
4 để xem liệu việc chuyển đổi chúng có hợp lý không. Tôi sẽ bắt đầu với một vài ví dụ về mã tôi đã viết

Một vài ví dụ đầu tiên là từ pygit, một tập hợp con đồ chơi của

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
96 vừa đủ cho một ứng dụng khách Git để tạo một repo, cam kết và đẩy chính nó lên GitHub [mã nguồn đầy đủ]

Tôi đã thu gọn các khối mã bên dưới theo mặc định. Chỉ cần nhấp vào mũi tên hoặc đoạn tóm tắt để mở rộng. Mở rộng tất cả Thu gọn tất cả

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
5

Ví dụ về ________ 497. Một số khía cạnh đẹp hơn một chút, nhưng nhìn chung tôi nghĩ viết lại nó để sử dụng
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
7 là sử dụng quá nhiều tính năng này

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
70

Nó khá rõ ràng, nhưng hãy xem liệu nó có đơn giản hơn khi sử dụng

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
7 không

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
71

Bản thân các trường hợp đẹp hơn một chút, đặc biệt là cách

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
50 được tự động ràng buộc thay vì sử dụng
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
51

Tuy nhiên, có một điều không hay là cách “trường hợp thành công” kết thúc ở giữa, vì vậy đường dẫn mã bình thường bị mất một chút. Bạn có thể hack nó đến cùng bằng cách sau [nhưng sau đó nó chắc chắn không rõ ràng như bản gốc]

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
72

Ngoài ra, bạn có thể đặt trường hợp thành công [cụ thể nhất] trước, cách này hay hơn một chút

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
73

Ví dụ về ________ 552, hiển thị hai cách khác nhau. Có vẻ như một chiến thắng nhẹ

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
74

Một bản dịch trực tiếp sẽ như sau [lưu ý

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
7 lồng nhau trong trường hợp “khá”]

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
75

Chúng tôi đang sử dụng

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
7 như một công tắc đơn giản, nhưng đó là một chiến thắng rất nhỏ. Điều gì sẽ xảy ra nếu chúng tôi thử làm lại nó để khớp với
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
55 và
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
56 cùng lúc với một bộ dữ liệu

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
76

Bây giờ nó được sắp xếp hợp lý hơn so với ban đầu, mặc dù có thể nói là không rõ ràng hơn

Ví dụ từ phân tích cú pháp đối số, khi bật lệnh phụ CLI. Có ý nghĩa khi sử dụng
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
7, nhưng đó là một chuyển đổi đơn giản, không sử dụng các tính năng cấu trúc

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
77

Sử dụng

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
7 sẽ giảm tiếng ồn thị giác

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
78

Nhưng chúng tôi không thực sự cần một tính năng hoàn toàn mới cho điều đó. Bạn có thể giảm hầu hết nhiễu thị giác bằng cách gán một tên biến ngắn

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
79

Dưới đây là một số ví dụ khác từ Khung toán tử Python của Canonical, trong

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
59, mà tôi đã viết cho công việc

Ví dụ từ
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
700. Nó xử lý các loại khác nhau được phép cho tham số
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
701. Ít nhiễu hình ảnh hơn, mặc dù cũng ít rõ ràng hơn

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
70

Phiên bản

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
7 sử dụng cú pháp khớp lớp

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
71

Phiên bản

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
7 có rõ ràng hơn không? . Ngoài ra, các dấu ngoặc đơn trống trong các trường hợp khác nhau hơi kỳ lạ – chúng trông không cần thiết nếu không có đối số vị trí hoặc thuộc tính, nhưng nếu không có chúng thì
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
7 sẽ liên kết các biến mới có tên là
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
706 hoặc
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
707

Lúc đầu, tôi nghĩ thật kỳ lạ khi các biến bị ràng buộc [và được gán] trong khối

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
8 tồn tại lâu hơn toàn bộ khối
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
7. Nhưng như đã trình bày ở trên, nó có ý nghĩa – bạn sẽ thường muốn sử dụng các biến trong mã bên dưới
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
7

Ví dụ về ________ 1711, mã tôi đang làm việc. Không rõ ràng hơn trong trường hợp này

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
72

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
7 có giúp đơn giản hóa việc kiểm tra đó không?

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
73

Tôi cho rằng điều này không rõ ràng hơn.

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
713 thật khó xử – chúng ta có thể tránh nó bằng cách gói toàn bộ nội dung trong
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
714 giống như mã ban đầu đã làm, nhưng điều đó làm tăng thêm cấp độ lồng thứ ba, mức này kém lý tưởng hơn

Trường hợp mặc định có bảo vệ,

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
715, cũng khó hiểu hơn một chút so với phiên bản gốc của
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
716. Tất nhiên, bạn có thể chỉ cần sử dụng
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
717 và sau đó lồng vào
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
718

Có thể tôi không viết nhiều loại mã có lợi từ tính năng này, nhưng tôi đoán là có rất nhiều người rơi vào trường hợp này. Tuy nhiên, hãy quét mã từ một vài dự án Python phổ biến để xem những gì chúng ta có thể tìm thấy

Sử dụng nó trong các dự án khác

Đi đến. . . . .

Tôi sẽ chọn các ví dụ từ ba loại mã khác nhau. mã thư viện [từ thư viện chuẩn], mã khung [từ khung web Django] và mã ứng dụng [từ Warehouse, máy chủ hỗ trợ Chỉ mục gói Python, từ Mercurial và từ Ansible]

Để công bằng, tôi đã cố gắng tìm các ví dụ thực sự có lợi từ

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
7 và còn hơn cả một
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
2 được tôn vinh [có rất nhiều ví dụ như vậy, nhưng chúng không sử dụng phần cấu trúc của khớp mẫu, vì vậy việc chuyển đổi chúng không . Tôi đã tìm kiếm các khối
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
716 trông giống như chúng kiểm tra cấu trúc của dữ liệu. Có thể có một số cách sử dụng tốt của
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
7 trong mã chỉ sử dụng
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
26 mà không có
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
716, nhưng tôi nghĩ điều đó sẽ rất hiếm

thư viện tiêu chuẩn

Thư viện chuẩn của Python có khoảng 709.000 dòng mã, bao gồm các bài kiểm tra [được đo bằng scc]. Công cụ tìm kiếm ripgrep [______1725] cho chúng tôi biết rằng 2529 trong số những dòng đó là câu lệnh

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
716 hoặc 0. 4%. Tôi nhận ra rằng điều này sẽ tìm thấy "elif " trong các nhận xét, nhưng có lẽ điều đó rất hiếm

Ví dụ từ
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
727, trong trình trợ giúp
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
728. Không có gì đáng ngạc nhiên, trường hợp sử dụng thực sự tốt đầu tiên mà tôi tìm thấy là trong quá trình xử lý AST. Chắc chắn là một chiến thắng

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
74

Chuyển đổi nó để sử dụng

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
7

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
75

Chắc chắn là một chiến thắng. Xử lý cây cú pháp có vẻ như là trường hợp sử dụng lý tưởng cho

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
7. Trong Trăn 3. 10, các loại nút của mô-đun
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
731 đã có bộ
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
27, do đó làm cho nó sạch hơn bằng cách tránh kiểu lặp lại của
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
733

Tuy nhiên, tôi muốn tìm một cái bên ngoài mô-đun

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
731. Tôi sẽ không đưa nó vào đây, nhưng có một chuỗi
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
4 dài đẹp đẽ trong
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
736 ở. nó chủ yếu là một công tắc đơn giản, nhưng nó sẽ được hưởng lợi từ
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
5 với một vài người bảo vệ
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
26

Ví dụ từ
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
740, trong
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
741. Giảm nhiễu hình ảnh để cải thiện một chút

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
76

Hãy thử chuyển đổi nó thành

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
7

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
77

Một cải tiến nhỏ tốt đẹp, mặc dù

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
717 đầu tiên với người bảo vệ
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
26 hơi kỳ lạ. Nó có thể được chuyển sang một câu lệnh
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
26 thông thường bên trong
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
717 cuối cùng, nhưng tôi không biết mã đủ rõ để biết liệu lệnh đó có còn thực hiện những gì được yêu cầu hay không

Ví dụ từ
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
747, trong
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
748. Phù hợp với giải nén tuple làm cho nó sạch hơn một chút

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
78

Hãy thử chuyển đổi nó để sử dụng

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
7

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
79

Đây chắc chắn là một chút sạch hơn. Luôn luôn có một chút khó khăn khi bạn sử dụng

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
750 để kiểm tra độ dài trước khi giải nén một bộ [bạn cũng có thể bắt gặp ngoại lệ
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
751, nhưng nó không rõ ràng và mức độ lồng nhau hơi nhiều]

lưu ý bên lề. phương pháp

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
752 thường hữu ích trong những trường hợp như thế này, nhưng chỉ khi bạn có hai mục có dấu phân cách ở giữa

Thật thú vị, trong khi kiểm tra

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
748, tôi phát hiện ra rằng mã này có một lỗi làm tăng
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
754 khi người dùng nhập không hợp lệ. nếu bạn vượt qua một khoảng thời gian có nhiều hơn 3 phân đoạn chấm như ________ 1755, thì các biến ________ 1756/________ 1757/
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
758 sẽ không được xác định cho đoạn mã sau. kiểm tra cái này

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
10

Tất cả những gì nó cần là một

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
759 khác trong trường hợp dấu chấm. Tôi đã mở một sự cố và yêu cầu kéo bổ sung trường hợp thử nghiệm cho vấn đề này và sửa lỗi

Django

Django có 327.000 dòng mã, bao gồm cả các bài kiểm tra. Trong số này, có 905 cách sử dụng

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
716 hoặc 0. 3%

Ví dụ từ kiểm tra của quản trị viên Django, trong
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
761. Kết hợp cấu trúc ở đây rất tuyệt, nhưng không giúp tạo ra các thông báo lỗi tốt

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
11

Hay đấy. nó thực hiện rất nhiều kết hợp cấu trúc lồng nhau, có vẻ như rất phù hợp. Hãy xem về việc chuyển đổi nó. Loại này làm công việc

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
12

Nếu các thông báo lỗi cụ thể không quan trọng, điều đó thực sự tốt. Tuy nhiên, trong trường hợp này có lẽ họ làm được, nếu không thì nó đã không được chia ra một cách cẩn thận như vậy. Để khắc phục điều đó, chúng ta cần chỉ định tất cả các trường hợp, nhưng theo thứ tự ngược lại với trường hợp ban đầu, sao cho trường hợp cụ thể nhất được khớp trước

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
13

Có rõ ràng hơn không? . Có một chút kỳ lạ khi lặp lại chính mình, ngày càng ít cụ thể hơn. Nó cũng có vẻ ít rõ ràng hơn đối với tôi với các trường hợp “ngược”, rơi vào các trận đấu lỏng lẻo hơn. Và

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
762 theo sau bởi
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
763 có nghĩa là "không dài 2" là không chính xác rõ ràng

Kho

Kho, mã máy chủ của PyPI, có 59.000 dòng mã Python, bao gồm cả các bài kiểm tra. Có 35 cách sử dụng

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
716, hoặc 0. 06%. Thật thú vị, đó là một mức độ nhỏ hơn so với thư viện tiêu chuẩn hoặc Django, điều này phù hợp với phỏng đoán của tôi rằng
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
7 sẽ không mang lại nhiều lợi ích bằng mã "thông thường"

Ví dụ về đồng bộ hóa BigQuery, trong
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
766. Đây là ví dụ duy nhất tôi tìm thấy trong Kho mà [lúc đầu. ] có vẻ như sẽ được hưởng lợi từ
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
7, nhưng hóa ra không phải vậy

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
14

Tuy nhiên, khi xem xét kỹ hơn, các thử nghiệm cấu trúc này đang được thực hiện trên ba giá trị khác nhau [

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
768,
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
769 và
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
770] và cấu trúc mà chúng đang được thử nghiệm là động. Lúc đầu, tôi đã nghĩ rằng
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
771 sẽ làm những gì chúng tôi muốn, nhưng mã thực sự khớp với một thuộc tính có tên là bất kỳ giá trị nào của
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
772. Khó khăn

Có vẻ như Kho hàng không thực sự khóc vì

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
7. Dù sao thì tôi cũng quyết định giữ nó ở đây, vì tôi nghĩ đó là một điểm đối lập tốt. Hãy tìm thêm một vài ví dụ bằng cách lướt qua hai ứng dụng lớn khác. Mercurial và Ansible

Không kiên định

Mercurial, hệ thống kiểm soát phiên bản, có 268.000 dòng mã Python, bao gồm cả các bài kiểm tra. Có 1941 lần sử dụng

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
716, hoặc 0. 7% – tỷ lệ cao nhất từ ​​trước đến nay

Ví dụ về ________ 1775, trong ________ 1776. Cải thiện nhỏ bằng cách sử dụng giải nén tuple

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
15

Thay đổi điều đó để sử dụng

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
7

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
16

Có khá nhiều trường hợp như thế này, nó có thể không phải là một chiến thắng lớn, nhưng nó là một sự cải thiện nhỏ về “chất lượng cuộc sống” cho các nhà phát triển

ansible

Ansible là một hệ thống quản lý cấu hình được sử dụng rộng rãi được viết bằng Python. Nó có 217.000 dòng mã Python, bao gồm cả các bài kiểm tra. Có 1594 lần sử dụng

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
716, một lần nữa là 0. 7%. Dưới đây là một vài trường hợp tôi thấy có thể được hưởng lợi từ việc khớp mẫu

Ví dụ về ________ 1779, trong ________ 1780. Cải thiện khả năng đọc nhỏ

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
17

Sử dụng

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
7 với một số mẫu cấu trúc nhẹ mang lại cho chúng tôi một cải thiện nhỏ về khả năng đọc – mặc dù tôi không chắc cách tốt nhất để xử lý các loại khác trong
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
782

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
18

Ví dụ về ________ 1783, trong ________ 1784, một số mã so sánh phiên bản. Kiểm tra loại tốt hơn một chút với
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
7

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
19

Một lần nữa, nó đẹp hơn một chút với

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
7

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
0

Trong tất cả các dự án này, có nhiều trường hợp khác có thể được chuyển đổi để sử dụng

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
7, nhưng tôi đã cố gắng chọn ra một vài loại mã khác nhau mà ít nhất nó hợp lý để thử.

Một số vấn đề với tính năng

Như tôi đã trình bày, đối sánh mẫu giúp mã rõ ràng hơn trong một số trường hợp, nhưng tôi có một số lo ngại với tính năng này. Rõ ràng là con tàu đã ra khơi – Python 3. 10 sẽ ra mắt trong vài ngày tới. – nhưng tôi nghĩ việc xem xét các vấn đề cho các thiết kế trong tương lai là rất có giá trị. [Python chắc chắn không cung cấp mọi tính năng mà mọi người muốn. những điều thú vị để nghiên cứu. ]

Có một số nội dung tầm thường như cách

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
5 yêu cầu hai mức thụt đầu dòng. các tác giả PEP có nhiều lựa chọn thay thế khác nhau và tôi tin rằng họ đã chọn đúng con đường – đó chỉ là một phiền toái nhỏ. Nhưng còn những vấn đề lớn hơn thì sao?

Đường cong học tập và diện tích bề mặt. Như bạn có thể thấy từ kích thước của thông số kỹ thuật PEP, có rất nhiều tính năng cho tính năng này, với khoảng 10 tính năng phụ được gói gọn trong một tính năng. Python luôn là một ngôn ngữ dễ học và tính năng này, mặc dù nó có thể trông đẹp trên trang, nhưng có rất nhiều phức tạp trong ngữ nghĩa của nó

Một cách khác để làm mọi thứ. Zen of Python nói, “Nên có một – và tốt nhất là chỉ một – cách rõ ràng để làm điều đó. ” Trên thực tế, Python luôn có nhiều cách khác nhau để làm việc. Nhưng bây giờ có một thứ bổ sung thêm một chút tải nhận thức cho các nhà phát triển. như thể hiện trong nhiều ví dụ, các nhà phát triển thường có thể cần phải thử cả khi có và không có

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
7, và vẫn còn tranh cãi xem cái nào “rõ ràng” hơn

Chỉ hữu ích trong các miền hiếm hơn. Như đã trình bày ở trên, có trường hợp

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
7 thực sự tỏa sáng. Nhưng chúng cách nhau rất ít, chủ yếu là khi xử lý cây cú pháp và viết trình phân tích cú pháp. Rất nhiều mã có chuỗi
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
4, nhưng chúng thường là giá trị chuyển đổi đơn giản, trong đó
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
716 gần như cũng hoạt động tốt hoặc các điều kiện mà chúng đang thử nghiệm là sự kết hợp phức tạp hơn của các thử nghiệm không phù hợp với các mẫu
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
8

Linh cảm của tôi là các tác giả PEP [Brandt Bucher và Guido van Rossum, cả hai đều là nhà phát triển cốt lõi của Python] thường xuyên viết loại mã có lợi từ việc khớp mẫu, nhưng hầu hết các nhà phát triển ứng dụng và người viết kịch bản sẽ ít cần đến

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
7 hơn. Đặc biệt, Guido van Rossum đã làm việc với trình kiểm tra kiểu Mypy được một thời gian và hiện anh ấy đang làm việc để tăng tốc CPython – trình biên dịch hoạt động chắc chắn liên quan đến AST

Cú pháp hoạt động khác nhau. Có ít nhất hai phần của tính năng này trong đó cú pháp trông giống như một thứ trong “Python bình thường” hoạt động khác bên trong một mẫu

  1. Tên biến. một biến trong mệnh đề
    match event.get[]:
        case Click[[x, y], button=Button.LEFT]:  # This is a left click
            handle_click_at[x, y]
        case Click[]:
            pass  # ignore other clicks
        case KeyPress[key_name="Q"] | Quit[]:
            game.quit[]
        case KeyPress[key_name="up arrow"]:
            game.go_north[]
        ...
        case KeyPress[]:
            pass # Ignore other keystrokes
        case other_event:
            raise ValueError[f"Unrecognized event: {other_event}"]
    
    8 không trả về giá trị của nó như trong mã thông thường, nó liên kết nó dưới dạng tên. Điều này có nghĩa là
    match event.get[]:
        case Click[[x, y], button=Button.LEFT]:  # This is a left click
            handle_click_at[x, y]
        case Click[]:
            pass  # ignore other clicks
        case KeyPress[key_name="Q"] | Quit[]:
            game.quit[]
        case KeyPress[key_name="up arrow"]:
            game.go_north[]
        ...
        case KeyPress[]:
            pass # Ignore other keystrokes
        case other_event:
            raise ValueError[f"Unrecognized event: {other_event}"]
    
    798 không hoạt động như bạn mong đợi – nó sẽ đặt một biến mới có tên là
    match event.get[]:
        case Click[[x, y], button=Button.LEFT]:  # This is a left click
            handle_click_at[x, y]
        case Click[]:
            pass  # ignore other clicks
        case KeyPress[key_name="Q"] | Quit[]:
            game.quit[]
        case KeyPress[key_name="up arrow"]:
            game.go_north[]
        ...
        case KeyPress[]:
            pass # Ignore other keystrokes
        case other_event:
            raise ValueError[f"Unrecognized event: {other_event}"]
    
    799, không khớp với hằng số màu của bạn. Để khớp với các hằng số, chúng phải có một dấu chấm trong đó – vì vậy
    match event.get[]:
        case Click[[x, y], button=Button.LEFT]:  # This is a left click
            handle_click_at[x, y]
        case Click[]:
            pass  # ignore other clicks
        case KeyPress[key_name="Q"] | Quit[]:
            game.quit[]
        case KeyPress[key_name="up arrow"]:
            game.go_north[]
        ...
        case KeyPress[]:
            pass # Ignore other keystrokes
        case other_event:
            raise ValueError[f"Unrecognized event: {other_event}"]
    
    700 hoạt động. Khi viết một số mã ở trên, tôi thực sự đã mắc lỗi này. Tôi đã viết ________ 2701, hy vọng nó khớp nếu mục thứ hai của bộ dữ liệu bằng với ________ 555, nhưng tất nhiên nó sẽ đặt
    match event.get[]:
        case Click[[x, y], button=Button.LEFT]:  # This is a left click
            handle_click_at[x, y]
        case Click[]:
            pass  # ignore other clicks
        case KeyPress[key_name="Q"] | Quit[]:
            game.quit[]
        case KeyPress[key_name="up arrow"]:
            game.go_north[]
        ...
        case KeyPress[]:
            pass # Ignore other keystrokes
        case other_event:
            raise ValueError[f"Unrecognized event: {other_event}"]
    
    55 cho mục thứ hai
  2. mẫu lớp. chúng trông giống như các lời gọi hàm, nhưng chúng thực sự là các bài kiểm tra
    match event.get[]:
        case Click[[x, y], button=Button.LEFT]:  # This is a left click
            handle_click_at[x, y]
        case Click[]:
            pass  # ignore other clicks
        case KeyPress[key_name="Q"] | Quit[]:
            game.quit[]
        case KeyPress[key_name="up arrow"]:
            game.go_north[]
        ...
        case KeyPress[]:
            pass # Ignore other keystrokes
        case other_event:
            raise ValueError[f"Unrecognized event: {other_event}"]
    
    704 và
    match event.get[]:
        case Click[[x, y], button=Button.LEFT]:  # This is a left click
            handle_click_at[x, y]
        case Click[]:
            pass  # ignore other clicks
        case KeyPress[key_name="Q"] | Quit[]:
            game.quit[]
        case KeyPress[key_name="up arrow"]:
            game.go_north[]
        ...
        case KeyPress[]:
            pass # Ignore other keystrokes
        case other_event:
            raise ValueError[f"Unrecognized event: {other_event}"]
    
    705. Nó trông đẹp, nhưng đôi khi nó khó hiểu. Điều đó cũng có nghĩa là bạn không thể khớp với kết quả của một lệnh gọi hàm thực tế - điều đó sẽ phải ở trong một lớp bảo vệ
    match event.get[]:
        case Click[[x, y], button=Button.LEFT]:  # This is a left click
            handle_click_at[x, y]
        case Click[]:
            pass  # ignore other clicks
        case KeyPress[key_name="Q"] | Quit[]:
            game.quit[]
        case KeyPress[key_name="up arrow"]:
            game.go_north[]
        ...
        case KeyPress[]:
            pass # Ignore other keystrokes
        case other_event:
            raise ValueError[f"Unrecognized event: {other_event}"]
    
    26

Cơ sở lý luận PEP thừa nhận các biến thể cú pháp này trong

Mặc dù bề ngoài các mẫu có thể trông giống như các biểu thức, nhưng điều quan trọng cần lưu ý là có sự phân biệt rõ ràng. Trên thực tế, không có mẫu nào là hoặc chứa một biểu thức. Sẽ hiệu quả hơn khi nghĩ về các mẫu như các phần tử khai báo tương tự như các tham số chính thức trong định nghĩa hàm

Phép thuật __match_args__. Theo tôi, tính năng

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
27 quá kỳ diệu và yêu cầu các nhà phát triển quyết định thuộc tính nào của lớp sẽ phù hợp với vị trí, nếu có. Cũng lạ là thứ tự của
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
27 có thể khác với thứ tự của các tham số
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
709 của lớp [mặc dù trong thực tế, bạn sẽ cố gắng không làm như vậy]. Tôi có thể hiểu tại sao họ lại đưa vào tính năng này, vì nó làm cho nút AST khớp với lượt thích thực sự thú vị, nhưng nó không rõ ràng lắm

Chi phí triển khai khác. Cho đến nay, CPython là trình thông dịch Python được sử dụng phổ biến nhất, nhưng cũng có những trình thông dịch khác, chẳng hạn như PyPy và MicroPython, sẽ phải quyết định có triển khai tính năng này hay không. Các trình thông dịch khác luôn bắt kịp, nhưng một tính năng có kích thước này ở giai đoạn này trong lịch sử của Python sẽ khiến các triển khai khác khó theo kịp hơn

Ban đầu, tôi cũng lo ngại rằng các mẫu lớp của

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
7 không phù hợp với việc sử dụng cách gõ vịt của Python, nơi bạn chỉ cần truy cập các thuộc tính và gọi các phương thức trên một đối tượng mà không cần kiểm tra loại của nó trước [ví dụ: khi sử dụng ]. Tuy nhiên, với các mẫu lớp, bạn chỉ định loại và nó thực hiện kiểm tra
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
704. Vẫn có thể gõ vịt bằng cách sử dụng
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
712, nhưng sẽ hơi lạ

Tuy nhiên, bây giờ tôi đã sử dụng tính năng này, tôi nghĩ đây chủ yếu là mối quan tâm về mặt lý thuyết – những nơi bạn sử dụng các mẫu lớp không thực sự trùng lặp với những nơi bạn sử dụng kiểu gõ vịt

Mối quan tâm gõ vịt này nằm trong lý do PEP

Tuy nhiên, để tôn vinh bản chất động của Python với tính năng 'gõ vịt', chúng tôi cũng đã thêm một cách trực tiếp hơn để chỉ định sự hiện diện hoặc các ràng buộc đối với các thuộc tính cụ thể. Thay vì

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
713, bạn cũng có thể viết
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
714, loại bỏ hiệu quả việc kiểm tra
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
704 và do đó hỗ trợ bất kỳ đối tượng nào có thuộc tính
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
716 và
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
717

kết thúc

Tôi thích một số khía cạnh của khớp mẫu và một số mã chắc chắn sạch hơn với

match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
5 so với
match event.get[]:
    case Click[[x, y], button=Button.LEFT]:  # This is a left click
        handle_click_at[x, y]
    case Click[]:
        pass  # ignore other clicks
    case KeyPress[key_name="Q"] | Quit[]:
        game.quit[]
    case KeyPress[key_name="up arrow"]:
        game.go_north[]
    ...
    case KeyPress[]:
        pass # Ignore other keystrokes
    case other_event:
        raise ValueError[f"Unrecognized event: {other_event}"]
4. Nhưng tính năng này có cung cấp đủ giá trị để biện minh cho sự phức tạp hay không, chưa kể đến gánh nặng nhận thức mà nó đặt lên những người học Python hoặc đọc mã Python?

Điều đó nói rằng, Python luôn là ngôn ngữ lập trình thực dụng, không phải là giấc mơ thuần túy. Như Bjarne Stroustrup, người tạo ra C++, đã nói, “Chỉ có hai loại ngôn ngữ. những thứ mọi người phàn nàn và những thứ không ai sử dụng. ” Tôi luôn thích Python và tôi đã sử dụng thành công nó trong nhiều năm. Tôi gần như chắc chắn sẽ tiếp tục sử dụng nó cho nhiều nhiệm vụ. Nó không hoàn hảo, nhưng nếu có, sẽ không ai sử dụng nó

Gần đây tôi cũng đã sử dụng Go rất nhiều và chắc chắn có điều gì đó tốt về việc ngôn ngữ thay đổi chậm như thế nào [theo thiết kế]. Hầu hết các ghi chú phát hành đều bắt đầu bằng “không có thay đổi nào đối với ngôn ngữ” – ví dụ: trong Go 1. 16 tất cả các thay đổi đều có trong thư viện công cụ và tiêu chuẩn. Điều đó nói rằng, Go sẽ có tính năng mới lớn của riêng mình trong một vài tháng với các thuốc generic sắp ra mắt trong Go 1. 18

Nhìn chung, tôi hơi bi quan về khớp mẫu cấu trúc trong Python. Nó chỉ là một tính năng lớn được thêm vào quá muộn trong trò chơi [Python năm nay 30 tuổi]. Có phải ngôn ngữ bắt đầu phát nổ dưới sức nặng của chính nó?

Hoặc, như bạn tôi dự đoán, đó có phải là một trong những tính năng sẽ được sử dụng quá mức cho mọi thứ trong vài năm, sau đó cộng đồng sẽ ổn định và chỉ sử dụng nó ở những nơi nó thực sự cải thiện mã?

Bình luận tại Lobsters hoặc Hacker News

Tôi rất vui nếu bạn tài trợ cho tôi trên GitHub – nó sẽ thúc đẩy tôi làm việc với các dự án nguồn mở của mình và viết nhiều nội dung hay hơn. Cảm ơn

Liệu Python 3. 9 có phù hợp trường hợp?

Trong Python 3. 9, bạn sẽ sử dụng câu lệnh if có bốn nhánh. Trước tiên, chúng tôi xác định, sau từ khóa khớp, biến mà chúng tôi muốn khớp. Sau đó, mỗi trường hợp bắt đầu bằng từ khóa trường hợp, theo sau là mẫu mà chúng tôi muốn kiểm tra .

Phiên bản Python nào hỗ trợ khớp?

Trăn 3. 10 đã được phát hành vào giữa năm 2021 và đi kèm với tính năng so khớp mẫu cấu trúc, còn được gọi là câu lệnh so khớp.

Có chức năng khớp trong Python không?

match[] cả hai đều là chức năng của mô-đun re trong python . Các chức năng này rất hiệu quả và nhanh chóng để tìm kiếm trong chuỗi. Hàm tìm kiếm một số chuỗi con trong một chuỗi và trả về một đối tượng khớp nếu tìm thấy, nếu không, nó sẽ không trả về. Có một sự khác biệt giữa việc sử dụng cả hai chức năng.

Liệu Python 3. 10 có câu lệnh switch không?

Từ phiên bản 3. 10 trở lên, Python đã triển khai tính năng trường hợp chuyển đổi có tên là “khớp mẫu cấu trúc” . Bạn có thể triển khai tính năng này với từ khóa đối sánh và trường hợp. Một số người tranh luận liệu đối sánh và trường hợp có phải là từ khóa trong Python hay không.

Chủ Đề