← Back to Python Course | Chapter 4: Functions | Lesson 4 of 9

Python में Default Arguments

Default argument एक backup value है जिसे function तब इस्तेमाल करता है जब आप कोई value नहीं देते, जैसे कोई pizza जब तक आप कुछ और न मांगें cheese के साथ आता है। यह कुछ inputs को optional बना देता है।
Syntax
python
def function_name(parameter, optional=default_value):
    # body
    return value

Default Arguments क्या हैं?

Default argument एक fallback value देता है जो अपने-आप तब इस्तेमाल होता है जब caller वह argument छोड़ देता है -- इसे function definition में = से लिखा जाता है, जैसे def greet(name="friend"): -- जो उस parameter को effectively optional बना देता है।

उदाहरण: What are Default Arguments?

python
def greet(name="friend"):  # name defaults to "friend" if the caller omits it
    print("Hello,", name)

greet()  # uses the default value
greet("Alex")  # overrides the default

Default Arguments के नियम

function की definition में हर default-value वाले parameter को बिना default वाले सभी parameters के बाद आना चाहिए; किसी required parameter को किसी optional parameter के बाद रखने से SyntaxError आता है, क्योंकि Python के पास यह पता करने का और कोई तरीका नहीं होता कि कौन-सा argument किस parameter से mapped है।

उदाहरण: Rules for Default Arguments

python
def greet(greeting, name="friend"):  # required parameter must come before the defaulted one
    print(greeting, name)

greet("Hi")  # name falls back to its default

Mutable Default Arguments की Warning

[] या {} जैसे mutable object को default value के रूप में इस्तेमाल करना Python का एक classic trap है: default सिर्फ़ एक बार बनता है जब function *define* होता है, हर बार call होने पर नहीं, इसलिए जो भी call इसे override नहीं करता वह उसी list को share करती है -- और चुपचाप उसे mutate भी कर सकती है।

उदाहरण: Mutable Default Arguments Warning

python
def add_item(item, cart=[]):
    cart.append(item)
    return cart

print(add_item("apple"))
print(add_item("banana"))  # shares the same list from the first call

Dynamic Default Values

उस trap का standard fix है parameter का default None रखना और ज़रूरत पड़ने पर असली default value को function body के अंदर बनाना -- इससे यह guarantee होता है कि हर call पर एक fresh object बने, न कि सभी calls में एक ही object reuse हो।

उदाहरण: Dynamic Default Values

python
def add_item(item, cart=None):  # None avoids sharing one list across every call
    if cart is None:
        cart = []  # a fresh list is created for this call
    cart.append(item)
    return cart

print(add_item("apple"))
print(add_item("banana"))

Positional, Keyword और Defaults को मिलाना

एक ही call में positional, keyword और default arguments को मिलाया जा सकता है, लेकिन positional arguments को हमेशा पहले लिखना ज़रूरी है -- Python पहले उन्हें बाएँ से दाएँ resolve करता है, फिर बचे हुए keyword arguments को उनके parameters से match करता है।

उदाहरण: Combining Positional, Keyword and Defaults

python
def order(item, qty=1, note="none"):
    print(item, qty, note)

order("book", note="gift wrap")  # item is positional, note is keyword, qty uses its default
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.