Accessing Docstrings via help() and doc

๐Ÿท๏ธ Functions / Docstrings

๐Ÿงญ Introduction

When you write functions in Python, you can include a description of what the function does, what parameters it expects, and what it returns. This description is called a docstring (short for documentation string). Once a docstring is written, you can access it in two ways: using the built-in help() function or by reading the doc attribute directly. This helps you and others understand how to use a function without reading the full source code.


๐Ÿ“– What is a Docstring?

  • A docstring is a string literal that appears as the first statement in a function, class, or module.
  • It is enclosed in triple quotes (either single or double).
  • Docstrings are used to document what the code does.
  • Python ignores the docstring during execution, but stores it as a special attribute.

Example of a function with a docstring:

  • def greet(name):
  • """This function greets the person whose name is passed as input."""
  • print(f"Hello, {name}!")

๐Ÿ› ๏ธ Accessing Docstrings with help()

  • The help() function opens an interactive help viewer in the terminal.
  • It displays the docstring along with other information like function signature.
  • To use it, simply pass the function name (without parentheses) to help().

Example:

  • help(greet)

Expected output (formatted inline):

  • Help on function greet in module main:
  • greet(name)
  • This function greets the person whose name is passed as input.

๐Ÿ•ต๏ธ Accessing Docstrings with doc

  • Every function in Python has a built-in attribute called doc.
  • This attribute stores the docstring as a plain string.
  • You can access it directly using dot notation.

Example:

  • print(greet.doc)

Expected output:

  • This function greets the person whose name is passed as input.

โš™๏ธ Key Differences Between help() and doc

Feature help() doc
Output format Formatted with function signature and extra info Raw string only
Usage Interactive exploration Programmatic access
Requires function name Yes, without parentheses Yes, without parentheses
Best for Learning and debugging Extracting documentation in code

๐Ÿ“Š When to Use Each Approach

  • Use help() when you are exploring a new function interactively in the Python shell or a script.
  • Use doc when you want to capture the docstring as a string variable for logging, testing, or generating documentation automatically.

Example of using doc programmatically:

  • doc_text = greet.doc
  • print(f"Documentation: {doc_text}")

Expected output:

  • Documentation: This function greets the person whose name is passed as input.

๐Ÿงช Practical Tips for Engineers

  • Always write docstrings for your functions, even simple ones. It saves time later.
  • Follow the PEP 257 convention: use triple double quotes for docstrings.
  • Keep docstrings concise but descriptive.
  • Use help() to quickly recall how a function works without opening the source file.
  • Use doc when you need to display documentation in a custom help menu or log file.

โœ… Summary

  • Docstrings are embedded documentation inside functions.
  • help() gives a formatted view of the docstring plus function signature.
  • doc returns the raw docstring as a string.
  • Both methods require the function name without parentheses.
  • Using docstrings consistently makes your code more maintainable and team-friendly.

Docstrings are documentation strings attached to functions, classes, or modules, and help() and __doc__ are two ways to read that documentation.


๐Ÿ›  Example 1: Using help() on a simple function

This shows how to call help() to see the full documentation for a function you define.

def greet():
    """Prints a friendly greeting message."""
    print("Hello, engineer!")

help(greet)

๐Ÿ“ค Output: Help on function greet in module main: greet() Prints a friendly greeting message.


๐Ÿ“„ Example 2: Using __doc__ to get just the docstring text

This shows how to access only the docstring string directly, without the extra formatting from help().

def add(a, b):
    """Returns the sum of two numbers."""
    return a + b

print(add.__doc__)

๐Ÿ“ค Output: Returns the sum of two numbers.


๐Ÿ“ Example 3: Multi-line docstring with help()

This shows how help() displays a multi-line docstring with parameter descriptions.

def calculate_area(length, width):
    """
    Calculates the area of a rectangle.

    Parameters:
    length (float): The length of the rectangle.
    width (float): The width of the rectangle.

    Returns:
    float: The area of the rectangle.
    """
    return length * width

help(calculate_area)

๐Ÿ“ค Output: Help on function calculate_area in module main: calculate_area(length, width) Calculates the area of a rectangle. Parameters: length (float): The length of the rectangle. width (float): The width of the rectangle. Returns: float: The area of the rectangle.


๐Ÿ” Example 4: Accessing __doc__ on a built-in function

This shows that built-in Python functions also have docstrings you can access.

print(len.__doc__)

๐Ÿ“ค Output: Return the number of items in a container.


๐Ÿงฉ Example 5: Using help() on a function with no docstring

This shows what happens when you call help() on a function that has no docstring defined.

def no_doc():
    pass

help(no_doc)

๐Ÿ“ค Output: Help on function no_doc in module main: no_doc()


๐Ÿ“Š Comparison Table: help() vs __doc__

Feature help() __doc__
Output format Full formatted help text Raw string only
Includes function signature Yes No
Works on built-in functions Yes Yes
Shows parameter details Yes Only if written in docstring
Returns a string you can store No (prints directly) Yes

๐Ÿงญ Introduction

