Medium👤 3-5 years👤 8-15 years 1 min read

What are *args and **kwargs, and how do you use them?

Asked inAmazonDeloitteMicrosoftInfosys
#args#kwargs#variadic#unpacking#functions
Report issue

⚡ 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.

Coffee Chat Question

Concept Made Simple

What are *args and **kwargs, and how do you use them?

🧠Mind Map Answer

Remember It Faster

*argsextra positional → tuple
**kwargsextra keyword → dict
Call side*list / **dict unpack into args
Orderpos, *args, kw-only, **kwargs

⌨️Hands-on Keyboard

Learn by Doing

python
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 → 6
Output
INFO ('a', 'b') {'user': 'guru', 'id': 7}
6

🔥What If?

Think Beyond the Expected

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 → dictaccept variable arityunpack at call siteused 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

  • 1What are keyword-only and positional-only parameters?
  • 2How does argument order/precedence work?
  • 3How does this pair with functools.wraps in decorators?

🧩Related Technologies

decoratorsfunctools.wrapsunpacking

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.
Open inChatGPTGeminiClaude

Was this answer helpful?

Support our platform by exploring our recommended products.

As an Amazon affiliate, purchases through these links may earn us a small commission — at no extra cost to you. It helps keep Full Stack Interview Guru free.

Related Questions