Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

การสร้างเอกสารด้วย Pydoc Module ใน Python

บทนำ

pydoc สร้างเอกสารจาก Python modules อัตโนมัติ แสดงได้เป็น: console text, web browser, HTML files

การเริ่มต้น

import pydoc

Interactive Help Shell

>>> import pydoc
>>> pydoc.help()
# เข้า interactive help → พิมพ์ชื่อ module/function/class

แสดงเอกสารบน Browser

python -m pydoc -b
# Server ready at https://localhost:PORT/ → เปิด browser ค้นหา/อ่าน docs

Docstrings: สร้างเอกสารของตัวเอง

def documentation():
    '''Documentation using docstrings'''
    pass

print(documentation.__doc__)  # Documentation using docstrings
help(documentation)           # แสดง help format

คำอธิบาย

  • Docstring (triple quotes) เป็นเอกสารของ function/class/module
  • เข้าถึงได้ด้วย __doc__ attribute หรือ help()
  • pydoc -b เริ่ม HTTP server serve docs ทุก module ที่ installed

ทำไมต้องมี Documentation?

- ช่วย team เข้าใจโค้ด
- ป้องกัน bugs จากความเข้าใจผิด
- Maintenance ง่ายขึ้น
- Generate API docs อัตโนมัติ (Sphinx, pdoc, etc.)