Câu nào đúng với hàm Python?

The global variable's value remains unchanged. Variable inside the function is a local variable. Learn local and global variables in Python

Question 2. What is not true about formal and actual parameters?

  • Their Number and type must match
  • Need not be given in same order
  • Their names must be identical
  • All statements are true
Check Answer

Trả lời. C

Parameter names used in the function definition are formal, hence they need not be the same as variables whose value is passed when the function is called. Their number and type however, must match. If used as keyword arguments, their order of definition can be different from the order of call. Tìm hiểu chức năng Python

câu hỏi 3. In Python, functions are

  • Always called by passing reference
  • Always called by passing value
  • May be called by passing reference or value
  • None of these
Check Answer

Trả lời. A

In Python, variables are just labels to objects in memory. Hence formal and actual argument variables point to the same object in memory. So, functions in Python are always called by passing reference. Learn Python Function

câu hỏi 4. Biến được sử dụng bên trong một hàm trở thành biến của nó

  • Biến cục bộ
  • Biến toàn cục
  • biến tham chiếu
  • None of these
Check Answer

Trả lời. A

Phạm vi của một biến bị giới hạn bởi ngữ cảnh mà nó được xác định. Do đó, một biến được xác định bên trong một hàm không có sẵn để sử dụng bên ngoài nó. Vì vậy, nó là một biến cục bộ. Tìm hiểu chức năng Python

câu hỏi 5. Điều gì là đúng về chuỗi tài liệu của một chức năng?

  • Nó là một chuỗi được viết ở dòng đầu tiên trong khối chức năng
  • Nó có thể được viết bằng dấu nháy đơn, kép hoặc ba
  • Nó bị bỏ qua bởi trình thông dịch
  • Tất cả những điều trên
Check Answer

Trả lời. Đ.

Nếu dòng đầu tiên trong khối nội dung của hàm là một chuỗi tùy chọn, thì nó không được trình thông dịch Python xử lý mà hoạt động như một chuỗi tài liệu của đối tượng hàm. Tìm hiểu chức năng Python

Câu lệnh ghép chứa (nhóm) câu lệnh khác; . Nói chung, các câu lệnh ghép kéo dài trên nhiều dòng, mặc dù trong các phiên bản đơn giản, toàn bộ câu lệnh ghép có thể được chứa trong một dòng

Các câu lệnh , và triển khai các cấu trúc luồng điều khiển truyền thống. chỉ định trình xử lý ngoại lệ và/hoặc mã dọn dẹp cho một nhóm câu lệnh, trong khi câu lệnh cho phép thực thi mã khởi tạo và hoàn thiện xung quanh một khối mã. Các định nghĩa hàm và lớp cũng là các câu lệnh ghép về mặt cú pháp

Một câu lệnh ghép bao gồm một hoặc nhiều ‘mệnh đề. ' Một mệnh đề bao gồm một tiêu đề và một 'bộ. ' Các tiêu đề mệnh đề của một câu lệnh ghép cụ thể đều ở cùng một mức thụt đầu dòng. Mỗi tiêu đề mệnh đề bắt đầu bằng một từ khóa xác định duy nhất và kết thúc bằng dấu hai chấm. Một bộ là một nhóm các câu lệnh được điều khiển bởi một mệnh đề. Một bộ có thể là một hoặc nhiều câu lệnh đơn giản được phân tách bằng dấu chấm phẩy trên cùng một dòng với tiêu đề, theo sau dấu hai chấm của tiêu đề hoặc nó có thể là một hoặc nhiều câu lệnh thụt lề trên các dòng tiếp theo. Chỉ dạng sau của bộ mới có thể chứa các câu lệnh ghép lồng nhau;

if test1: if test2: print(x)

Cũng lưu ý rằng dấu chấm phẩy liên kết chặt chẽ hơn dấu hai chấm trong ngữ cảnh này, do đó, trong ví dụ sau, tất cả hoặc không lệnh gọi nào được thực hiện

if x < y < z: print(x); print(y); print(z)

tổng kết

compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]

Lưu ý rằng các câu lệnh luôn kết thúc bằng

for i in range(10):
    print(i)
    i = 5             # this will not affect the for-loop
                      # because i will be overwritten with the next
                      # index in the range
0 có thể theo sau bởi một
for i in range(10):
    print(i)
    i = 5             # this will not affect the for-loop
                      # because i will be overwritten with the next
                      # index in the range
1. Cũng lưu ý rằng các mệnh đề tiếp tục tùy chọn luôn bắt đầu bằng một từ khóa không thể bắt đầu một câu lệnh, do đó không có sự mơ hồ (vấn đề 'dangling' được giải quyết trong Python bằng cách yêu cầu các câu lệnh lồng nhau được thụt vào)

Định dạng của các quy tắc ngữ pháp trong các phần sau đặt mỗi mệnh đề trên một dòng riêng biệt cho rõ ràng

8. 1. Câu lệnh for_stmt ::= "for" target_list "in" starred_list ":" suite ["else" ":" suite] 2

Câu lệnh được sử dụng để thực hiện có điều kiện

if_stmt ::=  "if" assignment_expression ":" suite
             ("elif" assignment_expression ":" suite)*
             ["else" ":" suite]

Nó chọn chính xác một trong các bộ bằng cách đánh giá từng biểu thức cho đến khi một biểu thức được tìm thấy là đúng (xem phần định nghĩa đúng và sai); . Nếu tất cả các biểu thức là sai, bộ mệnh đề, nếu có, được thực thi

8. 2. Câu lệnh for_stmt ::= "for" target_list "in" starred_list ":" suite ["else" ":" suite] 3

Câu lệnh được sử dụng để thực hiện lặp lại miễn là một biểu thức là đúng

________số 8_______

Điều này lặp đi lặp lại kiểm tra biểu thức và, nếu nó đúng, sẽ thực thi bộ đầu tiên;

Một câu lệnh được thực hiện trong bộ đầu tiên kết thúc vòng lặp mà không thực hiện bộ của mệnh đề

for_stmt ::=  "for" target_list "in" starred_list ":" suite
              ["else" ":" suite]
8. Một câu lệnh được thực hiện trong bộ đầu tiên bỏ qua phần còn lại của bộ và quay lại kiểm tra biểu thức

8. 3. Câu lệnh for_stmt ::= "for" target_list "in" starred_list ":" suite ["else" ":" suite] 4

Câu lệnh được sử dụng để lặp qua các phần tử của chuỗi (chẳng hạn như chuỗi, bộ dữ liệu hoặc danh sách) hoặc đối tượng có thể lặp khác

for_stmt ::=  "for" target_list "in" starred_list ":" suite
              ["else" ":" suite]

Biểu thức

try_stmt  ::=  try1_stmt | try2_stmt | try3_stmt
try1_stmt ::=  "try" ":" suite
               ("except" [expression ["as" identifier]] ":" suite)+
               ["else" ":" suite]
               ["finally" ":" suite]
try2_stmt ::=  "try" ":" suite
               ("except" "*" expression ["as" identifier] ":" suite)+
               ["else" ":" suite]
               ["finally" ":" suite]
try3_stmt ::=  "try" ":" suite
               "finally" ":" suite
6 được đánh giá một lần; . An được tạo cho iterable đó. Mục đầu tiên do iterator cung cấp sau đó được gán cho danh sách đích bằng cách sử dụng các quy tắc chuẩn cho phép gán (xem phần ) và bộ phần mềm được thực thi. Điều này lặp lại cho mỗi mục được cung cấp bởi iterator. Khi bộ vòng lặp cạn kiệt, bộ trong mệnh đề
for_stmt ::=  "for" target_list "in" starred_list ":" suite
              ["else" ":" suite]
