*args & **kwargsIntermediate
1Question 1 of 1
pythonHow do *args and **kwargs handle variable positional and keyword arguments?
Explanation & Concept
• `*args`: Collects extra positional arguments passed to a function into a `tuple`. • `**kwargs`: Collects extra keyword (named) arguments into a `dict`. • They can also be used as unpack operators when calling functions.
Code Example
python
def log_event(event_type, *tags, **metadata):
print(f"Event: {event_type}")
print(f"Tags tuple: {tags}")
print(f"Metadata dict: {metadata}")
log_event("USER_SIGNUP", "mobile", "ios", user_id=402, ip="192.168.1.1")
# Output:
# Event: USER_SIGNUP
# Tags tuple: ('mobile', 'ios')
# Metadata dict: {'user_id': 402, 'ip': '192.168.1.1'}How It Works & Key Takeaways
`*args` and `**kwargs` allow writing flexible wrapper functions, decorators, and generic API clients.
Mock Test · 1 Questions · Optional
Ready for Iterators & Generators?
Take a quick test to evaluate yourself, or move straight to the next module.
Intermediate1 questions