Python là ngôn ngữ động chính được sử dụng tại Google. Hướng dẫn về phong cách này là danh sách những điều nên làm và không nên làm đối với các chương trình Python
Để giúp bạn định dạng mã chính xác, chúng tôi đã tạo tệp cài đặt cho Vim. Đối với Emacs, cài đặt mặc định sẽ ổn
Nhiều nhóm sử dụng trình định dạng tự động yapf để tránh tranh cãi về định dạng
2 Quy tắc ngôn ngữ Python
2. 1 xơ vải
Chạy
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
18 qua mã của bạn bằng cách sử dụng pylintrc này2. 1. 1 Definition
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
18 is a tool for finding bugs and style problems in Python source code. It finds problems that are typically caught by a compiler for less dynamic languages like C and C++. Because of the dynamic nature of Python, some warnings may be incorrect; however, spurious warnings should be fairly infrequent2. 1. 2 Pros
Catches easy-to-miss errors like typos, using-vars-before-assignment, etc
2. 1. 3 Cons
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
18 isn’t perfect. To take advantage of it, sometimes we’ll need to write around it, suppress its warnings or fix it2. 1. 4 Decision
Make sure you run
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
18 on your codeSuppress warnings if they are inappropriate so that other issues are not hidden. To suppress warnings, you can set a line-level comment
dict = 'something awful' # Bad Idea.. pylint: disable=redefined-builtin
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
18 warnings are each identified by symbolic name [def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
23] Google-specific warnings start with def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
24If the reason for the suppression is not clear from the symbolic name, add an explanation
Suppressing in this way has the advantage that we can easily search for suppressions and revisit them
You can get a list of
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
18 warnings by doingTo get more information on a particular message, use
Prefer
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
26 to the deprecated older form def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
27Unused argument warnings can be suppressed by deleting the variables at the beginning of the function. Always include a comment explaining why you are deleting it. “Unused. ” is sufficient. For example
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
Other common forms of suppressing this warning include using ‘
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
28’ as the identifier for the unused argument or prefixing the argument name with ‘def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
29’, or assigning them to ‘def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
28’. These forms are allowed but no longer encouraged. These break callers that pass arguments by name and do not enforce that the arguments are actually unused2. 2 Imports
Use
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
31 statements for packages and modules only, not for individual classes or functions2. 2. 1 Definition
Reusability mechanism for sharing code from one module to another
2. 2. 2 Pros
The namespace management convention is simple. The source of each identifier is indicated in a consistent way;
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
32 says that object def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
33 is defined in module def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
342. 2. 3 Cons
Module names can still collide. Some module names are inconveniently long
2. 2. 4 Decision
- Use
35 for importing packages and modulesdef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
- Use
36 wheredef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
34 is the package prefix anddef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
38 is the module name with no prefixdef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
- Use
39 if two modules nameddef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
38 are to be imported, ifdef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
38 conflicts with a top-level name defined in the current module, or ifdef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
38 is an inconveniently long namedef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
- Use
43 only whendef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
44 is a standard abbreviation [e. g. ,def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
45 fordef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
46]def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
Ví dụ: mô-đun
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
47 có thể được nhập như saudef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
2Do not use relative names in imports. Even if the module is in the same package, use the full package name. This helps prevent unintentionally importing a package twice
2. 2. 4. 1 ExemptionsExemptions from this rule
- Symbols from the following modules are used to support static analysis and type checking
- Redirects from the six. moves module
2. 3 Packages
Import each module using the full pathname location of the module
2. 3. 1 Pros
Avoids conflicts in module names or incorrect imports due to the module search path not being what the author expected. Makes it easier to find modules
2. 3. 2 Cons
Makes it harder to deploy code because you have to replicate the package hierarchy. Not really a problem with modern deployment mechanisms
2. 3. 3 Decision
All new code should import each module by its full package name
Imports should be as follows
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
3def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
4[assume this file lives in
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
48 where def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
49 also exists]def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
7The directory the main binary is located in should not be assumed to be in
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
50 despite that happening in some environments. This being the case, code should assume that def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
51 refers to a third party or top level package named def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
52, not a local def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
492. 4 Exceptions
Exceptions are allowed but must be used carefully
2. 4. 1 Definition
Exceptions are a means of breaking out of normal control flow to handle errors or other exceptional conditions
2. 4. 2 Pros
Luồng điều khiển của mã hoạt động bình thường không bị lộn xộn bởi mã xử lý lỗi. Nó cũng cho phép luồng điều khiển bỏ qua nhiều khung khi một điều kiện nhất định xảy ra, e. g. , quay lại từ N hàm lồng nhau trong một bước thay vì phải tìm mã lỗi thông qua
2. 4. 3 nhược điểm
Có thể khiến luồng điều khiển bị nhầm lẫn. Dễ bỏ sót các trường hợp lỗi khi gọi thư viện
2. 4. 4 Quyết định
Ngoại lệ phải tuân theo các điều kiện nhất định
Sử dụng các lớp ngoại lệ tích hợp khi nó hợp lý. Ví dụ: tăng
54 để chỉ ra lỗi lập trình như điều kiện tiên quyết bị vi phạm [chẳng hạn như nếu bạn được thông qua số âm nhưng yêu cầu số dương]. Không sử dụng câu lệnhdef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
55 để xác thực giá trị đối số của API công khai.def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
55 được sử dụng để đảm bảo tính chính xác bên trong, không phải để thực thi cách sử dụng đúng cũng như để chỉ ra rằng một số sự kiện không mong muốn đã xảy ra. Nếu một ngoại lệ được mong muốn trong các trường hợp sau, hãy sử dụng câu lệnh nâng cao. Ví dụdef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
5dict = 'something awful' # Bad Idea.. pylint: disable=redefined-builtin
6dict = 'something awful' # Bad Idea.. pylint: disable=redefined-builtin
Thư viện hoặc gói có thể xác định ngoại lệ của riêng họ. Khi làm như vậy, họ phải kế thừa từ một lớp ngoại lệ hiện có. Tên ngoại lệ phải kết thúc bằng
57 và không được lặp lại [def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
58]def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
Không bao giờ sử dụng câu lệnh bắt tất cả
59 hoặc bắtdef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
60 hoặcdef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
61, trừ khi bạndef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
- tăng lại ngoại lệ, hoặc
- tạo một điểm cô lập trong chương trình nơi các ngoại lệ không được lan truyền mà thay vào đó được ghi lại và loại bỏ, chẳng hạn như bảo vệ một luồng khỏi sự cố bằng cách bảo vệ khối ngoài cùng của nó
Python rất khoan dung về vấn đề này và
59 sẽ thực sự nắm bắt mọi thứ kể cả tên sai chính tả, sys. các cuộc gọi exit[], Ctrl+C ngắt, lỗi nhỏ nhất và tất cả các loại ngoại lệ khác mà bạn đơn giản là không muốn nắm bắtdef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
Giảm thiểu số lượng mã trong khối
63/_______064. Phần thân củadef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
63 càng lớn thì càng có nhiều khả năng một ngoại lệ sẽ được đưa ra bởi một dòng mã mà bạn không mong đợi sẽ đưa ra một ngoại lệ. Trong những trường hợp đó, khốidef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
63/def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
64 ẩn một lỗi thực sựdef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
Sử dụng mệnh đề
68 để thực thi mã cho dù có hay không một ngoại lệ được đưa ra trong khốidef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
63. Điều này thường hữu ích cho việc dọn dẹp, tôi. e. , đóng một tập tindef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
2. 5 trạng thái toàn cầu có thể thay đổi
Tránh trạng thái toàn cầu có thể thay đổi
2. 5. 1 Định nghĩa
Các giá trị cấp mô-đun hoặc thuộc tính lớp có thể bị thay đổi trong quá trình thực thi chương trình
2. 5. 2 Ưu điểm
Thỉnh thoảng hữu ích
2. 5. 3 nhược điểm
Phá vỡ đóng gói. Thiết kế như vậy có thể gây khó khăn cho việc đạt được các mục tiêu hợp lệ. Ví dụ: nếu trạng thái chung được sử dụng để quản lý kết nối cơ sở dữ liệu, thì việc kết nối đồng thời với hai cơ sở dữ liệu khác nhau [chẳng hạn như đối với sự khác biệt về tính toán trong quá trình di chuyển] sẽ trở nên khó khăn. Các vấn đề tương tự dễ dàng phát sinh với các cơ quan đăng ký toàn cầu
Có khả năng thay đổi hành vi của mô-đun trong quá trình nhập, vì việc gán cho các biến toàn cục được thực hiện khi mô-đun được nhập lần đầu
2. 5. 4 Quyết định
Tránh trạng thái toàn cầu có thể thay đổi
Trong những trường hợp hiếm hoi khi sử dụng trạng thái toàn cầu được đảm bảo, các thực thể toàn cầu có thể thay đổi phải được khai báo ở cấp độ mô-đun hoặc dưới dạng thuộc tính lớp và được đặt bên trong bằng cách thêm một
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
28 vào tên. Nếu cần, quyền truy cập bên ngoài vào trạng thái toàn cầu có thể thay đổi phải được thực hiện thông qua các hàm công khai hoặc phương thức lớp. Xem cách đặt tên bên dưới. Vui lòng giải thích lý do thiết kế tại sao trạng thái chung có thể thay đổi đang được sử dụng trong nhận xét hoặc tài liệu được liên kết từ nhận xétHằng số cấp mô-đun được cho phép và khuyến khích. Ví dụ.
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
71 cho hằng số sử dụng nội bộ hoặc def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
72 cho hằng số API công khai. Các hằng số phải được đặt tên bằng cách sử dụng tất cả các chữ hoa có dấu gạch dưới. Xem cách đặt tên bên dưới2. 6 Các lớp và hàm lồng nhau/cục bộ/bên trong
Các hàm hoặc lớp cục bộ lồng nhau vẫn ổn khi được sử dụng để đóng trên một biến cục bộ. Các lớp bên trong vẫn ổn
2. 6. 1 Định nghĩa
Một lớp có thể được định nghĩa bên trong một phương thức, hàm hoặc lớp. Một hàm có thể được định nghĩa bên trong một phương thức hoặc hàm. Nested functions have read-only access to variables defined in enclosing scopes
2. 6. 2 Ưu điểm
Cho phép định nghĩa các lớp và chức năng tiện ích chỉ được sử dụng bên trong phạm vi rất hạn chế. Rất ADT-y. Thường được sử dụng để thực hiện trang trí
2. 6. 3 nhược điểm
Các hàm và lớp lồng nhau không thể được kiểm tra trực tiếp. Việc lồng nhau có thể làm cho chức năng bên ngoài dài hơn và khó đọc hơn
2. 6. 4 Quyết định
Họ ổn với một số lưu ý. Tránh các hàm hoặc lớp lồng nhau trừ khi đóng trên một giá trị cục bộ khác với
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
73 hoặc def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
74. Không lồng chức năng chỉ để ẩn nó khỏi người dùng mô-đun. Thay vào đó, hãy đặt tiền tố tên của nó bằng _ ở cấp độ mô-đun để nó vẫn có thể được truy cập bằng các thử nghiệm2. 7 cách hiểu và biểu thức trình tạo
Được rồi để sử dụng cho các trường hợp đơn giản
2. 7. 1 Định nghĩa
Khả năng hiểu List, Dict và Set cũng như các biểu thức trình tạo cung cấp một cách ngắn gọn và hiệu quả để tạo các loại vùng chứa và trình vòng lặp mà không cần sử dụng các vòng lặp truyền thống,
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
75, def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
76 hoặc def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
772. 7. 2 Ưu điểm
Việc hiểu đơn giản có thể rõ ràng và đơn giản hơn các kỹ thuật tạo chính tả, danh sách hoặc tập hợp khác. Biểu thức trình tạo có thể rất hiệu quả, vì chúng tránh hoàn toàn việc tạo danh sách
2. 7. 3 nhược điểm
Có thể khó đọc các biểu thức trình tạo hoặc hiểu phức tạp
2. 7. 4 Quyết định
Được rồi để sử dụng cho các trường hợp đơn giản. Mỗi phần phải vừa trên một dòng. biểu thức ánh xạ, mệnh đề
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
78, biểu thức bộ lọc. Nhiều mệnh đề def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
78 hoặc biểu thức bộ lọc không được phép. Thay vào đó, hãy sử dụng các vòng lặp khi mọi thứ trở nên phức tạp hơndef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
0def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
12. 8 Iterator và Operator mặc định
Sử dụng các trình lặp và toán tử mặc định cho các loại hỗ trợ chúng, như danh sách, từ điển và tệp
2. 8. 1 Định nghĩa
Các loại vùng chứa, như từ điển và danh sách, xác định các trình vòng lặp mặc định và toán tử kiểm tra tư cách thành viên [“in” và “not in”]
2. 8. 2 Ưu điểm
Các trình vòng lặp và toán tử mặc định rất đơn giản và hiệu quả. Chúng thể hiện thao tác trực tiếp mà không cần gọi thêm phương thức. Một hàm sử dụng các toán tử mặc định là chung chung. Nó có thể được sử dụng với bất kỳ loại nào hỗ trợ hoạt động
2. 8. 3 nhược điểm
Bạn không thể biết loại đối tượng bằng cách đọc tên phương thức [trừ khi biến có chú thích loại]. Đây cũng là một lợi thế
2. 8. 4 Quyết định
Sử dụng các trình lặp và toán tử mặc định cho các loại hỗ trợ chúng, như danh sách, từ điển và tệp. Các loại tích hợp cũng xác định các phương thức lặp. Ưu tiên các phương thức này hơn các phương thức trả về danh sách, ngoại trừ việc bạn không nên thay đổi vùng chứa trong khi lặp lại nó
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
0def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
12. 9 máy phát điện
Sử dụng máy phát điện khi cần thiết
2. 9. 1 Định nghĩa
Hàm tạo trả về một trình vòng lặp mang lại một giá trị mỗi khi nó thực thi câu lệnh năng suất. Sau khi nó mang lại một giá trị, trạng thái thời gian chạy của hàm tạo bị tạm dừng cho đến khi cần giá trị tiếp theo
2. 9. 2 Ưu điểm
Mã đơn giản hơn, vì trạng thái của các biến cục bộ và luồng điều khiển được giữ nguyên cho mỗi cuộc gọi. Trình tạo sử dụng ít bộ nhớ hơn so với hàm tạo toàn bộ danh sách giá trị cùng một lúc
2. 9. 3 nhược điểm
Các biến cục bộ trong trình tạo sẽ không được thu gom rác cho đến khi trình tạo bị tiêu thụ đến mức cạn kiệt hoặc chính nó đã được thu gom rác
2. 9. 4 Quyết định
Khỏe. Sử dụng “Năng suất. ” thay vì “Trả về. ” trong chuỗi tài liệu cho các hàm tạo
Nếu trình tạo quản lý một tài nguyên đắt tiền, hãy đảm bảo buộc dọn sạch
Một cách hay để dọn dẹp là bọc trình tạo bằng trình quản lý ngữ cảnh PEP-0533
2. 10 Hàm Lambda
Được rồi cho một lớp lót. Thích các biểu thức trình tạo hơn
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
75 hoặc def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
76 với một def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
772. 10. 1 Định nghĩa
Lambdas định nghĩa các hàm ẩn danh trong một biểu thức, trái ngược với một câu lệnh
2. 10. 2 Ưu điểm
Tiện lợi
2. 10. 3 nhược điểm
Khó đọc và gỡ lỗi hơn các chức năng cục bộ. Việc thiếu tên có nghĩa là dấu vết ngăn xếp khó hiểu hơn. Tính biểu cảm bị hạn chế vì chức năng chỉ có thể chứa một biểu thức
2. 10. 4 Quyết định
Được rồi để sử dụng chúng cho một lớp lót. Nếu mã bên trong hàm lambda dài hơn 60-80 ký tự, thì có lẽ tốt hơn nên xác định nó là một hàm lồng nhau thông thường
Đối với các hoạt động phổ biến như phép nhân, hãy sử dụng các hàm từ mô-đun
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
83 thay vì các hàm lambda. Ví dụ: thích def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
84 hơn là def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
852. 11 Biểu thức điều kiện
Được rồi cho các trường hợp đơn giản
2. 11. 1 Định nghĩa
Biểu thức điều kiện [đôi khi được gọi là “toán tử bậc ba”] là cơ chế cung cấp cú pháp ngắn hơn cho câu lệnh if. Ví dụ.
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
862. 11. 2 Ưu điểm
Ngắn gọn và thuận tiện hơn câu lệnh if
2. 11. 3 nhược điểm
Có thể khó đọc hơn câu lệnh if. Điều kiện có thể khó xác định nếu biểu thức dài
2. 11. 4 Quyết định
Được rồi để sử dụng cho các trường hợp đơn giản. Mỗi phần phải vừa trên một dòng. biểu thức đúng, biểu thức if, biểu thức khác. Sử dụng câu lệnh if hoàn chỉnh khi mọi thứ trở nên phức tạp hơn
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
2def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
32. 12 Default Argument Values
Okay in most cases
2. 12. 1 Definition
You can specify values for variables at the end of a function’s parameter list, e. g. ,
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
87. If def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
88 is called with only one argument, def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
89 is set to 0. If it is called with two arguments, def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
89 has the value of the second argument2. 12. 2 Ưu điểm
Often you have a function that uses lots of default values, but on rare occasions you want to override the defaults. Default argument values provide an easy way to do this, without having to define lots of functions for the rare exceptions. As Python does not support overloaded methods/functions, default arguments are an easy way of “faking” the overloading behavior
2. 12. 3 Cons
Default arguments are evaluated once at module load time. This may cause problems if the argument is a mutable object such as a list or a dictionary. If the function modifies the object [e. g. , by appending an item to a list], the default value is modified
2. 12. 4 Decision
Okay to use with the following caveat
Do not use mutable objects as default values in the function or method definition
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
4def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
52. 13 Properties
Properties may be used to control getting or setting attributes that require trivial computations or logic. Việc triển khai thuộc tính phải phù hợp với kỳ vọng chung của quyền truy cập thuộc tính thông thường. rằng chúng rẻ, đơn giản và không gây ngạc nhiên
2. 13. 1 Định nghĩa
Một cách để gói các cuộc gọi phương thức để nhận và đặt thuộc tính làm quyền truy cập thuộc tính tiêu chuẩn
2. 13. 2 Pros
- Cho phép API gán và truy cập thuộc tính thay vì gọi phương thức getter và setter
- Có thể được sử dụng để tạo thuộc tính chỉ đọc
- Allows calculations to be lazy
- Provides a way to maintain the public interface of a class when the internals evolve independently of class users
2. 13. 3 Cons
- Can hide side-effects much like operator overloading
- Can be confusing for subclasses
2. 13. 4 Decision
Properties are allowed, but, like operator overloading, should only be used when necessary and match the expectations of typical attribute access; follow the getters and setters rules otherwise
For example, using a property to simply both get and set an internal attribute isn’t allowed. there is no computation occurring, so the property is unnecessary [make the attribute public instead]. In comparison, using a property to control attribute access or to calculate a trivially derived value is allowed. the logic is simple and unsurprising
Properties should be created with the
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
91 decorator. Manually implementing a property descriptor is considered a power featureInheritance with properties can be non-obvious. Do not use properties to implement computations a subclass may ever want to override and extend
2. 14 True/False Evaluations
Use the “implicit” false if at all possible
2. 14. 1 Definition
Python evaluates certain values as
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
92 when in a boolean context. A quick “rule of thumb” is that all “empty” values are considered false, so def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
93 all evaluate as false in a boolean context2. 14. 2 Ưu điểm
Conditions using Python booleans are easier to read and less error-prone. In most cases, they’re also faster
2. 14. 3 Cons
May look strange to C/C++ developers
2. 14. 4 Decision
Use the “implicit” false if possible, e. g. ,
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
94 rather than def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
95. There are a few caveats that you should keep in mind thoughAlways use
96 [ordef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
97] to check for adef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
98 value. E. g. , when testing whether a variable or argument that defaults todef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
98 was set to some other value. The other value might be a value that’s false in a boolean contextdef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
Never compare a boolean variable to
92 usingdef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
201. Usedef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
202 instead. If you need to distinguishdef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
92 fromdef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
98 then chain the expressions, such asdef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
205def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
For sequences [strings, lists, tuples], use the fact that empty sequences are false, so
206 anddef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
207 are preferable todef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
208 anddef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
209 respectivelydef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
When handling integers, implicit false may involve more risk than benefit [i. e. , accidentally handling
98 as 0]. You may compare a value which is known to be an integer [and is not the result ofdef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
211] against the integer 0def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
6def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
7def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
Note that
212 [i. e. ,def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
213 as string] evaluates to truedef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
Note that Numpy arrays may raise an exception in an implicit boolean context. Prefer the
214 attribute when testing emptiness of adef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
215 [e. g.def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
216]def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
2. 16 Lexical Scoping
Okay to use
2. 16. 1 Definition
A nested Python function can refer to variables defined in enclosing functions, but cannot assign to them. Variable bindings are resolved using lexical scoping, that is, based on the static program text. Any assignment to a name in a block will cause Python to treat all references to that name as a local variable, even if the use precedes the assignment. If a global declaration occurs, the name is treated as a global variable
An example of the use of this feature is
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
82. 16. 2 Pros
Often results in clearer, more elegant code. Especially comforting to experienced Lisp and Scheme [and Haskell and ML and …] programmers
2. 16. 3 Cons
Can lead to confusing bugs. Such as this example based on PEP-0227
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
9So
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
217 will print def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
218, not def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
2192. 16. 4 Decision
Okay to use
2. 17 Function and Method Decorators
Use decorators judiciously when there is a clear advantage. Avoid
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
220 and limit use of def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
2212. 17. 1 Definition
Decorators for Functions and Methods [a. k. một “ký hiệu
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
222”]. One common decorator is def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
91, used for converting ordinary methods into dynamically computed attributes. However, the decorator syntax allows for user-defined decorators as well. Specifically, for some function def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
224, thisdef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
20is equivalent to
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
212. 17. 2 Pros
Elegantly specifies some transformation on a method; the transformation might eliminate some repetitive code, enforce invariants, etc
2. 17. 3 Cons
Trình trang trí có thể thực hiện các thao tác tùy ý trên đối số của hàm hoặc trả về giá trị, dẫn đến hành vi ngầm đáng ngạc nhiên. Ngoài ra, các trình trang trí thực thi tại thời điểm xác định đối tượng. Đối với các đối tượng cấp mô-đun [lớp, chức năng mô-đun,…] điều này xảy ra tại thời điểm nhập. Lỗi trong mã trang trí hầu như không thể phục hồi từ
2. 17. 4 Quyết định
Sử dụng decorators một cách thận trọng khi có một lợi thế rõ ràng. Người trang trí phải tuân theo các nguyên tắc nhập và đặt tên giống như các chức năng. Decorator pydoc should clearly state that the function is a decorator. Write unit tests for decorators
Avoid external dependencies in the decorator itself [e. g. don’t rely on files, sockets, database connections, etc. ], since they might not be available when the decorator runs [at import time, perhaps from
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
225 or other tools]. A decorator that is called with valid parameters should [as much as possible] be guaranteed to succeed in all casesDecorators are a special case of “top level code” - see main for more discussion
Never use
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
220 unless forced to in order to integrate with an API defined in an existing library. Write a module level function insteadUse
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
221 only when writing a named constructor, or a class-specific routine that modifies necessary global state such as a process-wide cache2. 18 Threading
Do not rely on the atomicity of built-in types
While Python’s built-in data types such as dictionaries appear to have atomic operations, there are corner cases where they aren’t atomic [e. g. if
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
228 or def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
229 are implemented as Python methods] and their atomicity should not be relied upon. Neither should you rely on atomic variable assignment [since this in turn depends on dictionaries]Use the Queue module’s
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
230 data type as the preferred way to communicate data between threads. Otherwise, use the threading module and its locking primitives. Ưu tiên các biến điều kiện và def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
231 thay vì sử dụng các khóa cấp thấp hơn2. 19 Power Features
Avoid these features
2. 19. 1 Definition
Python is an extremely flexible language and gives you many fancy features such as custom metaclasses, access to bytecode, on-the-fly compilation, dynamic inheritance, object reparenting, import hacks, reflection [e. g. một số cách sử dụng của
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
232], sửa đổi nội bộ hệ thống, phương pháp def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
233 thực hiện dọn dẹp tùy chỉnh, v.v.2. 19. 2 Ưu điểm
Đây là những tính năng ngôn ngữ mạnh mẽ. Họ có thể làm cho mã của bạn gọn hơn
2. 19. 3 nhược điểm
Rất hấp dẫn khi sử dụng những tính năng “hay ho” này khi chúng không thực sự cần thiết. Khó đọc, hiểu và gỡ lỗi mã đang sử dụng các tính năng bất thường bên dưới. Thoạt đầu có vẻ không phải như vậy [đối với tác giả gốc], nhưng khi xem lại mã, nó có xu hướng khó hơn mã dài hơn nhưng đơn giản
2. 19. 4 Quyết định
Tránh các tính năng này trong mã của bạn
Các mô-đun và lớp thư viện tiêu chuẩn sử dụng nội bộ các tính năng này đều được phép sử dụng [ví dụ:
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
234, def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
235 và def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
236]2. 20 con trăn hiện đại. từ __future__ nhập khẩu
Các thay đổi ngữ nghĩa của phiên bản ngôn ngữ mới có thể được kiểm soát sau quá trình nhập đặc biệt trong tương lai để kích hoạt chúng trên cơ sở từng tệp trong thời gian chạy trước đó
2. 20. 1 Định nghĩa
Có thể bật một số tính năng hiện đại hơn thông qua câu lệnh
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
237 cho phép sử dụng sớm các tính năng từ các phiên bản Python dự kiến trong tương lai2. 20. 2 Ưu điểm
Điều này đã được chứng minh là giúp nâng cấp phiên bản thời gian chạy mượt mà hơn vì các thay đổi có thể được thực hiện trên cơ sở từng tệp trong khi khai báo tính tương thích và ngăn chặn hồi quy trong các tệp đó. Mã hiện đại dễ bảo trì hơn vì ít có khả năng tích lũy nợ kỹ thuật sẽ gây ra sự cố trong quá trình nâng cấp thời gian chạy trong tương lai
2. 20. 3 nhược điểm
Mã như vậy có thể không hoạt động trên các phiên bản thông dịch viên rất cũ trước khi đưa ra câu lệnh tương lai cần thiết. Nhu cầu này phổ biến hơn trong các dự án hỗ trợ rất nhiều môi trường
2. 20. 4 Quyết định
từ __future__ nhập khẩuViệc sử dụng các câu lệnh
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
237 được khuyến khích. Nó cho phép một tệp nguồn nhất định bắt đầu sử dụng các tính năng cú pháp Python hiện đại hơn ngày nay. Sau khi bạn không còn cần chạy trên phiên bản có các tính năng bị ẩn đằng sau lần nhập def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
239, vui lòng xóa các dòng đóTrong mã có thể thực thi trên các phiên bản cũ như 3. 5 thay vì >= 3. 7, nhập khẩu
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
22Để biết thêm thông tin, hãy đọc tài liệu định nghĩa câu lệnh tương lai của Python
Vui lòng không xóa các mục nhập này cho đến khi bạn tin rằng mã này chỉ được sử dụng trong một môi trường đủ hiện đại. Ngay cả khi bạn hiện không sử dụng tính năng mà một tính năng nhập cụ thể trong tương lai cho phép trong mã của bạn hôm nay, thì việc giữ nguyên tính năng này trong tệp sẽ ngăn việc vô tình sửa đổi mã sau này tùy thuộc vào hành vi cũ hơn
Sử dụng các báo cáo nhập khẩu
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
240 khác khi bạn thấy phù hợp2. 21 Loại mã chú thích
Bạn có thể chú thích mã Python bằng gợi ý loại theo PEP-484 và kiểm tra loại mã khi xây dựng bằng công cụ kiểm tra loại như pytype
Chú thích loại có thể có trong nguồn hoặc trong tệp pyi còn sơ khai. Bất cứ khi nào có thể, chú thích nên ở trong nguồn. Sử dụng tệp pyi cho bên thứ ba hoặc mô-đun mở rộng
2. 21. 1 Định nghĩa
Chú thích kiểu [hoặc "gợi ý kiểu"] dành cho hàm hoặc đối số phương thức và giá trị trả về
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
23Bạn cũng có thể khai báo loại biến bằng cú pháp PEP-526 tương tự
2. 21. 2 Ưu điểm
Loại chú thích cải thiện khả năng đọc và bảo trì mã của bạn. Trình kiểm tra loại sẽ chuyển đổi nhiều lỗi thời gian chạy thành lỗi thời gian xây dựng và giảm khả năng sử dụng các Tính năng mạnh mẽ của bạn
2. 21. 3 nhược điểm
Bạn sẽ phải cập nhật các khai báo kiểu. Bạn có thể thấy lỗi loại mà bạn nghĩ là mã hợp lệ. Việc sử dụng trình kiểm tra loại có thể làm giảm khả năng sử dụng các Tính năng Nguồn của bạn
2. 21. 4 Quyết định
You are strongly encouraged to enable Python type analysis when updating code. Khi thêm hoặc sửa đổi API công khai, hãy bao gồm các chú thích loại và cho phép kiểm tra qua pytype trong hệ thống xây dựng. Vì phân tích tĩnh còn tương đối mới đối với Python, chúng tôi thừa nhận rằng các tác dụng phụ không mong muốn [chẳng hạn như các loại được suy luận sai] có thể ngăn một số dự án áp dụng. Trong những trường hợp đó, tác giả được khuyến khích thêm nhận xét bằng TODO hoặc liên kết đến lỗi mô tả [các] sự cố hiện đang ngăn cản việc áp dụng chú thích loại trong tệp BUILD hoặc trong chính mã khi thích hợp
3 quy tắc kiểu Python
3. 1 dấu chấm phẩy
Không kết thúc dòng của bạn bằng dấu chấm phẩy và không sử dụng dấu chấm phẩy để đặt hai câu lệnh trên cùng một dòng
3. 2 Chiều dài dòng
Độ dài dòng tối đa là 80 ký tự
Ngoại lệ rõ ràng đối với giới hạn 80 ký tự
- Báo cáo nhập khẩu dài
- URL, tên đường dẫn hoặc cờ dài trong nhận xét
- Các hằng số cấp mô-đun chuỗi dài không chứa khoảng trắng sẽ gây bất tiện khi chia thành các dòng như URL hoặc tên đường dẫn
- Pylint vô hiệu hóa bình luận. [e. g.
241]def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
- Pylint vô hiệu hóa bình luận. [e. g.
Không sử dụng tiếp tục dòng gạch chéo ngược ngoại trừ các câu lệnh
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
242 yêu cầu ba trình quản lý ngữ cảnh trở lênSử dụng cách nối dòng ẩn của Python bên trong dấu ngoặc đơn, dấu ngoặc và dấu ngoặc nhọn. Nếu cần, bạn có thể thêm một cặp dấu ngoặc đơn xung quanh một biểu thức
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
24Khi một chuỗi ký tự không vừa trên một dòng, hãy sử dụng dấu ngoặc đơn để nối dòng ẩn
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
25Trong các nhận xét, hãy đặt các URL dài trên dòng riêng của chúng nếu cần
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
26def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
27Được phép sử dụng tiếp tục dấu gạch chéo ngược khi xác định câu lệnh
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
242 với ba trình quản lý bối cảnh trở lên. Đối với hai trình quản lý bối cảnh, hãy sử dụng câu lệnh def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
242 lồng nhaudef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
28def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
29def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
30Lưu ý về sự thụt đầu dòng của các phần tử trong các ví dụ tiếp tục dòng ở trên;
Trong tất cả các trường hợp khác khi một dòng vượt quá 80 ký tự và trình định dạng tự động yapf không giúp đưa dòng xuống dưới giới hạn, thì dòng đó được phép vượt quá mức tối đa này. Các tác giả được khuyến khích ngắt dòng theo cách thủ công theo ghi chú ở trên khi thấy hợp lý
3. 3 dấu ngoặc đơn
Sử dụng dấu ngoặc đơn một cách tiết kiệm
Nó là tốt, mặc dù không bắt buộc, để sử dụng dấu ngoặc đơn xung quanh bộ dữ liệu. Không sử dụng chúng trong các câu lệnh trả về hoặc câu lệnh có điều kiện trừ khi sử dụng dấu ngoặc đơn để tiếp tục dòng ngụ ý hoặc để chỉ ra một bộ
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
31def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
323. 4 Thụt đầu dòng
Thụt lề các khối mã của bạn với 4 dấu cách
Không bao giờ sử dụng các tab. Việc tiếp tục dòng ngụ ý phải căn chỉnh các phần tử được bao theo chiều dọc [xem ví dụ về độ dài dòng] hoặc sử dụng thụt lề 4 khoảng trắng treo. Dấu ngoặc đóng [tròn, vuông hoặc cong] có thể được đặt ở cuối biểu thức hoặc trên các dòng riêng biệt, nhưng sau đó phải được thụt vào giống như dòng có dấu ngoặc mở tương ứng
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
33def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
343. 4. 1 Dấu phẩy ở cuối dãy các mục?
Dấu phẩy ở cuối trong chuỗi các mục chỉ được khuyến nghị khi mã thông báo vùng chứa đóng
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
245, def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
246 hoặc def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
247 không xuất hiện trên cùng một dòng với phần tử cuối cùng. Sự hiện diện của dấu phẩy ở cuối cũng được sử dụng như một gợi ý cho trình định dạng tự động mã Python YAPF của chúng tôi để hướng dẫn nó tự động định dạng vùng chứa các mục thành một mục trên mỗi dòng khi có ____ ______3248 sau phần tử cuối cùngdef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
353. 5 dòng trống
Hai dòng trống giữa các định nghĩa cấp cao nhất, có thể là định nghĩa hàm hoặc lớp. Một dòng trống giữa các định nghĩa phương thức và giữa chuỗi tài liệu của
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
249 và phương thức đầu tiên. Không có dòng trống nào sau dòng def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
250. Sử dụng các dòng trống đơn khi bạn đánh giá phù hợp trong các hàm hoặc phương thứcCác dòng trống không cần phải được neo vào định nghĩa. Ví dụ: các nhận xét liên quan ngay trước các định nghĩa hàm, lớp và phương thức có thể có ý nghĩa. Consider if your comment might be more useful as part of the docstring
3. 6 Khoảng trắng
Thực hiện theo các quy tắc đánh máy tiêu chuẩn để sử dụng khoảng trắng xung quanh dấu chấm câu
Không có khoảng trắng bên trong dấu ngoặc đơn, dấu ngoặc hoặc dấu ngoặc nhọn
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
36def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
37Không có khoảng trắng trước dấu phẩy, dấu chấm phẩy hoặc dấu hai chấm. Sử dụng khoảng trắng sau dấu phẩy, dấu chấm phẩy hoặc dấu hai chấm, ngoại trừ ở cuối dòng
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
38def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
39Không có khoảng trắng trước dấu ngoặc đơn/ngoặc mở bắt đầu danh sách đối số, lập chỉ mục hoặc cắt
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
40def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
41Không có khoảng trắng ở cuối
Bao quanh các toán tử nhị phân với một khoảng trắng ở hai bên để gán [
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
251], so sánh [def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
252] và Booleans [def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
253]. Sử dụng phán đoán tốt hơn của bạn để chèn khoảng trắng xung quanh các toán tử số học [def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
254, def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
255, def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
256, def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
257, def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
258, def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
259, def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
260, def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
222]Không bao giờ sử dụng khoảng trắng xung quanh
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
251 khi chuyển đối số từ khóa hoặc xác định giá trị tham số mặc định, với một ngoại lệ. khi có chú thích loại, hãy sử dụng khoảng trắng xung quanh def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
251 cho giá trị tham số mặc địnhdef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
42def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
43Không sử dụng khoảng trắng để sắp xếp theo chiều dọc các mã thông báo trên các dòng liên tiếp, vì nó sẽ trở thành gánh nặng bảo trì [áp dụng cho
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
264, def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
265, def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
251, v.v. ]def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
44def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
453. 7 dòng Shebang
Hầu hết các tệp
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
267 không cần bắt đầu bằng dòng def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
268. Bắt đầu tệp chính của chương trình với def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
269 [để hỗ trợ virtualenv] hoặc def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
270 mỗi PEP-394Dòng này được nhân sử dụng để tìm trình thông dịch Python, nhưng bị Python bỏ qua khi nhập mô-đun. Nó chỉ cần thiết trên một tệp dự định được thực thi trực tiếp
Đảm bảo sử dụng đúng kiểu cho mô-đun, hàm, chuỗi tài liệu phương thức và nhận xét nội tuyến
3. 8. 1 tài liệu
Python sử dụng docstrings để mã tài liệu. Chuỗi tài liệu là một chuỗi là câu lệnh đầu tiên trong gói, mô-đun, lớp hoặc hàm. Các chuỗi này có thể được trích xuất tự động thông qua thành viên
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
271 của đối tượng và được sử dụng bởi def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
225. [Hãy thử chạy def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
225 trên mô-đun của bạn để xem nó trông như thế nào. ] Luôn sử dụng định dạng ba trích dẫn kép def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
274 cho các chuỗi tài liệu [theo PEP 257]. Chuỗi tài liệu phải được tổ chức dưới dạng một dòng tóm tắt [một dòng vật lý không quá 80 ký tự] được kết thúc bằng dấu chấm, dấu chấm hỏi hoặc dấu chấm than. Khi viết thêm [khuyến khích], dòng này phải được theo sau bởi một dòng trống, tiếp theo là phần còn lại của chuỗi tài liệu bắt đầu ở cùng vị trí con trỏ với trích dẫn đầu tiên của dòng đầu tiên. Có nhiều hướng dẫn định dạng hơn cho các tài liệu bên dưới3. 8. 2 mô-đun
Mỗi tệp phải chứa giấy phép soạn sẵn. Chọn bản soạn sẵn thích hợp cho giấy phép được dự án sử dụng [ví dụ: Apache 2. 0, BSD, LGPL, GPL]
Các tệp phải bắt đầu bằng một chuỗi tài liệu mô tả nội dung và cách sử dụng mô-đun
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
463. 8. 2. 1 mô-đun thử nghiệmKhông bắt buộc phải có chuỗi tài liệu cấp mô-đun cho các tệp thử nghiệm. Chúng chỉ nên được đưa vào khi có thông tin bổ sung có thể được cung cấp
Các ví dụ bao gồm một số chi tiết cụ thể về cách chạy thử nghiệm, giải thích về mẫu thiết lập bất thường, sự phụ thuộc vào môi trường bên ngoài, v.v.
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
47Không nên sử dụng các chuỗi tài liệu không cung cấp bất kỳ thông tin mới nào
3. 8. 3 Hàm và Phương thức
Trong phần này, "hàm" có nghĩa là một phương thức, chức năng, trình tạo hoặc thuộc tính
Một chuỗi tài liệu là bắt buộc đối với mọi chức năng có một hoặc nhiều thuộc tính sau
- là một phần của API công khai
- kích thước không tầm thường
- logic không rõ ràng
Một chuỗi tài liệu phải cung cấp đủ thông tin để viết lệnh gọi đến hàm mà không cần đọc mã của hàm. Chuỗi tài liệu phải mô tả cú pháp gọi của hàm và ngữ nghĩa của nó, nhưng nói chung không phải là chi tiết triển khai của nó, trừ khi những chi tiết đó có liên quan đến cách sử dụng hàm. Ví dụ: một hàm thay đổi một trong các đối số của nó dưới dạng tác dụng phụ cần lưu ý rằng trong chuỗi tài liệu của nó. Mặt khác, các chi tiết tinh tế nhưng quan trọng về việc triển khai chức năng không liên quan đến người gọi sẽ được thể hiện dưới dạng nhận xét bên cạnh mã tốt hơn là trong chuỗi tài liệu của chức năng
The docstring may be descriptive-style [
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
275] or imperative-style [def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
276], but the style should be consistent within a file. Chuỗi tài liệu cho bộ mô tả dữ liệu def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
91 phải sử dụng cùng kiểu với chuỗi tài liệu cho thuộc tính hoặc đối số hàm [def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
278, thay vì def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
279]Một phương thức ghi đè một phương thức từ lớp cơ sở có thể có một chuỗi tài liệu đơn giản gửi trình đọc đến chuỗi tài liệu của phương thức được ghi đè, chẳng hạn như
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
280. Cơ sở lý luận là không cần phải lặp lại ở nhiều nơi tài liệu đã có trong chuỗi tài liệu của phương thức cơ sở. Tuy nhiên, nếu hành vi của phương thức ghi đè về cơ bản khác với phương thức bị ghi đè hoặc cần cung cấp thông tin chi tiết [e. g. , ghi lại các tác dụng phụ bổ sung], một chuỗi tài liệu có ít nhất những điểm khác biệt đó là bắt buộc đối với phương thức ghi đèMột số khía cạnh của một chức năng nên được ghi lại trong các phần đặc biệt, được liệt kê bên dưới. Mỗi phần bắt đầu bằng một dòng tiêu đề, kết thúc bằng dấu hai chấm. Tất cả các phần không phải là tiêu đề nên duy trì thụt lề treo hai hoặc bốn khoảng trắng [nhất quán trong một tệp]. Các phần này có thể được bỏ qua trong trường hợp tên và chữ ký của hàm đủ thông tin để có thể mô tả chính xác bằng cách sử dụng chuỗi tài liệu một dòng
lập luận. Liệt kê từng tham số theo tên. Mô tả phải theo sau tên và được phân tách bằng dấu hai chấm, sau đó là khoảng trắng hoặc xuống dòng. Nếu mô tả quá dài để vừa với một dòng 80 ký tự, hãy sử dụng thụt lề treo nhiều hơn 2 hoặc 4 khoảng trắng so với tên tham số [nhất quán với phần còn lại của chuỗi tài liệu trong tệp]. Mô tả phải bao gồm [các] loại bắt buộc nếu mã không chứa chú thích loại tương ứng. Nếu một hàm chấp nhậndef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
281 [danh sách đối số có độ dài thay đổi] và/hoặc def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
282 [đối số từ khóa tùy ý], thì chúng phải được liệt kê là def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
281 và def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
282. trả lại. [hoặc Sản lượng. cho trình tạo] Mô tả loại và ngữ nghĩa của giá trị trả về. Nếu hàm chỉ trả về Không thì không cần phần này. Nó cũng có thể được bỏ qua nếu chuỗi tài liệu bắt đầu bằng Returns hoặc Yields [e. g. def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
285] và câu mở đầu đủ để mô tả giá trị trả về. Không bắt chước 'kiểu NumPy' [ví dụ], kiểu này thường ghi lại giá trị trả về của bộ dữ liệu như thể đó là nhiều giá trị trả về với các tên riêng lẻ [không bao giờ đề cập đến bộ dữ liệu]. Thay vào đó, hãy mô tả một giá trị trả về như. “Trả về. Một bộ [mat_a, mat_b], trong đó mat_a là …, và…”. Các tên phụ trợ trong chuỗi tài liệu không nhất thiết phải tương ứng với bất kỳ tên nội bộ nào được sử dụng trong thân hàm [vì chúng không phải là một phần của API]. tăng. Liệt kê tất cả các ngoại lệ có liên quan đến giao diện theo sau là mô tả. Sử dụng tên ngoại lệ tương tự + dấu hai chấm + dấu cách hoặc dòng mới và kiểu thụt lề treo như được mô tả trong Args. Bạn không nên ghi lại các trường hợp ngoại lệ được nêu ra nếu API được chỉ định trong chuỗi tài liệu bị vi phạm [vì điều này nghịch lý sẽ tạo ra hành vi vi phạm phần API của API]def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
48Tương tự, biến thể này trên
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
286 có ngắt dòng cũng được cho phépdef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
493. 8. 4 lớp
Các lớp nên có một chuỗi tài liệu bên dưới định nghĩa lớp mô tả lớp. Nếu lớp của bạn có các thuộc tính công khai, chúng phải được ghi lại ở đây trong phần
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
287 và tuân theo cùng định dạng như phần def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
288 của hàmdef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
70Tất cả các chuỗi tài liệu lớp phải bắt đầu bằng một bản tóm tắt một dòng mô tả nội dung mà thể hiện của lớp đại diện. Điều này ngụ ý rằng các lớp con của
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
60 cũng nên mô tả ngoại lệ đại diện cho cái gì chứ không phải bối cảnh mà nó có thể xảy ra. Chuỗi tài liệu lớp không được lặp lại thông tin không cần thiết, chẳng hạn như lớp là một lớpdef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
71def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
723. 8. 5 Khối và Nhận xét Nội tuyến
Nơi cuối cùng để có nhận xét là ở những phần phức tạp của mã. Nếu bạn phải giải thích nó trong lần đánh giá mã tiếp theo, bạn nên bình luận ngay bây giờ. Các hoạt động phức tạp nhận được một vài dòng nhận xét trước khi các hoạt động bắt đầu. Những người không rõ ràng nhận được bình luận ở cuối dòng
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
73Để cải thiện mức độ dễ đọc, các nhận xét này phải bắt đầu cách mã ít nhất 2 dấu cách với ký tự nhận xét
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
265, theo sau là ít nhất một khoảng trắng trước văn bản của chính nhận xét đóMặt khác, không bao giờ mô tả mã. Giả sử người đọc mã biết Python [mặc dù không phải thứ bạn đang cố gắng làm] tốt hơn bạn
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
743. 8. 6 Dấu câu, Chính tả và Ngữ pháp
Chú ý đến dấu câu, chính tả và ngữ pháp;
Nhận xét phải dễ đọc như văn bản tường thuật, với cách viết hoa và dấu câu thích hợp. Trong nhiều trường hợp, các câu hoàn chỉnh dễ đọc hơn các đoạn câu. Shorter comments, such as comments at the end of a line of code, can sometimes be less formal, but you should be consistent with your style
Although it can be frustrating to have a code reviewer point out that you are using a comma when you should be using a semicolon, it is very important that source code maintain a high level of clarity and readability. Proper punctuation, spelling, and grammar help with that goal
3. 10 Dây
Use an f-string, the
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
259 operator, or the def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
292 method for formatting strings, even when the parameters are all strings. Use your best judgment to decide between string formatting options. A single join with def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
254 is okay but do not format with def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
254def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
75def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
76Avoid using the
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
254 and def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
296 operators to accumulate a string within a loop. In some conditions, accumulating a string with addition can lead to quadratic rather than linear running time. Although common accumulations of this sort may be optimized on CPython, that is an implementation detail. The conditions under which an optimization applies are not easy to predict and may change. Instead, add each substring to a list and def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
297 the list after the loop terminates, or write each substring to an def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
298 buffer. These techniques consistently have amortized-linear run time complexitydef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
77def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
78Be consistent with your choice of string quote character within a file. Pick
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
299 or def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
300 and stick with it. It is okay to use the other quote character on a string to avoid the need to backslash-escape quote characters within the stringdef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
79dict = 'something awful' # Bad Idea.. pylint: disable=redefined-builtin
50Prefer
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
274 for multi-line strings rather than def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
302. Projects may choose to use def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
302 for all non-docstring multi-line strings if and only if they also use def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
299 for regular strings. Docstrings must use def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
274 regardlessMulti-line strings do not flow with the indentation of the rest of the program. If you need to avoid embedding extra space in the string, use either concatenated single-line strings or a multi-line string with
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
306 to remove the initial space on each linedict = 'something awful' # Bad Idea.. pylint: disable=redefined-builtin
51dict = 'something awful' # Bad Idea.. pylint: disable=redefined-builtin
52dict = 'something awful' # Bad Idea.. pylint: disable=redefined-builtin
53dict = 'something awful' # Bad Idea.. pylint: disable=redefined-builtin
54dict = 'something awful' # Bad Idea.. pylint: disable=redefined-builtin
553. 10. 1 Logging
For logging functions that expect a pattern-string [with %-placeholders] as their first argument. Always call them with a string literal [not an f-string. ] as their first argument with pattern-parameters as subsequent arguments. Some logging implementations collect the unexpanded pattern-string as a queryable field. It also prevents spending time rendering a message that no logger is configured to output
dict = 'something awful' # Bad Idea.. pylint: disable=redefined-builtin
56dict = 'something awful' # Bad Idea.. pylint: disable=redefined-builtin
57dict = 'something awful' # Bad Idea.. pylint: disable=redefined-builtin
583. 10. 2 Error Messages
Error messages [such as. message strings on exceptions like
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
54, or messages shown to the user] should follow three guidelinesThe message needs to precisely match the actual error condition
Interpolated pieces need to always be clearly identifiable as such
They should allow simple automated processing [e. g. grepping]
dict = 'something awful' # Bad Idea.. pylint: disable=redefined-builtin
59dict = 'something awful' # Bad Idea.. pylint: disable=redefined-builtin
603. 11 Files, Sockets, and similar Stateful Resources
Explicitly close files and sockets when done with them. This rule naturally extends to closeable resources that internally use sockets, such as database connections, and also other resources that need to be closed down in a similar fashion. To name only a few examples, this also includes mmap mappings, h5py File objects, and matplotlib. pyplot figure windows
Leaving files, sockets or other such stateful objects open unnecessarily has many downsides
- They may consume limited system resources, such as file descriptors. Code that deals with many such objects may exhaust those resources unnecessarily if they’re not returned to the system promptly after use
- Holding files open may prevent other actions such as moving or deleting them, or unmounting a filesystem
- Files and sockets that are shared throughout a program may inadvertently be read from or written to after logically being closed. Nếu chúng thực sự bị đóng, các nỗ lực đọc hoặc ghi từ chúng sẽ đưa ra các ngoại lệ, làm cho vấn đề được biết đến sớm hơn
Furthermore, while files and sockets [and some similarly behaving resources] are automatically closed when the object is destructed, coupling the lifetime of the object to the state of the resource is poor practice
- There are no guarantees as to when the runtime will actually invoke the
233 method. Different Python implementations use different memory management techniques, such as delayed garbage collection, which may increase the object’s lifetime arbitrarily and indefinitelydef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
- Unexpected references to the file, e. g. in globals or exception tracebacks, may keep it around longer than intended
Relying on finalizers to do automatic cleanup that has observable side effects has been rediscovered over and over again to lead to major problems, across many decades and multiple languages [see e. g. this article for Java]
The preferred way to manage files and similar resources is using the
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
242 statementdict = 'something awful' # Bad Idea.. pylint: disable=redefined-builtin
61For file-like objects that do not support the
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
242 statement, use def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
311dict = 'something awful' # Bad Idea.. pylint: disable=redefined-builtin
62In rare cases where context-based resource management is infeasible, code documentation must explain clearly how resource lifetime is managed
Use
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
312 comments for code that is temporary, a short-term solution, or good-enough but not perfectA
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
312 comment begins with the word def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
312 in all caps, and a parenthesized context identifier. Ideally a bug reference, sometimes a username. A bug reference like def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
315 is preferable, because bugs are tracked and have follow-up comments, whereas individuals move around and may lose context over time. The def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
312 is followed by an explanation of what there is to doThe purpose is to have a consistent
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
312 format that can be searched to find out how to get more details. A def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
312 is not a commitment that the person referenced will fix the problem. Thus when you create a def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
312 with a username, it is almost always your own username that is givendict = 'something awful' # Bad Idea.. pylint: disable=redefined-builtin
63If your
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
312 is of the form “At a future date do something” make sure that you either include a very specific date [“Fix by November 2009”] or a very specific event [“Remove this code when all clients can handle XML responses. ”] that future code maintainers will comprehend3. 13 Imports formatting
Imports should be on separate lines; there are exceptions for
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
321 and def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
322 importsE. g
dict = 'something awful' # Bad Idea.. pylint: disable=redefined-builtin
64Imports are always put at the top of the file, just after any module comments and docstrings and before module globals and constants. Imports should be grouped from most generic to least generic
Python future import statements. For example
65dict = 'something awful' # Bad Idea.. pylint: disable=redefined-builtin
See above for more information about those
Python standard library imports. For example
third-party module or package imports. For example
Code repository sub-package imports. For example
66dict = 'something awful' # Bad Idea.. pylint: disable=redefined-builtin
Deprecated. application-specific imports that are part of the same top level sub-package as this file. For example
67dict = 'something awful' # Bad Idea.. pylint: disable=redefined-builtin
You may find older Google Python Style code doing this, but it is no longer required. New code is encouraged not to bother with this. Đơn giản chỉ cần xử lý các lần nhập gói phụ dành riêng cho ứng dụng giống như các lần nhập gói phụ khác
Within each grouping, imports should be sorted lexicographically, ignoring case, according to each module’s full package path [the
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
323 in def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
324]. Code may optionally place a blank line between import sectionsdict = 'something awful' # Bad Idea.. pylint: disable=redefined-builtin
683. 14 Statements
Generally only one statement per line
However, you may put the result of a test on the same line as the test only if the entire statement fits on one line. In particular, you can never do so with
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
63/def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
64 since the def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
63 and def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
64 can’t both fit on the same line, and you can only do so with an def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
329 if there is no def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
330dict = 'something awful' # Bad Idea.. pylint: disable=redefined-builtin
693. 15 Getters and Setters
Getter and setter functions [also called accessors and mutators] should be used when they provide a meaningful role or behavior for getting or setting a variable’s value
Đặc biệt, chúng nên được sử dụng khi nhận hoặc thiết lập biến phức tạp hoặc chi phí đáng kể, hiện tại hoặc trong tương lai hợp lý
If, for example, a pair of getters/setters simply read and write an internal attribute, the internal attribute should be made public instead. Để so sánh, nếu việc đặt một biến có nghĩa là một số trạng thái bị vô hiệu hóa hoặc được xây dựng lại, thì đó phải là một hàm setter. The function invocation hints that a potentially non-trivial operation is occurring. Alternatively, properties may be an option when simple logic is needed, or refactoring to no longer need getters and setters
Getters and setters should follow the Naming guidelines, such as
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
331 and def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
332If the past behavior allowed access through a property, do not bind the new getter/setter functions to the property. Any code still attempting to access the variable by the old method should break visibly so they are made aware of the change in complexity
3. 16 Naming
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
333, def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
334, def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
335, def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
336, def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
337, def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
338, def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
339, def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
340, def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
341, def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
342, def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
343, def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
344, def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
345Function names, variable names, and filenames should be descriptive; avoid abbreviation. In particular, do not use abbreviations that are ambiguous or unfamiliar to readers outside your project, and do not abbreviate by deleting letters within a word
Always use a
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
267 filename extension. Never use dashes3. 16. 1 Names to Avoid
single character names, except for specifically allowed cases
- counters or iterators [e. g.
347,def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
348,def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
349,def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
350, et al. ]def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
351 as an exception identifier indef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
352 statementsdef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
353 as a file handle indef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
242 statementsdef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
- private
355s with no constraints [e. g.def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
356,def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
357,def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
358]def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
Please be mindful not to abuse single-character naming. Generally speaking, descriptiveness should be proportional to the name’s scope of visibility. For example,
347 might be a fine name for 5-line code block but within multiple nested scopes, it is likely too vaguedef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
- counters or iterators [e. g.
dashes [
255] in any package/module namedef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
361 names [reserved by Python]def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
offensive terms
names that needlessly include the type of the variable [for example.
362]def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
3. 16. 2 Naming Conventions
“Internal” means internal to a module, or protected or private within a class
Prepending a single underscore [
28] has some support for protecting module variables and functions [linters will flag protected member access]def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
Prepending a double underscore [
364 aka “dunder”] to an instance variable or method effectively makes the variable or method private to its class [using name mangling]; we discourage its use as it impacts readability and testability, and isn’t really private. Prefer a single underscoredef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
Place related classes and top-level functions together in a module. Unlike Java, there is no need to limit yourself to one class per module
Use CapWords for class names, but lower_with_under. py for module names. Although there are some old modules named CapWords. py, this is now discouraged because it’s confusing when the module happens to be named after a class. [“wait – did I write
365 ordef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
366?”]def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
Underscores may appear in unittest method names starting with
367 to separate logical components of the name, even if those components use CapWords. One possible pattern isdef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
368; for exampledef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
369 is okay. There is no One Correct Way to name test methodsdef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
3. 16. 3 File Naming
Python filenames must have a
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
267 extension and must not contain dashes [def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
255]. This allows them to be imported and unittested. If you want an executable to be accessible without the extension, use a symbolic link or a simple bash wrapper containing def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
3723. 16. 4 Guidelines derived from Guido’s Recommendations
TypePublicInternalPackagesdef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
373Modulesdef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
373def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
375Classesdef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
376def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
377Exceptionsdef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
376Functionsdef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
379def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
380Global/Class Constantsdef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
381def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
382Global/Class Variablesdef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
373def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
375Instance Variablesdef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
373def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
375 [protected]Method Namesdef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
379def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
380 [protected]Function/Method Parametersdef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
373Local Variablesdef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
3733. 16. 5 Mathematical Notation
For mathematically heavy code, short variable names that would otherwise violate the style guide are preferred when they match established notation in a reference paper or algorithm. When doing so, reference the source of all naming conventions in a comment or docstring or, if the source is not accessible, clearly document the naming conventions. Prefer PEP8-compliant
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
391 for public APIs, which are much more likely to be encountered out of context3. 17 chính
In Python,
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
225 as well as unit tests require modules to be importable. If a file is meant to be used as an executable, its main functionality should be in a def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
393 function, and your code should always check def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
394 before executing your main program, so that it is not executed when the module is importedWhen using absl, use
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
395def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
00Otherwise, use
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
01Tất cả mã ở cấp cao nhất sẽ được thực thi khi mô-đun được nhập. Be careful not to call functions, create objects, or perform other operations that should not be executed when the file is being
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
225ed3. 18 Function length
Prefer small and focused functions
We recognize that long functions are sometimes appropriate, so no hard limit is placed on function length. If a function exceeds about 40 lines, think about whether it can be broken up without harming the structure of the program
Even if your long function works perfectly now, someone modifying it in a few months may add new behavior. This could result in bugs that are hard to find. Keeping your functions short and simple makes it easier for other people to read and modify your code
You could find long and complicated functions when working with some code. Do not be intimidated by modifying existing code. if working with such a function proves to be difficult, you find that errors are hard to debug, or you want to use a piece of it in several different contexts, consider breaking up the function into smaller and more manageable pieces
3. 19 Type Annotations
3. 19. 1 General Rules
Familiarize yourself with PEP-484
In methods, only annotate
73, ordef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
74 if it is necessary for proper type information. e. g. ,def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
02def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
Similarly, don’t feel compelled to annotate the return value of
399 [wheredef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
98 is the only valid option]def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
If any other variable or a returned type should not be expressed, use
401def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
You are not required to annotate all the functions in a module
- At least annotate your public APIs
- Use judgment to get to a good balance between safety and clarity on the one hand, and flexibility on the other
- Annotate code that is prone to type-related errors [previous bugs or complexity]
- Annotate code that is hard to understand
- Annotate code as it becomes stable from a types perspective. In many cases, you can annotate all the functions in mature code without losing too much flexibility
3. 19. 2 Line Breaking
Try to follow the existing indentation rules
Sau khi chú thích, nhiều chữ ký hàm sẽ trở thành “mỗi dòng một tham số”. To ensure the return type is also given its own line, a comma can be placed after the last parameter
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
03Luôn ưu tiên ngắt giữa các biến và không, ví dụ, giữa tên biến và chú thích loại. Tuy nhiên, nếu mọi thứ phù hợp trên cùng một dòng, hãy tiếp tục
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
04Nếu tổ hợp tên hàm, tham số cuối cùng và kiểu trả về quá dài, hãy thụt lề 4 trong một dòng mới. Khi sử dụng ngắt dòng, hãy ưu tiên đặt từng tham số và kiểu trả về trên các dòng riêng của chúng và căn chỉnh dấu ngoặc đơn đóng với
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
250def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
05Tùy chọn, kiểu trả về có thể được đặt trên cùng một dòng với tham số cuối cùng
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
06def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
18 cho phép bạn di chuyển dấu ngoặc đơn đóng sang một dòng mới và căn chỉnh với dòng mở đầu, nhưng điều này khó đọc hơndef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
07Như trong các ví dụ trên, không muốn ngắt các loại. Tuy nhiên, đôi khi chúng quá dài để nằm trên một dòng [cố gắng giữ cho các loại phụ không bị gián đoạn]
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
08Nếu một tên và loại quá dài, hãy cân nhắc sử dụng bí danh cho loại. Phương án cuối cùng là ngắt sau dấu hai chấm và thụt vào 4
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
09def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
103. 19. 3 Tuyên bố chuyển tiếp
Nếu bạn cần sử dụng một tên lớp [từ cùng một mô-đun] chưa được xác định – ví dụ: nếu bạn cần tên lớp bên trong phần khai báo của lớp đó hoặc nếu bạn sử dụng một lớp được xác định sau trong mã –
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
11def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
123. 19. 4 giá trị mặc định
Theo PEP-008, chỉ sử dụng khoảng trắng xung quanh
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
251 cho các đối số có cả chú thích loại và giá trị mặc địnhdef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
13def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
143. 19. 5 Không có Loại
Trong hệ thống kiểu Python,
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
406 là kiểu "hạng nhất" và vì mục đích đánh máy, def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
98 là bí danh của def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
406. Nếu một đối số có thể là def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
98, thì nó phải được khai báo. Bạn có thể sử dụng def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
410, nhưng nếu chỉ có một loại khác, hãy sử dụng def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
411Sử dụng
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
411 rõ ràng thay vì def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
411 ngầm định. Các phiên bản trước của PEP 484 cho phép def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
414 được hiểu là def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
415, nhưng đó không còn là hành vi ưa thích nữadef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
15def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
163. 19. 6 bí danh loại
Bạn có thể khai báo bí danh của các loại phức tạp. Tên của bí danh phải là CapWorded. Nếu bí danh chỉ được sử dụng trong mô-đun này, thì bí danh đó phải là _Private
Ví dụ: nếu tên của mô-đun cùng với tên của loại quá dài
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
17Các ví dụ khác là các kiểu lồng nhau phức tạp và nhiều biến trả về từ một hàm [dưới dạng một bộ]
3. 19. 7 kiểu phớt lờ
Bạn có thể tắt kiểm tra loại trên một dòng bằng nhận xét đặc biệt
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
416def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
417 có tùy chọn tắt đối với các lỗi cụ thể [tương tự như lint]def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
183. 19. 8 biến gõ
Phép gán được chú thích Nếu một biến nội bộ có loại khó hoặc không thể suy luận, hãy chỉ định loại của biến đó bằng phép gán có chú thích - sử dụng dấu hai chấm và nhập giữa tên biến và giá trị [tương tự như được thực hiện với các đối số hàm có giá trị mặc định]def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
19Nhập CommentsMặc dù bạn có thể thấy chúng còn lại trong cơ sở mã [chúng cần thiết trước Python 3. 6], không thêm bất kỳ cách sử dụng nào nữa của nhận xét def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
418 ở cuối dòngdef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
003. 19. 9 Tuples vs Danh sách
Danh sách đã nhập chỉ có thể chứa các đối tượng thuộc một loại. Các bộ dữ liệu đã nhập có thể có một loại lặp lại duy nhất hoặc một số phần tử được đặt với các loại khác nhau. Cái sau thường được sử dụng làm kiểu trả về từ một hàm
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
013. 19. 10 LoạiVars
Hệ thống loại Python có thuốc generic. Chức năng nhà máy
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
355 là một cách phổ biến để sử dụng chúngThí dụ
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
02Một TypeVar có thể bị ràng buộc
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
03Biến loại được xác định trước phổ biến trong mô-đun
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
321 là def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
421. Sử dụng nó cho nhiều chú thích có thể là def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
422 hoặc def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
423 và tất cả phải cùng loạidef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
04TypeVar phải có tên mô tả, trừ khi nó đáp ứng tất cả các tiêu chí sau
- không thể nhìn thấy bên ngoài
- không bị hạn chế
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
05def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
063. 19. 11 loại chuỗi
Không sử dụng
424 trong mã mới. Nó chỉ tương thích với Python 2/3def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
Sử dụng
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
423 cho dữ liệu chuỗi/văn bản. Đối với mã liên quan đến dữ liệu nhị phân, hãy sử dụng def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
422def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
07Nếu tất cả các kiểu chuỗi của một hàm luôn giống nhau, chẳng hạn nếu kiểu trả về giống với kiểu đối số trong đoạn mã trên, hãy sử dụng AnyStr
3. 19. 12 Nhập khẩu để gõ
Đối với các ký hiệu từ mô-đun
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
321 và def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
322 được sử dụng để hỗ trợ phân tích tĩnh và kiểm tra loại, hãy luôn nhập chính ký hiệu đó. Điều này giữ cho các chú thích phổ biến ngắn gọn hơn và phù hợp với các cách gõ được sử dụng trên khắp thế giới. Bạn rõ ràng được phép nhập nhiều lớp cụ thể trên một dòng từ mô-đun def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
321 và def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
322. Bán tạidef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
08Cho rằng cách nhập này sẽ thêm các mục vào không gian tên cục bộ, các tên trong
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
321 hoặc def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
322 phải được xử lý tương tự như các từ khóa và không được xác định trong mã Python của bạn, được nhập hay không. Nếu có sự xung đột giữa một loại và một tên hiện có trong một mô-đun, hãy nhập nó bằng cách sử dụng def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
433def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
09Ưu tiên sử dụng các loại tích hợp làm chú thích nếu có. Python hỗ trợ các chú thích loại bằng cách sử dụng các loại vùng chứa tham số qua PEP-585, được giới thiệu trong Python 3. 9
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
10GHI CHÚ. Người dùng Apache Beam nên tiếp tục nhập vùng chứa tham số từ
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
321def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
113. 19. 13 Nhập khẩu có điều kiện
Chỉ sử dụng nhập có điều kiện trong các trường hợp đặc biệt khi phải tránh nhập bổ sung cần thiết để kiểm tra loại trong thời gian chạy. Mô hình này không được khuyến khích;
Các mục nhập chỉ cần thiết cho chú thích loại có thể được đặt trong khối
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
435- Các loại được nhập có điều kiện cần được tham chiếu dưới dạng chuỗi, để tương thích chuyển tiếp với Python 3. 6 nơi các biểu thức chú thích thực sự được đánh giá
- Chỉ các thực thể được sử dụng duy nhất để nhập mới được xác định ở đây; . Nếu không, đó sẽ là lỗi thời gian chạy, vì mô-đun sẽ không được nhập vào thời gian chạy
- Khối phải ở ngay sau tất cả các lần nhập bình thường
- Không được có dòng trống nào trong danh sách nhập nhập
- Sắp xếp danh sách này như thể nó là một danh sách nhập thông thường
12def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
3. 19. 14 phụ thuộc tuần hoàn
Các phụ thuộc tròn gây ra bởi việc gõ là mã có mùi. Mã như vậy là một ứng cử viên tốt để tái cấu trúc. Mặc dù về mặt kỹ thuật, có thể giữ các phụ thuộc vòng tròn, nhưng các hệ thống xây dựng khác nhau sẽ không cho phép bạn làm như vậy vì mỗi mô-đun phải phụ thuộc vào mô-đun khác
Thay thế các mô-đun tạo nhập khẩu phụ thuộc vòng tròn bằng
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
401. Đặt bí danh có tên có ý nghĩa và sử dụng tên loại thực từ mô-đun này [bất kỳ thuộc tính nào của Any là Any]. Các định nghĩa bí danh phải được phân tách khỏi lần nhập cuối cùng bằng một dòngdef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
133. 19. 15 thuốc gốc
Khi chú thích, ưu tiên chỉ định tham số loại cho các loại chung;
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
14def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
15Nếu tham số loại tốt nhất cho một tên chung là
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
401, hãy làm cho nó rõ ràng, nhưng hãy nhớ rằng trong nhiều trường hợp, def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
355 có thể phù hợp hơndef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
16def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
174 Lời Chia Tay
HÃY KIÊN NHẪN
Nếu bạn đang chỉnh sửa mã, hãy dành vài phút để xem mã xung quanh bạn và xác định phong cách của nó. Nếu họ sử dụng khoảng trắng xung quanh tất cả các toán tử số học của họ, thì bạn cũng nên. Nếu nhận xét của họ có các hộp dấu thăng nhỏ xung quanh, hãy làm cho nhận xét của bạn cũng có các hộp dấu thăng nhỏ xung quanh chúng
Mục đích của việc có các hướng dẫn về phong cách là có một vốn từ vựng chung về viết mã để mọi người có thể tập trung vào những gì bạn đang nói hơn là vào cách bạn nói. Chúng tôi trình bày các quy tắc phong cách toàn cầu ở đây để mọi người biết từ vựng, nhưng phong cách địa phương cũng rất quan trọng. Nếu mã bạn thêm vào một tệp trông khác hẳn so với mã hiện có xung quanh nó, nó sẽ khiến người đọc mất nhịp khi họ đọc nó. Tránh điều này