Mảng không có dấu phẩy python

Một chuỗi tài liệu Python là một chuỗi được sử dụng để ghi lại một mô-đun, lớp, hàm hoặc phương thức Python, vì vậy các lập trình viên có thể hiểu nó làm gì mà không cần phải đọc chi tiết về cách triển khai

Ngoài ra, thực tế phổ biến là tự động tạo tài liệu trực tuyến (html) từ chuỗi tài liệu. Nhân sư phục vụ mục đích này

Ví dụ tiếp theo đưa ra ý tưởng về cách một chuỗi 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 is
the docstring of a very simple function.

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 tồn tại về chuỗi tài liệu, vì vậy chúng dễ đọc hơn và chúng có thể được 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 chuỗi tài liệu Python phải tuân theo được xác định trong PEP-257

Vì PEP-257 khá mở và một số tiêu chuẩn khác tồn tại trên nó. Trong trường hợp gấu trúc, quy ước chuỗi tài liệu gọn gàng được tuân theo. Các quy ước được giải thích trong tài liệu này

  • hướng dẫn chuỗi tài liệu numpydoc (dựa trên tài liệu Hướng dẫn ban đầu về tài liệu NumPy/SciPy)

numpydoc là một tiện ích mở rộng của Sphinx để hỗ trợ quy ước chuỗi tài liệu numpy

Tiêu chuẩn sử dụng reStructuredText (reST). reStructuredText là ngôn ngữ đánh dấu cho phép mã hóa các kiểu trong tệp văn bản thuần túy. Tài liệu về reStructuredText có thể được tìm thấy trong

  • Sphinx reStructuredText primer
  • Tái cấu trúc nhanhTham khảo văn bản
  • Thông số kỹ thuật tái cấu trúc đầy đủ

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 thêm quy ước cụ thể cho dự án gấu trúc

Viết một chuỗi tài liệu¶

Quy định chung¶

Các tài liệu phải được xác định bằng ba dấu ngoặc kép. Không được để trống dòng nào trước hoặc sau chuỗi tài liệu. Văn bản bắt đầu ở dòng tiếp theo sau dấu ngoặc kép mở đầu. Các trích dẫn kết thúc có dòng riêng (có nghĩa là chúng không ở cuối câu cuối cùng)

Trong những trường hợp hiếm hoi, các kiểu reST như văn bản in đậm hoặc in nghiêng sẽ được sử dụng trong tài liệu, nhưng thông thường có mã nội tuyến, được trình bày giữa các dấu gạch ngược. Nó được coi là mã nội tuyến

  • Tên của một tham số
  • Mã Python, một mô-đun, chức năng, tích hợp sẵn, loại, chữ… (e. g.
    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
    
    2,
    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
    
    3,
    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
    
    4,
    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
    
    5,
    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
    
    6)
  • Một lớp gấu trúc (ở dạ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 + bar
    
    7)
  • Phương thức pandas (ở dạ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 + bar
    
    8)
  • Hàm pandas (ở dạ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 + bar
    
    9)

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, hãy đặt tiền tố cho nó là ____. Ví dụ:

def astype(dtype):
    """
    Cast Series type.

    This section will provide further details.
    """
    pass
1 sẽ liên kết đến
def astype(dtype):
    """
    Cast Series type.

    This section will provide further details.
    """
    pass
2 nhưng chỉ hiển thị phần cuối cùng,
def astype(dtype):
    """
    Cast Series type.

    This section will provide further details.
    """
    pass
3 dưới dạng văn bản liên kết. Xem cú pháp tham chiếu chéo Sphinx để biết chi tiết

Tố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 + bar
3

Xấ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 + bar

Phần 1. Tóm tắt ngắn¶

Tóm tắt ngắn gọn là một câu duy nhất diễn đạt những gì chức năng thực hiện một cách ngắn gọn

