2008-09-04 27 views
44

Vậy đó. Nếu bạn muốn ghi lại một hàm hoặc một lớp, bạn đặt một chuỗi ngay sau định nghĩa. Ví dụ:Làm cách nào để ghi lại mô-đun bằng Python?

def foo(): 
    """This function does nothing.""" 
    pass 

Nhưng còn mô-đun thì sao? Làm cách nào tôi có thể ghi lại những gì mà file.py làm gì?

+2

Nhìn này, tôi vừa tìm thấy điều này: http://docs.python.org/devguide/documenting.html Hy vọng hữu ích cho bạn. –

Trả lời

36

Đối với các gói, bạn có thể ghi tài liệu trong __init__.py. Đối với các mô-đun, bạn có thể thêm một chuỗi tài liệu đơn giản trong tệp mô-đun.

Tất cả các thông tin ở đây: http://www.python.org/dev/peps/pep-0257/

4

Thật dễ dàng, bạn chỉ cần thêm một chuỗi tài liệu ở đầu mô-đun.

6

Bạn làm điều đó với cùng một cách chính xác. Đặt một chuỗi vào làm câu lệnh đầu tiên trong mô-đun.

+0

Đây là những gì thực hiện tự động khi bạn tạo một mô-đun mới. – Rivka

28

Thêm chuỗi tài liệu của bạn làm first statement in the module.

Vì tôi thích nhìn thấy một ví dụ:

""" 
Your module's verbose yet thorough docstring. 
""" 

import foo 

# ... 
2

Dưới đây là một Example Google Style Python Docstrings về cách mô-đun có thể được ghi lại. Về cơ bản có một thông tin về một mô-đun, cách thực thi nó và thông tin về các biến cấp mô-đun và danh sách các mục ToDo.

"""Example Google style docstrings. 

This module demonstrates documentation as specified by the `Google 
Python Style Guide`_. Docstrings may extend over multiple lines. 
Sections are created with a section header and a colon followed by a 
block of indented text. 

Example: 
    Examples can be given using either the ``Example`` or ``Examples`` 
    sections. Sections support any reStructuredText formatting, including 
    literal blocks:: 

     $ python example_google.py 

Section breaks are created by resuming unindented text. Section breaks 
are also implicitly created anytime a new section starts. 

Attributes: 
    module_level_variable1 (int): Module level variables may be documented in 
     either the ``Attributes`` section of the module docstring, or in an 
     inline docstring immediately following the variable. 

     Either form is acceptable, but the two should not be mixed. Choose 
     one convention to document module level variables and be consistent 
     with it. 

Todo: 
    * For module TODOs 
    * You have to also use ``sphinx.ext.todo`` extension 

.. _Google Python Style Guide: 
http://google.github.io/styleguide/pyguide.html 

""" 

module_level_variable1 = 12345 

def my_function(): 
    pass 
... 
... 
Các vấn đề liên quan