Sitemap
Moksh Mahawar

Moksh Mahawar

Making code readable, maintainable, and joyful. If you’re new here, welcome—you’re who I write for.

The Lore Of Python

*args

2 min readSep 24, 2025

--

Press enter or click to view image in full size

What is *args in layman terms ?

Imagine you’re the one in your family who goes out to buy groceries. Your mother gives you a list — sometimes 3 items, sometimes 10. You don’t say, “Mom, please stick to only 5 items.” (Don’t try if you’re alive💀).

Get Moksh Mahawar’s stories in your inbox

Join Medium for free to get updates from this writer.

You simply take whatever she gives and go buy it.

That’s exactly what *args does in Python.

It lets a function accept any number of inputs — just like your grocery list. Whether it’s 2 or 20 items, the function handles them all without complaint.

So, What is *args in professional terms ?

In Python, *args allows a function to accept a variable number of positional arguments. It collects all extra arguments passed to the function into a tuple, enabling flexible and dynamic function calls without requiring a fixed number of inputs.

This is especially useful when:

  • You don’t know in advance how many arguments a user might pass.
  • You want to build reusable, adaptable functions.
  • You’re designing APIs, decorators, or wrappers that need to forward arguments.

for example

def log_events(*args):
for event in args:
print("Event:", event)

log_events("Login", "File Upload", "Logout")

Output:

Event: Login
Event: File Upload
Event: Logout

Can’t I just take a list as an input rather than *args?

Yes, you can pass a list to a function — but the way you call the function and how it receives the data will be different

1. Using a list

def myFunction(items):
print("User gave input: ", items)

myFunction(["item_1", "item_2", "item_3"])
# Here, the function gets one argument - a list.

Output

User gave input: ['item_1', 'item_2', 'item_3']

2. Using *args

def that_function(*items):
print("User gave input: ", items)

that_function("item_1", "item_2", "item_3")
# Here, the function gets three separate arguments, collected into a tuple.

Output

User gave input: ('item_1', 'item_2', 'item_3')

Why Use *args Then

  • By using *args function to accept any number of arguments directly,
  • We can unpack a list and pass it into a*args function, as follows:
my_list = ["item_1", "item_2", "item_3"]
that_function(*my_list)
  • This gives you the best of both worlds — flexibility and clarity

Didn’t *args made the traditional method deprecated ?

Not at all. Both have their place

Use Traditional Arguments When:

  • You want exactly 2 or 3 inputs.
  • You want to validate or enforce specific parameters.
  • You want clarity and simplicity for fixed tasks
def greet(name, time_of_day):
print(f"Good {time_of_day}, {name}!")

Use *args When:

  • You want to accept any number of inputs.
  • You’re building reusable, generic, or helper functions.
  • You don’t want to restrict the caller.
def greet_all(*names):
for name in names:
print(f"Hello, {name}!")

--

Moksh Mahawar
Moksh Mahawar

Written by Moksh Mahawar

Making code readable, maintainable, and joyful. If you’re new here, welcome—you’re who I write for.