Skip to content

Conversation

@martin-martin
Copy link
Contributor

Where to put new files:

  • New files should go into a top-level subfolder, named after the article slug. For example: my-awesome-article

How to merge your changes:

  1. Make sure the CI code style tests all pass (+ run the automatic code formatter if necessary).
  2. Find an RP Team member on Slack and ask them to review & approve your PR.
  3. Once the PR has one positive ("approved") review, GitHub lets you merge the PR.
  4. 🎉

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@martin-martin
Copy link
Contributor Author

@bzaczynski Copilot suggested to add quantity checks to the two functions that add to the shopping list via *args and **kwargs. I accepted, I think it makes sense, but decided not to make that same update in the tutorial text.

IMO the tutorial focuses the code on the essential aspect (how do *args and **kwargs work, with an example) so didactically it makes sense to keep those functions as small as possible. However, I don't mind readers getting the (slightly) more code with the extra check with their materials download and reason about the difference themselves. Seems like a helpful learning experience. LMK if you disagree :)

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 = {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔧 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?

Copy link
Contributor

@bzaczynski bzaczynski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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!

@bzaczynski bzaczynski merged commit f980b2b into master Oct 5, 2025
1 check passed
@bzaczynski bzaczynski deleted the optional-arguments branch October 5, 2025 15:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants