Строки документации в Python используются для предоставления документации по функциям, классам и модулям. Они заключены в тройные кавычки и обычно размещаются в начале определения. Вот несколько способов использования строк документации с примерами кода:
-
Основная строка документации:
def add(a, b): """ This function adds two numbers and returns the result. """ return a + b
-
Многострочная строка документации:
def multiply(a, b): """ This function multiplies two numbers and returns the result. Parameters: a (int): The first number. b (int): The second number. Returns: int: The product of the two numbers. """ return a * b
-
Строка документации с примерами:
def divide(a, b): """ This function divides two numbers and returns the result. Parameters: a (int): The numerator. b (int): The denominator. Returns: float: The quotient of the division. Examples: >>> divide(10, 2) 5.0 >>> divide(100, 0) Traceback (most recent call last): ... ZeroDivisionError: division by zero """ return a / b
-
Строка документации со ссылками:
def greet(name): """ This function greets a person by their name. Parameters: name (str): The name of the person. Returns: str: A greeting message. References: - For more information on greetings, see the Greeting Protocol Specification. - Example implementation: https://github.com/example/greeting-library """ return f"Hello, {name}!"