Tóm tắt ngắn phải bắt đầu bằng chữ in hoa, kết thúc bằng dấu chấm và nằm trong 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 hàm và phương thức, phần tóm tắt ngắn phải bắt đầu bằng một động từ nguyên thể

Tốt

def astype(dtype):
    """
    Cast Series type.

    This section will provide further details.
    """
    pass

Xấ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 + bar
6

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 sâu vào chi tiết của các thông số, hoặc thảo luận về ghi chú thực hiện, mà sẽ đi vào các phần khác

Một dòng trống được để lại giữa phần tóm tắt ngắn và phần tóm tắt mở rộng. Và mỗi đoạn trong phần tóm tắt mở rộng được kết thúc bởi một dấu chấm

Phần 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

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
7

Phần 3. Thông số¶

Chi tiết các tham số sẽ được bổ sung trong phần này. Phần này có tiêu đề “Thông số”, theo sau là một dòng có dấu gạch nối dưới mỗi chữ cái của từ “Thông số”. Một dòng trống được để lại trước tiêu đề của phần, nhưng không phải sau và không phải giữa dòng có từ "Tham số" và dòng 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 *args và **kwargs, nhưng không phải

Các tham số được xác định theo tên của chúng, theo sau là khoảng trắng, dấu hai chấm, dấu cách khác và loại (hoặc các loại). Lưu ý rằng khoảng cách giữa tên và dấu hai chấm rất quan trọng. Các loại không được xác định cho *args và **kwargs, nhưng phải được xác định cho tất cả các tham số khác. Sau phần định nghĩa tham số bắt buộc phải có một dòng mô tả tham số, được thụt vào trong, có thể có nhiều dòng. Mô tả phải bắt đầu bằng chữ in hoa và kết thúc bằng dấu chấm

Đối với các đối số từ khóa có giá trị mặc định, giá trị 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, default 0”. Trong một số trường hợp, có thể hữu ích khi giải thích đối số mặc định nghĩa là gì, có thể được thêm vào sau dấu phẩy “int, default -1, có nghĩa là tất cả cpus”

Trong trường hợp giá trị mặc định là Không có nghĩa là giá trị đó sẽ không được sử dụng. Thay vì “str, mặc định Không có”, nên viết “str, tùy chọn”. Khi Không có giá trị nào đang được sử dụng, chúng tôi sẽ giữ nguyên dạng “str, mặc định Không có”. Ví dụ, trong df. to_csv(compression=None), Không có giá trị nào đang được sử dụng nhưng có nghĩa là tính năng nén là tùy chọn và không có tính năng 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 str, tùy chọn. Chỉ trong các trường hợp như func(value=None) và Không có gì đang được sử dụng giống như 0 hoặc foo sẽ được sử dụng, thì chúng tôi sẽ chỉ định “str, int hoặc Không có, mặc định là Không có”

Tố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 + bar
8

Xấ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 + bar
9

Các loại tham số¶

Khi chỉ định các loại tham số, có thể sử dụng trực tiếp các loại dữ liệu tích hợp sẵn của Python (loại Python được ưa thích hơn chuỗi dài dòng, số nguyên, boolean, v.v.)

  • int
  • trôi nổi
  • str
  • bool

Đối với các loại phức tạp, xác định các loại phụ. Đối với dict và tuple, vì có nhiều loại, chúng tôi sử dụng dấu ngoặc để giúp đọc loại (dấu ngoặc nhọn cho dict và dấu ngoặc vuông bình thường cho tuple)

  • danh sách int
  • lệnh của {str. int}
  • bộ của (str, int, int)
  • bộ của (str,)
  • bộ str

Trong trường hợp chỉ có một bộ giá trị được phép, hãy liệt kê chúng trong dấu ngoặc nhọn và phân tách bằng dấu phẩy (theo sau là khoảng trắng). Nếu các giá trị là thứ tự và chúng có thứ tự, 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, nếu có

  • {0, 10, 25}
  • {'đơn giản', 'nâng cao'}
  • {'thấp trung bình cao'}
  • {'mèo', 'chó', 'chim'}

