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 Show
Question 2. What is not true about formal and actual parameters?
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
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ó
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?
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 ::= 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 range0 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 range1. 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] 2Câu lệnh được sử dụng để thực hiện có điều kiện if_stmt ::= "if" 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] 3Câ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"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] 4Câ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" Biểu thức try_stmt ::=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"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"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"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: foo3 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 statementThe for_stmt ::= "for"5 statement specifies exception handlers and/or cleanup code for a group of statements try_stmt ::= 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 clauseThe except E as N: foo7 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"5 suite, a search for an exception handler is started. This search inspects the except E as N: foo7 clauses in turn until one is found that matches the exception. An expression-less except E as N: foo7 clause, if present, must be last; it matches any exception. For an except E as N: foo7 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: foo7 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: foo7 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: foo7 clause is found, the exception is assigned to the target specified after the except E as N: try: foo finally: del N8 keyword in that except E as N: foo7 clause, if present, and the except E as N: foo7 clause’s suite is executed. All except E as N: foo7 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"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: foo7 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: foo7 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: foo7 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 clauseThe 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"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"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"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 statementThe 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
Đ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 ::=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) 87Mớ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 ::=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à
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
8. 6. 1. Tổng quanDưới đây là tổng quan về luồng logic của câu lệnh so khớp
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 ::=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 ::=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
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
8. 6. 4. PatternsGhi chú This section uses grammar notations beyond standard EBNF
Cú pháp cấp cao nhất cho compound_stmt ::=14 là compound_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ẶCMẫu OR là hai hoặc nhiều mẫu được phân tách bằng các thanh dọc compound_stmt ::=15. cú pháp compound_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 ::=16 sẽ cố gắng khớp với compound_stmt ::=17, nếu thất bại, nó sẽ cố gắng khớp với compound_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 ASMẫ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 ::=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 ::=20 cannot be a a compound_stmt ::=21 In simple terms compound_stmt ::=22 will match with compound_stmt ::=23, and on success it will set compound_stmt ::=24 8. 6. 4. 3. Literal PatternsMột mẫu chữ tương ứng với hầu hết trong Python. cú pháp compound_stmt ::=7 The rule compound_stmt ::=25 and the token compound_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 ::=27 và compound_stmt ::=28 là để bày tỏ ; . E. g. compound_stmt ::=29 In simple terms, compound_stmt ::=30 will succeed only if compound_stmt ::=31. For the singletons if x < y < z: print(x); print(y); print(z)81, compound_stmt ::=33 and compound_stmt ::=34, the operator is used 8. 6. 4. 4. Chụp mẫuMột mẫu chụp liên kết giá trị chủ đề với một tên. Syntax compound_stmt ::=8 Một dấu gạch dưới duy nhất compound_stmt ::=21 không phải là một mẫu chụp (đây là những gì mà compound_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 ::=39 is invalid while compound_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 ::=43 sẽ luôn thành công và nó sẽ thiết lập compound_stmt ::=24 8. 6. 4. 5. Mẫu ký tự đại diệnMẫ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 ::=9 compound_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 ::=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"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 ::=50) Nói một cách đơn giản, compound_stmt ::=51 sẽ chỉ thành công nếu compound_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ómMẫ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"1 Nói một cách đơn giản, compound_stmt ::=53 có tác dụng tương tự như compound_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"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 ::=55 đấu với compound_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 ::=57) là một. Trong khi một mẫu duy nhất được đặt trong ngoặc vuông (e. g. compound_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ủ đề
In simple terms compound_stmt ::=64 … compound_stmt ::=65 matches only if all the following happens
8. 6. 4. 9. Mapping PatternsMẫ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"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ủ đề
Ghi chú Key-value pairs are matched using the two-argument form of the mapping subject’s compound_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 ::=77 hoặc compound_stmt ::=78 In simple terms compound_stmt ::=79 matches only if all the following happens
8. 6. 4. 10. Mẫu lớpMộ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"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
In simple terms if_stmt ::= "if"16 matches only if the following happens
Xem thêm
8. 7. Định nghĩa hàmMộ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"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"6 gần tương đương với if_stmt ::= "if"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"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"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"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"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"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"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"27” hoặc “ if_stmt ::= "if"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"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"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"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"29 hoặc if_stmt ::= "if"30. Các chức năng có thể có chú thích “trả về” ở dạng “ if_stmt ::= "if"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"39 của đối tượng hàm. If the if_stmt ::= "if"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"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"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 AnnotationsThông số kỹ thuật ban đầu cho các chú thích chức năng PEP 484 - Type HintsDefinition of a standard meaning for annotations. type hints PEP 526 - Cú pháp cho chú thích biếnAbility to type hint variable declarations, including class variables and instance variables PEP 563 - Postponed Evaluation of AnnotationsSupport for forward references within annotations by preserving annotations in a string form at runtime instead of eager evaluation 8. 8. Class definitionsMột định nghĩa lớp định nghĩa một đối tượng lớp (xem phần ) if_stmt ::= "if"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"0 tương đương với while_stmt ::= "while"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"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"2 gần tương đương với while_stmt ::= "while"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"48. Both class and instance attributes are accessible through the notation “ if_stmt ::= "if"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 3000The proposal that changed the declaration of metaclasses to the current syntax, and the semantics for how classes with metaclasses are constructed PEP 3129 - Class DecoratorsThe proposal that added class decorators. Function and method decorators were introduced in PEP 318 8. 9. CoroutinesNew in version 3. 5 8. 9. 1. Coroutine function definitionwhile_stmt ::= "while"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"53 syntax are always coroutine functions, even if they do not contain if_stmt ::= "if"50 or if_stmt ::= "if"55 keywords It is a to use a if_stmt ::= "if"57 expression inside the body of a coroutine function An example of a coroutine function while_stmt ::= "while"5 Changed in version 3. 7. if_stmt ::= "if"50 and if_stmt ::= "if"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 statementwhile_stmt ::= "while"6 An provides an if_stmt ::= "if"61 method that directly returns an , which can call asynchronous code in its if_stmt ::= "if"62 method The if_stmt ::= "if"51 statement allows convenient iteration over asynchronous iterables Đoạn mã sau while_stmt ::= "while"7 Is semantically equivalent to while_stmt ::= "while"8 See also and for details It is a to use an if_stmt ::= "if"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 statementwhile_stmt ::= "while"9 An is a that is able to suspend execution in its enter and exit methods Đoạn mã sau for_stmt ::= "for"0 về mặt ngữ nghĩa tương đương với for_stmt ::= "for"1 See also and for details Đó là sử dụng một câu lệnh if_stmt ::= "if"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 syntaxThe 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
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 ::=59, compound_stmt ::=60 và compound_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
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"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ị . |