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 Định nghĩa
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
18 là một công cụ để tìm lỗi và các vấn đề về kiểu dáng trong mã nguồn Python. Nó tìm thấy các vấn đề thường được trình biên dịch bắt gặp đối với các ngôn ngữ kém năng động hơn như C và C++. Do tính chất động của Python, một số cảnh báo có thể không chính xác; 2. 1. 2 Ưu điểm
Bắt các lỗi dễ bỏ sót như lỗi chính tả, sử dụng-vars-trước khi gán, v.v.
2. 1. 3 nhược điểm
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
18 không hoàn hảo. Để tận dụng lợi thế của nó, đôi khi chúng ta cần phải viết xung quanh nó, loại bỏ các cảnh báo hoặc sửa chữa nó2. 1. 4 Quyết định
Đảm bảo bạn 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 trên mã của mìnhLoại bỏ các cảnh báo nếu chúng không phù hợp để các vấn đề khác không bị ẩn. Để chặn cảnh báo, bạn có thể đặt nhận xét cấp dòng
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 nhập khẩu
Chỉ 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
31 cho các gói và mô-đun, không dùng cho các lớp hoặc chức năng riêng lẻ2. 2. 1 Định nghĩa
Cơ chế tái sử dụng để chia sẻ mã từ mô-đun này sang mô-đun khác
2. 2. 2 Ưu điểm
Quy ước quản lý không gian tên rất đơn giản. Nguồn của mỗi mã định danh được chỉ định một cách nhất quán;
2. 2. 3 nhược điểm
Tên mô-đun vẫn có thể xung đột. Một số tên mô-đun dài bất tiện
2. 2. 4 Quyết định
- Sử dụng
35 để nhập các gói và mô-đundef 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
36 trong đódef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
34 là tiền tố gói vàdef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
38 là tên mô-đun không có tiền tố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
39 nếu hai mô-đun có têndef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
38 sẽ được nhập, nếudef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
38 xung đột với tên cấp cao nhất được xác định trong mô-đun hiện tại hoặc nếudef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
38 là một tên dài bất tiệndef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
- Chỉ sử dụng
43 khidef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
44 là từ viết tắt tiêu chuẩn [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 chodef 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
2Không sử dụng tên họ hàng trong nhập khẩu. Ngay cả khi mô-đun nằm trong cùng một gói, hãy sử dụng tên gói đầy đủ. Điều này giúp ngăn việc vô tình nhập một gói hai lần
2. 2. 4. 1 miễn trừMiễn trừ từ quy tắc này
- Các ký hiệu từ các mô-đun sau được sử dụng để hỗ trợ phân tích tĩnh và kiểm tra kiểu
- Chuyển hướng từ sáu. di chuyển mô-đun
2. 3 gói
Nhập từng mô-đun bằng vị trí tên đường dẫn đầy đủ của mô-đun
2. 3. 1 Ưu điểm
Tránh xung đột về tên mô-đun hoặc nhập sai do đường dẫn tìm kiếm mô-đun không như tác giả mong đợi. Làm cho nó dễ dàng hơn để tìm các mô-đun
2. 3. 2 nhược điểm
Làm cho việc triển khai mã trở nên khó khăn hơn vì bạn phải sao chép hệ thống phân cấp gói. Không thực sự là một vấn đề với các cơ chế triển khai hiện đại
2. 3. 3 Quyết định
Tất cả mã mới phải nhập từng mô-đun theo tên gói đầy đủ của nó
Nhập khẩu phải như sau
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[giả sử tệp này nằm trong
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
48 nơi mà def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
49 cũng tồn tại]def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
7Thư mục chứa tệp nhị phân chính không nên được coi là trong
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
50 mặc dù điều đó xảy ra trong một số môi trường. Trong trường hợp này, mã nên giả định rằng def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
51 đề cập đến bên thứ ba hoặc gói cấp cao nhất có tê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
52, không phải là def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
49 cục bộ2. 4 ngoại lệ
Ngoại lệ được cho phép nhưng phải được sử dụng cẩn thận
2. 4. 1 Định nghĩa
Ngoại lệ là phương tiện thoát ra khỏi luồng điều khiển thông thường để xử lý lỗi hoặc các điều kiện ngoại lệ khác
2. 4. 2 Ưu điểm
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 với hai cơ sở dữ liệu khác nhau cùng một lúc [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. Các hàm lồng nhau có quyền truy cập chỉ đọc vào các biến được xác định trong phạm vi kèm theo
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
If the generator manages an expensive resource, make sure to force the clean up
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. Prefer generator expressions over
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
75 or def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
76 with a 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
Thuận tiện
2. 10. 3 Cons
Khó đọc và gỡ lỗi hơn các chức năng cục bộ. The lack of names means stack traces are more difficult to understand. Expressiveness is limited because the function may only contain an expression
2. 10. 4 Decision
Okay to use them for one-liners. If the code inside the lambda function is longer than 60-80 chars, it’s probably better to define it as a regular nested function
For common operations like multiplication, use the functions from the
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
83 module instead of lambda functions. For example, prefer def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
84 to 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 Conditional Expressions
Okay for simple cases
2. 11. 1 Definition
Conditional expressions [sometimes called a “ternary operator”] are mechanisms that provide a shorter syntax for if statements. 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
862. 11. 2 Pros
Shorter and more convenient than an if statement
2. 11. 3 Cons
May be harder to read than an if statement. The condition may be difficult to locate if the expression is long
2. 11. 4 Decision
Okay to use for simple cases. Each portion must fit on one line. true-expression, if-expression, else-expression. Use a complete if statement when things get more complicated
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 Pros
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 thuộc tính
Properties may be used to control getting or setting attributes that require trivial computations or logic. Property implementations must match the general expectations of regular attribute access. that they are cheap, straightforward, and unsurprising
2. 13. 1 Definition
A way to wrap method calls for getting and setting an attribute as a standard attribute access
2. 13. 2 Ưu điểm
- 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
- Cho phép tính toán lười biếng
- Cung cấp một cách để duy trì giao diện chung của một lớp khi các phần bên trong phát triển độc lập với người dùng lớp
2. 13. 3 nhược điểm
- Có thể ẩn các tác dụng phụ giống như quá tải toán tử
- Có thể gây nhầm lẫn cho các lớp con
2. 13. 4 Quyết định
Các thuộc tính được cho phép, nhưng, giống như quá tải toán tử, chỉ nên được sử dụng khi cần thiết và phù hợp với mong đợi của truy cập thuộc tính điển hình;
Ví dụ: không được phép sử dụng thuộc tính để vừa lấy vừa đặt thuộc tính nội bộ. không có tính toán xảy ra, vì vậy thuộc tính là không cần thiết [thay vào đó hãy đặt thuộc tính công khai]. Trong khi đó, việc sử dụng một thuộc tính để kiểm soát quyền truy cập thuộc tính hoặc để tính toán một giá trị có nguồn gốc tầm thường được cho phép. logic rất đơn giản và không có gì đáng ngạc nhiên
Các thuộc tính nên được tạo bằng trình trang trí
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
91. Thực hiện thủ công một bộ mô tả thuộc tính được coi là một tính năng quyền lựcKế thừa với các thuộc tính có thể không rõ ràng. Không sử dụng các thuộc tính để thực hiện tính toán mà một lớp con có thể muốn ghi đè và mở rộng
2. 14 Đánh giá Đúng/Sai
Sử dụng sai "ngầm" nếu có thể
2. 14. 1 Định nghĩa
Python đánh giá các giá trị nhất định là
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
92 khi ở trong ngữ cảnh boolean. Một "quy tắc ngón tay cái" nhanh là tất cả các giá trị "trống rỗng" đều được coi là sai, vì vậy tất cả các giá trị def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
93 đều được đánh giá là sai trong ngữ cảnh boolean2. 14. 2 Ưu điểm
Các điều kiện sử dụng booleans Python dễ đọc hơn và ít bị lỗi hơn. Trong hầu hết các trường hợp, chúng cũng nhanh hơn
2. 14. 3 nhược điểm
Có thể trông lạ đối với các nhà phát triển C/C++
2. 14. 4 Quyết định
Sử dụng sai “ngầm” nếu có thể, 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 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
95. Có một vài cảnh báo mà bạn nên ghi nhớ mặc dùAlways 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. , khi kiểm tra xem một biến hoặc đối số mặc định 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 có được đặt thành một số giá trị khác không. Giá trị khác có thể là một giá trị sai trong ngữ cảnh booleandef 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ờ so sánh một biến boolean với
92 bằng cách sử dụngdef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
201. Sử dụngdef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
202 để thay thế. Nếu bạn cần phân biệtdef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
92 vớidef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
98 thì hãy xâu chuỗi các biểu thức, 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
205def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
Đối với các chuỗi [chuỗi, danh sách, bộ dữ liệu], sử dụng thực tế là các chuỗi trống là sai, do đó,
206 vàdef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
207 được ưu tiên hơn so vớidef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
208 vàdef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
209 tương ứngdef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
Khi xử lý các số nguyên, sai ẩn có thể gây ra nhiều rủi ro hơn là lợi ích [i. e. , vô tình xử lý
98 là 0]. Bạn có thể so sánh một giá trị đã biết là một số nguyên [và không phải là kết quả củadef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
211] với số nguyên 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
Lưu ý rằng
212 [tô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. a “the
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
222 notation”]. 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
Decorators can perform arbitrary operations on a function’s arguments or return values, resulting in surprising implicit behavior. Additionally, decorators execute at object definition time. For module-level objects [classes, module functions, …] this happens at import time. Failures in decorator code are pretty much impossible to recover from
2. 17. 4 Decision
Use decorators judiciously when there is a clear advantage. Decorators should follow the same import and naming guidelines as functions. 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. không dựa vào tệp, ổ cắm, kết nối cơ sở dữ liệu, v.v. ], 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. Thay vào đó hãy viết một hàm cấp mô-đunChỉ 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
221 khi viết hàm tạo có tên hoặc quy trình dành riêng cho lớp để sửa đổi trạng thái chung cần thiết, chẳng hạn như bộ đệm trên toàn quy trình2. 18 luồng
Do not rely on the atomicity of built-in types
Mặc dù các kiểu dữ liệu tích hợp sẵn của Python, chẳng hạn như từ điển, dường như có các hoạt động nguyên tử, nhưng có một số trường hợp chúng không phải là nguyên tử [e. g. nếu
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
228 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
229 được triển khai dưới dạng các phương thức Python] và không nên dựa vào tính nguyên tử của chúng. Neither should you rely on atomic variable assignment [since this in turn depends on dictionaries]Sử dụng kiểu 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
230 của mô-đun Hàng đợi làm cách ưa thích để giao tiếp dữ liệu giữa các luồng. Nếu không, hãy sử dụng mô-đun luồng và các nguyên hàm khóa của nó. Ư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 là một ngôn ngữ cực kỳ linh hoạt và cung cấp cho bạn nhiều tính năng ưa thích như siêu dữ liệu tùy chỉnh, quyền truy cập vào mã byte, biên dịch nhanh, kế thừa động, sửa chữa đối tượng, hack nhập, phản ánh [e. g. some uses of
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
232], modification of system internals, def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
233 methods implementing customized cleanup, etc2. 19. 2 Pros
These are powerful language features. 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. It’s harder to read, understand, and debug code that’s using unusual features underneath. 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
Standard library modules and classes that internally use these features are okay to use [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
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, and 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. from __future__ imports
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 Pros
Đ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 đó. Modern code is more maintainable as it is less likely to accumulate technical debt that will be problematic during future runtime upgrades
2. 20. 3 nhược điểm
Such code may not work on very old interpreter versions prior to the introduction of the needed future statement. 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
from __future__ importsViệ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. Once you no longer need to run on a version where the features are hidden behind a def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
239 import, feel free to remove those linesIn code that may execute on versions as old as 3. 5 rather than >= 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
22For more information read the Python future statement definitions documentation
Please don’t remove these imports until you are confident the code is only ever used in a sufficiently modern environment. Even if you do not currently use the feature a specific future import enables in your code today, keeping it in place in the file prevents later modifications of the code from inadvertently depending on the older behavior
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 Type Annotated Code
You can annotate Python code with type hints according to PEP-484, and type-check the code at build time with a type checking tool like pytype
Chú thích loại có thể có trong nguồn hoặc trong tệp pyi còn sơ khai. Whenever possible, annotations should be in the source. Sử dụng tệp pyi cho bên thứ ba hoặc mô-đun mở rộng
2. 21. 1 Định nghĩa
Type annotations [or “type hints”] are for function or method arguments and return values
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
23You can also declare the type of a variable using similar PEP-526 syntax
2. 21. 2 Pros
Type annotations improve the readability and maintainability of your code. The type checker will convert many runtime errors to build-time errors, and reduce your ability to use Power Features
2. 21. 3 Cons
You will have to keep the type declarations up to date. You might see type errors that you think are valid code. Use of a type checker may reduce your ability to use Power Features
2. 21. 4 Decision
You are strongly encouraged to enable Python type analysis when updating code. When adding or modifying public APIs, include type annotations and enable checking via pytype in the build system. As static analysis is relatively new to Python, we acknowledge that undesired side-effects [such as wrongly inferred types] may prevent adoption by some projects. In those situations, authors are encouraged to add a comment with a TODO or link to a bug describing the issue[s] currently preventing type annotation adoption in the BUILD file or in the code itself as appropriate
3 Python Style Rules
3. 1 Semicolons
Do not terminate your lines with semicolons, and do not use semicolons to put two statements on the same line
3. 2 Line length
Maximum line length is 80 characters
Explicit exceptions to the 80 character limit
- Long import statements
- URLs, pathnames, or long flags in comments
- Long string module level constants not containing whitespace that would be inconvenient to split across lines such as URLs or pathnames
- Pylint disable comments. [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 disable comments. [e. g.
Do not use backslash line continuation except for
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
242 statements requiring three or more context managersMake use of Python’s implicit line joining inside parentheses, brackets and braces. If necessary, you can add an extra pair of parentheses around an expression
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
24When a literal string won’t fit on a single line, use parentheses for implicit line joining
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
25Within comments, put long URLs on their own line if necessary
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
27It is permissible to use backslash continuation when defining a
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 with three or more context managers. For two context managers, use a nested def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
242 statementdef 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;
In all other cases where a line exceeds 80 characters, and the yapf auto-formatter does not help bring the line below the limit, the line is allowed to exceed this maximum. 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 Parentheses
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. Do not use them in return statements or conditional statements unless using parentheses for implied line continuation or to indicate a tuple
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 Indentation
Indent your code blocks with 4 spaces
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. The presence of a trailing comma is also used as a hint to our Python code auto-formatter YAPF to direct it to auto-format the container of items to one item per line when the def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
248 after the final element is presentdef 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. No blank line following a def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
250 line. 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. Cân nhắc xem nhận xét của bạn có thể hữu ích hơn như một phần của chuỗi tài liệu không
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
37No whitespace before a comma, semicolon, or colon. 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]Never use spaces around
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
251 when passing keyword arguments or defining a default parameter value, with one exception. 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 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. Start the main file of a program with def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
269 [to support virtualenvs] or def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
270 per 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 Docstrings
Python uses docstrings to document code. 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]. A docstring should be organized as a summary line [one physical line not exceeding 80 characters] terminated by a period, question mark, or exclamation point. 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
Examples include some specifics on how the test should be run, an explanation of an unusual setup pattern, dependency on the external environment, and so on
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
47Docstrings that do not provide any new information should not be used
3. 8. 3 Functions and Methods
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
A docstring is mandatory for every function that has one or more of the following properties
- being part of the public API
- nontrivial size
- non-obvious logic
A docstring should give enough information to write a call to the function without reading the function’s code. 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ừ mộ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ở. However, if the overriding method’s behavior is substantially different from the overridden method, or details need to be provided [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 đèCertain aspects of a function should be documented in special sections, listed below. 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]. These sections can be omitted in cases where the function’s name and signature are informative enough that it can be aptly described using a one-line docstring
Args. List each parameter by name. 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ề. If the function only returns None, this section is not required. It may also be omitted if the docstring starts with Returns or 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]. Instead, describe such a return value as. “Returns. Một bộ [mat_a, mat_b], trong đó mat_a là …, và…”. The auxiliary names in the docstring need not necessarily correspond to any internal names used in the function body [as those are not part of the API]. Raises. Liệt kê tất cả các ngoại lệ có liên quan đến giao diện theo sau là mô tả. Use a similar exception name + colon + space or newline and hanging indent style as described in Args. You should not document exceptions that get raised if the API specified in the docstring is violated [because this would paradoxically make behavior under violation of the API part of the API]def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
48Similarly, this variation on
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
286 with a line break is also alloweddef 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
Classes should have a docstring below the class definition describing the class. 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. The class docstring should not repeat unnecessary information, such as that the class is a classdef 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
73To improve legibility, these comments should start at least 2 spaces away from the code with the comment character
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
265, followed by at least one space before the text of the comment itselfOn the other hand, never describe the code. 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. In many cases, complete sentences are more readable than sentence fragments. Các chú thích ngắn hơn, chẳng hạn như chú thích ở cuối dòng mã, đôi khi có thể kém trang trọng hơn, nhưng bạn nên nhất quán với phong cách của mình
Mặc dù có thể khó chịu khi người đánh giá mã chỉ ra rằng bạn đang sử dụng dấu phẩy trong khi lẽ ra bạn nên sử dụng dấu chấm phẩy, nhưng điều rất quan trọng là mã nguồn phải duy trì mức độ rõ ràng và dễ đọc cao. Dấu chấm câu, chính tả và ngữ pháp phù hợp giúp đạt được mục tiêu đó
3. 10 Strings
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. Sử dụng phán đoán tốt nhất của bạn để quyết định giữa các tùy chọn định dạng chuỗi. Một liên kết duy nhất 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
254 thì được nhưng không định dạ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
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
76Tránh sử dụng các toán tử
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
254 và def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
296 để tích lũy một chuỗi trong một vòng lặp. Trong một số điều kiện, tích lũy một chuỗi với phép cộng có thể dẫn đến thời gian chạy bậc hai thay vì tuyến tính. Mặc dù các tích lũy phổ biến thuộc loại này có thể được tối ưu hóa trên CPython, nhưng đó là chi tiết triển khai. Các điều kiện áp dụng tối ưu hóa không dễ dự đoán và có thể thay đổi. Thay vào đó, hãy thêm từng chuỗi con vào danh sách và def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
297 danh sách sau khi vòng lặp kết thúc hoặc ghi từng chuỗi con vào bộ đệm def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
298. Các kỹ thuật này luôn có độ phức tạp thời gian chạy tuyến tính được khấu haodef 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. Chọn
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
299 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
300 và gắn bó với nó. Bạn có thể sử dụng ký tự trích dẫn khác trên một chuỗi để tránh phải ký tự trích dẫn thoát dấu gạch chéo ngược trong chuỗidef 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. Các dự án có thể chọn 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
302 cho tất cả các chuỗi nhiều dòng không phải chuỗi doc nếu và chỉ khi chúng cũng 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
299 cho các chuỗi thông thường. Docstrings phải 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
274 bất kểCác chuỗi nhiều dòng không chạy với phần thụt đầu dòng của phần còn lại của chương trình. Nếu bạn cần tránh nhúng thêm khoảng trắng vào chuỗi, hãy sử dụng chuỗi một dòng được nối hoặc chuỗi nhiều dò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
306 để xóa khoảng trắng ban đầu trên mỗi dòngdict = '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 nhật ký
For logging functions that expect a pattern-string [with %-placeholders] as their first argument. Luôn gọi chúng bằng một chuỗi ký tự [không phải chuỗi f. ] as their first argument with pattern-parameters as subsequent arguments. Một số triển khai ghi nhật ký thu thập chuỗi mẫu chưa được mở rộng dưới dạng trường có thể truy vấn. Nó cũng ngăn việc dành thời gian hiển thị thông báo mà không có trình ghi nhật ký nào được định cấu hình để xuất
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 thông báo lỗi
Thông báo lỗi [chẳng hạn như. chuỗi thông báo về các trường hợp ngoại 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
54 hoặc thông báo được hiển thị cho người dùng] phải tuân theo ba nguyên tắcThông báo cần khớp chính xác với tình trạng lỗi thực tế
Các phần được nội suy cần phải luôn được xác định rõ ràng như vậy
Chúng nên cho phép xử lý tự động đơn giản [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
Đóng các tệp và ổ cắm một cách rõ ràng khi hoàn thành với chúng. Quy tắc này tự nhiên mở rộng cho các tài nguyên có thể đóng sử dụng ổ cắm bên trong, chẳng hạn như kết nối cơ sở dữ liệu và cả các tài nguyên khác cần được đóng theo cách tương tự. Chỉ nêu tên một số ví dụ, điều này cũng bao gồm ánh xạ mmap, đối tượng Tệp h5py và matplotlib. cửa sổ hình pyplot
Leaving files, sockets or other such stateful objects open unnecessarily has many downsides
- Chúng có thể tiêu tốn tài nguyên hệ thống hạn chế, chẳng hạn như bộ mô tả tệp. Mã xử lý nhiều đối tượng như vậy có thể làm cạn kiệt các tài nguyên đó một cách không cần thiết nếu chúng không được trả lại hệ thống ngay sau khi sử dụng
- Việc giữ các tệp đang mở có thể ngăn các hành động khác như di chuyển hoặc xóa chúng hoặc ngắt kết nối hệ thống tệp
- Các tệp và ổ cắm được chia sẻ trong toàn bộ chương trình có thể vô tình được đọc hoặc ghi vào sau khi được đóng một cách hợp lý. If they are actually closed, attempts to read or write from them will raise exceptions, making the problem known sooner
Hơn nữa, trong khi các tệp và ổ cắm [và một số tài nguyên hoạt động tương tự] tự động đóng khi đối tượng bị hủy, thì việc ghép thời gian tồn tại của đối tượng với trạng thái của tài nguyên là một cách thực hành kém.
- Không có gì đảm bảo khi nào bộ thực thi sẽ thực sự gọi phương thức
233. 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
- Các tham chiếu không mong muốn đến tệp, e. g. trong toàn cầu hoặc theo dõi ngoại lệ, có thể giữ nó lâu hơn dự định
Việc dựa vào các công cụ hoàn thiện để thực hiện dọn dẹp tự động có các tác dụng phụ có thể quan sát được đã được khám phá lại nhiều lần để dẫn đến các vấn đề lớn, qua nhiều thập kỷ và nhiều ngôn ngữ [xem e. g. bài viết này cho Java]
Cách ưu tiên để quản lý tệp và các tài nguyên tương tự là 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
242dict = 'something awful' # Bad Idea.. pylint: disable=redefined-builtin
61Đối với các đối tượng giống như tệp không hỗ trợ 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, 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
311dict = 'something awful' # Bad Idea.. pylint: disable=redefined-builtin
62Trong những trường hợp hiếm hoi khi quản lý tài nguyên dựa trên ngữ cảnh là không khả thi, tài liệu mã phải giải thích rõ ràng cách quản lý thời gian tồn tại của tài nguyên
Sử dụng 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
312 cho mã tạm thời, giải pháp ngắn hạn hoặc đủ tốt nhưng không hoàn hảoA
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. Lý tưởng nhất là tham chiếu lỗi, đôi khi là tên người dùng. Một tài liệu tham khảo lỗi như def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
315 thích hợp hơn, bởi vì các lỗi được theo dõi và có các nhận xét tiếp theo, trong khi các cá nhân di chuyển xung quanh và có thể mất bối cảnh theo thời gian. def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
312 được theo sau bởi một lời giải thích về những việc phải làmThe 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. Vì vậy, khi bạn tạo 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
312 với tên người dùng, thì hầu như tên người dùng của bạn luôn được cung cấpdict = 'something awful' # Bad Idea.. pylint: disable=redefined-builtin
63Nếu
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
312 của bạn có dạng “Vào một ngày trong tương lai, hãy làm điều gì đó”, hãy đảm bảo rằng bạn bao gồm một ngày rất cụ thể [“Khắc phục trước tháng 11 năm 2009”] hoặc một sự kiện rất cụ thể [“Xóa mã này khi tất cả khách hàng có thể xử lý phản hồi XML. ”] mà những người bảo trì mã trong tương lai sẽ hiểu3. 13 Nhập định dạng
Nhập khẩu nên được trên các dòng riêng biệt;
E. g
dict = 'something awful' # Bad Idea.. pylint: disable=redefined-builtin
64Các mục nhập luôn được đặt ở đầu tệp, ngay sau bất kỳ nhận xét và chuỗi tài liệu nào của mô-đun cũng như trước các hằng số và toàn cầu của mô-đun. Imports should be grouped from most generic to least generic
Báo cáo nhập khẩu trong tương lai của Python. For example
65dict = 'something awful' # Bad Idea.. pylint: disable=redefined-builtin
Xem ở trên để biết thêm thông tin về những
Nhập thư viện chuẩn Python. Ví dụ
third-party module or package imports. For example
Nhập gói con kho lưu trữ mã. For example
66dict = 'something awful' # Bad Idea.. pylint: disable=redefined-builtin
Deprecated. nhập dành riêng cho ứng dụng là một phần của cùng gói phụ cấp cao nhất như tệp này. For example
67dict = 'something awful' # Bad Idea.. pylint: disable=redefined-builtin
Bạn có thể tìm thấy mã Google Python Style cũ hơn để thực hiện việc này, nhưng nó không còn cần thiết nữa. Mã mới được khuyến khích không bận tâm với điều này. Đơ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
Trong mỗi nhóm, các mục nhập phải được sắp xếp theo từ điển, bỏ qua trường hợp, theo đường dẫn gói đầy đủ của mỗi mô-đun [_______3323 trong ____3324]. Mã có thể tùy chọn đặt một dòng trống giữa các phần nhập
dict = 'something awful' # Bad Idea.. pylint: disable=redefined-builtin
683. 14 Statements
Nói chung chỉ có một tuyên bố trên mỗi dòng
Tuy nhiên, bạn chỉ có thể đặt kết quả của một bài kiểm tra trên cùng một dòng với bài kiểm tra nếu toàn bộ câu lệnh nằm trên một dòng. Đặc biệt, bạn không bao giờ có thể làm như vậy 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
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 vì cả def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
63 và def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
64 đều không thể nằm trên cùng một dòng và bạn chỉ có thể làm như vậy 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
329 nếu không có 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 và Setters
Các hàm getter và setter [còn được gọi là bộ truy cập và bộ biến đổi] nên được sử dụng khi chúng cung cấp vai trò hoặc hành vi có ý nghĩa để nhận hoặc đặt giá trị của biến
In particular, they should be used when getting or setting the variable is complex or the cost is significant, either currently or in a reasonable future
Ví dụ: nếu một cặp getters/setters chỉ cần đọc và ghi một thuộc tính nội bộ, thì thuộc tính nội bộ sẽ được công khai thay thế. Để 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 và setters phải tuân theo Nguyên tắc đặt tên, 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
331 và def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
332Nếu hành vi trong quá khứ cho phép truy cập thông qua một thuộc tính, không liên kết các hàm getter/setter mới với thuộc tính. Bất kỳ mã nào vẫn đang cố truy cập vào biến theo phương pháp cũ sẽ bị hỏng rõ ràng để chúng nhận thức được sự thay đổi về độ phức tạp
3. 16 đặt tên
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
345Tên hàm, tên biến và tên tệp phải mang tính mô tả; . Đặc biệt, không sử dụng các từ viết tắt mơ hồ hoặc không quen thuộc với người đọc bên ngoài dự án của bạn và không viết tắt bằng cách xóa các chữ cái trong một từ
Luôn sử dụng phần mở rộng tên 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. 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, 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
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
- các
355 riêng tư không có ràng buộc [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
Xin lưu ý không lạm dụng đặt tên một ký tự. Nói chung, tính mô tả phải tỷ lệ thuận với phạm vi hiển thị của tên. 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.
dấu gạch ngang [
255] trong bất kỳ tên gói/mô-đun nàodef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
361 tên [dành riêng cho Python]def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
điều khoản xúc phạm
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 Quy ước đặt tên
“Internal” means internal to a module, or protected or private within a class
Việc thêm trước một dấu gạch dưới [
28] có một số hỗ trợ để bảo vệ các biến và chức năng của mô-đun [linters sẽ gắn cờ quyền truy cập của thành viên được bảo vệ]def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
Việc thêm trước một dấu gạch dưới kép [
364 hay còn gọi là “dunder”] vào một biến thể hiện hoặc phương thức sẽ làm cho biến hoặc phương thức đó trở nên riêng tư đối với lớp của nó [sử dụng xáo trộn tên]; . Thích một dấu gạch dướidef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
Đặt các lớp liên quan và các hàm cấp cao nhất cùng nhau trong một mô-đun. Không giống như Java, không cần giới hạn bản thân trong một lớp cho mỗi mô-đun
Sử dụng CapWords cho tên lớp, nhưng Lower_with_under. py cho tên mô-đun. Mặc dù có một số mô-đun cũ có tên là CapWords. py, điều này hiện không được khuyến khích vì thật khó hiểu khi mô-đun được đặt tên theo một lớp. [“chờ đã – tôi đã viết
365 haydef 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
Dấu gạch dưới có thể xuất hiện trong các tên phương thức kém nhất bắt đầu bằng
367 để phân tách các thành phần logic của tên, ngay cả khi các thành phần đó sử dụng CapWords. Một mẫu 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
368; . Không có một cách chính xác nào để đặt tên cho các phương pháp thử nghiệmdef 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 Đặt tên tệp
Tên tệp Python phải có phần mở rộng là
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
267 và không được chứa dấu gạch ngang [def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
255]. Điều này cho phép chúng được nhập và không được kiểm tra. Nếu bạn muốn có thể truy cập tệp thực thi mà không cần tiện ích mở rộng, hãy sử dụng liên kết tượng trưng hoặc trình bao bọc bash đơn giản có chứa 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 Nguyên tắc bắt nguồn từ Khuyến nghị của Guido
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 Ký hiệu toán học
Đối với mã nặng về mặt toán học, các tên biến ngắn sẽ vi phạm hướng dẫn kiểu được ưu tiên hơn khi chúng khớp với ký hiệu đã thiết lập trong tài liệu tham khảo hoặc thuật toán. Khi làm như vậy, hãy tham khảo nguồn của tất cả các quy ước đặt tên trong một nhận xét hoặc chuỗi tài liệu hoặc, nếu nguồn không thể truy cập được, hãy ghi lại rõ ràng các quy ước đặt tên. Ưu tiên
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
391 tuân thủ PEP8 cho các API công khai, có nhiều khả năng gặp phải ngoài ngữ cảnh hơn3. 17 chính
Trong 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 cũng như các bài kiểm tra đơn vị yêu cầu các mô-đun có thể nhập được. Nếu một tệp được dùng làm tệp thực thi, thì chức năng chính của nó phải ở 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
393 và mã của bạn phải luôn kiểm tra def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
394 trước khi thực thi chương trình chính của bạn, để nó không được thực thi khi mô-đun được nhậpKhi sử dụng absl, 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
395def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
00Nếu không, 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
01Tất cả mã ở cấp cao nhất sẽ được thực thi khi mô-đun được nhập. Cẩn thận không gọi hàm, tạo đối tượng hoặc thực hiện các thao tác khác không nên thực hiện khi tệp đang được
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 Chiều dài chức năng
Thích các chức năng nhỏ và tập trung
Chúng tôi nhận ra rằng các hàm dài đôi khi phù hợp, vì vậy không có giới hạn cứng nào được đặt cho độ dài của hàm. Nếu một hàm vượt quá khoảng 40 dòng, hãy suy nghĩ xem liệu nó có thể được chia nhỏ mà không làm hỏng cấu trúc của chương trình hay không
Ngay cả khi chức năng lâu dài của bạn hoạt động hoàn hảo ngay bây giờ, ai đó sửa đổi nó sau vài tháng có thể thêm hành vi mới. Điều này có thể dẫn đến các lỗi khó tìm. Giữ cho các chức năng của bạn ngắn gọn và đơn giản giúp người khác đọc và sửa đổi mã của bạn dễ dàng hơn
Bạn có thể tìm thấy các hàm dài và phức tạp khi làm việc với một số mã. Đừng bị đe dọa bằng cách sửa đổi mã hiện có. nếu làm việc với một chức năng như vậy tỏ ra khó khăn, bạn thấy rằng các lỗi khó gỡ lỗi hoặc bạn muốn sử dụng một phần của nó trong nhiều ngữ cảnh khác nhau, hãy cân nhắc chia chức năng thành các phần nhỏ hơn và dễ quản lý hơn
3. 19 Chú thích Loại
3. 19. 1 Quy tắc chung
Làm quen với PEP-484
Trong các phương thức, chỉ chú thích
73 hoặcdef viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
74 nếu cần thiết cho thông tin loại phù hợp. 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
Tương tự, đừng cảm thấy bắt buộc phải chú thích giá trị trả về của
399 [trong đó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à tùy chọn hợp lệ duy nhất]def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str: del beans, eggs # Unused by vikings. return spam + spam + spam
Nếu bất kỳ biến nào khác hoặc kiểu trả về không được biểu thị, hãy sử dụng
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
- Ít nhất hãy chú thích các API công khai của bạn
- Sử dụng phán đoán để đạt được sự cân bằng tốt giữa một mặt là an toàn và rõ ràng, mặt khác là linh hoạt
- Chú thích mã dễ bị lỗi liên quan đến loại [lỗi trước đó hoặc độ phức tạp]
- Chú thích mã khó hiểu
- Chú thích mã khi nó trở nên ổn định từ góc độ loại. Trong nhiều trường hợp, bạn có thể chú thích tất cả các chức năng trong mã trưởng thành mà không mất quá nhiều tính linh hoạt
3. 19. Ngắt 2 dòng
Cố gắng tuân theo các quy tắc thụt đầu dòng hiện có
Sau khi chú thích, nhiều chữ ký hàm sẽ trở thành “mỗi dòng một tham số”. Để đảm bảo kiểu trả về cũng được cung cấp dòng riêng, có thể đặt dấu phẩy sau 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
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 đường thẳ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. However, sometimes they are too long to be on a single line [try to keep sub-types unbroken]
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 Type Aliases
You can declare aliases of complex types. The name of an alias should be CapWorded. If the alias is used only in this module, it should be _Private
For example, if the name of the module together with the name of the type is too long
def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
17Other examples are complex nested types and multiple return variables from a function [as a tuple]
3. 19. 7 Ignoring Types
You can disable type checking on a line with the special comment
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 has a disable option for specific errors [similar to 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 Typing Variables
Annotated AssignmentsIf an internal variable has a type that is hard or impossible to infer, specify its type with an annotated assignment - use a colon and type between the variable name and value [the same as is done with function arguments that have a default value]def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
19Type CommentsThough you may see them remaining in the codebase [they were necessary before Python 3. 6], do not add any more uses of a def viking_cafe_order[spam: str, beans: str, eggs: Optional[str] = None] -> str:
del beans, eggs # Unused by vikings.
return spam + spam + spam
418 comment on the end of the linedef 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úngVí 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 thông 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