Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Tạo tài liệu tự động với module pydoc trong Python

Giới thiệu

pydoc tự động sinh tài liệu từ module Python. Hiển thị trên console, trình duyệt web, hoặc lưu file HTML.

Bắt đầu

Module pydoc có sẵn trong Python.

import pydoc

Shell tương tác với help()

>>> import pydoc
>>> pydoc.help()

Nhập tên module, kiểu dữ liệu, hàm, class... để xem tài liệu ngay.

Xem tài liệu trên trình duyệt

python -m pydoc -b

Khởi động server local, mở browser tại https://localhost:PORT/. Tìm kiếm từ khóa.

Server ready at https://localhost:50621/
Server commands: [b]rowser, [q]uit
server> q
Server stopped

Làm việc với docstring

def documentation():
    '''Tài liệu bằng docstring'''
print(documentation.__doc__)
help(documentation)
Tài liệu bằng docstring
Help on function documentation in module __main__:
documentation()
    Tài liệu bằng docstring

Kết luận

Đã biết xem tài liệu offline với pydoc, tự viết docstring cho script. Giữ tài liệu tốt giúp tránh nhầm lẫn, lỗi runtime khi dự án lớn. Xem thêm tại docs.