Skip to content

Commit f721b0f

Browse files
committed
Add supporting code
1 parent 5704140 commit f721b0f

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Using Python Optional Arguments When Defining Functions
2+
3+
This folder contains accompanying code to the Real Python tutorial on [Using Python Optional Arguments When Defining Functions](https://realpython.com/python-optional-arguments/).
4+
5+
You can read each file and its comments, and run the files to see the code's output.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
"""
2+
Optional arguments in Python — consolidated, runnable examples.
3+
4+
This module collects the tutorial's final, good-practice implementations:
5+
6+
- show_list(shopping_list, include_quantities=True)
7+
- add_item(item_name, quantity, shopping_list=None)
8+
- add_items_args(shopping_list, *item_names)
9+
- add_items_kwargs(shopping_list, **things_to_buy)
10+
11+
Run the module directly to see a short demo.
12+
"""
13+
14+
15+
def show_list(shopping_list, include_quantities=True):
16+
print()
17+
for item_name, quantity in shopping_list.items():
18+
if include_quantities:
19+
print(f"{quantity}x {item_name}")
20+
else:
21+
print(item_name)
22+
23+
24+
def add_item(item_name, quantity, shopping_list=None):
25+
"""Add (or increment) an item in a list using the safe 'None' default."""
26+
if shopping_list is None:
27+
shopping_list = {}
28+
if item_name in shopping_list:
29+
shopping_list[item_name] += quantity
30+
else:
31+
shopping_list[item_name] = quantity
32+
return shopping_list
33+
34+
35+
def add_items_args(shopping_list, *item_names):
36+
"""Add any number of item names with default quantity 1 using *args."""
37+
for item_name in item_names:
38+
shopping_list[item_name] = 1
39+
return shopping_list
40+
41+
42+
def add_items_kwargs(shopping_list, **things_to_buy):
43+
"""Add any number of items with explicit quantities using **kwargs."""
44+
for item_name, quantity in things_to_buy.items():
45+
shopping_list[item_name] = quantity
46+
return shopping_list
47+
48+
49+
# --- Using required + optional parameters (safe default pattern) ---
50+
hardware_store_list = {}
51+
hardware_store_list = add_item("Nails", 1, hardware_store_list)
52+
hardware_store_list = add_item("Screwdriver", 1, hardware_store_list)
53+
hardware_store_list = add_item("Glue", 3, hardware_store_list)
54+
55+
supermarket_list = {}
56+
supermarket_list = add_item("Bread", 1, supermarket_list)
57+
supermarket_list = add_item("Milk", 2, supermarket_list)
58+
59+
show_list(hardware_store_list) # With quantities
60+
show_list(supermarket_list, False) # Names only
61+
62+
# Create new lists on the fly by omitting shopping_list
63+
clothes_shop_list = add_item(
64+
"Shirt", 3
65+
) # New dict created inside the function
66+
electronics_store_list = add_item("USB cable", 1) # New dict created again
67+
show_list(clothes_shop_list)
68+
show_list(electronics_store_list)
69+
70+
# --- Using *args to add many items at once (defaults quantity to 1) ---
71+
multi_add_list = {}
72+
multi_add_list = add_items_args(
73+
multi_add_list, "Coffee", "Tea", "Cake", "Bread"
74+
)
75+
show_list(multi_add_list)
76+
77+
# --- Using **kwargs to add items with explicit quantities ---
78+
kw_list = {}
79+
kw_list = add_items_kwargs(kw_list, coffee=1, tea=2, cake=1, bread=3)
80+
show_list(kw_list)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""
2+
Demonstrates why using a mutable default (like {}) is a bad idea.
3+
4+
This mirrors the tutorial's buggy example so you can reproduce the issue.
5+
Run this file directly to see both variables share the same underlying dict.
6+
"""
7+
8+
9+
def add_item(item_name, quantity, shopping_list={}):
10+
# BAD: the default dict is created once and reused
11+
if item_name in shopping_list:
12+
shopping_list[item_name] += quantity
13+
else:
14+
shopping_list[item_name] = quantity
15+
return shopping_list
16+
17+
18+
clothes_shop_list = add_item("Shirt", 3) # Uses the shared default dict
19+
electronics_store_list = add_item("USB cable", 1) # Same shared dict!
20+
21+
print("\nclothes_shop_list:")
22+
for k, v in clothes_shop_list.items():
23+
print(f"{v}x {k}")
24+
25+
print("\nelectronics_store_list:")
26+
for k, v in electronics_store_list.items():
27+
print(f"{v}x {k}")
28+
29+
print(
30+
"\nNote how both lists contain the same combined items "
31+
"due to the shared default."
32+
)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"""
2+
Unpacking operator demo to support the *args discussion.
3+
"""
4+
5+
some_items = ["Coffee", "Tea", "Cake", "Bread"]
6+
7+
print("Passing the list as a single argument:")
8+
print(some_items) # -> ['Coffee', 'Tea', 'Cake', 'Bread']
9+
10+
print("\nUnpacking the list with *some_items:")
11+
print(*some_items) # -> Coffee Tea Cake Bread

0 commit comments

Comments
 (0)