When you write functions in Python, you can include a description of what the function does, what parameters it expects, and what it returns. This description is called a docstring (short for documentation string). Once a docstring is written, you can access it in two ways: using the built-in help() function or by reading the doc attribute directly. This helps you and others understand how to use a function without reading the full source code.


๐Ÿ“– What is a Docstring?

  • A docstring is a string literal that appears as the first statement in a function, class, or module.
  • It is enclosed in triple quotes (either single or double).
  • Docstrings are used to document what the code does.
  • Python ignores the docstring during execution, but stores it as a special attribute.

Example of a function with a docstring:

  • def greet(name):
  • """This function greets the person whose name is passed as input."""
  • print(f"Hello, {name}!")

๐Ÿ› ๏ธ Accessing Docstrings with help()

  • The help() function opens an interactive help viewer in the terminal.
  • It displays the docstring along with other information like function signature.
  • To use it, simply pass the function name (without parentheses) to help().

Example:

  • help(greet)

Expected output (formatted inline):

  • Help on function greet in module main:
  • greet(name)
  • This function greets the person whose name is passed as input.

๐Ÿ•ต๏ธ Accessing Docstrings with doc

  • Every function in Python has a built-in attribute called doc.
  • This attribute stores the docstring as a plain string.
  • You can access it directly using dot notation.

Example:

  • print(greet.doc)

Expected output:

  • This function greets the person whose name is passed as input.

โš™๏ธ Key Differences Between help() and doc

Feature help() doc
Output format Formatted with function signature and extra info Raw string only
Usage Interactive exploration Programmatic access
Requires function name Yes, without parentheses Yes, without parentheses
Best for Learning and debugging Extracting documentation in code

๐Ÿ“Š When to Use Each Approach

  • Use help() when you are exploring a new function interactively in the Python shell or a script.
  • Use doc when you want to capture the docstring as a string variable for logging, testing, or generating documentation automatically.

Example of using doc programmatically:

  • doc_text = greet.doc
  • print(f"Documentation: {doc_text}")

Expected output:

  • Documentation: This function greets the person whose name is passed as input.

๐Ÿงช Practical Tips for Engineers

  • Always write docstrings for your functions, even simple ones. It saves time later.
  • Follow the PEP 257 convention: use triple double quotes for docstrings.
  • Keep docstrings concise but descriptive.
  • Use help() to quickly recall how a function works without opening the source file.
  • Use doc when you need to display documentation in a custom help menu or log file.

โœ… Summary

  • Docstrings are embedded documentation inside functions.
  • help() gives a formatted view of the docstring plus function signature.
  • doc returns the raw docstring as a string.
  • Both methods require the function name without parentheses.
  • Using docstrings consistently makes your code more maintainable and team-friendly.

Interactive Views

You are currently in ๐Ÿ“š All-in-One mode. Use the tabs at the top to switch to ๐Ÿ“– Theory Only or ๐Ÿ’ป Code Only views.

Docstrings are documentation strings attached to functions, classes, or modules, and help() and __doc__ are two ways to read that documentation.


๐Ÿ›  Example 1: Using help() on a simple function

This shows how to call help() to see the full documentation for a function you define.

def greet():
    """Prints a friendly greeting message."""
    print("Hello, engineer!")

help(greet)

๐Ÿ“ค Output: Help on function greet in module main: greet() Prints a friendly greeting message.


๐Ÿ“„ Example 2: Using __doc__ to get just the docstring text

This shows how to access only the docstring string directly, without the extra formatting from help().

def add(a, b):
    """Returns the sum of two numbers."""
    return a + b

print(add.__doc__)

๐Ÿ“ค Output: Returns the sum of two numbers.


๐Ÿ“ Example 3: Multi-line docstring with help()

This shows how help() displays a multi-line docstring with parameter descriptions.

def calculate_area(length, width):
    """
    Calculates the area of a rectangle.

    Parameters:
    length (float): The length of the rectangle.
    width (float): The width of the rectangle.

    Returns:
    float: The area of the rectangle.
    """
    return length * width

help(calculate_area)

๐Ÿ“ค Output: Help on function calculate_area in module main: calculate_area(length, width) Calculates the area of a rectangle. Parameters: length (float): The length of the rectangle. width (float): The width of the rectangle. Returns: float: The area of the rectangle.


๐Ÿ” Example 4: Accessing __doc__ on a built-in function

This shows that built-in Python functions also have docstrings you can access.

print(len.__doc__)

๐Ÿ“ค Output: Return the number of items in a container.


๐Ÿงฉ Example 5: Using help() on a function with no docstring

This shows what happens when you call help() on a function that has no docstring defined.

def no_doc():
    pass

help(no_doc)

๐Ÿ“ค Output: Help on function no_doc in module main: no_doc()


๐Ÿ“Š Comparison Table: help() vs __doc__

Feature help() __doc__
Output format Full formatted help text Raw string only
Includes function signature Yes No
Works on built-in functions Yes Yes
Shows parameter details Yes Only if written in docstring
Returns a string you can store No (prints directly) Yes