Python Series β Part 10: *args, **kwargs & Function Parameter Magic
π₯ Introduction
"def my_func(*args, **kwargs)... Ye line Python ke har intermediate coder ne dekhi hai.
Lekin iska real magic kya hai? Kya ye sirf multiple arguments lene ke liye hai ya kuch aur bhi chhupa hua hai?
Aaj hum sikhenge ye *args, **kwargs ka jaadu.
Real-life examples, memory level understanding, aur kuch hidden concepts bhi!
Letβs dive deep into the magic of Python function parameters."
π Section 1: Basics of Function Parameters
def greet(name, age):
print(f"Hello {name}, you are {age} years old.")
"Fixed parameters. Tumhe jitne likhe, utne hi dene padenge.
Lekin agar tum chahte ho flexibility, tab entry hoti hai
*args
aur**kwargs
ki."
πΈ Section 2: *args β Positional Arguments
def add_numbers(*nums):
print(nums) # tuple
print(sum(nums)) # sum all
"*args ka matlab: extra positional arguments ko tuple bana do.
Tum 2 do ya 10 β function chill rahega."
Example:
add_numbers(10, 20, 30)
# Output: (10, 20, 30)
# 60
"Loop bhi laga sakte ho uspar β it's dynamic."
πΈ Section 3: **kwargs β Keyword Arguments
def display_info(**data):
print(data)
"**kwargs = extra named arguments ko dict me convert kar do.
Perfect for config-driven functions."
Example:
display_info(name="Amit", age=23, city="Delhi")
# Output: {'name': 'Amit', 'age': 23, 'city': 'Delhi'}
πΈ Section 4: *args + **kwargs Together
def mixed(a, *args, b=10, **kwargs):
print(a, args, b, kwargs)
"Tum fixed + dynamic + keyword sab combine kar sakte ho. Order matters!"
Correct Order:
def fun(pos, *args, kwd=0, **kwargs):
πΈ Section 5: args Unpacking
def fun(a, b, c):
print(a, b, c)
data = (1, 2, 3)
fun(*data)
"*args ko call karte waqt bhi unpack kar sakte ho. Useful when arguments are dynamic."
πΈ Section 6: kwargs Unpacking
info = {"name": "Ravi", "age": 25}
display_info(**info)
"**kwargs se dict ko named parameters ke form me pass karte hain. Helpful in APIs, configs, JSON."
πΉ Section 7: Real-Life Example
def log_event(event_type, *details, **meta):
print(f"[{event_type}] β Details: {details} | Metadata: {meta}")
log_event("LOGIN", "user123", ip="192.168.1.1", device="mobile")
"Real use in logging, analytics, decorators."
β Section 8: Interview Trick & Gotchas
- *args = tuple
- **kwargs = dict
- Correct order:
def fun(a, *args, b=0, **kwargs)
- You can't use multiple
*
- Positional first, then keyword
π§ Section 9: Behind the Scenes
"Python packs extra arguments into tuple/dict using * / ** operator.
Interpreter handles packing on definition, unpacking on call."
β Outro + Practice Questions
"Ab tum *args, **kwargs, aur parameter order ke master ho.
Practice karo β tabhi depth samajh aayegi."
Practice:
- Write function that accepts any number of numbers and returns average.
- Logger function that logs via args and kwargs
- Decorator that works with arbitrary args
π€ See You:
"Next video hoga Python String Formatting ka jaadu β old style %, format(), aur modern f-strings tak!
Toh channel ko subscribe karo, aur agar kuch doubt ho to comment section me chhod dena! π"