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

Hàm help() trong Python: Tra cứu tài liệu nhanh

Khi cần tra cứu tài liệu Python về hàm, module, class... Python cung cấp hàm built-in help() cho phép xem tài liệu trực tiếp trong interpreter.

Cú pháp

help('tên_đối_tượng')

Ví dụ

print(help('time'))

Kết quả (rút gọn)

Help on built-in module time:

NAME
    time - This module provides various functions to manipulate time values.

DESCRIPTION
    There are two standard representations of time.
    One is the number of seconds since the Epoch...
    
FUNCTIONS
    asctime(...)
    ctime(...)
    gmtime(...)
    localtime(...)
    sleep(...)
    strftime(...)
    strptime(...)
    time(...)
    ...

Các cách sử dụng help()

# Module
help('os')
help('json')

# Hàm built-in
help('print')
help('len')

# Method của đối tượng
help(str.split)
help(list.append)

# Class
help(dict)

# Trong interpreter: help() không tham số → chế độ tương tác

Mẹo

  • Trong Jupyter Notebook: dùng ?time hoặc ??time cho docstring
  • help() hiển thị docstring (__doc__) của đối tượng
  • Viết docstring tốt cho code của mình để help() hữu ích