Follow the transformation
Place the docstring immediately after def/class or at the top of a module.
Start with a concise purpose statement.
Document parameters/returns when their meaning is not obvious from the signature.
A docstring is a string literal placed as the first statement of a module, function, class or method to document its public purpose and interface.
A docstring is a string literal placed as the first statement of a module, function, class or method to document its public purpose and interface. Python stores it in the object’s __doc__ attribute, and help() can display it. A useful function docstring explains what the function does, important inputs, returned value and exceptional conditions without duplicating the implementation line by line.
Learning goal: explain why Docstrings behaves this way, apply it to a small example, and verify the result independently. Begin by being able to justify this first step: Place the docstring immediately after def/class or at the top of a module.
Treat this as a sequence of observable decisions rather than one opaque command. Stage 1: Place the docstring immediately after def/class or at the top of a module. Stage 2: Start with a concise purpose statement. Stage 3: Document parameters/returns when their meaning is not obvious from the signature. Final checkpoint: Keep the docstring correct as the interface changes.
Place the docstring immediately after def/class or at the top of a module.
Start with a concise purpose statement.
Document parameters/returns when their meaning is not obvious from the signature.
Place the docstring immediately after def/class or at the top of a module. For Docstrings, identify the exact state before this stage, the operation or rule applied here, and the observable state afterwards so the mechanism remains inspectable.
# Step 1 — Define the reusable `percentage` function; its indented body describes what happens for each call.
def percentage(part, whole):
# Step 2 — Execute this statement and inspect how it changes the current value, object or program state.
"""Return part as a percentage of whole; whole must be positive."""
# Step 3 — Evaluate this condition and execute the indented branch only when the condition is true.
if whole <= 0:
# Step 4 — Raise an explicit exception to signal that the required condition or input contract was violated.
raise ValueError("whole must be positive")
# Step 5 — Return the computed value to the caller so the result can be reused or tested.
return 100 * part / whole
# Step 6 — Display the current value explicitly so the result/state can be inspected during execution.
print(percentage.__doc__)The docstring is attached to the function object and can be surfaced by help(percentage).
For Docstrings, trace the specific input through the mechanism above and independently verify one returned value, state change or side effect.
InputObjects/values supplied to the operation.StateNames or mutable objects that may change during execution.OutputReturned value, side effect, file, plot or exception to inspect.Use Docstrings when it answers a defined question in Functions & Reusable Code and its inputs/assumptions match the current data or program state.
Reconsider Docstrings when the required information is unavailable, the operation would violate a validation/data boundary, or a simpler operation answers the question more transparently.
Construct a tiny example of Docstrings. First place the docstring immediately after def/class or at the top of a module. Then start with a concise purpose statement. Predict the result before execution and explain one boundary or failure case.
Which approach best demonstrates understanding of Docstrings?
Step 1Place the docstring immediately after def/class or at the top of a module.Step 2Start with a concise purpose statement.Step 3Document parameters/returns when their meaning is not obvious from the signature.Step 4Mention important units, side effects or raised exceptions.