Nếu loại được xác định trong mô-đun Python, thì mô-đun đó phải được chỉ định

  • ngày giờ. ngày
  • ngày giờ. ngày giờ
  • số thập phân. Số thập phân

Nếu loại nằm trong một gói, mô-đun cũng phải được chỉ định

  • cục mịch. ndarray
  • scipy. thưa thớt. coo_matrix

Nếu loại là loại gấu trúc, hãy chỉ định gấu trúc ngoại trừ Sê-ri và Khung dữ liệu

  • Loạt
  • Khung dữ liệu
  • gấu trúc. Mục lục
  • gấu trúc. phân loại
  • gấu trúc. mảng thưa thớt

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 có nhiều mảng, thì có thể chỉ định giống như mảng. Nếu bất kỳ loại nào có thể lặp lại được chấp nhận, thì có thể sử dụng iterable

  • dạng mảng
  • lặp đi lặp lại

Nếu nhiều hơn một loại được chấp nhận, hãy phân tách chúng bằng dấu phẩy, ngoại trừ hai loại cuối cùng, cần được phân tách bằng từ 'hoặc'

  • int hoặc float
  • phao, số thập phân. Số thập phân hoặc Không có
  • str hoặc danh sách str

Nếu

def astype(dtype):
    """
    Cast Series type.

    This section will provide further details.
    """
    pass
4 là một trong những giá trị được chấp nhận thì nó luôn phải là giá trị 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 'chỉ mục', 1 hoặc 'cột', Không có}, mặc định Không có

phần 4. Lợi nhuận hoặc Lợi nhuận¶

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ư “Thông số”. Với các tên “Returns” hoặc “Yields” theo sau là một dòng có nhiều dấu gạch nối bằng các chữ cái trong từ trước đó

Các tài liệu của sự trở lại cũng tương tự như các thông 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 “Lợi nhuận” và “Lợi nhuận” giống như các loại cho “Tham số”. Ngoài ra, phần mô tả phải kết thúc bằng dấu chấm

Ví dụ, với một giá trị duy 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 + bar
1

Với nhiều hơn một giá trị

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
2

Nếu phương pháp mang lại giá trị của 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 + bar
30

Phầ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 chức năng đang được ghi lại. Trong một số ít trường hợp, nếu không tìm thấy phương thức hoặc chức năng liên quan nào, phần này có thể được bỏ qua

Một ví dụ rõ ràng sẽ là các phương thức head() và tail(). Vì tail() thực hiện tương đương với head() nhưng ở cuối Sê-ri hoặc DataFrame thay vì ở đầu, bạn nên cho người dùng biết về điều đó

Để đưa ra một trực giác về những gì có thể được coi là liên quan, đây là một số ví dụ

  • def astype(dtype):
        """
        Cast Series type.
    
        This section will provide further details.
        """
        pass
    
    5 và
    def astype(dtype):
        """
        Cast Series type.
    
        This section will provide further details.
        """
        pass
    
    6, vì chúng làm tương tự, nhưng trong một trường hợp cung cấp các chỉ số và ở các vị trí khác
  • def astype(dtype):
        """
        Cast Series type.
    
        This section will provide further details.
        """
        pass
    
    7 và
    def astype(dtype):
        """
        Cast Series type.
    
        This section will provide further details.
        """
        pass
    
    8, vì chúng làm ngược lại
  • def astype(dtype):
        """
        Cast Series type.
    
        This section will provide further details.
        """
        pass
    
    9,
    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
    
    60 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 + bar
    
    61, vì người dùng dễ dàng tìm kiếm phương thức lặp qua các cột lại kết thúc bằng phương thức lặp qua hàng và ngược lạ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 + bar
    
    62 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 + bar
    
    63, vì cả hai phương pháp đều được sử dụng để xử lý các giá trị bị thiế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 + bar
    
    64 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 + bar
    
    65, vì chúng bổ sung cho nhau
  • 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
    
    66 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 + bar
    
    67, vì cái này là sự tổng quát hóa của cái kia
  • 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
    
    68 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 + bar
    
    69, vì người dùng có thể đang đọc tài liệu 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 + bar
    
    68 để biết cách truyền ngày tháng và cách thực hiện là vớ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 + bar
    
    69
  • 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
    
    72 có liên quan đế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 + bar
    
    73, 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 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
