*args and **kwargs explained in simple way
KARTHIK NARAYAN
Posted on August 16, 2023
let's break down *args and **kwargs using some fun and relatable examples!
1: *args - The Ice Cream Sundae of Arguments
Imagine you're at an ice cream parlour, and you want to create your ultimate sundae with as many toppings as you like. Well, *args is like those endless toppings. You don't know how many you'll choose, and you don't need to specify them all upfront.
def make_sundae(base_flavor, *toppings):
sundae = f"A {base_flavor} sundae with:"
for topping in toppings:
sundae += f" {topping}"
return sundae
print(make_sundae("vanilla", "chocolate sauce", "sprinkles", "whipped cream"))
Output:
A vanilla sundae with: chocolate sauce sprinkles whipped cream
In this example, the *toppings
parameter collects all the extra toppings you provide and bundles them into a tuple. It's like making a sundae with any number of toppings without knowing exactly how many.
2: **kwargs - The Party Planner of Arguments
Now, let's plan a party using **kwargs. You're the host, and you want everything to be just right, from decorations to music. **kwargs is like having a checklist of items with their specific details.
def plan_party(**details):
plan = "Party Details:"
for key, value in details.items():
plan += f"\n- {key}: {value}"
return plan
print(plan_party(theme="tropical", guests=50, music="DJ Bob", snacks="finger foods"))
Output:
Party Details:
- theme: tropical
- guests: 50
- music: DJ Bob
- snacks: finger foods
In this example, the **details
parameter collects the named arguments and their values into a dictionary. It's like planning a party with a bunch of different aspects, each with its specific information.
3: Combining *args
and **kwargs
- The Deadly-Act Show
Now, let's bring out the big guns and combine *args and **kwargs in a single function. Imagine you're ordering a customizable burger with various toppings, and you also want to note down specific preferences.
def custom_burger(base, *toppings, **preferences):
burger = f"You ordered a {base} burger with:"
for topping in toppings:
burger += f" {topping}"
burger += "\nPreferences:"
for key, value in preferences.items():
burger += f"\n- {key}: {value}"
return burger
print(custom_burger("beef", "cheese", "bacon", dressing="mayo", sides="fries"))
Output:
You ordered a beef burger with: cheese bacon
Preferences:
- dressing: mayo
- sides: fries
In this example, you're using both *args and **kwargs in the same function. The *toppings
collects extra toppings as a tuple, while **preferences
collects named preferences as a dictionary. It's like crafting a burger with various toppings and specifying your unique preferences.
And there you have it! *args and **kwargs are your dynamic duos for handling flexible function arguments in Python. Just like creating custom sundaes, planning parties, or building the perfect burger, these concepts let you handle a variety of inputs with flair and finesse.
Until next time, keep coding and keep smiling, because Python is all about making the magic happen! 🐍✨
Happy coding!!!
Posted on August 16, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.