What are *args and **kwargs, and how do you use them?
Reviewed by Gurusankar M.
Short Answer
`*args` collects extra positional arguments into a tuple; `**kwargs` collects extra keyword arguments into a dict. They let a function accept any number of arguments. The same `*`/`**` also unpack a list/dict back into arguments when calling — handy for forwarding args to another function.
On this page
Concept Made Simple
Coffee Chat Question
“What are *args and **kwargs, and how do you use them?”
Remember It Faster
Mind Map Answer
- *args
- extra positional → tuple
- **kwargs
- extra keyword → dict
- Call side
- *list / **dict unpack into args
- Order
- pos, *args, kw-only, **kwargs
Learn by Doing
Hands-on Keyboard
def log(level, *args, **kwargs):
print(level, args, kwargs)
log("INFO", "a", "b", user="guru", id=7)
# INFO ('a', 'b') {'user': 'guru', 'id': 7}
def add(a, b, c): return a + b + c
nums = [1, 2, 3]
print(add(*nums)) # unpack list → 6INFO ('a', 'b') {'user': 'guru', 'id': 7}
6Think Beyond the Expected
What If?
How do *args/**kwargs help when writing a wrapper or decorator?
They let the wrapper accept and forward ANY arguments to the wrapped function without knowing its signature: `def wrapper(*args, **kwargs): return fn(*args, **kwargs)`. That's exactly why decorators use them — one wrapper works for functions with any parameters.
Real World
Decorators, logging helpers, and thin API wrappers all rely on *args/**kwargs to pass arguments through transparently; unpacking (*/** at the call site) is the flip side used to spread collected data back into calls.
Interviewer's Expectation
Keywords they're listening for:
- *args → tuple
- **kwargs → dict
- accept variable arity
- unpack at call site
- used by decorators/wrappers
Common Mistakes
- Confusing collect (def) vs unpack (call)
- Wrong parameter order in the signature
- Overusing **kwargs and losing a clear API
Best Practices
- Use for wrappers/decorators and flexible APIs
- Prefer explicit params when the signature is known
- Document expected kwargs
Follow-up Questions
- What are keyword-only and positional-only parameters?
- How does argument order/precedence work?
- How does this pair with functools.wraps in decorators?
Continue Learning with AI
Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.
Plain-language foundations
I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner. Topic: Functions (Python) Interview question: "What are *args and **kwargs, and how do you use them?" Please: 1. Explain the core idea in simple, plain language, using an everyday analogy. 2. Define any technical terms you use. 3. Walk through one small, concrete example. 4. Finish with a single sentence I can easily remember. Keep the tone friendly and assume I'm new to this topic.
Was this answer helpful?
Affiliate links
Tools for Your Developer Workspace
Equipment for a comfortable, productive setup.
As an Amazon Associate I earn from qualifying purchases.
- MonitorView on Amazon : BenQ EW2790U 27-inch Monitor (opens in a new tab)
BenQ EW2790U 27-inch Monitor
- LaptopView on Amazon : Acer Aspire One Laptop (opens in a new tab)
Acer Aspire One Laptop
- Office ChairView on Amazon : Wakefit Gravita Ergonomic Office Chair (opens in a new tab)
Wakefit Gravita Ergonomic Office Chair