-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path07_args_kwargs.py
More file actions
30 lines (26 loc) · 988 Bytes
/
Copy path07_args_kwargs.py
File metadata and controls
30 lines (26 loc) · 988 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 07_args_kwargs.py
# Concept: Handling variable number of arguments
def make_pizza(size, *toppings):
"""
Summarize the pizza we are about to make.
*toppings collects as many positional arguments as the caller provides into a tuple.
"""
print(f"\nMaking a {size}-inch pizza with the following toppings:")
for topping in toppings:
print(f"- {topping}")
def build_profile(first, last, **user_info):
"""
Build a dictionary containing everything we know about a user.
**user_info collects arbitrary keyword arguments into a dictionary.
"""
user_info['first_name'] = first
user_info['last_name'] = last
return user_info
# Using *args
make_pizza(12, 'pepperoni')
make_pizza(16, 'mushrooms', 'green peppers', 'extra cheese')
# Using **kwargs
user_profile = build_profile('albert', 'einstein',
location='princeton',
field='physics')
print(f"\nUser Profile: {user_profile}")