Important Notice:

VarArgs Parameters in Python

VarArgs Parameters in Python

22 views 1 min read

VarArgs Parameters in Python

VarArgs का पूरा नाम Variable-Length Arguments है।

Hindi Definition: Python में VarArgs Parameters का उपयोग ऐसे function बनाने के लिए किया जाता है जो एक ही function call में arguments की अलग-अलग संख्या स्वीकार कर सकें।

English Definition: VarArgs parameters allow a function to accept a variable number of arguments.

Python में VarArgs के दो मुख्य प्रकार हैं:

  1. *args — Multiple positional arguments को accept करता है और उन्हें tuple के रूप में रखता है।

  2. **kwargs — Multiple keyword arguments को accept करता है और उन्हें dictionary के रूप में रखता है।

 Variable-Length Arguments

Hindi Definition: Python में Variable-Length Arguments का उपयोग तब किया जाता है, जब किसी function को दिए जाने वाले arguments की संख्या पहले से निश्चित नहीं होती है। Python में *args और **kwargs का उपयोग variable-length arguments प्राप्त करने के लिए किया जाता है।

English Definition: Variable-length arguments allow a function to accept a variable number of arguments. In Python, *args and **kwargs are commonly used for this purpose.

Example:

def add(*args):
    print(sum(args))

add(10, 20)
add(10, 20, 30)
add(10, 20, 30, 40)

Output:

30
60
100

Explanation:

  • Function अलग-अलग संख्या में arguments स्वीकार कर सकता है।

  • *args सभी positional arguments को एक tuple में collect करता है।

  • sum() tuple के सभी numbers का total निकालता है।

Related Notes