1.2.28 · D1 · HinglishIntroduction to Programming (Python)

FoundationsDefault parameters, keyword arguments

1,755 words8 min read↑ Read in English

1.2.28 · D1 · Coding › Introduction to Programming (Python) › Default parameters, keyword arguments

Isse pehle ki tum defaults aur keyword arguments safely use kar sako, chhe chhoti cheezein bilkul crystal clear honi chahiye. Yeh page inhe absolute zero se build karta hai, us order mein jisme yeh ek doosre par depend karte hain. Yahan kuch bhi yeh assume nahi karta ki tum parent topic pehle se jaante ho — hum iska floor bana rahe hain.


1. Variable kya hota hai (ek labelled box)

Figure — Default parameters, keyword arguments

Topic ko yeh kyun chahiye: ek parameter kuch nahi balki ek special variable hota hai jiska box us waqt bhara jaata hai jab function call kiya jaata hai. Agar "naam jo value se judaa ho" fuzzy lagta hai, toh "parameter" aur "argument" bhi fuzzy rahenge.


2. Function kya hota hai (ek machine jo def banata hai)

def area(length, width):     # 'area' naam hai; length, width knobs hain
    return length * width     # woh steps jo machine chalati hai

def, return, aur calling ka poora grammar Functions - def and return mein hai — yahan hume sirf do round-bracket pieces chahiye: ( ) ke andar definition par kya jaata hai, aur ( ) ke andar call par kya jaata hai.


3. Parameter vs argument (knob vs setting)

Yeh poore page ka sabse important distinction hai, aur beginners ise constantly blur karte hain.

Figure — Default parameters, keyword arguments

4. = sign ke do alag-alag roles hain

Symbol = do bilkul alag kaam ke liye use hota hai, aur inhe confuse karne se real errors aate hain.

Figure — Default parameters, keyword arguments

Topic ko yeh kyun chahiye: poora parent topic name=value par built hai jo brackets ke andar likha hota hai. Agar tum def f(x=10) ko "abhi 10 ko x mein assign karo" padhte ho toh tum galat samjhoge ki default kab create hota hai — yeh famous mutable-default bug ki root hai. Sahi reading hai: "agar caller x ke liye kuch nahi deta, toh 10 use karo."


5. None aur words is / if (the "kya kuch diya gaya tha?" test)

def collect(x, seen=None):     # list ki jagah blank marker
    if seen is None:           # kya kuch diya nahi gaya tha?
        seen = []              # toh abhi ek FRESH box banao
    seen.append(x)
    return seen

Topic ko yeh kyun chahiye: None standard sentinel hai — ek stand-in jo kehta hai "asli default fresh, call time par build honi chahiye." None, is, aur if samjhe bina, mutable-default trap ka sahi fix unreadable hai.


6. Mutable vs immutable (ek shared box kyun problem karta hai)

Figure — Default parameters, keyword arguments

Topic ko yeh kyun chahiye: ek default value ek baar create hoti hai, jab def line chalti hai. Agar woh default ek mutable list hai, toh har call jo argument chhod deta hai woh ek hi list share karta hai — figure ke do arrows ek box par point karte hain, isliye appends calls ke across pile up ho jaate hain. Agar default immutable hoti (jaise ek number), aisi koi sharing tumhe hurt nahi kar sakti. Yeh parent ki sabse badi warning ke peeche ka sabse gehra idea hai. Full treatment: Mutable vs immutable objects.


Prerequisite map

Variable - name on a box

Function built with def

Parameter vs Argument

The = sign two meanings

Default parameter

Keyword argument

None sentinel with if and is

Mutable-default fix

Mutable vs immutable

Default params and keyword arguments

Har foundation uske daaye wale ko feed karta hai; milke teeno streams — knob kya hai, = tag kya karta hai, aur ek shared box kyun khatarnak hai — parent topic par milte hain. Dekho bhi DRY principle - Don't Repeat Yourself (defaults kyun exist karte hain), Variable scope - local vs global (yeh names kahan rehte hain), aur *args and **kwargs (variadic functions) (fixed knobs ke aage ka agla step). Parent par wapas jao: the topic note.


Equipment checklist

Variable ko best picture kaise karte hain...
ek naam/label jo ek box par point karta hai jisme ek value rakhi hai.
def ka short form hai...
define — yeh named knobs wali ek nayi function (machine) banata hai.
Ek parameter hota hai...
def line ke brackets ke andar ek naam — ek khaali box jo bhara jaane ka intezaar kar raha hai.
Ek argument hota hai...
woh actual value jo call karte waqt brackets ke andar pass ki jaati hai.
def line ke andar, x=10 ka matlab hai...
parameter x ko default value 10 do, jo tabhi use hogi jab caller ise chhod de.
Ek call ke andar, x=10 ka matlab hai...
ek keyword argument — x naam wale knob ko 10 par dial karo, order chahe kuch bhi ho.
== aur = mein farq hai kyunki...
== poochta hai "kya yeh barabar hain?" (True/False); = ek value set ya tag karta hai.
None ka matlab hai...
ek deliberate "kuch nahi / yahan blank" value, sentinel ki tarah use hoti hai.
x is None check karta hai...
kya x bilkul wahi object hai jisko None kehte hain — yaani "kya kuch diya nahi gaya tha?".
Ek mutable value woh hai jo...
in place change ki ja sake (lists, dicts); uske do labels ek doosre ke edits dekhte hain.
Ek default value create hoti hai...
ek baar, jab def line chalti hai — har call par nahi.