Python args and kwargs in Hindi ๐ฅ | Functions เคฎें Arguments เคैเคธे Pass เคเคฐें?
Python args เคเคฐ kwargs - Functions เคฎें Arguments Pass เคเคฐเคจे เคा Complete Guide
Python เคฎें *args เคเคฐ **kwargs เคฆो เคธเคฌเคธे powerful เคเคฐ useful features เคนैं เคो เคเคชเคो flexible functions เคฌเคจाเคจे เคฎें เคฎเคฆเคฆ เคเคฐเคคे เคนैं। เค
เคเคฐ เคเคช Python เคธीเค เคฐเคนे เคนैं เคคो เคฏเคน concepts เคฌเคนुเคค เคเคฐूเคฐी เคนैं, เคाเคธเคเคฐ เคเคฌ เคเคช real-world projects, libraries เคฏा frameworks เคा code เคชเคข़เคคे เคนैं। เคเคธ tutorial เคฎें เคนเคฎ เคเคจ เคฆोเคจों เคो step-by-step, เคขेเคฐ เคธाเคฐे real examples เคे เคธाเคฅ เคธเคฎเคेंเคे — เคฌिเคฒ्เคुเคฒ เคถुเคฐुเคเคค เคธे เคฒेเคเคฐ advanced use cases เคคเค।
args เคเคฐ kwargs เค्เคฏा เคนैं?
args เคा เคฎเคคเคฒเคฌ เคนै "Arguments" เคเคฐ kwargs เคा เคฎเคคเคฒเคฌ เคนै "Keyword Arguments"। เคฏे เคฆोเคจों เคเคชเคो เคเค function เคฎें variable number of arguments pass เคเคฐเคจे เคी เคธुเคตिเคงा เคฆेเคคे เคนैं — เคฏाเคจी เคเคชเคो เคชเคนเคฒे เคธे fix เคจเคนीं เคเคฐเคจा เคชเคก़เคคा เคि function เคो เคिเคคเคจे arguments เคฎिเคฒेंเคे। เคฏเคน เคाเคธเคเคฐ เคคเคฌ เคाเคฎ เคเคคा เคนै เคเคฌ เคเคช เคเคธा function เคฌเคจा เคฐเคนे เคนों เคो เค เคฒเค-เค เคฒเค situations เคฎें เค เคฒเค-เค เคฒเค number of inputs handle เคเคฐे।
*args- Non-keyword (positional) arguments เคो tuple เคे เคฐूเคช เคฎें pass เคเคฐเคคा เคนै**kwargs- Keyword arguments เคो dictionary เคे เคฐूเคช เคฎें pass เคเคฐเคคा เคนै
Normal function เคฎें parameters เคी เคธंเค्เคฏा fix เคนोเคคी เคนै — เค
เคเคฐ เคเคชเคจे 2 parameters define เคिเค, เคคो call เคเคฐเคคे เคธเคฎเคฏ exactly 2 arguments เคนी เคฆेเคจे เคนोंเคे, เคตเคฐเคจा Python error เคฆेเคा। เคฏเคนी limitation *args เคเคฐ **kwargs เคธे เคฆूเคฐ เคนो เคाเคคी เคนै।
Normal Function เคी Limitation เคชเคนเคฒे เคธเคฎเคें
def add_two(a, b):
return a + b
print(add_two(5, 10)) # เค ीเค เคเคฒेเคा — Output: 15
print(add_two(5, 10, 15)) # Error! TypeError: add_two() takes 2 positional arguments but 3 were given
เคฏเคนाँ problem clear เคนै — function เคธिเคฐ्เคซ 2 values เคे เคฒिเค design เคนुเค เคนै। เค
เคเคฐ เคนเคฎें 2, 5 เคฏा 100 numbers เคोเคก़เคจे เคนों, เคคो เคนเคฎें เคเค flexible เคคเคฐीเคा เคाเคนिเค। เคฏเคนीं เคธे *args เคाเคฎ เคเคคा เคนै।
*args เคो เคธเคฎเคเคคे เคนैं
*args เคा use เคคเคฌ เคเคฐเคคे เคนैं เคเคฌ เคเคชเคो เคชเคนเคฒे เคธे เคชเคคा เคจเคนीं เคนोเคคा เคि function เคो เคिเคคเคจे arguments เคฎिเคฒेंเคे। Python เคเคจ เคธเคญी arguments เคो เคเค tuple เคฎें convert เคเคฐ เคฆेเคคा เคนै, เคिเคธे เคเคช loop เคเคฐเคे เคเคธाเคจी เคธे access เคเคฐ เคธเคเคคे เคนैं।
*args เคा Basic Example:
def print_args(*args):
print("Arguments type:", type(args))
print("Arguments:", args)
for arg in args:
print(f"Argument: {arg}")
# Function เคो call เคเคฐเคคे เคนैं
print_args(10, 20, 30, "Python")
print_args("Hello", "World")
Output:
Arguments type: <class 'tuple'>
Arguments: (10, 20, 30, 'Python')
Argument: 10
Argument: 20
Argument: 30
Argument: Python
Arguments type: <class 'tuple'>
Arguments: ('Hello', 'World')
Argument: Hello
Argument: World
*args เคे เคธाเคฅ Sum เคा Example:
def add_numbers(*args):
total = 0
for num in args:
total += num
return total
# เค
เคฒเค-เค
เคฒเค arguments เคे เคธाเคฅ call เคเคฐเคคे เคนैं
print(add_numbers(5, 10)) # Output: 15
print(add_numbers(1, 2, 3, 4, 5)) # Output: 15
print(add_numbers(100, 200, 300)) # Output: 600
print(add_numbers()) # Output: 0 (empty tuple)
เคฏเคน example เคฌเคคाเคคा เคนै เคि *args เคे เค
ंเคฆเคฐ เคนเคฎ เคเค normal loop เคเคฒाเคเคฐ เคนเคฐ value เคो เคเคธाเคจी เคธे access เคเคฐ เคธเคเคคे เคนैं, เคाเคนे arguments เคी เคธंเค्เคฏा เคुเค เคญी เคนो — เคฏเคนाँ เคคเค เคि zero arguments เคญी valid เคนै।
*args เคा เคเค เคเคฐ Real Example — Maximum Number เคจिเคाเคฒเคจा:
def find_max(*args):
if not args:
return None
biggest = args[0]
for num in args[1:]:
if num > biggest:
biggest = num
return biggest
print(find_max(4, 9, 2, 7)) # Output: 9
print(find_max(15, 3, 100, 22)) # Output: 100
**kwargs เคो เคธเคฎเคเคคे เคนैं
**kwargs keyword arguments เคो handle เคเคฐเคคा เคนै। เคเคธเคा use เคคเคฌ เคเคฐเคคे เคนैं เคเคฌ เคเคช named arguments pass เคเคฐเคจा เคाเคนเคคे เคนैं। เคฏे เคธเคญी keyword arguments เคो เคเค dictionary เคฎें convert เคเคฐ เคฆेเคคा เคนै, เคिเคธเคฎें key argument เคा เคจाเคฎ เคนोเคคा เคนै เคเคฐ value เคเคธเคी value।
**kwargs เคा Basic Example:
def print_kwargs(**kwargs):
print("Kwargs type:", type(kwargs))
print("Kwargs:", kwargs)
for key, value in kwargs.items():
print(f"{key}: {value}")
# Function เคो call เคเคฐเคคे เคนैं
print_kwargs(name="Raj", age=25, city="Delhi")
print_kwargs(subject="Python", level="Beginner", duration="2 months")
Output:
Kwargs type: <class 'dict'>
Kwargs: {'name': 'Raj', 'age': 25, 'city': 'Delhi'}
name: Raj
age: 25
city: Delhi
Kwargs type: <class 'dict'>
Kwargs: {'subject': 'Python', 'level': 'Beginner', 'duration': '2 months'}
subject: Python
level: Beginner
duration: 2 months
**kwargs เคा Practical Example — Student Profile:
def student_profile(**kwargs):
for key, value in kwargs.items():
print(f"{key.capitalize()}: {value}")
print("-" * 30)
student_profile(name="Ankit", course="Python", city="Lucknow")
student_profile(name="Priya", course="Data Analyst", marks=92)
เคฏเคนाँ เคนเคฐ เคฌाเคฐ function เคो เค
เคฒเค-เค
เคฒเค keys เคे เคธाเคฅ call เคिเคฏा เคा เคธเคเคคा เคนै, เคฌिเคจा function เคी definition เคฌเคฆเคฒे — เคฏเคนी **kwargs เคी เค
เคธเคฒी เคคाเคเคค เคนै। เค
เคเคฐ เคเคช normal parameters เคธे เคฏเคนी เคीเค़ เคเคฐเคจा เคाเคนें, เคคो เคนเคฐ student เคे เคฒिเค เค
เคฒเค-เค
เคฒเค number of parameters เคी เคเคฐूเคฐเคค เคชเคก़ेเคी, เคो possible เคจเคนीं เคนै।
**kwargs เคธे Default Values Handle เคเคฐเคจा (get method):
def create_user(**kwargs):
username = kwargs.get("username", "Guest")
role = kwargs.get("role", "User")
print(f"Username: {username}, Role: {role}")
create_user(username="ankit123", role="Admin")
create_user(username="raj99") # role เคจเคนीं เคฆिเคฏा, default "User" use เคนोเคा
create_user() # เคฆोเคจों default values use เคนोंเคी
Output:
Username: ankit123, Role: Admin
Username: raj99, Role: User
Username: Guest, Role: User
*args เคเคฐ **kwargs เคो เคเค เคธाเคฅ Use เคเคฐเคจा
Real projects เคฎें เค
เค्เคธเคฐ เคฆोเคจों เคो เคธाเคฅ use เคिเคฏा เคाเคคा เคนै। Python เคฎें rule เคฏเคน เคนै เคि เคชเคนเคฒे *args เคเคคा เคนै, เคซिเคฐ **kwargs।
def create_employee(*args, **kwargs):
print("Positional Arguments:", args)
print("Keyword Arguments:", kwargs)
create_employee("Ankit", "Maurya", role="Developer", experience=3)
Output:
Positional Arguments: ('Ankit', 'Maurya')
Keyword Arguments: {'role': 'Developer', 'experience': 3}
Normal Parameters เคे เคธाเคฅ Order:
เค
เคเคฐ เคเคช normal parameters, *args เคเคฐ **kwargs เคคीเคจों เคธाเคฅ use เคเคฐ เคฐเคนे เคนैं, เคคो order fix เคนोเคคा เคนै:
def full_example(a, b, *args, **kwargs):
print("a:", a)
print("b:", b)
print("args:", args)
print("kwargs:", kwargs)
full_example(1, 2, 3, 4, 5, city="Lucknow", country="India")
Output:
a: 1
b: 2
args: (3, 4, 5)
kwargs: {'city': 'Lucknow', 'country': 'India'}
Order เคนเคฎेเคถा เคฏเคนी เคฐเคนเคคा เคนै: normal parameters → *args → **kwargs। เค
เคเคฐ เคเคช order เคฌเคฆเคฒेंเคे — เคैเคธे **kwargs เคो *args เคธे เคชเคนเคฒे เคฒिเคेंเคे — เคคो Python SyntaxError เคฆेเคा।
Function Call เคเคฐเคคे เคธเคฎเคฏ *args เคเคฐ **kwargs (Unpacking)
*args เคเคฐ **kwargs เคธिเคฐ्เคซ function define เคเคฐเคคे เคธเคฎเคฏ เคนी เคจเคนीं, เคฌเคฒ्เคि function call เคเคฐเคคे เคธเคฎเคฏ เคญी use เคนोเคคे เคนैं — เคเคธे unpacking เคเคนเคคे เคนैं। เคเคธเคा เคซाเคฏเคฆा เคคเคฌ เคฎिเคฒเคคा เคนै เคเคฌ เคเคชเคा data เคชเคนเคฒे เคธे เคเค list, tuple เคฏा dictionary เคฎें เคฎौเคूเคฆ เคนो।
def add(a, b, c):
return a + b + c
numbers = [10, 20, 30]
print(add(*numbers)) # List เคो unpack เคเคฐเคे pass เคिเคฏा — Output: 60
data = {"a": 1, "b": 2, "c": 3}
print(add(**data)) # Dictionary เคो unpack เคเคฐเคे pass เคिเคฏा — Output: 6
เคฏเคน pattern เคाเคธเคคौเคฐ เคชเคฐ เคคเคฌ เคฌเคนुเคค useful เคนोเคคा เคนै เคเคฌ เคเคชเคा data เคชเคนเคฒे เคธे เคिเคธी list เคฏा string เคธे process เคนोเคเคฐ เค เคฐเคนा เคนो เคเคฐ เคเคธे เคธीเคงे function เคฎें pass เคเคฐเคจा เคนो — เคฌिเคจा เคนเคฐ value manually เคฒिเคे।
*args, **kwargs เคเคฐ Class Inheritance (super())
เคเค เคฌเคนुเคค common real-world use case เคนै — เคเคฌ เคนเคฎ เคिเคธी parent class เคो inherit เคเคฐเคคे เคนैं เคเคฐ เคเคธเคे __init__ method เคो เค
เคฒเค-เค
เคฒเค เคคเคฐเคน เคे arguments pass เคเคฐเคจे เคนोเคคे เคนैं, เคคो *args, **kwargs เคฌเคนुเคค เคाเคฎ เคเคคे เคนैं।
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
class Employee(Person):
def __init__(self, *args, salary, **kwargs):
super().__init__(*args, **kwargs)
self.salary = salary
emp = Employee("Ankit", 28, salary=45000)
print(emp.name, emp.age, emp.salary)
Output:
Ankit 28 45000
เคฏเคนाँ Employee class เคो เคฏเคน เคाเคจเคจे เคी เคเคฐूเคฐเคค เคจเคนीं เคि Person class เคो exactly เคिเคคเคจे เคฏा เคौเคจ-เคธे arguments เคाเคนिเค — *args, **kwargs เคเคจ्เคนें automatically เคเคे pass เคเคฐ เคฆेเคคे เคนैं।
*args, **kwargs เคเคฐ Decorators
Decorators เคฎें เคญी เคฏเคน concept เคฌเคนुเคค เคเคฐूเคฐी เคนै, เค्เคฏोंเคि decorator เคो เคจเคนीं เคชเคคा เคนोเคคा เคि เคिเคธ function เคो เคตो wrap เคเคฐ เคฐเคนा เคนै, เคเคธเคे parameters เค्เคฏा เคนैं।
def my_decorator(func):
def wrapper(*args, **kwargs):
print("Function เคถुเคฐू เคนो เคฐเคนा เคนै...")
result = func(*args, **kwargs)
print("Function เคเคค्เคฎ เคนुเค।")
return result
return wrapper
@my_decorator
def greet(name, greeting="Namaste"):
print(f"{greeting}, {name}!")
greet("Ankit")
greet("Priya", greeting="Hello")
Output:
Function เคถुเคฐू เคนो เคฐเคนा เคนै...
Namaste, Ankit!
Function เคเคค्เคฎ เคนुเค।
Function เคถुเคฐू เคนो เคฐเคนा เคนै...
Hello, Priya!
Function เคเคค्เคฎ เคนुเค।
เคเคธी เคตเคเคน เคธे เคฒเคเคญเค เคนเคฐ generic decorator เคฎें เคเคชเคो *args, **kwargs เคเคฐूเคฐ เคฆिเคेเคा — เคฏเคน decorator เคो เคिเคธी เคญी เคคเคฐเคน เคे function เคे เคธाเคฅ เคाเคฎ เคเคฐเคจे เคฒाเคฏเค เคฌเคจा เคฆेเคคा เคนै।
*args เคเคฐ **kwargs — Common Mistakes
- Wrong order:
**kwargsเคो*argsเคธे เคชเคนเคฒे เคฒिเคเคจा — เคฏเคน เคนเคฎेเคถा SyntaxError เคฆेเคा। - Naming confusion: เค
เคธเคฒ เคฎें เคเคฐूเคฐी เคธिเคฐ्เคซ
*เคเคฐ**symbols เคนैं, เคจाเคฎ เคुเค เคญी เคนो เคธเคเคคा เคนै (เคैเคธे*values,**info) — เคชเคฐ convention เคे เคนिเคธाเคฌ เคธेargs/kwargsเคนी use เคเคฐें เคคाเคि code เคฌाเคिเคฏों เคो เคญी readable เคฒเคे। - Keyword argument เคฆो เคฌाเคฐ เคฆेเคจा: เคैเคธे
create_employee(name="Ankit", name="Raj")— เคฏเคน TypeError เคฆेเคा। - Positional argument keyword เคे เคฌाเคฆ เคฆेเคจा: เคैเคธे
func(a=1, 2)— เคฏเคน เคญी SyntaxError เคนै, keyword arguments เคนเคฎेเคถा positional arguments เคे เคฌाเคฆ เคนी เคเคคे เคนैं। - *args เคฎें index เคธे over-access เคเคฐเคจा: เค
เคเคฐ args เคฎें เคธिเคฐ्เคซ 2 values เคนैं เคเคฐ เคเคช
args[5]access เคเคฐेंเคे, เคคो IndexError เคเคเคा — เคเคธीเคฒिเค loop เคฏा length check เคเคฐเคจा safe เคฐเคนเคคा เคนै।
*args เคเคฐ **kwargs เคเคฌ Use เคเคฐें?
- เคเคฌ function เคो เคिเคคเคจे arguments เคฎिเคฒेंเคे, เคฏเคน เคชเคนเคฒे เคธे เคชเคคा เคจ เคนो
- Wrapper functions เคเคฐ decorators เคฌเคจाเคคे เคธเคฎเคฏ (เคนเคฐ เคคเคฐเคน เคे arguments เคเคे pass เคเคฐเคจे เคे เคฒिเค)
- เคเคฌ เคเคช เคเค flexible API เคฏा utility function design เคเคฐ เคฐเคนे เคนों
- Class เคे
__init__method เคฎें เคเคฌ parent class เคो dynamic arguments pass เคเคฐเคจे เคนों - เคเคฌ เคเคช เคिเคธी existing function เคे เคเคชเคฐ เคเค extra layer เคฌเคจा เคฐเคนे เคนों (เคैเคธे logging, caching, validation)
Quick Summary Table
| Feature | *args | **kwargs |
|---|---|---|
| Data Type | Tuple | Dictionary |
| Use Case | Positional arguments | Keyword (named) arguments |
| Symbol | Single star (*) | Double star (**) |
| Access | Index / loop | Key-value pair (.items()) |
| Order in Function | Normal params เคे เคฌाเคฆ | *args เคे เคฌाเคฆ, เคธเคฌเคธे last เคฎें |
| Unpacking Symbol (call time) | * (list/tuple) | ** (dictionary) |
FAQ - Python args เคเคฐ kwargs เคธे เคुเคก़े เคธเคตाเคฒ
1. args เคเคฐ kwargs เคा เคจाเคฎ เค्เคฏा fix เคนोเคคा เคนै?
เคจเคนीं। เคธिเคฐ्เคซ * เคเคฐ ** symbols เคเคฐूเคฐी เคนैं, เคจाเคฎ เคเคช เคुเค เคญी เคฐเค เคธเคเคคे เคนैं เคैเคธे *values เคฏा **data। เคชเคฐ community convention เคฎें args เคเคฐ kwargs เคนी เคธเคฌเคธे เค्เคฏाเคฆा use เคนोเคคे เคนैं เคคाเคि code readable เคฐเคนे।
2. เคเค เคนी function เคฎें *args เคเคฐ **kwargs เคฆोเคจों use เคเคฐ เคธเคเคคे เคนैं?
เคนाँ, เคฌिเคฒ्เคुเคฒ। เคฌเคธ order fix เคฐเคนेเคा — เคชเคนเคฒे normal parameters, เคซिเคฐ *args, เคเคฐ เคธเคฌเคธे เคเคिเคฐ เคฎें **kwargs।
3. *args เคो call เคเคฐเคคे เคธเคฎเคฏ list เค्เคฏों use เคเคฐเคคे เคนैं?
List เคฏा tuple เคो * เคे เคธाเคฅ unpack เคเคฐเคจे เคชเคฐ เคเคธเคे เคนเคฐ element เคो เค
เคฒเค-เค
เคฒเค positional argument เคी เคคเคฐเคน เคญेเคा เคा เคธเคเคคा เคนै, เคฌिเคจा manually เคนเคฐ value เคฒिเคे।
4. **kwargs เคฎें duplicate keys เคฆे เคธเคเคคे เคนैं เค्เคฏा?
เคจเคนीं। เค เคเคฐ เคเคช same keyword argument เคฆो เคฌाเคฐ เคฆोเคे เคคो Python TypeError throw เคเคฐेเคा, เค्เคฏोंเคि dictionary เคฎें เคเค key เคธिเคฐ्เคซ เคเค เคฌाเคฐ valid เคนोเคคी เคนै।
5. *args เคเคฐ **kwargs decorators เคฎें เค्เคฏों เคเคฐूเคฐी เคนैं?
Decorators เคो เคชเคคा เคจเคนीं เคนोเคคा เคि เคिเคธ function เคो เคตो wrap เคเคฐ เคฐเคนे เคนैं, เคเคธे เคिเคคเคจे เคเคฐ เคिเคธ เคคเคฐเคน เคे arguments เคฎिเคฒेंเคे। เคเคธเคฒिเค *args, **kwargs use เคเคฐเคे เคธเคญी arguments เคเคे pass เคเคฐ เคฆिเค เคाเคคे เคนैं, เคाเคนे original function เคा signature เคुเค เคญी เคนो।
6. เค्เคฏा *args เคนเคฎेเคถा tuple เคนी return เคเคฐेเคा?
เคนाँ, function เคे เค
ंเคฆเคฐ *args เคนเคฎेเคถा เคเค tuple เคी เคคเคฐเคน behave เคเคฐเคคा เคนै, เคाเคนे เคเคช เคिเคคเคจे เคญी เคฏा เคिเคธी เคญी data type เคे arguments pass เคเคฐें।
7. เค्เคฏा เคนเคฎ *args เคฏा **kwargs เคे เคฌिเคจा เคญी เคोเค function flexible เคฌเคจा เคธเคเคคे เคนैं?
Technically default parameters เคธे เคฅोเคก़ी flexibility เคฎिเคฒ เคธเคเคคी เคนै, เคฒेเคिเคจ truly unlimited/unknown number of arguments เคो handle เคเคฐเคจे เคा เคธเคฌเคธे clean เคเคฐ Pythonic เคคเคฐीเคा เคธिเคฐ्เคซ *args เคเคฐ **kwargs เคนी เคนै।
8. super().__init__(*args, **kwargs) เค्เคฏों use เคเคฐเคคे เคนैं?
Inheritance เคฎें child class เคो parent class เคे exact parameters เคฏाเคฆ เคฐเคเคจे เคी เคเคฐूเคฐเคค เคจเคนीं เคชเคก़เคคी। *args, **kwargs เคธाเคฐे received arguments เคो automatically parent เคे __init__ เคคเค pass เคเคฐ เคฆेเคคे เคนैं, เคिเคธเคธे code maintainable เคฐเคนเคคा เคนै।
เคोเค เคिเคช्เคชเคฃी เคจเคนीं:
เคเค เคिเคช्เคชเคฃी เคญेเคें