74), trước tiên hãy sử dụng tên của mô-đun (không phải bí danh như
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
75). Nếu chức năng nằm trong một mô-đun không phải là mô-đun chính, chẳng hạn như
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
76, hãy liệt kê toàn bộ mô-đun (e. g.
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
77)

Phần này, như phần trước, cũng có tiêu đề, “Xem thêm” (lưu ý viết hoa S và A). Cũng theo sau dòng có dấu gạch nối và trước dòng trống

Sau tiêu đề, chúng tôi sẽ thêm một dòng cho từng phương thức hoặc chức năng có liên quan, theo sau là khoảng trắng, dấu hai chấm, một khoảng trắng khác và một mô tả ngắn minh họa chức năng của phương thức hoặc chức năng này, tại sao nó có liên quan trong ngữ cảnh này và điều gì . Phần mô tả cũng phải kết thúc bằng dấu chấm

Lưu ý rằng trong "Returns" và "Yields", mô tả nằm ở dòng sau so với loại. Nhưng ở phần này nó nằm cùng dòng, có dấu hai chấm ở giữa. Nếu mô tả không nằm trong cùng một dòng, nó có thể tiếp tục ở những dòng tiếp theo, nhưng nó phải được thụt vào trong chúng

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 + bar
31

Phần 6. Ghi chú¶

Đây là phần tùy chọn dùng để 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

Vui lòng bỏ qua nó, trừ khi bạn đã quen với việc triển khai thuật toán hoặc bạn phát hiện ra một số hành vi phản trực giác trong khi viết các ví dụ cho hàm

Phần này tuân 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 chuỗi tài liệu, ngay cả khi nó được đặt ở vị trí cuối cùng. Thông thường, mọi người hiểu các khái niệm tốt hơn với các ví dụ, hơn là với 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 cách sử dụng hàm hoặc phương thức, phải là mã Python hợp lệ, theo cách xác định trả về đầu ra được trình bày và người dùng có thể sao chép và chạy

Chúng được trình bày dưới dạng một phiên trong thiết bị đầu cuối Python. >>> được sử dụng để trình bày mã. … được sử dụng cho 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 đầ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 ví dụ như sau

  1. Nhập các thư viện cần thiết (ngoại trừ
    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
    
    74 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 + bar
    
    79)
  2. Tạo dữ liệu cần thiết cho ví dụ
  3. 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
  4. Thêm các ví dụ có giải thích minh họa cách sử dụng các tham số cho chức năng mở rộng

Một ví dụ đơn giản có thể 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 + bar
32

Các ví dụ nên ngắn gọn nhất có thể. Trong trường hợp độ phức tạp của hàm yêu cầu các ví dụ dài, nên sử dụng các khối có tiêu đề in đậm. Sử dụng sao đô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 + bar
80 để in đậm văn bản, như trong
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
81

Quy ước cho các ví dụ¶

Mã trong các ví dụ được giả định là luôn bắt đầu bằng hai dòng này không được hiển thị

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
33

Bấ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ỗi mô-đun trên một dòng (như khuyến nghị trong PEP-8) và tránh các bí danh. Tránh nhập quá nhiều, nhưng nếu cần, hãy nhập từ thư viện chuẩn trước, tiếp theo là thư viện của bên thứ ba (như matplotlib)

Khi minh họa các ví dụ bằng một

def astype(dtype):
    """
    Cast Series type.

    This section will provide further details.
    """
    pass
