Doc Strings in Python in Hindi – Python में Doc String क्या है?

Arpit Nageshwar
⏰ 5 min read

Doc Strings in Python in Hindi – Python में Doc String क्या है?

Table of Contents

Doc Strings in Hindi – डॉक स्ट्रिंग क्या है?

  • जब भी हम Python में कोई function, class या module बनाते हैं, तो कई बार यह ज़रूरी हो जाता है कि उसके साथ ही यह भी लिखा जाए कि वो code piece आखिर करता क्या है। इसी काम के लिए Python में Doc String (Documentation String) दिया गया है।

  • Doc String एक special तरह का string literal है जो किसी function, class, method या module के बिल्कुल पहले (first statement के रूप में) लिखा जाता है, और यह उस code का purpose, working और usage समझाने का काम करता है।

  • आसान भाषा में कहें तो, Doc String एक built-in तरीका है जिससे programmer अपने code के साथ ही उसकी "explanation" attach कर सकता है, और यह explanation बाद में help() function या __doc__ attribute की मदद से कभी भी देखी जा सकती है।

  • Doc String को लिखने के लिए triple quotes — """...""" या '''...''' — का इस्तेमाल किया जाता है, जिससे यह एक multi-line string भी बन सकती है, अगर explanation लंबी हो।

  • रोज़-मर्रा की life से example लें तो, Doc String बिल्कुल किसी दवाई के डिब्बे पर लिखी जानकारी की तरह है — डिब्बा खोले बिना ही आप पढ़ सकते हैं कि दवाई किस काम के लिए है, कैसे लेनी है, और उसकी doses क्या हैं। Doc String भी function/class को इस्तेमाल करने से पहले ही यह सारी जानकारी दे देती है।

  • Exam की भाषा में इसकी definition याद रखनी हो तो: "A Doc String is a string literal that appears as the first statement in a module, function, class, or method, used to describe what that code does."

  • यह समझना भी ज़रूरी है कि Doc String, normal string से अलग इसलिए है क्योंकि Python इसे automatically उस function/class/module के __doc__ attribute में store कर लेता है, जिसे बाद में program के through भी access किया जा सकता है।

Characteristics of Doc Strings in Hindi – डॉक स्ट्रिंग की विशेषताएं

1. हमेशा पहला Statement होता है

Doc String को function, class या module के अंदर हमेशा सबसे पहली line पर लिखा जाता है, तभी Python इसे Doc String के रूप में पहचानता है।

2. Triple Quotes में लिखा जाता है

