Python Comments
In this page:
# single-line comment
x = value # inline comment
"""
multi-line comment
(docstring-style)
"""
Single-Line Comments
# एक single-line comment की शुरुआत दर्शाता है; interpreter उस character से line के अंत तक सब कुछ अनदेखा कर देता है। code के चलने पर असर डाले बिना अपने लिए छोटा नोट छोड़ने का यह सबसे तेज़ तरीका है।
उदाहरण: Single-Line Comments
# This is a comment
print("Comments are ignored by the interpreter")
Multi-Line Comments
Python में कोई समर्पित multi-line comment syntax नहीं है — developers या तो कई # lines को एक साथ रखते हैं, या बिना असाइन की गई triple-quoted string ("""...""") को block comment की तरह इस्तेमाल करते हैं, क्योंकि अकेला string statement चलने पर कोई असर नहीं डालता।
उदाहरण: Multi-Line Comments
# This is line one
# This is line two
"""
This triple-quoted string
acts as a block comment
"""
print("done")
Code का Documentation
अच्छे comments यह समझाते हैं कि कोई code *क्यों* मौजूद है — कोई पेचीदा workaround, कोई business rule, कोई अस्पष्ट edge case — बजाय इसके कि वही दोहराएँ जो code पहले से साफ़ दिखा रहा है कि वह *क्या* करता है।
यही फ़र्क comments को अगले पढ़ने वाले के लिए, यानी भविष्य के आपके लिए भी, सचमुच उपयोगी बनाता है।
उदाहरण: Documenting Code
# Using a set here because duplicate ids would break the billing report
unique_ids = set()
print(unique_ids)
Code को Comment Out करना
किसी line या block को # में लपेट देने से वह बिना मिटाए चलना बंद हो जाता है, जो debugging के दौरान यह अलग करने का आम तरीका है कि script का कौन सा हिस्सा समस्या पैदा कर रहा है।
ज़्यादातर editors पूरे चयन को एक साथ comment/uncomment करने के लिए keyboard shortcut देते हैं।
उदाहरण: Commenting Out Code
print("this runs")
# print("this is commented out and does not run")
Comments के सर्वोत्तम तरीके
जो comments पुराने पड़ जाते हैं वे बिना comment के होने से भी बदतर हैं, क्योंकि वे अगले पाठक को code के बारे में सक्रिय रूप से गुमराह करते हैं। उन्हें छोटा रखिए, जब भी वर्णित code बदले तो उन्हें अपडेट कीजिए, और जो अब कोई जानकारी नहीं जोड़ते उन्हें हटा दीजिए।
उदाहरण: Comment Best Practices
# Retry limit tuned after the 2024 outage postmortem
MAX_RETRIES = 5
print(MAX_RETRIES)
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: