← Back to Python Course | Chapter 14: Advanced Python & Tools | Lesson 4 of 15

Python की आम गलतियाँ

यह lesson उन चूकों की सूची देता है जो कई नए Python सीखने वाले करते हैं, बिल्कुल सड़क के गड्ढों की एक guide की तरह। इन्हें पहले से जान लेने से आप इनसे बचकर निकल सकते हैं।

Mutable Default Arguments का जाल

किसी function के default parameter value के रूप में list या dict जैसी mutable object इस्तेमाल करना एक classic Python trap है: default values सिर्फ एक बार, function-definition के समय evaluate होती हैं, इसलिए जो भी call अपना argument पास नहीं करती वह उसी exact object को share और mutate करती चली जाती है।

उदाहरण: Mutable Default Arguments Trap

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

print(add_item("apple"))
print(add_item("banana"))  # unexpectedly shares the same list

Iteration के दौरान Lists को बदलना

किसी list पर directly iterate करते हुए उसमें से items हटाना या insert करना loop के बीच में underlying indices को shift कर देता है, जिससे Python चुपचाप elements skip कर देता है या अचानक IndexError raise कर देता है।

सुरक्षित तरीका है list की एक copy पर iterate करना (for x in list[:]) या original को in place mutate करने के बजाय एक नई filtered list बनाना।

उदाहरण: Modifying Lists During Iteration

python
numbers = [1, 2, 3, 4]
for n in numbers[:]:  # iterate over a copy
    if n % 2 == 0:
        numbers.remove(n)
print(numbers)

Built-in Functions को Shadow करना

किसी variable का नाम list, dict, str, या किसी और built-in जैसा रखना current scope में उस नाम को overwrite कर देता है, इसलिए उस scope में बाद का कोई भी code जो असली built-in इस्तेमाल करने की कोशिश करता है (जैसे नई list बनाने के लिए list() call करना) एक साफ naming-conflict warning की बजाय confusing TypeError के साथ टूट जाता है।

उदाहरण: Shadowing Built-in Functions

python
list = [1, 2, 3]  # shadows the built-in list()
print(list)
del list  # restore the built-in
print(list([4, 5]))

is बनाम == से जाँच करना

== यह compare करता है कि दो objects की *values* बराबर हैं या नहीं, जबकि is यह compare करता है कि दो names memory में बिल्कुल एक ही object को refer करते हैं या नहीं।

समान contents वाली दो अलग-अलग बनाई गई lists equal होती हैं (== का परिणाम True) लेकिन एक ही object नहीं होतीं (is का परिणाम False) -- इन दोनों को मिला देना subtle bugs की वजह बनता है, खासकर None checks में, जिनके लिए हमेशा is None इस्तेमाल करना चाहिए।

उदाहरण: Checking with is vs ==

python
a = [1, 2, 3]
b = [1, 2, 3]
print(a == b)  # True, same values
print(a is b)  # False, two separate list objects

Assignment से पहले Reference करना

उसी function के अंदर किसी local variable को value assign होने से पहले पढ़ना UnboundLocalError raise करता है, भले ही module scope में उसी नाम का variable मौजूद हो -- क्योंकि function के अंदर कहीं भी assign करना Python को उस नाम को पूरे function body के लिए local मान लेने पर मजबूर करता है, सिर्फ assignment line से आगे नहीं।

उदाहरण: Referencing Before Assignment

python
def show():
    try:
        print(count)  # local count hasn't been assigned yet at this point
        count = 1
    except UnboundLocalError as e:
        print("Error:", e)

count = 10  # a separate module-level variable, not used inside show()
show()
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.