Doc String को हमेशा triple quotes (""" या ''') के अंदर लिखा जाता है, जिससे यह single-line और multi-line, दोनों तरह से लिखी जा सकती है।

3. __doc__ Attribute में Store होता है

जो भी Doc String लिखी जाती है, वो automatically उस function/class/module के __doc__ नाम के special attribute में save हो जाती है।

4. help() Function से Access होता है

Doc String को help() function की मदद से आसानी से देखा जा सकता है, जो Python में built-in documentation दिखाने का सबसे common तरीका है।

5. Module, Class और Function तीनों पर लागू होता है

Doc String सिर्फ functions तक सीमित नहीं है — इसे पूरे module के शुरुआत में, किसी class के अंदर, और किसी method के अंदर भी अलग-अलग लिखा जा सकता है।

6. Comment नहीं है

Doc String technically एक string literal है, comment नहीं — इसलिए यह program run होते समय memory में एक actual object के रूप में मौजूद रहती है, जबकि comments को Python पूरी तरह ignore कर देता है।

7. Code को Self-Explanatory बनाता है

Doc String की वजह से कोई भी नया developer, बिना पूरा function पढ़े, सिर्फ उसकी documentation देखकर ही समझ सकता है कि वो function क्या करता है।

Creating Doc Strings in Hindi – डॉक स्ट्रिंग कैसे बनाएं?

Doc String बनाना बहुत simple है — बस function, class या module के बिल्कुल पहले statement के रूप में एक triple-quoted string लिख दी जाती है।

1. Function के लिए Doc String

def add_numbers(a, b):
    """Yeh function do numbers ko add karke result return karta hai."""
    return a + b

2. Multi-Line Doc String

def calculate_area(length, width):
    """
    Yeh function ek rectangle ka area calculate karta hai.

    Parameters:
    length (int/float): Rectangle ki length
    width (int/float): Rectangle ki width

    Returns:
    int/float: Rectangle ka area
    """
    return length * width

3. Class के लिए Doc String

class Student:
    """Yeh class ek student ki basic details store karti hai."""

    def __init__(self, name, age):
        self.name = name
        self.age = age

4. Module के लिए Doc String

# file: calculator.py
"""
Yeh module basic calculator operations provide karta hai,
jaise addition, subtraction, multiplication aur division.
"""

def add(a, b):
    return a + b

यहाँ Module Doc String को file की सबसे पहली line पर लिखा जाता है, यानी किसी भी import statement या code से भी पहले।

Accessing Doc Strings in Hindi – डॉक स्ट्रिंग कैसे Access करें?

एक बार Doc String लिख देने के बाद, उसे बाद में कभी भी access किया जा सकता है — इसके मुख्य रूप से दो तरीके होते हैं।

Doc String Access karne ke 2 Tarike def greet(): """Hello bolta hai""" Tarika 1: __doc__ print(greet.__doc__) Output: Hello bolta hai Tarika 2: help() help(greet) Formatted documentation dikhata hai __doc__ raw string deta hai help() usi ko readable format mein dikhata hai

यह diagram दिखा रहा है कि एक ही Doc String को दो अलग-अलग तरीकों से access किया जा सकता है — सीधे __doc__ attribute से raw string के रूप में, या फिर help() function से एक ज़्यादा readable और formatted तरीके से।

def greet():
    """Yeh function ek simple greeting print karta hai."""
    print("Hello!")

# Tarika 1: __doc__ attribute
print(greet.__doc__)
# Output: Yeh function ek simple greeting print karta hai.

# Tarika 2: help() function
help(greet)
# Output:
# Help on function greet in module __main__:
#
# greet()
#     Yeh function ek simple greeting print karta hai.

Class aur module ke doc strings ko bhi bilkul isi tarike se access kiya ja sakta hai:

class Student:
    """Yeh class student ki details store karti hai."""
    pass

print(Student.__doc__)
help(Student)

Importance of Documentation Strings in Hindi – डॉक स्ट्रिंग का महत्व

  • Doc String की वजह से कोई भी नया developer, बिना पूरा code पढ़े, सिर्फ documentation देखकर ही समझ सकता है कि function या class किस काम के लिए बना है।

  • बड़े projects और libraries (जैसे NumPy, Pandas) में Doc Strings की वजह से ही help() function से तुरंत यह पता चल जाता है कि किसी function को कैसे इस्तेमाल किया जाए।

  • Tools जैसे Sphinx, Doc Strings को automatically पढ़कर एक professional-looking documentation website generate कर देते हैं, जिससे manually documentation लिखने की मेहनत बच जाती है।

  • Team में काम करते समय Doc Strings एक common reference की तरह काम करती हैं, जिससे सभी developers को एक जैसी और सही जानकारी मिलती है।

  • Doc Strings code की maintainability को बढ़ाती हैं, क्योंकि कुछ महीनों बाद खुद programmer भी अपना पुराना code आसानी से समझ पाता है।

  • यह IDEs (जैसे VS Code, PyCharm) में auto-suggestion और tooltip के रूप में भी दिखती है, जिससे developer को function type करते ही उसकी जानकारी मिल जाती है।

Doc String vs Comment in Hindi – दोनों में अंतर

Point Doc String Comment
Syntax Triple quotes """...""" # से शुरू होता है
Position Function/class/module का पहला statement Code में कहीं भी लिखा जा सकता है
Memory में Store __doc__ attribute में store होता है Python पूरी तरह ignore कर देता है
Access __doc__ या help() से access हो सकता है Program के through access नहीं हो सकता
Use Case Function/class/module को document करने के लिए Code के अंदर छोटे notes लिखने के लिए

Points to Remember in Hindi – ध्यान रखने वाली बातें

  • Doc String हमेशा function, class या module के बिल्कुल पहले statement के रूप में लिखी जानी चाहिए, वरना Python उसे normal string मानकर __doc__ में store नहीं करेगा।

  • Doc String लिखने के लिए हमेशा triple quotes का इस्तेमाल करना चाहिए, चाहे description एक line की ही क्यों ना हो, ताकि आगे multi-line में बढ़ाना आसान रहे।

  • Doc String और Comment को एक-दूसरे का replacement नहीं समझना चाहिए — Doc String पूरे function/class का purpose बताती है, जबकि Comments code की internal, line-by-line logic explain करते हैं।

  • अच्छी Doc String में सिर्फ यह नहीं, बल्कि parameters, return value aur possible exceptions की जानकारी भी शामिल होनी चाहिए, खासकर बड़े functions के लिए।

  • Doc Strings को हमेशा clear, concise aur present tense में लिखना चाहिए, जैसे "Returns the sum of two numbers" ना कि "This will return the sum"।

FAQs

Doc String एक special string literal है जो function, class, method ya module ke bilkul pehle statement ke roop mein likhi jaati hai, aur uska purpose, working ya usage samjhaane ka kaam karti hai।
Doc String ko triple quotes (""" ya ''') ke andar likha jaata hai, aur ise function, class ya module ke sabse pehli line par rakha jaata hai, taaki Python use uske __doc__ attribute mein automatically store kar sake.
Doc String ko do tarikon se access kiya ja sakta hai — function ya class ke __doc__ attribute se seedha print karke, ya phir help() function ka use karke jo usi content ko ek readable, formatted tarike se dikhata hai.
Doc String ek triple-quoted string hoti hai jo __doc__ attribute mein store ho jaati hai aur program ke through access ki ja sakti hai, jabki Comment (# se shuru hone wala) Python dwara pura ignore kar diya jaata hai aur program ke through access nahi ho sakta.
Doc Strings code ko self-explanatory banati hain, naye developers ko onboarding mein madad karti hain, tools jaise Sphinx se automatic documentation generate karne mein use hoti hain, aur IDEs mein tooltip ke roop mein bhi dikhti hain।
Haan, Doc String ko function aur class ke alawa poore module ke liye bhi likha ja sakta hai — iske liye ise file ki sabse pehli line par, kisi bhi import statement se pehle likha jaata hai।
Arpit Nageshwar

✍️ Arpit Nageshwar

Post-graduated | Web Developer | +3 yr Experience | IIT Kharagpur Certified