Python में Multiline Comments कैसे लिखें – पूरी जानकारी हिंदी में
Multiline comment Python का वो comment होता है जो एक line में खत्म नहीं होता, बल्कि कई lines तक फैला रहता है। इसका इस्तेमाल तब होता है जब code को detail में explain करना हो, किसी बड़े हिस्से को temporarily disable करना हो, या फिर readability बढ़ानी हो। Python में इसके लिए कोई अलग से keyword नहीं दिया गया, लेकिन developers चार अलग-अलग तरीकों से यह काम निकाल लेते हैं।
यह comments code को समझने में आसानी देते हैं, documentation का काम करते हैं, team collaboration बेहतर बनाते हैं और debugging में भी मदद करते हैं।
Multiline Comments लिखने के तरीके
1. # Symbol वाला तरीका
यह सबसे common और सबसे ज्यादा recommend किया जाने वाला तरीका है। बस हर नई line के शुरुआत में # लगा दीजिए, Python interpreter उस पूरी line को skip कर देगा।
# यह एक multiline comment है
# हर line अलग से शुरू होती है #
# यह तरीका सबसे efficient माना जाता है
print("Namaste Python") # यह inline comment है
Output:
Namaste Python
Explanation: ऊपर तीनों # वाली lines को interpreter पूरी तरह ignore कर देता है, इसलिए वो execute नहीं होतीं। सिर्फ print statement चलता है और अंत में # के बाद लिखा inline comment भी छोड़ दिया जाता है।
2. Triple Quotes (''' या """) वाला तरीका
Python में तीन single quotes या तीन double quotes के बीच लिखा text multi-line string बन जाता है। अगर उसे किसी variable में assign नहीं किया जाए, तो वो सिर्फ बनकर रह जाता है और execute नहीं होता, यानी practically comment जैसा behave करता है।
'''
यह triple single quotes से बना
multiline comment है
'''
print("Triple Quotes Example")
"""
यह triple double quotes वाला
दूसरा तरीका है
"""
Output:
Triple Quotes Example
Explanation: दोनों triple-quoted blocks किसी variable में store नहीं हुए, इसलिए Python उन्हें बना तो देता है लेकिन उनका कोई असर code पर नहीं पड़ता। यह तरीका सबसे ज्यादा docstrings के लिए इस्तेमाल होता है, comments के लिए कम।
3. Backslash (\) वाला तरीका
यह तरीका थोड़ा कम जाना-पहचाना है। Backslash (\) असल में line continuation के लिए बना है, यानी एक statement को अगली line तक बढ़ाने के लिए। कई developers इसे # comments के साथ मिलाकर एक creative workaround की तरह इस्तेमाल करते हैं, जिससे लगातार कई lines पर comment जैसा असर दिखे।
# Backslash की मदद से multiline comment जैसा असर \
# यह एक साथ जुड़ी हुई lines जैसा दिखता है \
# लेकिन असल में यह line continuation है
print("Backslash Method Example")
Output:
Backslash Method Example
Explanation: यहां backslash हर line को अगली line से जोड़ने का काम करता है, लेकिन चूंकि हर line पहले से # से शुरू होती है, इसलिए interpreter उसे comment मानकर ही छोड़ देता है। यह तरीका ना तो सबसे साफ है और ना ही सबसे ज्यादा इस्तेमाल होता है, लेकिन जानना ज़रूरी है।
4. Docstrings वाला तरीका
जब triple quotes किसी function, class या module के सबसे पहले statement के रूप में लिखे जाते हैं, तो वो सिर्फ comment नहीं रहते बल्कि docstring बन जाते हैं। यह Python की metadata की तरह store होते हैं और .__doc__ attribute से runtime पर भी पढ़े जा सकते हैं।
def add_numbers(a, b):
"""
यह function दो numbers को जोड़ता है
और उनका result return करता है
"""
return a + b
print(add_numbers.__doc__)
Output:
यह function दो numbers को जोड़ता है
और उनका result return करता है
Explanation: Docstring function के अंदर first statement की तरह लिखा गया, इसलिए Python इसे automatically function की documentation मान लेता है। add_numbers.__doc__ लिखते ही वही text वापस मिल जाता है, जो साधारण comment के साथ कभी नहीं होता।
Docstring और Multiline Comment में फर्क
| Feature | Multiline Comment | Docstring |
|---|---|---|
| मकसद | code explain करना | function/class/module document करना |
| Syntax | हर line पर # या unassigned triple quotes | function/class के पहले statement के रूप में triple quotes |
| Execution | पूरी तरह ignore हो जाता है | string के रूप में store रहता है |
| कहां लिखें | code में कहीं भी | सिर्फ function, class या module के शुरुआत में |
| Access | runtime पर access नहीं होता | .__doc__ या help() से access होता है |
| Best Practice | actual comments के लिए # इस्तेमाल करें | documentation के लिए triple quotes इस्तेमाल करें |
Debugging में Multiline Comments का इस्तेमाल
Debugging के समय अक्सर code के कुछ हिस्सों को temporarily बंद करना पड़ता है, ताकि पता चल सके कि problem कहां से आ रही है।
# print("यह line अभी नहीं चलेगी")
# print("इसे testing के लिए बंद किया गया है")
'''
print("यह पूरा block भी फिलहाल off है")
'''
print("बाकी सब कुछ normal चल रहा है")
Explanation:
- Code को temporarily disable करने के लिए यह तरीका काफी काम आता है
- # वाले comments debugging के लिए ज्यादा safe माने जाते हैं
- Triple-quoted strings को debugging के लिए बार-बार इस्तेमाल करने से बचना चाहिए
Note: Google Colab या Jupyter Notebook जैसी जगहों पर अगर triple-quoted string किसी cell की last line हो, तो वो output की तरह print हो सकती है, भले ही आपने print() ना लिखा हो। Normal .py script में ऐसा नहीं होता, वहां print() लिखना ही पड़ता है।
(Related Articles)
Data Analyst Tutorial in Hindi
अक्सर पूछे जाने वाले सवाल (FAQ)
Python में multiline comment के लिए कितने तरीके हैं
मुख्य रूप से चार तरीके इस्तेमाल किए जाते हैं – हर line पर # लगाना, unassigned triple-quoted string, backslash continuation के साथ # comments, और docstring।
कौन सा तरीका सबसे ज्यादा recommend किया जाता है
PEP 8 के अनुसार हर line पर # लगाना सबसे Pythonic और साफ तरीका माना जाता है, खासकर debugging और actual comments के लिए।
Docstring को .__doc__ से क्यों access किया जा सकता है
क्योंकि docstring किसी function, class या module के पहले statement के रूप में लिखा गया triple-quoted string होता है, जिसे Python automatically उस object की metadata के रूप में store कर लेता है।
क्या triple-quoted comment हर जगह सुरक्षित है
नहीं, notebooks जैसे Google Colab या Jupyter में अगर यह किसी cell की last line हो, तो वो output के रूप में print हो सकता है, इसलिए वहां सावधानी से इस्तेमाल करना चाहिए।
कोई टिप्पणी नहीं:
एक टिप्पणी भेजें