Add supporting code for Python Optional Arguments tutorial#702
Add supporting code for Python Optional Arguments tutorial#702bzaczynski merged 5 commits intomasterfrom
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR adds supporting code files for a Real Python tutorial on Python optional arguments. The code demonstrates various patterns for working with optional parameters, including proper handling of mutable defaults and the use of *args and **kwargs.
- Provides working examples of safe optional parameter patterns using None defaults
- Demonstrates the mutable default bug and how to avoid it
- Includes examples of variadic functions using *args and **kwargs
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| README.md | Provides overview and instructions for the tutorial code |
| optional_params.py | Main examples showing proper optional parameter patterns |
| optional_params_mutable_default_bug.py | Demonstrates the dangerous mutable default antipattern |
| optional_params_unpacking_demo.py | Simple demonstration of argument unpacking with * operator |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
|
@bzaczynski Copilot suggested to add quantity checks to the two functions that add to the shopping list via IMO the tutorial focuses the code on the essential aspect (how do |
| def add_item(item_name, quantity, shopping_list=None): | ||
| """Add (or increment) an item in a list using the safe 'None' default.""" | ||
| if shopping_list is None: | ||
| shopping_list = {} |
There was a problem hiding this comment.
🔧 Technical Recommendation
The name shopping_list may imply Python's list data structure, while we actually use the variable to store a Python dictionary. How about renaming this variable to shopping_items or item_quantities instead?
bzaczynski
left a comment
There was a problem hiding this comment.
@martin-martin This looks good to me. I'd only consider renaming some of the variables for better clarity, but it's not a big deal at all!
Where to put new files:
my-awesome-articleHow to merge your changes: