Docstrings
Documenting functions
Interview Relevant: Code documentation
Docstrings
Document functions with docstrings.
Code Examples
Docstrings for documentation.
python
1def calculate_area(length, width):
2 """
3 Calculate the area of a rectangle.
4
5 Args:
6 length: The length of the rectangle
7 width: The width of the rectangle
8
9 Returns:
10 The area as length * width
11
12 Raises:
13 ValueError: If length or width is negative
14
15 Examples:
16 >>> calculate_area(3, 4)
17 12
18 """
19 if length < 0 or width < 0:
20 raise ValueError("Dimensions must be positive")
21 return length * width
22
23# Access docstring
24print(calculate_area.__doc__)
25help(calculate_area)