3 duy nhất, hãy sử dụng 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 + bar
83 và nếu minh họa bằng 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 + bar
84 duy nhất, hãy sử dụng 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 + bar
85. Đối với các chỉ số,
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
86 là tên ưa thích. Nếu sử dụng một bộ đồng nhất
def astype(dtype):
    """
    Cast Series type.

    This section will provide further details.
    """
    pass
3 hoặc
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
84, hãy đặt tên cho chúng 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 + bar
89,
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
90,
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
91… hoặc
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
92,
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
93,
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
94… Nếu dữ liệu không đồng nhất và cần nhiều hơn một cấu trúc, hãy đặt tên cho chúng bằng một cái gì đó có ý nghĩa, ví dụ như
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
95

Dữ liệu được sử dụng trong ví dụ phải càng nhỏ gọn càng tốt. Số lượng hàng được khuyến nghị là khoảng 4, nhưng hãy đặt nó thành một số có ý nghĩa đối với ví dụ cụ thể. Ví dụ trong phương thức

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
97, nó yêu cầu phải cao hơn 5, để hiển thị ví dụ với các giá trị mặc định. Nếu thực hiệ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 + bar
98, chúng ta có thể sử dụng một cái gì đó như
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
99, vì vậy dễ dàng thấy rằng giá trị được trả về là giá trị trung bình

Đối với 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ó diễn giải, chẳng hạn như ma trận các số ngẫu nhiên có các cột A, B, C, D… Thay vào đó, hãy sử dụng một ví dụ có ý nghĩa, giúp dễ hiểu khái niệm 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, đối số từ khóa

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
10 được ưu tiên hơn đối số vị trí
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
11

Tố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 + bar
34

Xấ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 + bar
35

Mẹo để các ví dụ của bạn vượt qua bài kiểm tra¶

Việc lấy 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ể 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ừ pandas và numpy, những thư viện này đã được nhập dưới dạ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 + bar
    
    12 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 + bar
    
    13) và xác định tất cả các biến bạn sử dụng trong ví dụ

  • 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, chẳng hạn như nếu hàm bạn đang ghi lại liên quan đến phân phối xác suất hoặc nếu lượng dữ liệu cần thiết để làm cho kết quả của hàm có ý nghĩa là quá nhiều, thì việc tạo thủ công sẽ rất cồng kềnh. Trong những trường hợp đó, hãy luôn sử dụng một nguồn gốc ngẫu nhiên cố định để làm cho các ví dụ được tạo có thể dự đoán được. Thí 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 + bar
    
    36

  • Nếu bạn có một đoạn mã bao gồm nhiều dòng, bạn cần sử dụng '…' trên các dòng tiếp theo

    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
    
    37

  • Nếu bạn muốn hiển thị một trường hợp ngoại lệ được nêu ra, bạn có thể là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 + bar
    
    38

    Điều cần thiết là bao gồm “Traceback (lần gọi gần đây nhất). ”, nhưng đối với lỗi thực tế thì chỉ tên lỗi là đủ

  • Nếu có một phần nhỏ của kết quả có thể thay đổi (e. g. một hàm băm trong biểu diễn đối tượng), bạn có thể sử dụng ________ 214 để biểu diễn phần này

    Nếu bạn muốn hiển thị rằ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 + bar
    
    15 trả về một đối tượng AxesSubplot matplotlib, điều này sẽ thất bại trong doctest

    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
    
    39

    Tuy nhiên, bạn có thể làm được (chú ý phần chú thích cần bổ sung)

    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
    
    0

Đồ thị trong các ví dụ¶

Có một số phương pháp trong gấu trúc trả về lô. Để hiển thị các ô được tạo bởi các ví dụ trong tài liệu, tồn tại chỉ thị

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
16

Để sử dụng nó, hãy đặt mã tiếp theo sau tiêu đề “Ví dụ” như hình bên dưới. Cốt truyện sẽ được tạo tự động khi xây dựng tài liệu