8, nếu có, sẽ được thực thi và vòng lặp kết thúc

Một câu lệnh được thực hiện trong bộ đầu tiên kết thúc vòng lặp mà không thực hiện bộ của mệnh đề

for_stmt ::=  "for" target_list "in" starred_list ":" suite
              ["else" ":" suite]
8. Một câu lệnh được thực hiện trong bộ đầu tiên sẽ bỏ qua phần còn lại của bộ và tiếp tục với mục tiếp theo hoặc với mệnh đề
for_stmt ::=  "for" target_list "in" starred_list ":" suite
              ["else" ":" suite]
8 nếu không có mục tiếp theo

Vòng lặp for thực hiện gán cho các biến trong danh sách đích. Thao tác này sẽ ghi đè lên tất cả các phép gán trước đó cho các biến đó, bao gồm cả các phép gán được thực hiện trong bộ vòng lặp for

for i in range(10):
    print(i)
    i = 5             # this will not affect the for-loop
                      # because i will be overwritten with the next
                      # index in the range

Names in the target list are not deleted when the loop is finished, but if the sequence is empty, they will not have been assigned to at all by the loop. Gợi ý. the built-in type represents immutable arithmetic sequences of integers. For instance, iterating

except E as N:
    foo
3 successively yields 0, 1, and then 2

Đã thay đổi trong phiên bản 3. 11. Starred elements are now allowed in the expression list.

8. 4. The for_stmt ::= "for" target_list "in" starred_list ":" suite ["else" ":" suite] 5 statement

The

for_stmt ::=  "for" target_list "in" starred_list ":" suite
              ["else" ":" suite]
5 statement specifies exception handlers and/or cleanup code for a group of statements

try_stmt  ::=  try1_stmt | try2_stmt | try3_stmt
try1_stmt ::=  "try" ":" suite
               ("except" [expression ["as" identifier]] ":" suite)+
               ["else" ":" suite]
               ["finally" ":" suite]
try2_stmt ::=  "try" ":" suite
               ("except" "*" expression ["as" identifier] ":" suite)+
               ["else" ":" suite]
               ["finally" ":" suite]
try3_stmt ::=  "try" ":" suite
               "finally" ":" suite

Thông tin bổ sung về ngoại lệ có thể được tìm thấy trong phần và thông tin về cách sử dụng câu lệnh để tạo ngoại lệ có thể được tìm thấy trong phần

8. 4. 1. except E as N: foo 7 clause

The

except E as N:
    foo
7 clause(s) specify one or more exception handlers. When no exception occurs in the clause, no exception handler is executed. When an exception occurs in the
for_stmt ::=  "for" target_list "in" starred_list ":" suite
              ["else" ":" suite]
5 suite, a search for an exception handler is started. This search inspects the
except E as N:
    foo
7 clauses in turn until one is found that matches the exception. An expression-less
except E as N:
    foo
7 clause, if present, must be last; it matches any exception. For an
except E as N:
    foo
7 clause with an expression, that expression is evaluated, and the clause matches the exception if the resulting object is “compatible” with the exception. An object is compatible with an exception if the object is the class or a of the exception object, or a tuple containing an item that is the class or a non-virtual base class of the exception object

If no

except E as N:
    foo
7 clause matches the exception, the search for an exception handler continues in the surrounding code and on the invocation stack.

Nếu việc đánh giá một biểu thức trong tiêu đề của mệnh đề

except E as N:
    foo
