Python Function Parameter Magic ✨ | args, kwargs Deep Explainer #10 || Python ka Pitara || DeathCode - DeathCode

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:

  1. Write function that accepts any number of numbers and returns average.
  2. Logger function that logs via args and kwargs
  3. 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! πŸš€"

Β© 2024 DeathCode. All Rights Reserved.