Trong Python 3.5 Python được thêm vào loại gợi ý. Trước khi Python 3.5 tồn tại, đã có một cuộc trò chuyện cởi mở về cách nhập gợi ý, và nó đã được thực hiện trong các bình luận. Pycharm hỗ trợ loại gợi ý loại trong cả hai. Tôi khuyên bạn nên làm theo cách Python vì một vài lý do, nhưng nó tùy thuộc vào bạn.
"""These should work"""
from Typing import Tuple
class Foo:
pass
def foo[a: int, b: float, c: Foo] -> Tuple[int, float, Foo]:
return a, b, c
def bar[a, b, c] -> Tuple[int, float, Foo]:
return a, b, c
def that[a, b, c]:
"""
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
def thing[a, b, c]:
"""
:rtype: [int, float, Foo]
"""
return a, b, c
"""This should fail because Tuple is not imported, but no warning"""
def that[a, b, c]:
"""
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
"""
This only partially works because 'Foo' isn't in scope.
This can easily happen if someone refactored the name of 'Foo',
but the comments wouldn't be updated.
"""
def thing[a, b, c]:
"""
:rtype: [int, float, Foo]
"""
return a, b, c
def documentation_nightmare[a, b, c]:
"""
There are no checks, and everything seems fine, but the documentation is all wrong and hard to maintain.
:param a: A
:type a: float
:param b: B
:type b: Bar
:param c: C
:type c: int
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
Pro Pro của Python Way là .. Mã của bạn sẽ bắt đầu cung cấp cho bạn gợi ý loại và hiển thị các cảnh báo nếu bạn có các loại sai bị kết nối. Nếu toàn bộ cơ sở mã của bạn được gõ thì bạn có thể chạy kiểm tra để đảm bảo mọi thứ được kết nối đúng. Một số người thích điều này, một số người thì không.
Lưu ý, tôi đã làm
def add_values[arr]: """ Add the values in ``arr``. This is equivalent to Python ``sum`` of :meth:`pandas.Series.sum`. Some sections are omitted here for simplicity. """ return sum[arr]0 trước đây và tôi ghét nó. Bây giờ tôi làm điều đó với việc đánh máy thực sự đầy đủ, và nó giúp tôi tiết kiệm nhiều thời gian hơn nó, nhưng nhiều người không thích nó.
Từ bài đăng này:
Danh sách đầy đủ của tên trường có thể được tìm thấy ở đây
- param, tham số, arg, đối số, khóa, từ khóa: Mô tả một tham số.: Description of a parameter.
- Loại: Loại tham số. Tạo một liên kết nếu có thể.: Type of a parameter. Creates a link if possible.
- Tăng, nâng cao, ngoại trừ, ngoại lệ: rằng [và khi] một ngoại lệ cụ thể được nêu ra.: That [and when] a specific exception is raised.
- Var, Ivar, CVAR: Mô tả một biến.: Description of a variable.
- vartype: loại của một biến. Tạo một liên kết nếu có thể. : Type of a variable. Creates a link if possible.
- Trả về, trả lại: Mô tả giá trị trả về.: Description of the return value.
- RTYPE: Loại trả lại. Tạo một liên kết nếu có thể.: Return type. Creates a link if possible.
- Meta: Thêm siêu dữ liệu vào mô tả của đối tượng Python. Siêu dữ liệu sẽ không được hiển thị trên tài liệu đầu ra. Ví dụ:: Meta Private: Cho biết đối tượng Python là thành viên riêng. Nó được sử dụng trong SPHINX.EXT.Autodoc để lọc các thành viên.: Add metadata to description of the python object. The metadata will not be shown on output document. For example, :meta private: indicates the python object is private member. It is used in sphinx.ext.autodoc for filtering members.
Về tài liệu và tiêu chuẩn#
DocString Python là một chuỗi được sử dụng để ghi lại mô -đun Python, lớp, chức năng hoặc phương thức, vì vậy các lập trình viên có thể hiểu những gì nó làm mà không cần phải đọc chi tiết về việc triển khai.
Ngoài ra, đó là một thực tế phổ biến để tạo tài liệu trực tuyến [HTML] tự động từ các tài liệu. Sphinx phục vụ mục đích này.
Ví dụ tiếp theo đưa ra một ý tưởng về một tài liệu trông như thế nào:
def add[num1, num2]: """ Add up two integer numbers. This function simply wraps the ``+`` operator, and does not do anything interesting, except for illustrating what the docstring of a very simple function looks like. Parameters ---------- num1 : int First number to add. num2 : int Second number to add. Returns ------- int The sum of ``num1`` and ``num2``. See Also -------- subtract : Subtract one integer from another. Examples -------- >>> add[2, 2] 4 >>> add[25, 0] 25 >>> add[10, -10] 0 """ return num1 + num2
Một số tiêu chuẩn liên quan đến các tài liệu tồn tại, giúp chúng dễ đọc hơn và cho phép chúng dễ dàng xuất sang các định dạng khác như HTML hoặc PDF.
Các quy ước đầu tiên mà mỗi tài liệu Python nên tuân theo được xác định trong PEP-257.
Vì PEP-257 khá rộng, các tiêu chuẩn cụ thể khác cũng tồn tại. Trong trường hợp của Pandas, Công ước DocString Numpy được tuân thủ. Các quy ước này được giải thích trong tài liệu này:
Hướng dẫn tài liệu của Numpydoc [dựa trên hướng dẫn ban đầu về tài liệu Numpy/Scipy]
Numpydoc là một phần mở rộng nhân sư để hỗ trợ Công ước DocString Numpy.
Tiêu chuẩn sử dụng tái cấu trúcText [REST]. Tái cấu trúcText là một ngôn ngữ đánh dấu cho phép mã hóa các kiểu trong các tệp văn bản thuần túy. Tài liệu về tái cấu trúc có thể được tìm thấy trong:
Sphinx Tái cấu trúc mồi
Tái cấu trúc nhanh chóng tham khảo
Đặc điểm kỹ thuật tái cấu trúc đầy đủ
Pandas có một số người trợ giúp để chia sẻ tài liệu giữa các lớp liên quan, xem chia sẻ tài liệu.Sharing docstrings.
Phần còn lại của tài liệu này sẽ tóm tắt tất cả các hướng dẫn trên và sẽ cung cấp các quy ước bổ sung cụ thể cho dự án Pandas.
Viết một tài liệu#
Quy tắc chung#
Docstrings phải được xác định với ba trích dẫn kép. Không nên để lại các dòng trống trước hoặc sau DocString. Văn bản bắt đầu trong dòng tiếp theo sau các trích dẫn mở. Các trích dẫn đóng có dòng riêng của chúng [có nghĩa là chúng không ở cuối câu cuối cùng].
Trong những dịp hiếm hoi, các kiểu phần còn lại như văn bản in đậm hoặc chữ nghiêng sẽ được sử dụng trong các tài liệu, nhưng nó có phổ biến để có mã nội tuyến, được trình bày giữa các backticks. Những điều sau đây được xem xét mã nội tuyến:
Tên của một tham số
Mã Python, một mô-đun, hàm, tích hợp, loại, nghĩa đen [ví dụ:
def add_values[arr]: """ Add the values in ``arr``. This is equivalent to Python ``sum`` of :meth:`pandas.Series.sum`. Some sections are omitted here for simplicity. """ return sum[arr]
1,def add_values[arr]: """ Add the values in ``arr``. This is equivalent to Python ``sum`` of :meth:`pandas.Series.sum`. Some sections are omitted here for simplicity. """ return sum[arr]
2,def add_values[arr]: """ Add the values in ``arr``. This is equivalent to Python ``sum`` of :meth:`pandas.Series.sum`. Some sections are omitted here for simplicity. """ return sum[arr]
3,def add_values[arr]: """ Add the values in ``arr``. This is equivalent to Python ``sum`` of :meth:`pandas.Series.sum`. Some sections are omitted here for simplicity. """ return sum[arr]
4,def add_values[arr]: """ Add the values in ``arr``. This is equivalent to Python ``sum`` of :meth:`pandas.Series.sum`. Some sections are omitted here for simplicity. """ return sum[arr]
5]Một lớp gấu trúc [ở dạng
def add_values[arr]: """ Add the values in ``arr``. This is equivalent to Python ``sum`` of :meth:`pandas.Series.sum`. Some sections are omitted here for simplicity. """ return sum[arr]
6]Phương pháp gấu trúc [ở dạng
def add_values[arr]: """ Add the values in ``arr``. This is equivalent to Python ``sum`` of :meth:`pandas.Series.sum`. Some sections are omitted here for simplicity. """ return sum[arr]
7]Hàm gấu trúc [ở dạng
def add_values[arr]: """ Add the values in ``arr``. This is equivalent to Python ``sum`` of :meth:`pandas.Series.sum`. Some sections are omitted here for simplicity. """ return sum[arr]
8]
Ghi chú
Để chỉ hiển thị thành phần cuối cùng của lớp, phương thức hoặc chức năng được liên kết, tiền tố với
def add_values[arr]: """ Add the values in ``arr``. This is equivalent to Python ``sum`` of :meth:`pandas.Series.sum`. Some sections are omitted here for simplicity. """ return sum[arr]9. Ví dụ:
def func[]: """Some function. With several mistakes in the docstring. It has a blank like after the signature ``def func[]:``. The text 'Some function' should go in the line after the opening quotes of the docstring, not in the same line. There is a blank line between the docstring and the first line of code ``foo = 1``. The closing quotes should be in the next line, not in this one.""" foo = 1 bar = 2 return foo + bar0 sẽ liên kết đến
def func[]: """Some function. With several mistakes in the docstring. It has a blank like after the signature ``def func[]:``. The text 'Some function' should go in the line after the opening quotes of the docstring, not in the same line. There is a blank line between the docstring and the first line of code ``foo = 1``. The closing quotes should be in the next line, not in this one.""" foo = 1 bar = 2 return foo + bar1 nhưng chỉ hiển thị phần cuối cùng,
def func[]: """Some function. With several mistakes in the docstring. It has a blank like after the signature ``def func[]:``. The text 'Some function' should go in the line after the opening quotes of the docstring, not in the same line. There is a blank line between the docstring and the first line of code ``foo = 1``. The closing quotes should be in the next line, not in this one.""" foo = 1 bar = 2 return foo + bar2 dưới dạng văn bản liên kết. Xem cú pháp tham khảo chéo Sphinx để biết chi tiết.
Good:
def add_values[arr]: """ Add the values in ``arr``. This is equivalent to Python ``sum`` of :meth:`pandas.Series.sum`. Some sections are omitted here for simplicity. """ return sum[arr]
Bad:
def func[]: """Some function. With several mistakes in the docstring. It has a blank like after the signature ``def func[]:``. The text 'Some function' should go in the line after the opening quotes of the docstring, not in the same line. There is a blank line between the docstring and the first line of code ``foo = 1``. The closing quotes should be in the next line, not in this one.""" foo = 1 bar = 2 return foo + bar
Phần 1: Tóm tắt ngắn#
Tóm tắt ngắn là một câu duy nhất thể hiện những gì chức năng làm một cách ngắn gọn.
Tóm tắt ngắn phải bắt đầu bằng một chữ cái viết hoa, kết thúc bằng một dấu chấm và vừa với một dòng. Nó cần thể hiện những gì đối tượng làm mà không cung cấp chi tiết. Đối với các chức năng và phương pháp, tóm tắt ngắn phải bắt đầu với một động từ nguyên bản.
Good:
def astype[dtype]: """ Cast Series type. This section will provide further details. """ pass
Bad:
def astype[dtype]: """ Casts Series type. Verb in third-person of the present simple, should be infinitive. """ pass
def astype[dtype]: """ Method to cast Series type. Does not start with verb. """ pass
def astype[dtype]: """ Cast Series type Missing dot at the end. """ pass
def astype[dtype]: """ Cast Series type from its current type to the new type defined in the parameter dtype. Summary is too verbose and doesn't fit in a single line. """ pass
Phần 2: Tóm tắt mở rộng#
Tóm tắt mở rộng cung cấp chi tiết về những gì chức năng làm. Nó không nên đi vào các chi tiết của các tham số, hoặc thảo luận về các ghi chú thực hiện, trong các phần khác.
Một dòng trống được để lại giữa bản tóm tắt ngắn và tóm tắt mở rộng. Mỗi đoạn trong bản tóm tắt mở rộng kết thúc bằng một dấu chấm.
Tóm tắt mở rộng sẽ cung cấp chi tiết về lý do tại sao chức năng này hữu ích và các trường hợp sử dụng của chúng, nếu nó không quá chung chung.
"""This should fail because Tuple is not imported, but no warning"""
def that[a, b, c]:
"""
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
"""
This only partially works because 'Foo' isn't in scope.
This can easily happen if someone refactored the name of 'Foo',
but the comments wouldn't be updated.
"""
def thing[a, b, c]:
"""
:rtype: [int, float, Foo]
"""
return a, b, c
def documentation_nightmare[a, b, c]:
"""
There are no checks, and everything seems fine, but the documentation is all wrong and hard to maintain.
:param a: A
:type a: float
:param b: B
:type b: Bar
:param c: C
:type c: int
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
0Phần 3: Tham số#
Các chi tiết của các tham số sẽ được thêm vào trong phần này. Phần này có tiêu đề Các tham số của người dùng, theo sau là một dòng có dấu gạch nối dưới mỗi chữ cái của từ tham số. Một dòng trống được để lại trước tiêu đề phần, nhưng không phải sau đó, chứ không phải giữa dòng với từ ngữ tham số, và phần có dấu gạch nối.
Sau tiêu đề, mỗi tham số trong chữ ký phải được ghi lại, bao gồm
def func[]: """Some function. With several mistakes in the docstring. It has a blank like after the signature ``def func[]:``. The text 'Some function' should go in the line after the opening quotes of the docstring, not in the same line. There is a blank line between the docstring and the first line of code ``foo = 1``. The closing quotes should be in the next line, not in this one.""" foo = 1 bar = 2 return foo + bar3 và
def func[]: """Some function. With several mistakes in the docstring. It has a blank like after the signature ``def func[]:``. The text 'Some function' should go in the line after the opening quotes of the docstring, not in the same line. There is a blank line between the docstring and the first line of code ``foo = 1``. The closing quotes should be in the next line, not in this one.""" foo = 1 bar = 2 return foo + bar4, nhưng không phải
def func[]: """Some function. With several mistakes in the docstring. It has a blank like after the signature ``def func[]:``. The text 'Some function' should go in the line after the opening quotes of the docstring, not in the same line. There is a blank line between the docstring and the first line of code ``foo = 1``. The closing quotes should be in the next line, not in this one.""" foo = 1 bar = 2 return foo + bar5.
Các tham số được xác định bởi tên của chúng, theo sau là một không gian, dấu hai chấm, không gian khác và loại [hoặc loại]. Lưu ý rằng không gian giữa tên và đại tràng là quan trọng. Các loại không được xác định cho
def func[]: """Some function. With several mistakes in the docstring. It has a blank like after the signature ``def func[]:``. The text 'Some function' should go in the line after the opening quotes of the docstring, not in the same line. There is a blank line between the docstring and the first line of code ``foo = 1``. The closing quotes should be in the next line, not in this one.""" foo = 1 bar = 2 return foo + bar3 và
def func[]: """Some function. With several mistakes in the docstring. It has a blank like after the signature ``def func[]:``. The text 'Some function' should go in the line after the opening quotes of the docstring, not in the same line. There is a blank line between the docstring and the first line of code ``foo = 1``. The closing quotes should be in the next line, not in this one.""" foo = 1 bar = 2 return foo + bar4, nhưng phải được xác định cho tất cả các tham số khác. Sau khi định nghĩa tham số, bắt buộc phải có một dòng với mô tả tham số, được thụt vào và có thể có nhiều dòng. Mô tả phải bắt đầu bằng một chữ cái viết hoa, và kết thúc với một dấu chấm.
Đối với các đối số từ khóa có giá trị mặc định, mặc định sẽ được liệt kê sau dấu phẩy ở cuối loại. Hình thức chính xác của loại trong trường hợp này sẽ là Int Int, mặc định 0. Trong một số trường hợp, có thể hữu ích để giải thích ý nghĩa của đối số mặc định, có thể được thêm vào sau dấu phẩy Int Int, mặc định -1, có nghĩa là tất cả CPU.
Trong trường hợp giá trị mặc định là
def func[]: """Some function. With several mistakes in the docstring. It has a blank like after the signature ``def func[]:``. The text 'Some function' should go in the line after the opening quotes of the docstring, not in the same line. There is a blank line between the docstring and the first line of code ``foo = 1``. The closing quotes should be in the next line, not in this one.""" foo = 1 bar = 2 return foo + bar8, có nghĩa là giá trị sẽ không được sử dụng. Thay vì
def func[]: """Some function. With several mistakes in the docstring. It has a blank like after the signature ``def func[]:``. The text 'Some function' should go in the line after the opening quotes of the docstring, not in the same line. There is a blank line between the docstring and the first line of code ``foo = 1``. The closing quotes should be in the next line, not in this one.""" foo = 1 bar = 2 return foo + bar9, nó được ưa thích để viết
def astype[dtype]: """ Cast Series type. This section will provide further details. """ pass0. Khi
def func[]: """Some function. With several mistakes in the docstring. It has a blank like after the signature ``def func[]:``. The text 'Some function' should go in the line after the opening quotes of the docstring, not in the same line. There is a blank line between the docstring and the first line of code ``foo = 1``. The closing quotes should be in the next line, not in this one.""" foo = 1 bar = 2 return foo + bar8 là một giá trị đang được sử dụng, chúng tôi sẽ giữ biểu mẫu STR, mặc định không có gì. Ví dụ, trong
def astype[dtype]: """ Cast Series type. This section will provide further details. """ pass2,
def func[]: """Some function. With several mistakes in the docstring. It has a blank like after the signature ``def func[]:``. The text 'Some function' should go in the line after the opening quotes of the docstring, not in the same line. There is a blank line between the docstring and the first line of code ``foo = 1``. The closing quotes should be in the next line, not in this one.""" foo = 1 bar = 2 return foo + bar8 không phải là một giá trị đang được sử dụng, nhưng có nghĩa là nén là tùy chọn và không có nén nào được sử dụng nếu không được cung cấp. Trong trường hợp này, chúng tôi sẽ sử dụng
def astype[dtype]: """ Cast Series type. This section will provide further details. """ pass0. Chỉ trong các trường hợp như
def astype[dtype]: """ Cast Series type. This section will provide further details. """ pass5 và
def func[]: """Some function. With several mistakes in the docstring. It has a blank like after the signature ``def func[]:``. The text 'Some function' should go in the line after the opening quotes of the docstring, not in the same line. There is a blank line between the docstring and the first line of code ``foo = 1``. The closing quotes should be in the next line, not in this one.""" foo = 1 bar = 2 return foo + bar8 mới được sử dụng theo cách tương tự như
def astype[dtype]: """ Cast Series type. This section will provide further details. """ pass7 hoặc
def astype[dtype]: """ Cast Series type. This section will provide further details. """ pass8 mới được sử dụng, thì chúng tôi sẽ chỉ định STR, int hoặc không, mặc định không có gì.
Good:
"""This should fail because Tuple is not imported, but no warning"""
def that[a, b, c]:
"""
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
"""
This only partially works because 'Foo' isn't in scope.
This can easily happen if someone refactored the name of 'Foo',
but the comments wouldn't be updated.
"""
def thing[a, b, c]:
"""
:rtype: [int, float, Foo]
"""
return a, b, c
def documentation_nightmare[a, b, c]:
"""
There are no checks, and everything seems fine, but the documentation is all wrong and hard to maintain.
:param a: A
:type a: float
:param b: B
:type b: Bar
:param c: C
:type c: int
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
1Bad:
"""This should fail because Tuple is not imported, but no warning"""
def that[a, b, c]:
"""
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
"""
This only partially works because 'Foo' isn't in scope.
This can easily happen if someone refactored the name of 'Foo',
but the comments wouldn't be updated.
"""
def thing[a, b, c]:
"""
:rtype: [int, float, Foo]
"""
return a, b, c
def documentation_nightmare[a, b, c]:
"""
There are no checks, and everything seems fine, but the documentation is all wrong and hard to maintain.
:param a: A
:type a: float
:param b: B
:type b: Bar
:param c: C
:type c: int
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
2Các loại tham số#
Khi chỉ định các loại tham số, các loại dữ liệu tích hợp Python có thể được sử dụng trực tiếp [loại Python được ưu tiên hơn chuỗi dài dòng, Integer, Boolean, v.v.]:
int
float
str
bool
Đối với các loại phức tạp, xác định các phân nhóm. Đối với
def astype[dtype]: """ Cast Series type. This section will provide further details. """ pass9 và
def astype[dtype]: """ Casts Series type. Verb in third-person of the present simple, should be infinitive. """ pass0, vì có nhiều loại có mặt, chúng tôi sử dụng giá đỡ để giúp đọc loại [dấu ngoặc xoăn cho
def astype[dtype]: """ Cast Series type. This section will provide further details. """ pass9 và dấu ngoặc thông thường cho
def astype[dtype]: """ Casts Series type. Verb in third-person of the present simple, should be infinitive. """ pass0]:
Danh sách int
Dict của {str: int}
Tuple of [str, int, int]
Tuple of [str,]
Bộ Str
Trong trường hợp chỉ có một tập hợp các giá trị được phép, hãy liệt kê chúng trong các dấu ngoặc xoăn và được phân tách bằng dấu phẩy [tiếp theo là một không gian]. Nếu các giá trị là thứ tự và chúng có một đơn đặt hàng, hãy liệt kê chúng theo thứ tự này. Nếu không, hãy liệt kê giá trị mặc định trước tiên, nếu có:
{0, 10, 25}
{’Đơn giản,’ nâng cao}
{'thấp trung bình cao'}
{‘Cat,‘ Dog, ‘Bird,}
Nếu loại được xác định trong mô -đun Python, mô -đun phải được chỉ định:
datetime.date
datetime.datetime
decimal.Decimal
Nếu loại nằm trong một gói, mô -đun cũng phải được chỉ định:
numpy.ndarray
scipy.sparse.coo_matrix
Nếu loại là loại gấu trúc, cũng chỉ định gấu trúc ngoại trừ loạt và dataFrame:
Loạt
Khung dữ liệu
-
pandas.Index
pandas.Categorical
pandas.arrays.SparseArray
Nếu loại chính xác không liên quan, nhưng phải tương thích với một mảng numpy, giống như mảng có thể được chỉ định. Nếu bất kỳ loại nào có thể được lặp lại được chấp nhận, có thể sử dụng ITEBLE:
array-like
Có thể lặp lại
Nếu có nhiều loại được chấp nhận, hãy tách chúng ra bằng dấu phẩy, ngoại trừ hai loại cuối cùng, cần phải được phân tách bằng từ ‘hoặc Hồi::
int hoặc float
phao, thập phân.decimal hoặc không
STR hoặc danh sách STR
Nếu
def func[]: """Some function. With several mistakes in the docstring. It has a blank like after the signature ``def func[]:``. The text 'Some function' should go in the line after the opening quotes of the docstring, not in the same line. There is a blank line between the docstring and the first line of code ``foo = 1``. The closing quotes should be in the next line, not in this one.""" foo = 1 bar = 2 return foo + bar8 là một trong những giá trị được chấp nhận, thì nó luôn cần phải là người cuối cùng trong danh sách.
Đối với trục, quy ước là sử dụng một cái gì đó như:
trục: {0 hoặc ‘index, 1 hoặc‘ cột, không}, không có mặc định
Phần 4: Trả về hoặc sản lượng#
Nếu phương thức trả về một giá trị, nó sẽ được ghi lại trong phần này. Ngoài ra nếu phương thức mang lại đầu ra của nó.
Tiêu đề của phần sẽ được xác định theo cách tương tự như các tham số của Google. Với các tên, trả về, hay, người khác đã tạo ra một dòng có nhiều dấu gạch ngang như các chữ cái trong từ trước.
Tài liệu về lợi nhuận cũng tương tự như các tham số. Nhưng trong trường hợp này, sẽ không có tên nào được cung cấp, trừ khi phương thức trả về hoặc mang lại nhiều hơn một giá trị [một bộ giá trị].
Các loại cho các loại trả về trên mạng và các sản phẩm của người khác, giống như các loại đối với các tham số của các tham số. Ngoài ra, mô tả phải kết thúc với một dấu chấm.
Ví dụ: với một giá trị duy nhất:
"""This should fail because Tuple is not imported, but no warning"""
def that[a, b, c]:
"""
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
"""
This only partially works because 'Foo' isn't in scope.
This can easily happen if someone refactored the name of 'Foo',
but the comments wouldn't be updated.
"""
def thing[a, b, c]:
"""
:rtype: [int, float, Foo]
"""
return a, b, c
def documentation_nightmare[a, b, c]:
"""
There are no checks, and everything seems fine, but the documentation is all wrong and hard to maintain.
:param a: A
:type a: float
:param b: B
:type b: Bar
:param c: C
:type c: int
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
3Với nhiều hơn một giá trị:
"""This should fail because Tuple is not imported, but no warning"""
def that[a, b, c]:
"""
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
"""
This only partially works because 'Foo' isn't in scope.
This can easily happen if someone refactored the name of 'Foo',
but the comments wouldn't be updated.
"""
def thing[a, b, c]:
"""
:rtype: [int, float, Foo]
"""
return a, b, c
def documentation_nightmare[a, b, c]:
"""
There are no checks, and everything seems fine, but the documentation is all wrong and hard to maintain.
:param a: A
:type a: float
:param b: B
:type b: Bar
:param c: C
:type c: int
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
4Nếu phương thức mang lại giá trị của nó:
"""This should fail because Tuple is not imported, but no warning"""
def that[a, b, c]:
"""
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
"""
This only partially works because 'Foo' isn't in scope.
This can easily happen if someone refactored the name of 'Foo',
but the comments wouldn't be updated.
"""
def thing[a, b, c]:
"""
:rtype: [int, float, Foo]
"""
return a, b, c
def documentation_nightmare[a, b, c]:
"""
There are no checks, and everything seems fine, but the documentation is all wrong and hard to maintain.
:param a: A
:type a: float
:param b: B
:type b: Bar
:param c: C
:type c: int
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
5Phần 5: Xem thêm#
Phần này được sử dụng để cho người dùng biết về chức năng gấu trúc liên quan đến phần được ghi lại. Trong các trường hợp hiếm hoi, nếu không có phương pháp hoặc chức năng liên quan nào có thể được tìm thấy, phần này có thể được bỏ qua.
Một ví dụ rõ ràng sẽ là các phương pháp
def astype[dtype]: """ Casts Series type. Verb in third-person of the present simple, should be infinitive. """ pass4 và
def astype[dtype]: """ Casts Series type. Verb in third-person of the present simple, should be infinitive. """ pass5. Vì
def astype[dtype]: """ Casts Series type. Verb in third-person of the present simple, should be infinitive. """ pass5 thực hiện tương đương với
def astype[dtype]: """ Casts Series type. Verb in third-person of the present simple, should be infinitive. """ pass4 nhưng vào cuối
def func[]: """Some function. With several mistakes in the docstring. It has a blank like after the signature ``def func[]:``. The text 'Some function' should go in the line after the opening quotes of the docstring, not in the same line. There is a blank line between the docstring and the first line of code ``foo = 1``. The closing quotes should be in the next line, not in this one.""" foo = 1 bar = 2 return foo + bar2 hoặc
def astype[dtype]: """ Casts Series type. Verb in third-person of the present simple, should be infinitive. """ pass9 thay vì lúc đầu, thật tốt khi cho người dùng biết về nó.
Để đưa ra trực giác về những gì có thể được xem xét liên quan, ở đây có một số ví dụ:
def astype[dtype]: """ Method to cast Series type. Does not start with verb. """ pass
0 vàdef astype[dtype]: """ Method to cast Series type. Does not start with verb. """ pass
1, vì chúng cũng làm như vậy, nhưng trong một trường hợp cung cấp các chỉ số và ở các vị trí khácdef astype[dtype]: """ Method to cast Series type. Does not start with verb. """ pass
2 vàdef astype[dtype]: """ Method to cast Series type. Does not start with verb. """ pass
3, khi họ làm điều ngược lạidef astype[dtype]: """ Method to cast Series type. Does not start with verb. """ pass
4,def astype[dtype]: """ Method to cast Series type. Does not start with verb. """ pass
5 vàdef astype[dtype]: """ Method to cast Series type. Does not start with verb. """ pass
6, vì thật dễ dàng là người dùng tìm kiếm phương thức lặp lại các cột kết thúc theo phương thức để lặp lại trên các hàng và ngược lạidef astype[dtype]: """ Method to cast Series type. Does not start with verb. """ pass
7 vàdef astype[dtype]: """ Method to cast Series type. Does not start with verb. """ pass
8, vì cả hai phương thức đều được sử dụng để xử lý các giá trị bị thiếudef astype[dtype]: """ Method to cast Series type. Does not start with verb. """ pass
9 vàdef astype[dtype]: """ Cast Series type Missing dot at the end. """ pass
0, vì chúng là bổ sungdef astype[dtype]: """ Cast Series type Missing dot at the end. """ pass
1 vàdef astype[dtype]: """ Cast Series type Missing dot at the end. """ pass
2, vì một người là một khái quát của người kiadef astype[dtype]: """ Cast Series type Missing dot at the end. """ pass
3 vàdef astype[dtype]: """ Cast Series type Missing dot at the end. """ pass
4, vì người dùng có thể đang đọc tài liệu củadef astype[dtype]: """ Cast Series type Missing dot at the end. """ pass
3 để biết cách diễn ra như một ngày và cách thực hiện nó là vớidef astype[dtype]: """ Cast Series type Missing dot at the end. """ pass
4def astype[dtype]: """ Cast Series type Missing dot at the end. """ pass
7 có liên quan đếndef astype[dtype]: """ Cast Series type Missing dot at the end. """ pass
8, vì chức năng của nó dựa trên nó
Khi quyết định những gì có liên quan, bạn chủ yếu nên sử dụng ý thức chung của mình và suy nghĩ về những gì có thể hữu ích cho người dùng đọc tài liệu, đặc biệt là những người ít kinh nghiệm hơn.
Khi liên quan đến các thư viện khác [chủ yếu là
def astype[dtype]: """ Cast Series type Missing dot at the end. """ pass9], trước tiên hãy sử dụng tên của mô -đun [không phải là bí danh như
def astype[dtype]: """ Cast Series type from its current type to the new type defined in the parameter dtype. Summary is too verbose and doesn't fit in a single line. """ pass0]. Nếu hàm nằm trong một mô -đun không phải là chính, như
def astype[dtype]: """ Cast Series type from its current type to the new type defined in the parameter dtype. Summary is too verbose and doesn't fit in a single line. """ pass1, hãy liệt kê mô -đun đầy đủ [ví dụ:
def astype[dtype]: """ Cast Series type from its current type to the new type defined in the parameter dtype. Summary is too verbose and doesn't fit in a single line. """ pass2].
Phần này có một tiêu đề, còn xem thêm [lưu ý vốn S và A], theo sau là dòng có dấu gạch nối và đi trước một dòng trống.
Sau khi tiêu đề, chúng tôi sẽ thêm một dòng cho từng phương pháp hoặc hàm liên quan, theo sau là một không gian, một dấu hai chấm, một không gian khác và một mô tả ngắn minh họa phương pháp hoặc chức năng này, tại sao nó có liên quan trong bối cảnh này và những gì Sự khác biệt chính là giữa hàm được ghi lại và một được tham chiếu. Mô tả cũng phải kết thúc bằng một dấu chấm.
Lưu ý rằng trong các trò chơi trả về của người Hồi giáo và các sản phẩm của người khác, mô tả được đặt trên dòng sau loại. Tuy nhiên, trong phần này, nó nằm trên cùng một dòng, với một dấu hai chấm ở giữa. Nếu mô tả không phù hợp với cùng một dòng, nó có thể tiếp tục vào các dòng khác phải được thụt thêm.
Ví dụ:
"""This should fail because Tuple is not imported, but no warning"""
def that[a, b, c]:
"""
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
"""
This only partially works because 'Foo' isn't in scope.
This can easily happen if someone refactored the name of 'Foo',
but the comments wouldn't be updated.
"""
def thing[a, b, c]:
"""
:rtype: [int, float, Foo]
"""
return a, b, c
def documentation_nightmare[a, b, c]:
"""
There are no checks, and everything seems fine, but the documentation is all wrong and hard to maintain.
:param a: A
:type a: float
:param b: B
:type b: Bar
:param c: C
:type c: int
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
6Phần 6: Ghi chú#
Đây là một phần tùy chọn được sử dụng cho các ghi chú về việc thực hiện thuật toán hoặc để ghi lại các khía cạnh kỹ thuật của hành vi chức năng.
Hãy bỏ qua nó, trừ khi bạn quen thuộc với việc thực hiện thuật toán hoặc bạn khám phá một số hành vi phản trực quan trong khi viết các ví dụ cho hàm.
Phần này theo định dạng giống như phần tóm tắt mở rộng.
Phần 7: Ví dụ#
Đây là một trong những phần quan trọng nhất của một tài liệu, mặc dù được đặt ở vị trí cuối cùng, vì thường mọi người hiểu các khái niệm tốt hơn bằng ví dụ hơn là thông qua các giải thích chính xác.
Các ví dụ trong tài liệu, bên cạnh việc minh họa việc sử dụng hàm hoặc phương thức, phải là mã Python hợp lệ, trả về đầu ra đã cho theo cách xác định và có thể được sao chép và chạy bởi người dùng.
Các ví dụ được trình bày như một phiên trong Terminal Python.
def astype[dtype]: """ Cast Series type from its current type to the new type defined in the parameter dtype. Summary is too verbose and doesn't fit in a single line. """ pass3 được sử dụng để hiện tại mã.
def astype[dtype]: """ Cast Series type from its current type to the new type defined in the parameter dtype. Summary is too verbose and doesn't fit in a single line. """ pass4 được sử dụng để mã tiếp tục từ dòng trước. Đầu ra được trình bày ngay sau dòng mã cuối cùng tạo ra đầu ra [không có dòng trống ở giữa]. Nhận xét mô tả các ví dụ có thể được thêm vào với các dòng trống trước và sau chúng.
Cách để trình bày các ví dụ như sau:
Nhập thư viện bắt buộc [trừ
def astype[dtype]: """ Cast Series type Missing dot at the end. """ pass
9 vàdef astype[dtype]: """ Cast Series type from its current type to the new type defined in the parameter dtype. Summary is too verbose and doesn't fit in a single line. """ pass
6]Tạo dữ liệu cần thiết cho ví dụ
Hiển thị một ví dụ rất cơ bản đưa ra ý tưởng về trường hợp sử dụng phổ biến nhất
Thêm các ví dụ với các giải thích minh họa cách các tham số có thể được sử dụng cho chức năng mở rộng
Một ví dụ đơn giản có thể là:
"""This should fail because Tuple is not imported, but no warning"""
def that[a, b, c]:
"""
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
"""
This only partially works because 'Foo' isn't in scope.
This can easily happen if someone refactored the name of 'Foo',
but the comments wouldn't be updated.
"""
def thing[a, b, c]:
"""
:rtype: [int, float, Foo]
"""
return a, b, c
def documentation_nightmare[a, b, c]:
"""
There are no checks, and everything seems fine, but the documentation is all wrong and hard to maintain.
:param a: A
:type a: float
:param b: B
:type b: Bar
:param c: C
:type c: int
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
7Các ví dụ nên ngắn gọn nhất có thể. Trong trường hợp sự phức tạp của hàm yêu cầu các ví dụ dài, được khuyến nghị sử dụng các khối có tiêu đề in đậm. Sử dụng Double Star
def astype[dtype]: """ Cast Series type from its current type to the new type defined in the parameter dtype. Summary is too verbose and doesn't fit in a single line. """ pass7 để tạo văn bản đậm, như trong
def astype[dtype]: """ Cast Series type from its current type to the new type defined in the parameter dtype. Summary is too verbose and doesn't fit in a single line. """ pass8.
Các quy ước cho các ví dụ#
Mã trong các ví dụ được giả định luôn bắt đầu với hai dòng không được hiển thị:
"""This should fail because Tuple is not imported, but no warning"""
def that[a, b, c]:
"""
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
"""
This only partially works because 'Foo' isn't in scope.
This can easily happen if someone refactored the name of 'Foo',
but the comments wouldn't be updated.
"""
def thing[a, b, c]:
"""
:rtype: [int, float, Foo]
"""
return a, b, c
def documentation_nightmare[a, b, c]:
"""
There are no checks, and everything seems fine, but the documentation is all wrong and hard to maintain.
:param a: A
:type a: float
:param b: B
:type b: Bar
:param c: C
:type c: int
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
8Bất kỳ mô -đun nào khác được sử dụng trong các ví dụ phải được nhập rõ ràng, một dòng trên mỗi dòng [như được khuyến nghị trong nhập khẩu PEP 8#] và tránh các bí danh. Tránh nhập khẩu quá mức, nhưng nếu cần, nhập từ thư viện tiêu chuẩn đi trước, tiếp theo là các thư viện của bên thứ ba [như matplotlib].PEP 8#imports] and avoiding aliases. Avoid excessive imports, but if needed, imports from the standard library go first, followed by third-party libraries [like matplotlib].
Khi minh họa các ví dụ với một
def func[]: """Some function. With several mistakes in the docstring. It has a blank like after the signature ``def func[]:``. The text 'Some function' should go in the line after the opening quotes of the docstring, not in the same line. There is a blank line between the docstring and the first line of code ``foo = 1``. The closing quotes should be in the next line, not in this one.""" foo = 1 bar = 2 return foo + bar2, hãy sử dụng tên
"""This should fail because Tuple is not imported, but no warning"""
def that[a, b, c]:
"""
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
"""
This only partially works because 'Foo' isn't in scope.
This can easily happen if someone refactored the name of 'Foo',
but the comments wouldn't be updated.
"""
def thing[a, b, c]:
"""
:rtype: [int, float, Foo]
"""
return a, b, c
def documentation_nightmare[a, b, c]:
"""
There are no checks, and everything seems fine, but the documentation is all wrong and hard to maintain.
:param a: A
:type a: float
:param b: B
:type b: Bar
:param c: C
:type c: int
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
00 và nếu minh họa với một def astype[dtype]: """ Casts Series type. Verb in third-person of the present simple, should be infinitive. """ pass9, hãy sử dụng tên
"""This should fail because Tuple is not imported, but no warning"""
def that[a, b, c]:
"""
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
"""
This only partially works because 'Foo' isn't in scope.
This can easily happen if someone refactored the name of 'Foo',
but the comments wouldn't be updated.
"""
def thing[a, b, c]:
"""
:rtype: [int, float, Foo]
"""
return a, b, c
def documentation_nightmare[a, b, c]:
"""
There are no checks, and everything seems fine, but the documentation is all wrong and hard to maintain.
:param a: A
:type a: float
:param b: B
:type b: Bar
:param c: C
:type c: int
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
02. Đối với các chỉ số, """This should fail because Tuple is not imported, but no warning"""
def that[a, b, c]:
"""
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
"""
This only partially works because 'Foo' isn't in scope.
This can easily happen if someone refactored the name of 'Foo',
but the comments wouldn't be updated.
"""
def thing[a, b, c]:
"""
:rtype: [int, float, Foo]
"""
return a, b, c
def documentation_nightmare[a, b, c]:
"""
There are no checks, and everything seems fine, but the documentation is all wrong and hard to maintain.
:param a: A
:type a: float
:param b: B
:type b: Bar
:param c: C
:type c: int
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
03 là tên ưa thích. Một tập hợp đồng nhất def func[]: """Some function. With several mistakes in the docstring. It has a blank like after the signature ``def func[]:``. The text 'Some function' should go in the line after the opening quotes of the docstring, not in the same line. There is a blank line between the docstring and the first line of code ``foo = 1``. The closing quotes should be in the next line, not in this one.""" foo = 1 bar = 2 return foo + bar2 hoặc
def astype[dtype]: """ Casts Series type. Verb in third-person of the present simple, should be infinitive. """ pass9 được sử dụng, hãy đặt tên cho chúng
"""This should fail because Tuple is not imported, but no warning"""
def that[a, b, c]:
"""
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
"""
This only partially works because 'Foo' isn't in scope.
This can easily happen if someone refactored the name of 'Foo',
but the comments wouldn't be updated.
"""
def thing[a, b, c]:
"""
:rtype: [int, float, Foo]
"""
return a, b, c
def documentation_nightmare[a, b, c]:
"""
There are no checks, and everything seems fine, but the documentation is all wrong and hard to maintain.
:param a: A
:type a: float
:param b: B
:type b: Bar
:param c: C
:type c: int
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
06, """This should fail because Tuple is not imported, but no warning"""
def that[a, b, c]:
"""
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
"""
This only partially works because 'Foo' isn't in scope.
This can easily happen if someone refactored the name of 'Foo',
but the comments wouldn't be updated.
"""
def thing[a, b, c]:
"""
:rtype: [int, float, Foo]
"""
return a, b, c
def documentation_nightmare[a, b, c]:
"""
There are no checks, and everything seems fine, but the documentation is all wrong and hard to maintain.
:param a: A
:type a: float
:param b: B
:type b: Bar
:param c: C
:type c: int
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
07, ________ 108, hoặc """This should fail because Tuple is not imported, but no warning"""
def that[a, b, c]:
"""
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
"""
This only partially works because 'Foo' isn't in scope.
This can easily happen if someone refactored the name of 'Foo',
but the comments wouldn't be updated.
"""
def thing[a, b, c]:
"""
:rtype: [int, float, Foo]
"""
return a, b, c
def documentation_nightmare[a, b, c]:
"""
There are no checks, and everything seems fine, but the documentation is all wrong and hard to maintain.
:param a: A
:type a: float
:param b: B
:type b: Bar
:param c: C
:type c: int
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
09, """This should fail because Tuple is not imported, but no warning"""
def that[a, b, c]:
"""
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
"""
This only partially works because 'Foo' isn't in scope.
This can easily happen if someone refactored the name of 'Foo',
but the comments wouldn't be updated.
"""
def thing[a, b, c]:
"""
:rtype: [int, float, Foo]
"""
return a, b, c
def documentation_nightmare[a, b, c]:
"""
There are no checks, and everything seems fine, but the documentation is all wrong and hard to maintain.
:param a: A
:type a: float
:param b: B
:type b: Bar
:param c: C
:type c: int
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
10, ________ 111 và """This should fail because Tuple is not imported, but no warning"""
def that[a, b, c]:
"""
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
"""
This only partially works because 'Foo' isn't in scope.
This can easily happen if someone refactored the name of 'Foo',
but the comments wouldn't be updated.
"""
def thing[a, b, c]:
"""
:rtype: [int, float, Foo]
"""
return a, b, c
def documentation_nightmare[a, b, c]:
"""
There are no checks, and everything seems fine, but the documentation is all wrong and hard to maintain.
:param a: A
:type a: float
:param b: B
:type b: Bar
:param c: C
:type c: int
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
13.Dữ liệu được sử dụng trong ví dụ phải nhỏ gọn nhất có thể. Số lượng hàng được khuyến nghị là khoảng 4, nhưng làm cho nó trở thành một số có ý nghĩa cho ví dụ cụ thể. Ví dụ: trong phương thức
"""This should fail because Tuple is not imported, but no warning"""
def that[a, b, c]:
"""
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
"""
This only partially works because 'Foo' isn't in scope.
This can easily happen if someone refactored the name of 'Foo',
but the comments wouldn't be updated.
"""
def thing[a, b, c]:
"""
:rtype: [int, float, Foo]
"""
return a, b, c
def documentation_nightmare[a, b, c]:
"""
There are no checks, and everything seems fine, but the documentation is all wrong and hard to maintain.
:param a: A
:type a: float
:param b: B
:type b: Bar
:param c: C
:type c: int
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
14, nó yêu cầu cao hơn 5, để hiển thị ví dụ với các giá trị mặc định. Nếu thực hiện """This should fail because Tuple is not imported, but no warning"""
def that[a, b, c]:
"""
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
"""
This only partially works because 'Foo' isn't in scope.
This can easily happen if someone refactored the name of 'Foo',
but the comments wouldn't be updated.
"""
def thing[a, b, c]:
"""
:rtype: [int, float, Foo]
"""
return a, b, c
def documentation_nightmare[a, b, c]:
"""
There are no checks, and everything seems fine, but the documentation is all wrong and hard to maintain.
:param a: A
:type a: float
:param b: B
:type b: Bar
:param c: C
:type c: int
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
15, chúng ta có thể sử dụng một cái gì đó như """This should fail because Tuple is not imported, but no warning"""
def that[a, b, c]:
"""
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
"""
This only partially works because 'Foo' isn't in scope.
This can easily happen if someone refactored the name of 'Foo',
but the comments wouldn't be updated.
"""
def thing[a, b, c]:
"""
:rtype: [int, float, Foo]
"""
return a, b, c
def documentation_nightmare[a, b, c]:
"""
There are no checks, and everything seems fine, but the documentation is all wrong and hard to maintain.
:param a: A
:type a: float
:param b: B
:type b: Bar
:param c: C
:type c: int
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
16, vì vậy thật dễ dàng để thấy rằng giá trị được trả về là giá trị trung bình.Để biết các ví dụ phức tạp hơn [ví dụ nhóm], tránh sử dụng dữ liệu mà không cần diễn giải, như ma trận các số ngẫu nhiên có cột A, B, C, D, và thay vào đó sử dụng một ví dụ có ý nghĩa, giúp dễ hiểu khái niệm này dễ dàng hơn. Trừ khi được yêu cầu bởi ví dụ, sử dụng tên của động vật, để giữ cho các ví dụ nhất quán. Và tính chất số của chúng.
Khi gọi phương thức, các từ khóa đối số
"""This should fail because Tuple is not imported, but no warning"""
def that[a, b, c]:
"""
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
"""
This only partially works because 'Foo' isn't in scope.
This can easily happen if someone refactored the name of 'Foo',
but the comments wouldn't be updated.
"""
def thing[a, b, c]:
"""
:rtype: [int, float, Foo]
"""
return a, b, c
def documentation_nightmare[a, b, c]:
"""
There are no checks, and everything seems fine, but the documentation is all wrong and hard to maintain.
:param a: A
:type a: float
:param b: B
:type b: Bar
:param c: C
:type c: int
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
17 được ưu tiên cho các đối số vị trí """This should fail because Tuple is not imported, but no warning"""
def that[a, b, c]:
"""
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
"""
This only partially works because 'Foo' isn't in scope.
This can easily happen if someone refactored the name of 'Foo',
but the comments wouldn't be updated.
"""
def thing[a, b, c]:
"""
:rtype: [int, float, Foo]
"""
return a, b, c
def documentation_nightmare[a, b, c]:
"""
There are no checks, and everything seems fine, but the documentation is all wrong and hard to maintain.
:param a: A
:type a: float
:param b: B
:type b: Bar
:param c: C
:type c: int
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
18.Good:
"""This should fail because Tuple is not imported, but no warning"""
def that[a, b, c]:
"""
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
"""
This only partially works because 'Foo' isn't in scope.
This can easily happen if someone refactored the name of 'Foo',
but the comments wouldn't be updated.
"""
def thing[a, b, c]:
"""
:rtype: [int, float, Foo]
"""
return a, b, c
def documentation_nightmare[a, b, c]:
"""
There are no checks, and everything seems fine, but the documentation is all wrong and hard to maintain.
:param a: A
:type a: float
:param b: B
:type b: Bar
:param c: C
:type c: int
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
9Bad:
def add[num1, num2]: """ Add up two integer numbers. This function simply wraps the ``+`` operator, and does not do anything interesting, except for illustrating what the docstring of a very simple function looks like. Parameters ---------- num1 : int First number to add. num2 : int Second number to add. Returns ------- int The sum of ``num1`` and ``num2``. See Also -------- subtract : Subtract one integer from another. Examples -------- >>> add[2, 2] 4 >>> add[25, 0] 25 >>> add[10, -10] 0 """ return num1 + num20
Mẹo để nhận các ví dụ của bạn vượt qua các tài liệu#
Nhận các ví dụ vượt qua các tài liệu trong tập lệnh xác thực đôi khi có thể là khó khăn. Dưới đây là một số điểm chú ý:
Nhập tất cả các thư viện cần thiết [ngoại trừ gấu trúc và numpy, chúng đã được nhập là
19 và"""This should fail because Tuple is not imported, but no warning""" def that[a, b, c]: """ :rtype: Tuple[int, float, Foo] """ return a, b, c """ This only partially works because 'Foo' isn't in scope. This can easily happen if someone refactored the name of 'Foo', but the comments wouldn't be updated. """ def thing[a, b, c]: """ :rtype: [int, float, Foo] """ return a, b, c def documentation_nightmare[a, b, c]: """ There are no checks, and everything seems fine, but the documentation is all wrong and hard to maintain. :param a: A :type a: float :param b: B :type b: Bar :param c: C :type c: int :rtype: Tuple[int, float, Foo] """ return a, b, c
20] và xác định tất cả các biến bạn sử dụng trong ví dụ."""This should fail because Tuple is not imported, but no warning""" def that[a, b, c]: """ :rtype: Tuple[int, float, Foo] """ return a, b, c """ This only partially works because 'Foo' isn't in scope. This can easily happen if someone refactored the name of 'Foo', but the comments wouldn't be updated. """ def thing[a, b, c]: """ :rtype: [int, float, Foo] """ return a, b, c def documentation_nightmare[a, b, c]: """ There are no checks, and everything seems fine, but the documentation is all wrong and hard to maintain. :param a: A :type a: float :param b: B :type b: Bar :param c: C :type c: int :rtype: Tuple[int, float, Foo] """ return a, b, c
Cố gắng tránh sử dụng dữ liệu ngẫu nhiên. Tuy nhiên, dữ liệu ngẫu nhiên có thể ổn trong một số trường hợp, như nếu hàm bạn đang ghi lại các giao dịch với phân phối xác suất hoặc nếu lượng dữ liệu cần thiết để làm cho hàm kết quả có ý nghĩa là quá nhiều, do đó việc tạo nó bằng tay rất cồng kềnh. Trong những trường hợp đó, luôn luôn sử dụng một hạt giống ngẫu nhiên cố định để làm cho các ví dụ được tạo ra có thể dự đoán được. Thí dụ:
def add[num1, num2]: """ Add up two integer numbers. This function simply wraps the ``+`` operator, and does not do anything interesting, except for illustrating what the docstring of a very simple function looks like. Parameters ---------- num1 : int First number to add. num2 : int Second number to add. Returns ------- int The sum of ``num1`` and ``num2``. See Also -------- subtract : Subtract one integer from another. Examples -------- >>> add[2, 2] 4 >>> add[25, 0] 25 >>> add[10, -10] 0 """ return num1 + num2
1Nếu bạn có đoạn mã kết thúc nhiều dòng, bạn cần sử dụng ‘, trên các dòng tiếp tục:
def add[num1, num2]: """ Add up two integer numbers. This function simply wraps the ``+`` operator, and does not do anything interesting, except for illustrating what the docstring of a very simple function looks like. Parameters ---------- num1 : int First number to add. num2 : int Second number to add. Returns ------- int The sum of ``num1`` and ``num2``. See Also -------- subtract : Subtract one integer from another. Examples -------- >>> add[2, 2] 4 >>> add[25, 0] 25 >>> add[10, -10] 0 """ return num1 + num2
2Nếu bạn muốn hiển thị một trường hợp được nêu ra ngoại lệ, bạn có thể làm:
def add[num1, num2]: """ Add up two integer numbers. This function simply wraps the ``+`` operator, and does not do anything interesting, except for illustrating what the docstring of a very simple function looks like. Parameters ---------- num1 : int First number to add. num2 : int Second number to add. Returns ------- int The sum of ``num1`` and ``num2``. See Also -------- subtract : Subtract one integer from another. Examples -------- >>> add[2, 2] 4 >>> add[25, 0] 25 >>> add[10, -10] 0 """ return num1 + num2
3Điều cần thiết là bao gồm TraceBack [cuộc gọi gần đây nhất cuối cùng]:, nhưng đối với lỗi thực tế, chỉ có tên lỗi là đủ.
Nếu có một phần nhỏ của kết quả có thể thay đổi [ví dụ: một hàm băm trong biểu diễn đối tượng], bạn có thể sử dụng
def astype[dtype]: """ Cast Series type from its current type to the new type defined in the parameter dtype. Summary is too verbose and doesn't fit in a single line. """ pass
4 để biểu diễn phần này.Nếu bạn muốn chỉ ra rằng
22 trả về một đối tượng matplotlib axessubplot, điều này sẽ thất bại"""This should fail because Tuple is not imported, but no warning""" def that[a, b, c]: """ :rtype: Tuple[int, float, Foo] """ return a, b, c """ This only partially works because 'Foo' isn't in scope. This can easily happen if someone refactored the name of 'Foo', but the comments wouldn't be updated. """ def thing[a, b, c]: """ :rtype: [int, float, Foo] """ return a, b, c def documentation_nightmare[a, b, c]: """ There are no checks, and everything seems fine, but the documentation is all wrong and hard to maintain. :param a: A :type a: float :param b: B :type b: Bar :param c: C :type c: int :rtype: Tuple[int, float, Foo] """ return a, b, c
def add[num1, num2]: """ Add up two integer numbers. This function simply wraps the ``+`` operator, and does not do anything interesting, except for illustrating what the docstring of a very simple function looks like. Parameters ---------- num1 : int First number to add. num2 : int Second number to add. Returns ------- int The sum of ``num1`` and ``num2``. See Also -------- subtract : Subtract one integer from another. Examples -------- >>> add[2, 2] 4 >>> add[25, 0] 25 >>> add[10, -10] 0 """ return num1 + num2
4Tuy nhiên, bạn có thể làm [chú ý nhận xét cần được thêm vào]
def add[num1, num2]: """ Add up two integer numbers. This function simply wraps the ``+`` operator, and does not do anything interesting, except for illustrating what the docstring of a very simple function looks like. Parameters ---------- num1 : int First number to add. num2 : int Second number to add. Returns ------- int The sum of ``num1`` and ``num2``. See Also -------- subtract : Subtract one integer from another. Examples -------- >>> add[2, 2] 4 >>> add[25, 0] 25 >>> add[10, -10] 0 """ return num1 + num2
5
Lô trong ví dụ#
Có một số phương pháp trong các lô trở lại gấu trúc. Để hiển thị các sơ đồ được tạo bởi các ví dụ trong tài liệu, chỉ thị
"""This should fail because Tuple is not imported, but no warning"""
def that[a, b, c]:
"""
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
"""
This only partially works because 'Foo' isn't in scope.
This can easily happen if someone refactored the name of 'Foo',
but the comments wouldn't be updated.
"""
def thing[a, b, c]:
"""
:rtype: [int, float, Foo]
"""
return a, b, c
def documentation_nightmare[a, b, c]:
"""
There are no checks, and everything seems fine, but the documentation is all wrong and hard to maintain.
:param a: A
:type a: float
:param b: B
:type b: Bar
:param c: C
:type c: int
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
23 tồn tại.Để sử dụng nó, hãy đặt mã tiếp theo sau tiêu đề của các ví dụ trên mạng như được hiển thị bên dưới. Cốt truyện sẽ được tạo tự động khi xây dựng tài liệu.
def add[num1, num2]: """ Add up two integer numbers. This function simply wraps the ``+`` operator, and does not do anything interesting, except for illustrating what the docstring of a very simple function looks like. Parameters ---------- num1 : int First number to add. num2 : int Second number to add. Returns ------- int The sum of ``num1`` and ``num2``. See Also -------- subtract : Subtract one integer from another. Examples -------- >>> add[2, 2] 4 >>> add[25, 0] 25 >>> add[10, -10] 0 """ return num1 + num26
Chia sẻ tài liệu#
Pandas có một hệ thống để chia sẻ các tài liệu, với các biến thể nhỏ, giữa các lớp. Điều này giúp chúng tôi giữ cho các tài liệu nhất quán, trong khi giữ cho mọi thứ rõ ràng cho người dùng đọc. Nó đến với chi phí của một số phức tạp khi viết.
Mỗi tài liệu được chia sẻ sẽ có một mẫu cơ sở với các biến, như
"""This should fail because Tuple is not imported, but no warning"""
def that[a, b, c]:
"""
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
"""
This only partially works because 'Foo' isn't in scope.
This can easily happen if someone refactored the name of 'Foo',
but the comments wouldn't be updated.
"""
def thing[a, b, c]:
"""
:rtype: [int, float, Foo]
"""
return a, b, c
def documentation_nightmare[a, b, c]:
"""
There are no checks, and everything seems fine, but the documentation is all wrong and hard to maintain.
:param a: A
:type a: float
:param b: B
:type b: Bar
:param c: C
:type c: int
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
24. Các biến được điền vào sau đó sử dụng trình trang trí """This should fail because Tuple is not imported, but no warning"""
def that[a, b, c]:
"""
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
"""
This only partially works because 'Foo' isn't in scope.
This can easily happen if someone refactored the name of 'Foo',
but the comments wouldn't be updated.
"""
def thing[a, b, c]:
"""
:rtype: [int, float, Foo]
"""
return a, b, c
def documentation_nightmare[a, b, c]:
"""
There are no checks, and everything seems fine, but the documentation is all wrong and hard to maintain.
:param a: A
:type a: float
:param b: B
:type b: Bar
:param c: C
:type c: int
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
25. Cuối cùng, Docstrings cũng có thể được thêm vào với nhà trang trí """This should fail because Tuple is not imported, but no warning"""
def that[a, b, c]:
"""
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
"""
This only partially works because 'Foo' isn't in scope.
This can easily happen if someone refactored the name of 'Foo',
but the comments wouldn't be updated.
"""
def thing[a, b, c]:
"""
:rtype: [int, float, Foo]
"""
return a, b, c
def documentation_nightmare[a, b, c]:
"""
There are no checks, and everything seems fine, but the documentation is all wrong and hard to maintain.
:param a: A
:type a: float
:param b: B
:type b: Bar
:param c: C
:type c: int
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
25.Trong ví dụ này, chúng tôi sẽ tạo ra một tài liệu cha mẹ bình thường [điều này giống như
"""This should fail because Tuple is not imported, but no warning"""
def that[a, b, c]:
"""
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
"""
This only partially works because 'Foo' isn't in scope.
This can easily happen if someone refactored the name of 'Foo',
but the comments wouldn't be updated.
"""
def thing[a, b, c]:
"""
:rtype: [int, float, Foo]
"""
return a, b, c
def documentation_nightmare[a, b, c]:
"""
There are no checks, and everything seems fine, but the documentation is all wrong and hard to maintain.
:param a: A
:type a: float
:param b: B
:type b: Bar
:param c: C
:type c: int
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
27. Sau đó, chúng tôi sẽ có hai con [như """This should fail because Tuple is not imported, but no warning"""
def that[a, b, c]:
"""
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
"""
This only partially works because 'Foo' isn't in scope.
This can easily happen if someone refactored the name of 'Foo',
but the comments wouldn't be updated.
"""
def thing[a, b, c]:
"""
:rtype: [int, float, Foo]
"""
return a, b, c
def documentation_nightmare[a, b, c]:
"""
There are no checks, and everything seems fine, but the documentation is all wrong and hard to maintain.
:param a: A
:type a: float
:param b: B
:type b: Bar
:param c: C
:type c: int
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
28 và """This should fail because Tuple is not imported, but no warning"""
def that[a, b, c]:
"""
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
"""
This only partially works because 'Foo' isn't in scope.
This can easily happen if someone refactored the name of 'Foo',
but the comments wouldn't be updated.
"""
def thing[a, b, c]:
"""
:rtype: [int, float, Foo]
"""
return a, b, c
def documentation_nightmare[a, b, c]:
"""
There are no checks, and everything seems fine, but the documentation is all wrong and hard to maintain.
:param a: A
:type a: float
:param b: B
:type b: Bar
:param c: C
:type c: int
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
29]. Chúng tôi sẽ thay thế các tên lớp trong tài liệu này.
def add[num1, num2]: """ Add up two integer numbers. This function simply wraps the ``+`` operator, and does not do anything interesting, except for illustrating what the docstring of a very simple function looks like. Parameters ---------- num1 : int First number to add. num2 : int Second number to add. Returns ------- int The sum of ``num1`` and ``num2``. See Also -------- subtract : Subtract one integer from another. Examples -------- >>> add[2, 2] 4 >>> add[25, 0] 25 >>> add[10, -10] 0 """ return num1 + num27
Các tài liệu kết quả là
def add[num1, num2]: """ Add up two integer numbers. This function simply wraps the ``+`` operator, and does not do anything interesting, except for illustrating what the docstring of a very simple function looks like. Parameters ---------- num1 : int First number to add. num2 : int Second number to add. Returns ------- int The sum of ``num1`` and ``num2``. See Also -------- subtract : Subtract one integer from another. Examples -------- >>> add[2, 2] 4 >>> add[25, 0] 25 >>> add[10, -10] 0 """ return num1 + num28
Notice:
Chúng tôi đã nối thêm các tài liệu phụ huynh cho các tài liệu của con cái, ban đầu trống rỗng.
Các tệp của chúng tôi thường sẽ chứa một cấp độ mô-đun
"""This should fail because Tuple is not imported, but no warning"""
def that[a, b, c]:
"""
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
"""
This only partially works because 'Foo' isn't in scope.
This can easily happen if someone refactored the name of 'Foo',
but the comments wouldn't be updated.
"""
def thing[a, b, c]:
"""
:rtype: [int, float, Foo]
"""
return a, b, c
def documentation_nightmare[a, b, c]:
"""
There are no checks, and everything seems fine, but the documentation is all wrong and hard to maintain.
:param a: A
:type a: float
:param b: B
:type b: Bar
:param c: C
:type c: int
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
30 với một số giá trị thay thế phổ biến [những thứ như """This should fail because Tuple is not imported, but no warning"""
def that[a, b, c]:
"""
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
"""
This only partially works because 'Foo' isn't in scope.
This can easily happen if someone refactored the name of 'Foo',
but the comments wouldn't be updated.
"""
def thing[a, b, c]:
"""
:rtype: [int, float, Foo]
"""
return a, b, c
def documentation_nightmare[a, b, c]:
"""
There are no checks, and everything seems fine, but the documentation is all wrong and hard to maintain.
:param a: A
:type a: float
:param b: B
:type b: Bar
:param c: C
:type c: int
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
31, """This should fail because Tuple is not imported, but no warning"""
def that[a, b, c]:
"""
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
"""
This only partially works because 'Foo' isn't in scope.
This can easily happen if someone refactored the name of 'Foo',
but the comments wouldn't be updated.
"""
def thing[a, b, c]:
"""
:rtype: [int, float, Foo]
"""
return a, b, c
def documentation_nightmare[a, b, c]:
"""
There are no checks, and everything seems fine, but the documentation is all wrong and hard to maintain.
:param a: A
:type a: float
:param b: B
:type b: Bar
:param c: C
:type c: int
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
32, v.v.].Bạn có thể thay thế và nối thêm một phát bằng một cái gì đó như
def add[num1, num2]: """ Add up two integer numbers. This function simply wraps the ``+`` operator, and does not do anything interesting, except for illustrating what the docstring of a very simple function looks like. Parameters ---------- num1 : int First number to add. num2 : int Second number to add. Returns ------- int The sum of ``num1`` and ``num2``. See Also -------- subtract : Subtract one integer from another. Examples -------- >>> add[2, 2] 4 >>> add[25, 0] 25 >>> add[10, -10] 0 """ return num1 + num29
trong đó
"""This should fail because Tuple is not imported, but no warning"""
def that[a, b, c]:
"""
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
"""
This only partially works because 'Foo' isn't in scope.
This can easily happen if someone refactored the name of 'Foo',
but the comments wouldn't be updated.
"""
def thing[a, b, c]:
"""
:rtype: [int, float, Foo]
"""
return a, b, c
def documentation_nightmare[a, b, c]:
"""
There are no checks, and everything seems fine, but the documentation is all wrong and hard to maintain.
:param a: A
:type a: float
:param b: B
:type b: Bar
:param c: C
:type c: int
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
33 có thể đến từ một cấp độ mô-đun """This should fail because Tuple is not imported, but no warning"""
def that[a, b, c]:
"""
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
"""
This only partially works because 'Foo' isn't in scope.
This can easily happen if someone refactored the name of 'Foo',
but the comments wouldn't be updated.
"""
def thing[a, b, c]:
"""
:rtype: [int, float, Foo]
"""
return a, b, c
def documentation_nightmare[a, b, c]:
"""
There are no checks, and everything seems fine, but the documentation is all wrong and hard to maintain.
:param a: A
:type a: float
:param b: B
:type b: Bar
:param c: C
:type c: int
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
34 Tên chức năng ánh xạ từ điển thành DocStrings. Bất cứ khi nào có thể, chúng tôi thích sử dụng """This should fail because Tuple is not imported, but no warning"""
def that[a, b, c]:
"""
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
"""
This only partially works because 'Foo' isn't in scope.
This can easily happen if someone refactored the name of 'Foo',
but the comments wouldn't be updated.
"""
def thing[a, b, c]:
"""
:rtype: [int, float, Foo]
"""
return a, b, c
def documentation_nightmare[a, b, c]:
"""
There are no checks, and everything seems fine, but the documentation is all wrong and hard to maintain.
:param a: A
:type a: float
:param b: B
:type b: Bar
:param c: C
:type c: int
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
25, vì các quy trình viết tài liệu gần với bình thường hơn một chút.Xem
"""This should fail because Tuple is not imported, but no warning"""
def that[a, b, c]:
"""
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
"""
This only partially works because 'Foo' isn't in scope.
This can easily happen if someone refactored the name of 'Foo',
but the comments wouldn't be updated.
"""
def thing[a, b, c]:
"""
:rtype: [int, float, Foo]
"""
return a, b, c
def documentation_nightmare[a, b, c]:
"""
There are no checks, and everything seems fine, but the documentation is all wrong and hard to maintain.
:param a: A
:type a: float
:param b: B
:type b: Bar
:param c: C
:type c: int
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
36 cho một mẫu ví dụ và """This should fail because Tuple is not imported, but no warning"""
def that[a, b, c]:
"""
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
"""
This only partially works because 'Foo' isn't in scope.
This can easily happen if someone refactored the name of 'Foo',
but the comments wouldn't be updated.
"""
def thing[a, b, c]:
"""
:rtype: [int, float, Foo]
"""
return a, b, c
def documentation_nightmare[a, b, c]:
"""
There are no checks, and everything seems fine, but the documentation is all wrong and hard to maintain.
:param a: A
:type a: float
:param b: B
:type b: Bar
:param c: C
:type c: int
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
37 và """This should fail because Tuple is not imported, but no warning"""
def that[a, b, c]:
"""
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
"""
This only partially works because 'Foo' isn't in scope.
This can easily happen if someone refactored the name of 'Foo',
but the comments wouldn't be updated.
"""
def thing[a, b, c]:
"""
:rtype: [int, float, Foo]
"""
return a, b, c
def documentation_nightmare[a, b, c]:
"""
There are no checks, and everything seems fine, but the documentation is all wrong and hard to maintain.
:param a: A
:type a: float
:param b: B
:type b: Bar
:param c: C
:type c: int
:rtype: Tuple[int, float, Foo]
"""
return a, b, c
38 cho các phiên bản điền.