7 làm phát sinh một ngoại lệ, thì quá trình tìm kiếm ban đầu cho một trình xử lý sẽ bị hủy và bắt đầu tìm kiếm ngoại lệ mới trong mã xung quanh và trên ngăn xếp cuộc gọi (nó được coi như thể toàn bộ

When a matching

except E as N:
    foo
7 clause is found, the exception is assigned to the target specified after the
except E as N:
    try:
        foo
    finally:
        del N
8 keyword in that
except E as N:
    foo
7 clause, if present, and the
except E as N:
    foo
7 clause’s suite is executed. All
except E as N:
    foo
7 clauses must have an executable block. When the end of this block is reached, execution continues normally after the entire statement. (This means that if two nested handlers exist for the same exception, and the exception occurs in the
for_stmt ::=  "for" target_list "in" starred_list ":" suite
              ["else" ":" suite]
5 clause of the inner handler, the outer handler will not handle the exception. )

When an exception has been assigned using

if x < y < z: print(x); print(y); print(z)
04, it is cleared at the end of the
except E as N:
    foo
7 clause. This is as if

except E as N:
    foo

was translated to

except E as N:
    try:
        foo
    finally:
        del N

This means the exception must be assigned to a different name to be able to refer to it after the

except E as N:
    foo
7 clause. Exceptions are cleared because with the traceback attached to them, they form a reference cycle with the stack frame, keeping all locals in that frame alive until the next garbage collection occurs

Before an

except E as N:
    foo
7 clause’s suite is executed, details about the exception are stored in the module and can be accessed via . trả về 3-tuple bao gồm lớp ngoại lệ, thể hiện ngoại lệ và đối tượng truy nguyên (xem phần ) xác định điểm trong chương trình xảy ra ngoại lệ. The details about the exception accessed via are restored to their previous values when leaving an exception handler

if x < y < z: print(x); print(y); print(z)
0

8. 4. 2. if x < y < z: print(x); print(y); print(z) 12 clause

The

if x < y < z: print(x); print(y); print(z)
12 clause(s) are used for handling s. The exception type for matching is interpreted as in the case of , but in the case of exception groups we can have partial matches when the type matches some of the exceptions in the group. This means that multiple
if x < y < z: print(x); print(y); print(z)
12 clauses can execute, each handling part of the exception group. Mỗi mệnh đề thực thi nhiều nhất một lần và xử lý một nhóm ngoại lệ gồm tất cả các ngoại lệ phù hợp. Each exception in the group is handled by at most one
if x < y < z: print(x); print(y); print(z)
12 clause, the first that matches it

if x < y < z: print(x); print(y); print(z)
1

Any remaining exceptions that were not handled by any

if x < y < z: print(x); print(y); print(z)
12 clause are re-raised at the end, combined into an exception group along with all exceptions that were raised from within
if x < y < z: print(x); print(y); print(z)
12 clauses

If the raised exception is not an exception group and its type matches one of the

if x < y < z: print(x); print(y); print(z)
12 clauses, it is caught and wrapped by an exception group with an empty message string

if x < y < z: print(x); print(y); print(z)
2

An

if x < y < z: print(x); print(y); print(z)
12 clause must have a matching type, and this type cannot be a subclass of . It is not possible to mix and
if x < y < z: print(x); print(y); print(z)
12 in the same . , and cannot appear in an
if x < y < z: print(x); print(y); print(z)
12 clause

8. 4. 3. ______5_______8 mệnh đề

The optional

for_stmt ::=  "for" target_list "in" starred_list ":" suite
              ["else" ":" suite]
8 clause is executed if the control flow leaves the suite, no exception was raised, and no , , or statement was executed. Exceptions in the
for_stmt ::=  "for" target_list "in" starred_list ":" suite
              ["else" ":" suite]
8 clause are not handled by the preceding clauses

8. 4. 4. ______1_______38 mệnh đề

Nếu có

if x < y < z: print(x); print(y); print(z)
38, nó chỉ định trình xử lý 'dọn dẹp'. Mệnh đề được thực hiện, bao gồm mệnh đề any và mệnh đề. If an exception occurs in any of the clauses and is not handled, the exception is temporarily saved. The
if x < y < z: print(x); print(y); print(z)
38 clause is executed. If there is a saved exception it is re-raised at the end of the
if x < y < z: print(x); print(y); print(z)
38 clause. If the
if x < y < z: print(x); print(y); print(z)
38 clause raises another exception, the saved exception is set as the context of the new exception. If the
if x < y < z: print(x); print(y); print(z)
38 clause executes a , or statement, the saved exception is discarded

if x < y < z: print(x); print(y); print(z)
3

The exception information is not available to the program during execution of the

if x < y < z: print(x); print(y); print(z)
38 clause

Khi một câu lệnh , hoặc được thực hiện trong bộ câu lệnh

for_stmt ::=  "for" target_list "in" starred_list ":" suite
              ["else" ":" suite]
5…
if x < y < z: print(x); print(y); print(z)
38, thì mệnh đề
if x < y < z: print(x); print(y); print(z)
38 cũng được thực hiện ‘on the way out. ’

The return value of a function is determined by the last statement executed. Since the

if x < y < z: print(x); print(y); print(z)
38 clause always executes, a
if x < y < z: print(x); print(y); print(z)
28 statement executed in the
if x < y < z: print(x); print(y); print(z)
38 clause will always be the last one executed

if x < y < z: print(x); print(y); print(z)
4

Changed in version 3. 8. Prior to Python 3. 8, a statement was illegal in the

if x < y < z: print(x); print(y); print(z)
38 clause due to a problem with the implementation.

8. 5. The for_stmt ::= "for" target_list "in" starred_list ":" suite ["else" ":" suite] 6 statement

The statement is used to wrap the execution of a block with methods defined by a context manager (see section ). Điều này cho phép đóng gói các mẫu sử dụng phổ biến để tái sử dụng thuận tiện

if x < y < z: print(x); print(y); print(z)
5

Việc thực hiện câu lệnh với một "mục" tiến hành như sau

  1. Biểu thức ngữ cảnh (biểu thức được đưa ra trong) được đánh giá để có được trình quản lý ngữ cảnh

  2. if x < y < z: print(x); print(y); print(z)
    
    71 của trình quản lý ngữ cảnh được tải để sử dụng sau này

  3. if x < y < z: print(x); print(y); print(z)
    
    72 của trình quản lý ngữ cảnh được tải để sử dụng sau này

  4. Phương thức

    if x < y < z: print(x); print(y); print(z)
    
    71 của trình quản lý bối cảnh được gọi

  5. Nếu một mục tiêu được bao gồm trong câu lệnh, giá trị trả về từ

    if x < y < z: print(x); print(y); print(z)
    
    71 được gán cho nó

    Ghi chú

    Câu lệnh đảm bảo rằng nếu phương thức

    if x < y < z: print(x); print(y); print(z)
    
    71 trả về mà không có lỗi, thì
    if x < y < z: print(x); print(y); print(z)
    
    72 sẽ luôn được gọi. Do đó, nếu một lỗi xảy ra trong quá trình gán cho danh sách đích, nó sẽ được xử lý giống như một lỗi xảy ra trong bộ sẽ được xử lý. Xem bước 7 bên dưới

  6. Bộ được thực hiện

  7. Phương thức

    if x < y < z: print(x); print(y); print(z)
    
    72 của trình quản lý bối cảnh được gọi. Nếu một ngoại lệ khiến bộ phần mềm bị thoát, loại, giá trị và truy nguyên của nó sẽ được chuyển thành đối số cho
    if x < y < z: print(x); print(y); print(z)
    
    72. Mặt khác, ba đối số được cung cấp

    Nếu bộ phần mềm đã bị thoát do một ngoại lệ và giá trị trả về từ phương thức

    if x < y < z: print(x); print(y); print(z)
    
    72 là sai, thì ngoại lệ đó sẽ được kích hoạt lại. Nếu giá trị trả về là true, ngoại lệ sẽ bị chặn và quá trình thực thi tiếp tục với câu lệnh theo sau câu lệnh

    Nếu bộ phần mềm đã bị thoát vì bất kỳ lý do nào ngoài lý do ngoại lệ, thì giá trị trả về từ

    if x < y < z: print(x); print(y); print(z)
    
    72 sẽ bị bỏ qua và quá trình thực thi sẽ tiếp tục ở vị trí bình thường đối với loại lần thoát đã được thực hiện

Đoạn mã sau

if x < y < z: print(x); print(y); print(z)
6

về mặt ngữ nghĩa tương đương với

if x < y < z: print(x); print(y); print(z)
7

Với nhiều mục, trình quản lý ngữ cảnh được xử lý như thể nhiều câu lệnh được lồng vào nhau

if x < y < z: print(x); print(y); print(z)
8

về mặt ngữ nghĩa tương đương với

if x < y < z: print(x); print(y); print(z)
9

Bạn cũng có thể viết trình quản lý bối cảnh nhiều mục trong nhiều dòng nếu các mục được bao quanh bởi dấu ngoặc đơn. Ví dụ

compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
0

Đã thay đổi trong phiên bản 3. 1. Hỗ trợ nhiều biểu thức ngữ cảnh.

Đã thay đổi trong phiên bản 3. 10. Hỗ trợ sử dụng dấu ngoặc đơn nhóm để ngắt câu lệnh trong nhiều dòng.

Xem thêm

PEP 343 - Câu lệnh “với”

Thông số kỹ thuật, nền tảng và ví dụ cho câu lệnh Python

8. 6. Tuyên bố if x < y < z: print(x); print(y); print(z) 87

Mới trong phiên bản 3. 10

Câu lệnh match được sử dụng để so khớp mẫu. cú pháp

compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
1

Ghi chú

Phần này sử dụng dấu nháy đơn để biểu thị

Đối sánh mẫu lấy một mẫu làm đầu vào (theo sau

if x < y < z: print(x); print(y); print(z)
88) và một giá trị chủ đề (theo sau
if x < y < z: print(x); print(y); print(z)
87). Mẫu (có thể chứa các mẫu con) được khớp với giá trị chủ đề. kết quả là

  • Thành công hay thất bại của trận đấu (còn được gọi là thành công hay thất bại của mẫu)

  • Có thể ràng buộc các giá trị phù hợp với một tên. Các điều kiện tiên quyết cho điều này được thảo luận thêm dưới đây

Các từ khóa

if x < y < z: print(x); print(y); print(z)
87 và
if x < y < z: print(x); print(y); print(z)
88 là

Xem thêm

  • PEP 634 – So khớp mẫu cấu trúc. Sự chỉ rõ

  • PEP 636 – So khớp mẫu cấu trúc. hướng dẫn

8. 6. 1. Tổng quan

Dưới đây là tổng quan về luồng logic của câu lệnh so khớp

  1. Biểu thức chủ đề

    if x < y < z: print(x); print(y); print(z)
    
    92 được đánh giá và giá trị chủ đề thu được. Nếu biểu thức chủ đề chứa dấu phẩy, một bộ dữ liệu được tạo bằng cách sử dụng

  2. Mỗi mẫu trong

    if x < y < z: print(x); print(y); print(z)
    
    93 được cố gắng khớp với giá trị chủ đề. Các quy tắc cụ thể để thành công hay thất bại được mô tả dưới đây. Nỗ lực đối sánh cũng có thể liên kết một số hoặc tất cả các tên độc lập trong mẫu. Các quy tắc ràng buộc mẫu chính xác khác nhau tùy theo loại mẫu và được chỉ định bên dưới. Các liên kết tên được thực hiện trong quá trình khớp mẫu thành công tồn tại lâu hơn khối được thực thi và có thể được sử dụng sau câu lệnh khớp

    Ghi chú

    Trong quá trình khớp mẫu không thành công, một số mẫu phụ có thể thành công. Không dựa vào các ràng buộc được thực hiện cho một trận đấu không thành công. Ngược lại, đừng dựa vào các biến vẫn không thay đổi sau khi khớp không thành công. Hành vi chính xác phụ thuộc vào việc triển khai và có thể thay đổi. Đây là một quyết định có chủ ý được thực hiện để cho phép các triển khai khác nhau thêm tối ưu hóa

  3. Nếu mẫu thành công, bộ bảo vệ tương ứng (nếu có) được đánh giá. Trong trường hợp này, tất cả các ràng buộc tên được đảm bảo đã xảy ra

    • Nếu người bảo vệ đánh giá là đúng hoặc bị thiếu, thì

      if x < y < z: print(x); print(y); print(z)
      
      94 bên trong
      if x < y < z: print(x); print(y); print(z)
      
      93 được thực thi

    • Otherwise, the next

      if x < y < z: print(x); print(y); print(z)
      
      93 is attempted as described above

    • Nếu không có khối trường hợp nào nữa, câu lệnh so khớp được hoàn thành

Ghi chú

Người dùng thường không bao giờ nên dựa vào một mẫu được đánh giá. Tùy thuộc vào việc triển khai, trình thông dịch có thể lưu trữ các giá trị hoặc sử dụng các tối ưu hóa khác để bỏ qua các đánh giá lặp lại

Một tuyên bố đối sánh mẫu

compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
2

Trong trường hợp này,

if x < y < z: print(x); print(y); print(z)
97 là bảo vệ. Read more about that in the next section

8. 6. 2. Người bảo vệ

compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
3

A

if x < y < z: print(x); print(y); print(z)
98 (which is part of the
if x < y < z: print(x); print(y); print(z)
88) must succeed for code inside the
if x < y < z: print(x); print(y); print(z)
88 block to execute. Nó có dạng. theo sau là một biểu thức

The logical flow of a

if x < y < z: print(x); print(y); print(z)
88 block with a
if x < y < z: print(x); print(y); print(z)
98 follows

  1. Check that the pattern in the

    if x < y < z: print(x); print(y); print(z)
    
    88 block succeeded. Nếu mẫu không thành công, khối
    if x < y < z: print(x); print(y); print(z)
    
    98 không được đánh giá và khối tiếp theo
    if x < y < z: print(x); print(y); print(z)
    
    88 được chọn

  2. If the pattern succeeded, evaluate the

    if x < y < z: print(x); print(y); print(z)
    
    98

    • If the

      if x < y < z: print(x); print(y); print(z)
      
      98 condition evaluates as true, the case block is selected

    • Nếu điều kiện

      if x < y < z: print(x); print(y); print(z)
      
      98 được đánh giá là sai, thì khối trường hợp không được chọn

    • If the

      if x < y < z: print(x); print(y); print(z)
      
      98 raises an exception during evaluation, the exception bubbles up

Các lính canh được phép có tác dụng phụ vì chúng là biểu thức. Guard evaluation must proceed from the first to the last case block, one at a time, skipping case blocks whose pattern(s) don’t all succeed. (TÔI. e. , đánh giá bảo vệ phải xảy ra theo thứ tự. ) Guard evaluation must stop once a case block is selected

8. 6. 3. Khối trường hợp không thể bác bỏ

Khối trường hợp không thể bác bỏ là khối trường hợp khớp tất cả. A match statement may have at most one irrefutable case block, and it must be last

A case block is considered irrefutable if it has no guard and its pattern is irrefutable. A pattern is considered irrefutable if we can prove from its syntax alone that it will always succeed. Only the following patterns are irrefutable

  • whose left-hand side is irrefutable

  • containing at least one irrefutable pattern

  • parenthesized irrefutable patterns

8. 6. 4. Patterns

Ghi chú

This section uses grammar notations beyond standard EBNF

Cú pháp cấp cao nhất cho

compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
14 là

compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
4

The descriptions below will include a description “in simple terms” of what a pattern does for illustration purposes (credits to Raymond Hettinger for a document that inspired most of the descriptions). Lưu ý rằng những mô tả này hoàn toàn nhằm mục đích minh họa và có thể không phản ánh việc triển khai cơ bản. Hơn nữa, chúng không bao gồm tất cả các hình thức hợp lệ

8. 6. 4. 1. Mẫu HOẶC

Mẫu OR là hai hoặc nhiều mẫu được phân tách bằng các thanh dọc

compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
15. cú pháp

compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
5

Only the final subpattern may be , and each subpattern must bind the same set of names to avoid ambiguity

An OR pattern matches each of its subpatterns in turn to the subject value, until one succeeds. The OR pattern is then considered successful. Mặt khác, nếu không có mẫu con nào thành công, mẫu OR sẽ thất bại

Nói một cách đơn giản,

compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
16 sẽ cố gắng khớp với
compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
17, nếu thất bại, nó sẽ cố gắng khớp với
compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
18, thành công ngay lập tức nếu thành công, thất bại nếu không

8. 6. 4. 2. Mẫu AS

Mẫu AS khớp với mẫu OR ở bên trái của từ khóa đối với chủ đề. cú pháp

compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
6

Nếu mẫu OR không thành công, mẫu AS không thành công. Mặt khác, mẫu AS liên kết chủ thể với tên ở bên phải của từ khóa as và thành công.

compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
20 cannot be a a
compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
21

In simple terms

compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
22 will match with
compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
23, and on success it will set
compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
24

8. 6. 4. 3. Literal Patterns

Một mẫu chữ tương ứng với hầu hết trong Python. cú pháp

compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
7

The rule

compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
25 and the token
compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
26 are defined in the standard Python grammar . Triple-quoted strings are supported. Chuỗi thô và chuỗi byte được hỗ trợ. không được hỗ trợ.

Các mẫu

compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
27 và
compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
28 là để bày tỏ ; . E. g.
compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
29

In simple terms,

compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
30 will succeed only if
compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
31. For the singletons
if x < y < z: print(x); print(y); print(z)
81,
compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
33 and
compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
34, the operator is used

8. 6. 4. 4. Chụp mẫu

Một mẫu chụp liên kết giá trị chủ đề với một tên. Syntax

compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
8

Một dấu gạch dưới duy nhất

compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
21 không phải là một mẫu chụp (đây là những gì mà
compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
37 thể hiện). Thay vào đó, nó được coi là một

In a given pattern, a given name can only be bound once. e. g.

compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
39 is invalid while
compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
40 is allowed

Chụp mẫu luôn thành công. The binding follows scoping rules established by the assignment expression operator in PEP 572; the name becomes a local variable in the closest containing function scope unless there’s an applicable or statement

Nói một cách đơn giản,

compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
43 sẽ luôn thành công và nó sẽ thiết lập
compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
24

8. 6. 4. 5. Mẫu ký tự đại diện

Mẫu ký tự đại diện luôn thành công (khớp với mọi thứ) và không liên kết tên. cú pháp

compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
9

compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
21 nằm trong bất kỳ mẫu nào, nhưng chỉ trong các mẫu. Nó là một định danh, như thường lệ, ngay cả trong
if x < y < z: print(x); print(y); print(z)
87 biểu thức chủ đề, các khối
if x < y < z: print(x); print(y); print(z)
98 và khối
if x < y < z: print(x); print(y); print(z)
88

Nói một cách đơn giản,

compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
21 sẽ luôn thành công

8. 6. 4. 6. Mẫu giá trị

A value pattern represents a named value in Python. cú pháp

if_stmt ::=  "if" assignment_expression ":" suite
             ("elif" assignment_expression ":" suite)*
             ["else" ":" suite]
0

Tên chấm trong mẫu được tra cứu bằng Python tiêu chuẩn. Mẫu thành công nếu giá trị được tìm thấy so sánh bằng với giá trị chủ thể (sử dụng toán tử đẳng thức

compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
50)

Nói một cách đơn giản,

compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
51 sẽ chỉ thành công nếu
compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
52

Ghi chú

Nếu cùng một giá trị xuất hiện nhiều lần trong cùng một câu lệnh so khớp, trình thông dịch có thể lưu vào bộ đệm giá trị đầu tiên được tìm thấy và sử dụng lại thay vì lặp lại cùng một lần tra cứu. Bộ đệm này được liên kết chặt chẽ với một lần thực thi nhất định của một câu lệnh đối sánh nhất định

8. 6. 4. 7. Mẫu nhóm

Mẫu nhóm cho phép người dùng thêm dấu ngoặc đơn xung quanh mẫu để nhấn mạnh mục đích nhóm. Mặt khác, nó không có cú pháp bổ sung. cú pháp

if_stmt ::=  "if" assignment_expression ":" suite
             ("elif" assignment_expression ":" suite)*
             ["else" ":" suite]
1

Nói một cách đơn giản,

compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
53 có tác dụng tương tự như
compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
23

8. 6. 4. 8. Mẫu trình tự

Một mẫu trình tự chứa một số mẫu con được so khớp với các phần tử trình tự. Cú pháp tương tự như giải nén danh sách hoặc bộ dữ liệu

if_stmt ::=  "if" assignment_expression ":" suite
             ("elif" assignment_expression ":" suite)*
             ["else" ":" suite]
2

Không có sự khác biệt nếu dấu ngoặc đơn hoặc dấu ngoặc vuông được sử dụng cho các mẫu trình tự (i. e.

compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
55 đấu với
compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
56 )

Ghi chú

Một mẫu duy nhất được đặt trong ngoặc đơn không có dấu phẩy ở cuối (e. g.

compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
57) là một. Trong khi một mẫu duy nhất được đặt trong ngoặc vuông (e. g.
compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
58) vẫn là một mẫu trình tự

Nhiều nhất một mẫu con sao có thể nằm trong một mẫu trình tự. Mẫu con ngôi sao có thể xảy ra ở bất kỳ vị trí nào. Nếu không có mẫu con sao nào, thì mẫu trình tự là mẫu trình tự có độ dài cố định;

Sau đây là luồng logic để khớp một mẫu trình tự với một giá trị chủ đề

  1. Nếu giá trị chủ đề không phải là một chuỗi, mẫu chuỗi không thành công

  2. Nếu giá trị chủ thể là một phiên bản của

    compound_stmt ::=  if_stmt
                       | while_stmt
                       | for_stmt
                       | try_stmt
                       | with_stmt
                       | match_stmt
                       | funcdef
                       | classdef
                       | async_with_stmt
                       | async_for_stmt
                       | async_funcdef
    suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
    statement     ::=  stmt_list NEWLINE | compound_stmt
    stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
    
    59,
    compound_stmt ::=  if_stmt
                       | while_stmt
                       | for_stmt
                       | try_stmt
                       | with_stmt
                       | match_stmt
                       | funcdef
                       | classdef
                       | async_with_stmt
                       | async_for_stmt
                       | async_funcdef
    suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
    statement     ::=  stmt_list NEWLINE | compound_stmt
    stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
    
    60 hoặc
    compound_stmt ::=  if_stmt
                       | while_stmt
                       | for_stmt
                       | try_stmt
                       | with_stmt
                       | match_stmt
                       | funcdef
                       | classdef
                       | async_with_stmt
                       | async_for_stmt
                       | async_funcdef
    suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
    statement     ::=  stmt_list NEWLINE | compound_stmt
    stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
    
    61 thì mẫu trình tự không thành công

  3. Các bước tiếp theo phụ thuộc vào việc mẫu trình tự là cố định hay có độ dài thay đổi

    Nếu mẫu trình tự có độ dài cố định

    1. Nếu độ dài của chuỗi chủ đề không bằng số lượng mẫu con, thì mẫu chuỗi không thành công

    2. Các mẫu con trong mẫu trình tự được khớp với các mục tương ứng của chúng trong trình tự chủ đề từ trái sang phải. Matching stops as soon as a subpattern fails. Nếu tất cả các mẫu con thành công trong việc so khớp mục tương ứng của chúng, thì mẫu trình tự thành công

    Mặt khác, nếu mẫu trình tự có độ dài thay đổi

    1. Nếu độ dài của chuỗi chủ đề nhỏ hơn số lượng mẫu con không phải sao, thì mẫu chuỗi không thành công

    2. Các mẫu con không phải ngôi sao hàng đầu được khớp với các mục tương ứng của chúng đối với các chuỗi có độ dài cố định

    3. Nếu bước trước đó thành công, mẫu con ngôi sao khớp với danh sách được tạo thành từ các mục chủ đề còn lại, ngoại trừ các mục còn lại tương ứng với mẫu con không phải ngôi sao theo sau mẫu con ngôi sao

    4. Các mẫu con không phải dấu sao còn lại được khớp với các mục chủ đề tương ứng của chúng, như đối với chuỗi có độ dài cố định

    Ghi chú

    Độ dài của chuỗi chủ đề có được thông qua (i. e. via the

    compound_stmt ::=  if_stmt
                       | while_stmt
                       | for_stmt
                       | try_stmt
                       | with_stmt
                       | match_stmt
                       | funcdef
                       | classdef
                       | async_with_stmt
                       | async_for_stmt
                       | async_funcdef
    suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
    statement     ::=  stmt_list NEWLINE | compound_stmt
    stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
    
    63 protocol). Độ dài này có thể được trình thông dịch lưu vào bộ đệm theo cách tương tự như

In simple terms

compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
64 …
compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
65 matches only if all the following happens

8. 6. 4. 9. Mapping Patterns

Mẫu ánh xạ chứa một hoặc nhiều mẫu khóa-giá trị. Cú pháp tương tự như việc xây dựng một từ điển. Syntax

if_stmt ::=  "if" assignment_expression ":" suite
             ("elif" assignment_expression ":" suite)*
             ["else" ":" suite]
3

Nhiều nhất một mẫu sao đôi có thể ở dạng ánh xạ. The double star pattern must be the last subpattern in the mapping pattern

Duplicate keys in mapping patterns are disallowed. Duplicate literal keys will raise a . Hai khóa khác có cùng giá trị sẽ tăng a khi chạy

Sau đây là luồng logic để so khớp mẫu ánh xạ với giá trị chủ đề

  1. If the subject value is not a mapping ,the mapping pattern fails

  2. Nếu mọi khóa được cung cấp trong mẫu ánh xạ đều có trong ánh xạ chủ thể và mẫu cho từng khóa khớp với mục tương ứng của ánh xạ chủ thể, thì mẫu ánh xạ thành công

  3. If duplicate keys are detected in the mapping pattern, the pattern is considered invalid. A is raised for duplicate literal values; or a for named keys of the same value

Ghi chú

Key-value pairs are matched using the two-argument form of the mapping subject’s

compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
76 method. Các cặp khóa-giá trị phù hợp phải có sẵn trong ánh xạ và không được tạo nhanh chóng qua
compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
77 hoặc
compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
78

In simple terms

compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
79 matches only if all the following happens

8. 6. 4. 10. Mẫu lớp

Một mẫu lớp đại diện cho một lớp và các đối số từ khóa và vị trí của nó (nếu có). cú pháp

if_stmt ::=  "if" assignment_expression ":" suite
             ("elif" assignment_expression ":" suite)*
             ["else" ":" suite]
4

The same keyword should not be repeated in class patterns

The following is the logical flow for matching a class pattern against a subject value

  1. If

    compound_stmt ::=  if_stmt
                       | while_stmt
                       | for_stmt
                       | try_stmt
                       | with_stmt
                       | match_stmt
                       | funcdef
                       | classdef
                       | async_with_stmt
                       | async_for_stmt
                       | async_funcdef
    suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
    statement     ::=  stmt_list NEWLINE | compound_stmt
    stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
    
    84 is not an instance of the builtin , raise

  2. If the subject value is not an instance of

    compound_stmt ::=  if_stmt
                       | while_stmt
                       | for_stmt
                       | try_stmt
                       | with_stmt
                       | match_stmt
                       | funcdef
                       | classdef
                       | async_with_stmt
                       | async_for_stmt
                       | async_funcdef
    suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
    statement     ::=  stmt_list NEWLINE | compound_stmt
    stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
    
    84 (tested via ), the class pattern fails

  3. If no pattern arguments are present, the pattern succeeds. Mặt khác, các bước tiếp theo phụ thuộc vào việc có các mẫu đối số từ khóa hoặc vị trí hay không

    Đối với một số loại tích hợp sẵn (được chỉ định bên dưới), một mẫu con vị trí duy nhất được chấp nhận sẽ khớp với toàn bộ chủ đề;

    Nếu chỉ có các mẫu từ khóa, chúng sẽ được xử lý như sau, từng cái một

    I. Từ khóa được tra cứu như một thuộc tính về chủ đề

    • Nếu điều này làm phát sinh một ngoại lệ khác với , thì ngoại lệ đó sẽ nổi lên

    • Nếu điều này tăng lên, mẫu lớp đã thất bại

    • Else, the subpattern associated with the keyword pattern is matched against the subject’s attribute value. If this fails, the class pattern fails; if this succeeds, the match proceeds to the next keyword

    II. If all keyword patterns succeed, the class pattern succeeds

    Nếu có bất kỳ mẫu vị trí nào, chúng sẽ được chuyển đổi thành mẫu từ khóa bằng cách sử dụng thuộc tính trên lớp

    compound_stmt ::=  if_stmt
                       | while_stmt
                       | for_stmt
                       | try_stmt
                       | with_stmt
                       | match_stmt
                       | funcdef
                       | classdef
                       | async_with_stmt
                       | async_for_stmt
                       | async_funcdef
    suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
    statement     ::=  stmt_list NEWLINE | compound_stmt
    stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
    
    84 trước khi khớp

    I. The equivalent of

    compound_stmt ::=  if_stmt
                       | while_stmt
                       | for_stmt
                       | try_stmt
                       | with_stmt
                       | match_stmt
                       | funcdef
                       | classdef
                       | async_with_stmt
                       | async_for_stmt
                       | async_funcdef
    suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
    statement     ::=  stmt_list NEWLINE | compound_stmt
    stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
    
    93 is called

    Xem thêm

    II. Khi tất cả các mẫu vị trí đã được chuyển đổi thành các mẫu từ khóa,

    trận đấu diễn ra như thể chỉ có các mẫu từ khóa

    Đối với các loại tích hợp sau, việc xử lý các mẫu con vị trí là khác nhau

    These classes accept a single positional argument, and the pattern there is matched against the whole object rather than an attribute. Ví dụ:

    if_stmt ::=  "if" assignment_expression ":" suite
                 ("elif" assignment_expression ":" suite)*
                 ["else" ":" suite]
    
    13 khớp với giá trị
    if_stmt ::=  "if" assignment_expression ":" suite
                 ("elif" assignment_expression ":" suite)*
                 ["else" ":" suite]
    
    14, nhưng không khớp với giá trị
    if_stmt ::=  "if" assignment_expression ":" suite
                 ("elif" assignment_expression ":" suite)*
                 ["else" ":" suite]
    
    15

In simple terms

if_stmt ::=  "if" assignment_expression ":" suite
             ("elif" assignment_expression ":" suite)*
             ["else" ":" suite]
16 matches only if the following happens

Xem thêm

  • PEP 634 – So khớp mẫu cấu trúc. Sự chỉ rõ

  • PEP 636 – So khớp mẫu cấu trúc. hướng dẫn

8. 7. Định nghĩa hàm

Một định nghĩa hàm định nghĩa một đối tượng hàm do người dùng định nghĩa (xem phần )

if_stmt ::=  "if" assignment_expression ":" suite
             ("elif" assignment_expression ":" suite)*
             ["else" ":" suite]
5

Một định nghĩa hàm là một câu lệnh thực thi. Việc thực thi của nó liên kết tên hàm trong không gian tên cục bộ hiện tại với một đối tượng hàm (một trình bao bọc xung quanh mã thực thi cho hàm). This function object contains a reference to the current global namespace as the global namespace to be used when the function is called

Định nghĩa hàm không thực thi thân hàm; .

A function definition may be wrapped by one or more expressions. Các biểu thức trang trí được đánh giá khi hàm được xác định, trong phạm vi chứa định nghĩa hàm. Kết quả phải là một hàm có thể gọi được, được gọi với đối tượng hàm làm đối số duy nhất. The returned value is bound to the function name instead of the function object. Multiple decorators are applied in nested fashion. For example, the following code

if_stmt ::=  "if" assignment_expression ":" suite
             ("elif" assignment_expression ":" suite)*
             ["else" ":" suite]
6

gần tương đương với

if_stmt ::=  "if" assignment_expression ":" suite
             ("elif" assignment_expression ":" suite)*
             ["else" ":" suite]
7

ngoại trừ chức năng ban đầu tạm thời không bị ràng buộc với tên

if_stmt ::=  "if" assignment_expression ":" suite
             ("elif" assignment_expression ":" suite)*
             ["else" ":" suite]
24

Changed in version 3. 9. Các chức năng có thể được trang trí bằng bất kỳ giá trị nào. Trước đây, ngữ pháp hạn chế hơn nhiều; .

When one or more have the form parameter

if_stmt ::=  "if" assignment_expression ":" suite
             ("elif" assignment_expression ":" suite)*
             ["else" ":" suite]
26 expression, the function is said to have “default parameter values. ” Đối với một tham số có giá trị mặc định, giá trị tương ứng có thể bị bỏ qua khi gọi, trong trường hợp đó, giá trị mặc định của tham số được thay thế. If a parameter has a default value, all following parameters up until the “
if_stmt ::=  "if" assignment_expression ":" suite
             ("elif" assignment_expression ":" suite)*
             ["else" ":" suite]
27” must also have a default value — this is a syntactic restriction that is not expressed by the grammar

Các giá trị tham số mặc định được đánh giá từ trái sang phải khi định nghĩa hàm được thực thi. This means that the expression is evaluated once, when the function is defined, and that the same “pre-computed” value is used for each call. Điều này đặc biệt quan trọng để hiểu khi một giá trị tham số mặc định là một đối tượng có thể thay đổi, chẳng hạn như danh sách hoặc từ điển. nếu chức năng sửa đổi đối tượng (e. g. bằng cách thêm một mục vào danh sách), giá trị tham số mặc định có hiệu lực được sửa đổi. This is generally not what was intended. Một cách giải quyết vấn đề này là sử dụng

if x < y < z: print(x); print(y); print(z)
81 làm giá trị mặc định và kiểm tra rõ ràng nó trong phần thân của hàm, e. g

if_stmt ::=  "if" assignment_expression ":" suite
             ("elif" assignment_expression ":" suite)*
             ["else" ":" suite]
8

Function call semantics are described in more detail in section . Một lệnh gọi hàm luôn gán giá trị cho tất cả các tham số được đề cập trong danh sách tham số, từ đối số vị trí, từ đối số từ khóa hoặc từ giá trị mặc định. Nếu có dạng “

if_stmt ::=  "if" assignment_expression ":" suite
             ("elif" assignment_expression ":" suite)*
             ["else" ":" suite]
29”, nó được khởi tạo thành một bộ nhận bất kỳ tham số vị trí thừa nào, mặc định là bộ trống. If the form “
if_stmt ::=  "if" assignment_expression ":" suite
             ("elif" assignment_expression ":" suite)*
             ["else" ":" suite]
30” is present, it is initialized to a new ordered mapping receiving any excess keyword arguments, defaulting to a new empty mapping of the same type. Các tham số sau “
if_stmt ::=  "if" assignment_expression ":" suite
             ("elif" assignment_expression ":" suite)*
             ["else" ":" suite]
27” hoặc “
if_stmt ::=  "if" assignment_expression ":" suite
             ("elif" assignment_expression ":" suite)*
             ["else" ":" suite]
29” là các tham số chỉ dành cho từ khóa và chỉ có thể được chuyển bởi các đối số từ khóa. Các tham số trước “
if_stmt ::=  "if" assignment_expression ":" suite
             ("elif" assignment_expression ":" suite)*
             ["else" ":" suite]
33” là các tham số chỉ vị trí và chỉ có thể được truyền bởi các đối số vị trí

Đã thay đổi trong phiên bản 3. 8. The

if_stmt ::=  "if" assignment_expression ":" suite
             ("elif" assignment_expression ":" suite)*
             ["else" ":" suite]
33 function parameter syntax may be used to indicate positional-only parameters. See PEP 570 for details.

Các tham số có thể có dạng “

if_stmt ::=  "if" assignment_expression ":" suite
             ("elif" assignment_expression ":" suite)*
             ["else" ":" suite]
35” sau tên tham số. Bất kỳ tham số nào cũng có thể có chú thích, kể cả những tham số có dạng
if_stmt ::=  "if" assignment_expression ":" suite
             ("elif" assignment_expression ":" suite)*
             ["else" ":" suite]
29 hoặc
if_stmt ::=  "if" assignment_expression ":" suite
             ("elif" assignment_expression ":" suite)*
             ["else" ":" suite]
30. Các chức năng có thể có chú thích “trả về” ở dạng “
if_stmt ::=  "if" assignment_expression ":" suite
             ("elif" assignment_expression ":" suite)*
             ["else" ":" suite]
38” sau danh sách tham số. Các chú thích này có thể là bất kỳ biểu thức Python hợp lệ nào. Sự hiện diện của các chú thích không làm thay đổi ngữ nghĩa của một chức năng. Các giá trị chú thích có sẵn dưới dạng giá trị của từ điển được khóa bởi tên của tham số trong thuộc tính
if_stmt ::=  "if" assignment_expression ":" suite
             ("elif" assignment_expression ":" suite)*
             ["else" ":" suite]
39 của đối tượng hàm. If the
if_stmt ::=  "if" assignment_expression ":" suite
             ("elif" assignment_expression ":" suite)*
             ["else" ":" suite]
40 import from is used, annotations are preserved as strings at runtime which enables postponed evaluation. Mặt khác, chúng được đánh giá khi định nghĩa hàm được thực thi. In this case annotations may be evaluated in a different order than they appear in the source code

It is also possible to create anonymous functions (functions not bound to a name), for immediate use in expressions. This uses lambda expressions, described in section . Note that the lambda expression is merely a shorthand for a simplified function definition; a function defined in a “” statement can be passed around or assigned to another name just like a function defined by a lambda expression. The “

if_stmt ::=  "if" assignment_expression ":" suite
             ("elif" assignment_expression ":" suite)*
             ["else" ":" suite]
42” form is actually more powerful since it allows the execution of multiple statements and annotations

Programmer’s note. Functions are first-class objects. A “

if_stmt ::=  "if" assignment_expression ":" suite
             ("elif" assignment_expression ":" suite)*
             ["else" ":" suite]
42” statement executed inside a function definition defines a local function that can be returned or passed around. Các biến tự do được sử dụng trong hàm lồng nhau có thể truy cập các biến cục bộ của hàm chứa def. Xem phần để biết chi tiết

Xem thêm

PEP 3107 - Function Annotations

Thông số kỹ thuật ban đầu cho các chú thích chức năng

PEP 484 - Type Hints

Definition of a standard meaning for annotations. type hints

PEP 526 - Cú pháp cho chú thích biến

Ability to type hint variable declarations, including class variables and instance variables

PEP 563 - Postponed Evaluation of Annotations

Support for forward references within annotations by preserving annotations in a string form at runtime instead of eager evaluation

8. 8. Class definitions

Một định nghĩa lớp định nghĩa một đối tượng lớp (xem phần )

if_stmt ::=  "if" assignment_expression ":" suite
             ("elif" assignment_expression ":" suite)*
             ["else" ":" suite]
9

A class definition is an executable statement. The inheritance list usually gives a list of base classes (see for more advanced uses), so each item in the list should evaluate to a class object which allows subclassing. Theo mặc định, các lớp không có danh sách kế thừa sẽ kế thừa từ lớp cơ sở;

while_stmt ::=  "while" assignment_expression ":" suite
                ["else" ":" suite]
0

tương đương với

while_stmt ::=  "while" assignment_expression ":" suite
                ["else" ":" suite]
1

The class’s suite is then executed in a new execution frame (see ), using a newly created local namespace and the original global namespace. (Thông thường, bộ này chứa hầu hết các định nghĩa hàm. ) When the class’s suite finishes execution, its execution frame is discarded but its local namespace is saved. Sau đó, một đối tượng lớp được tạo bằng cách sử dụng danh sách thừa kế cho các lớp cơ sở và không gian tên cục bộ đã lưu cho từ điển thuộc tính. The class name is bound to this class object in the original local namespace

Thứ tự mà các thuộc tính được định nghĩa trong phần thân của lớp được giữ nguyên trong lớp mới

if_stmt ::=  "if" assignment_expression ":" suite
             ("elif" assignment_expression ":" suite)*
             ["else" ":" suite]
46. Lưu ý rằng điều này chỉ đáng tin cậy ngay sau khi lớp được tạo và chỉ dành cho các lớp được xác định bằng cú pháp định nghĩa

Tạo lớp có thể được tùy chỉnh rất nhiều bằng cách sử dụng

Classes can also be decorated. just like when decorating functions,

while_stmt ::=  "while" assignment_expression ":" suite
                ["else" ":" suite]
2

gần tương đương với

while_stmt ::=  "while" assignment_expression ":" suite
                ["else" ":" suite]
3

The evaluation rules for the decorator expressions are the same as for function decorators. The result is then bound to the class name

Changed in version 3. 9. Classes may be decorated with any valid . Previously, the grammar was much more restrictive; see PEP 614 for details.

Programmer’s note. Variables defined in the class definition are class attributes; they are shared by instances. Instance attributes can be set in a method with

if_stmt ::=  "if" assignment_expression ":" suite
             ("elif" assignment_expression ":" suite)*
             ["else" ":" suite]
48. Both class and instance attributes are accessible through the notation “
if_stmt ::=  "if" assignment_expression ":" suite
             ("elif" assignment_expression ":" suite)*
             ["else" ":" suite]
49”, and an instance attribute hides a class attribute with the same name when accessed in this way. Class attributes can be used as defaults for instance attributes, but using mutable values there can lead to unexpected results. can be used to create instance variables with different implementation details

Xem thêm

PEP 3115 - Metaclasses in Python 3000

The proposal that changed the declaration of metaclasses to the current syntax, and the semantics for how classes with metaclasses are constructed

PEP 3129 - Class Decorators

The proposal that added class decorators. Function and method decorators were introduced in PEP 318

8. 9. Coroutines

New in version 3. 5

8. 9. 1. Coroutine function definition

while_stmt ::=  "while" assignment_expression ":" suite
                ["else" ":" suite]
4

Execution of Python coroutines can be suspended and resumed at many points (see ). expressions, and can only be used in the body of a coroutine function

Functions defined with

if_stmt ::=  "if" assignment_expression ":" suite
             ("elif" assignment_expression ":" suite)*
             ["else" ":" suite]
53 syntax are always coroutine functions, even if they do not contain
if_stmt ::=  "if" assignment_expression ":" suite
             ("elif" assignment_expression ":" suite)*
             ["else" ":" suite]
50 or
if_stmt ::=  "if" assignment_expression ":" suite
             ("elif" assignment_expression ":" suite)*
             ["else" ":" suite]
55 keywords

It is a to use a

if_stmt ::=  "if" assignment_expression ":" suite
             ("elif" assignment_expression ":" suite)*
             ["else" ":" suite]
57 expression inside the body of a coroutine function

An example of a coroutine function

while_stmt ::=  "while" assignment_expression ":" suite
                ["else" ":" suite]
5

Changed in version 3. 7.

if_stmt ::=  "if" assignment_expression ":" suite
             ("elif" assignment_expression ":" suite)*
             ["else" ":" suite]
50 and
if_stmt ::=  "if" assignment_expression ":" suite
             ("elif" assignment_expression ":" suite)*
             ["else" ":" suite]
55 are now keywords; previously they were only treated as such inside the body of a coroutine function.

8. 9. 2. The if_stmt ::= "if" assignment_expression ":" suite ("elif" assignment_expression ":" suite)* ["else" ":" suite] 51 statement

while_stmt ::=  "while" assignment_expression ":" suite
                ["else" ":" suite]
6

An provides an

if_stmt ::=  "if" assignment_expression ":" suite
             ("elif" assignment_expression ":" suite)*
             ["else" ":" suite]
61 method that directly returns an , which can call asynchronous code in its
if_stmt ::=  "if" assignment_expression ":" suite
             ("elif" assignment_expression ":" suite)*
             ["else" ":" suite]
62 method

The

if_stmt ::=  "if" assignment_expression ":" suite
             ("elif" assignment_expression ":" suite)*
             ["else" ":" suite]
51 statement allows convenient iteration over asynchronous iterables

Đoạn mã sau

while_stmt ::=  "while" assignment_expression ":" suite
                ["else" ":" suite]
7

Is semantically equivalent to

while_stmt ::=  "while" assignment_expression ":" suite
                ["else" ":" suite]
8

See also and for details

It is a to use an

if_stmt ::=  "if" assignment_expression ":" suite
             ("elif" assignment_expression ":" suite)*
             ["else" ":" suite]
51 statement outside the body of a coroutine function

8. 9. 3. The if_stmt ::= "if" assignment_expression ":" suite ("elif" assignment_expression ":" suite)* ["else" ":" suite] 52 statement

while_stmt ::=  "while" assignment_expression ":" suite
                ["else" ":" suite]
9

An is a that is able to suspend execution in its enter and exit methods

Đoạn mã sau

for_stmt ::=  "for" target_list "in" starred_list ":" suite
              ["else" ":" suite]
0

về mặt ngữ nghĩa tương đương với

for_stmt ::=  "for" target_list "in" starred_list ":" suite
              ["else" ":" suite]
1

See also and for details

Đó là sử dụng một câu lệnh

if_stmt ::=  "if" assignment_expression ":" suite
             ("elif" assignment_expression ":" suite)*
             ["else" ":" suite]
52 bên ngoài phần thân của một hàm coroutine

Xem thêm

PEP 492 - Coroutines with async and await syntax

The proposal that made coroutines a proper standalone concept in Python, and added supporting syntax

Footnotes

Ngoại lệ được truyền tới ngăn xếp lời gọi trừ khi có một mệnh đề xảy ra để đưa ra một ngoại lệ khác. That new exception causes the old one to be lost

In pattern matching, a sequence is defined as one of the following

  • một lớp kế thừa từ

  • a Python class that has been registered as

  • một lớp dựng sẵn có bit (CPython) được đặt

  • một lớp kế thừa từ bất kỳ lớp nào ở trên

Các lớp thư viện tiêu chuẩn sau đây là các chuỗi

Ghi chú

Các giá trị chủ đề của loại

compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
59,
compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
60 và
compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
61 không khớp với các mẫu trình tự

Trong khớp mẫu, một ánh xạ được định nghĩa là một trong những điều sau đây

  • một lớp kế thừa từ

  • a Python class that has been registered as

  • một lớp dựng sẵn có bit (CPython) được đặt

  • một lớp kế thừa từ bất kỳ lớp nào ở trên

The standard library classes and are mappings

Một chuỗi ký tự xuất hiện dưới dạng câu lệnh đầu tiên trong phần thân của lớp được chuyển đổi thành mục

if_stmt ::=  "if" assignment_expression ":" suite
             ("elif" assignment_expression ":" suite)*
             ["else" ":" suite]
91 của không gian tên và do đó là mục của lớp.

Điều nào sau đây đúng với hàm Python?

Việc sử dụng hàm nào sau đây trong python? . Functions are reusable pieces of programs . Chúng cho phép bạn đặt tên cho một khối câu lệnh, cho phép bạn chạy khối đó bằng tên đã chỉ định ở bất kỳ đâu trong chương trình của bạn và bất kỳ số lần nào. 2.

Phát biểu nào sau đây không đúng về hàm Python?

Trả lời. Python 4 là phiên bản mới nhất của python . tuyên bố này không đúng với python.

4 loại hàm trong Python là gì?

Sau đây là các loại Hàm Python khác nhau. .
Hàm tích hợp Python
Hàm đệ quy Python
Hàm Lambda trong Python
Các hàm do người dùng định nghĩa trong Python

Phát biểu nào sau đây là đúng khi hàm Python chỉ có thể trả về một giá trị duy nhất?

Hàm Python luôn trả về một giá trị .