Add a dry-run mode and explicit confirmation before destructive commands.
What It Does and Why It Matters
Dry-run should report exactly which objects a command would change without changing them. For real changes, validate targets before proceeding and require an explicit --yes flag for automation rather than an ambiguous prompt. Keep the preview and execution selection logic consistent so the displayed plan matches the operation.
This example intentionally never deletes files; production tools need explicit safeguards and a separate execution path.
Step-by-Step Example
Start with the complete example below. Read the inputs and predict what the final print statement will show before you run it.
def remove_items(names, *, dry_run=True):
for name in names:
if dry_run:
print("Would remove:", name)
else:
print("Simulated removal:", name)
remove_items(["old.log", "temp.tmp"])
How the Code Works
- The definition of remove_items creates reusable behavior; its body runs only when called or instantiated as appropriate.
- The loop advances through values one at a time. Note when the condition is checked and when the state changes.
- The print calls display the results. Compare these results with the method’s documented return type and the input used.
- With dry_run=True, the loop prints the planned filenames but does not call any filesystem deletion method.
Expected output:
Would remove: old.log
Would remove: temp.tmp
Each displayed line comes from the corresponding calculation or check. The output helps verify the return values before you adapt the example to your own input.
What to Notice
Pay attention to the accepted input types, the returned value and the boundary behavior. Predict what changes if you replace the example input with an empty, zero, negative or missing value, where that value is meaningful for this operation.
If the task uses a library function, inspect its documented exceptions and return type before reusing it inside a larger function.
Where You Would Use This
Use the technique when it makes a real task easier to understand or safer to maintain. Check input and output behavior with small tests before combining it with larger application code.
Practice Exercise
Add an argparse --dry-run/--no-dry-run flag and test that the default mode changes nothing.
Before running your edited example, write down the result you expect. Then test at least one valid boundary or deliberately invalid value, using appropriate exception handling where necessary.
Reference: official Python documentation for the complete API contract and version notes.
Common Beginner Mistakes
- Writing files, deleting data or mutating state while dry-run=True.
- Checking only the easiest input and missing a relevant error or boundary case.
Key Takeaways
- Add a dry-run mode and explicit confirmation before destructive commands.
- Dry-run should report exactly which objects a command would change without changing them.
- Add an argparse --dry-run/--no-dry-run flag and test that the default mode changes nothing.