Python की आम गलतियाँ
In this page:
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
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
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
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 ==
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
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()
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first:
- Python PEP 8 Style Guide
- Python Debugging Techniques
- Python Testing with unittest
- Python Common Mistakes
- Python Interview Questions
- Python map() & filter()
- Python reduce()
- Python zip() & enumerate()
- Python sorted() & key Functions
- Python Comprehensions Advanced
- Python Turtle Graphics
- Python tkinter Introduction
- Python tkinter Widgets
- Python pygame Introduction
- Python Mini Projects