पायथन डॉकस्ट्रिंग्स (उदाहरण के साथ)

इस ट्यूटोरियल में, हम पायथन डॉकस्ट्रिंग्स के बारे में जानेंगे। विशेष रूप से, हम सीखेंगे कि कैसे और क्यों डोकस्ट्रिंग्स का उपयोग उदाहरणों की मदद से किया जाता है।

पायथन डॉकस्ट्रिंग्स एक स्ट्रिंग स्ट्रिंग शाब्दिक हैं जो किसी फ़ंक्शन, विधि, वर्ग या मॉड्यूल की परिभाषा के ठीक बाद दिखाई देते हैं। एक उदाहरण लेते हैं।

उदाहरण 1: Docstrings

 def square(n): '''Takes in a number n, returns the square of n''' return n**2

यहाँ, स्ट्रिंग शाब्दिक:

 '' 'एक नंबर n में ले जाता है, n का वर्ग लौटाता है' ''

ट्रिपल उद्धरण चिह्नों के अंदर फ़ंक्शन का डॉकस्ट्रिंग है square()क्योंकि यह अपनी परिभाषा के ठीक बाद दिखाई देता है।

नोट: हम """docstrings बनाने के लिए ट्रिपल कोटेशन का भी उपयोग कर सकते हैं ।

अजगर टिप्पणियाँ बनाम डॉकस्ट्रिंग्स

अजगर टिप्पणियाँ

टिप्पणियां ऐसे विवरण हैं जो प्रोग्रामर को प्रोग्राम के इरादे और कार्यक्षमता को बेहतर ढंग से समझने में मदद करते हैं। वे पायथन दुभाषिया द्वारा पूरी तरह से नजरअंदाज कर दिए जाते हैं।

पायथन में, हम #सिंगल-लाइन टिप्पणी लिखने के लिए हैश प्रतीक का उपयोग करते हैं । उदाहरण के लिए,

 # Program to print "Hello World" print("Hello World") 

पायथन टिप्पणियाँ स्ट्रिंग्स का उपयोग करना

यदि हम किसी भी चर के लिए स्ट्रिंग्स असाइन नहीं करते हैं, तो वे टिप्पणियों के रूप में कार्य करते हैं। उदाहरण के लिए,

 "I am a single-line comment" ''' I am a multi-line comment! ''' print("Hello World")

नोट: हम मल्टी-लाइन स्ट्रिंग्स के लिए ट्रिपल उद्धरण चिह्नों का उपयोग करते हैं।

पायथन डॉकस्ट्रिंग्स

जैसा कि ऊपर उल्लेख किया गया है, पायथन डॉकस्ट्रिंग्स एक फ़ंक्शन, विधि, वर्ग, या मॉड्यूल की परिभाषा के बाद उपयोग किए गए तार हैं ( उदाहरण 1 में )। उनका उपयोग हमारे कोड को दस्तावेज करने के लिए किया जाता है।

हम __doc__विशेषता का उपयोग करके इन डॉकस्ट्रिंग्स तक पहुंच सकते हैं ।

पायथन __doc__ विशेषता

जब भी किसी फ़ंक्शन, मॉड्यूल, वर्ग या विधि की परिभाषा के बाद स्ट्रिंग शाब्दिक मौजूद होते हैं, तो वे ऑब्जेक्ट के साथ उनकी __doc__विशेषता के रूप में जुड़े होते हैं । हम बाद में इस विशेषता का उपयोग इस डॉकस्ट्रिंग को पुनः प्राप्त करने के लिए कर सकते हैं।

उदाहरण 2: प्रिंटिंग डॉकस्ट्रिंग

 def square(n): '''Takes in a number n, returns the square of n''' return n**2 print(square.__doc__)

आउटपुट

 एक संख्या n में ले जाता है, n का वर्ग लौटाता है

यहां, विशेषता square()का उपयोग करके हमारे फ़ंक्शन के प्रलेखन तक पहुंचा जा सकता है __doc__

अब, अंतर्निहित कार्य के लिए docstrings को देखें print():

उदाहरण 3: अंतर्निहित प्रिंट () फ़ंक्शन के लिए डॉकस्ट्रिंग्स

 print(print.__doc__)

आउटपुट

प्रिंट (मान,…, एसईपी = ’’, अंत = ' n ’, फ़ाइल = sys.stdout, flush = False) मानों को किसी स्ट्रीम में प्रिंट करता है, या डिफ़ॉल्ट रूप से sys.stdout को। वैकल्पिक कीवर्ड तर्क: फ़ाइल: एक फ़ाइल की तरह ऑब्जेक्ट (स्ट्रीम); वर्तमान sys.stdout में चूक। sep: मानों के बीच डाला गया स्ट्रिंग, एक स्थान को डिफॉल्ट करता है। अंत: स्ट्रिंग अंतिम मान के बाद जोड़ा गया है, एक नई पंक्ति को डिफ़ॉल्ट करें। फ्लश: चाहे तो जबरन स्ट्रीम फ्लश करें।

यहां, हम देख सकते हैं कि print()फ़ंक्शन का प्रलेखन __doc__इस फ़ंक्शन की विशेषता के रूप में मौजूद है ।

पायथन में सिंगल-लाइन डॉकस्ट्रिंग्स

सिंगल लाइन डॉकस्ट्रिंग्स एक लाइन में फिट होने वाले दस्तावेज हैं।

एकल-पंक्ति डॉकस्ट्रिंग्स लिखने के लिए मानक सम्मेलन:

  • भले ही वे एकल-पंक्तिबद्ध हों, हम अभी भी इन docstrings के आसपास ट्रिपल उद्धरण का उपयोग करते हैं क्योंकि उन्हें बाद में आसानी से विस्तारित किया जा सकता है।
  • शुरुआती उद्धरण उसी पंक्ति पर हैं जो शुरुआती उद्धरण हैं।
  • डॉकस्ट्रिंग से पहले या बाद में कोई खाली लाइन नहीं है।
  • उन्हें वर्णनात्मक नहीं होना चाहिए, बल्कि उन्हें "ऐसा करें, वापस लौटें" संरचना को एक अवधि के साथ समाप्त करना चाहिए।

एक उदाहरण लेते हैं।

उदाहरण 4: किसी फ़ंक्शन के लिए एकल-पंक्ति docstrings लिखें

 def multiplier(a, b): """Takes in two numbers, returns their product.""" return a*b

पायथन में मल्टी-लाइन डॉकस्ट्रिंग्स

मल्टी-लाइन डॉकस्ट्रिंग्स में एक-लाइन डॉकस्ट्रिंग की तरह एक सारांश रेखा होती है, उसके बाद एक खाली लाइन, जिसके बाद एक अधिक विस्तृत विवरण होता है।

पीईपी 257 दस्तावेज विभिन्न वस्तुओं के लिए मल्टी-लाइन डॉकस्ट्रिंग्स लिखने के लिए मानक सम्मेलनों को प्रदान करता है।

कुछ को नीचे सूचीबद्ध किया गया है:

1. पायथन मॉड्यूल्स के लिए डॉकस्ट्रिंग्स

  • पायथन मॉड्यूल के लिए डॉकस्ट्रिंग्स को सभी उपलब्ध वर्गों, कार्यों, वस्तुओं और अपवादों को सूचीबद्ध करना चाहिए जो मॉड्यूल आयात होने पर आयात किए जाते हैं।
  • They should also have a one-line summary for each item.

They are written at the beginning of the Python file.

Let's look at the docstrings for the builtin module in Python called pickle.

Example 4: Docstrings of Python module

 import pickle print(pickle.__doc__)

Output

 Create portable serialized representations of Python objects. See module copyreg for a mechanism for registering custom picklers. See module pickletools source for extensive comments. Classes: Pickler Unpickler Functions: dump(object, file) dumps(object) -> string load(file) -> object loads(string) -> object Misc variables: __version__ format_version compatible_formats

Here, we can see that the docstring written at the beginning of the pickle.py module file can be accessed as its docstring.

2. Docstrings for Python Functions

  • The docstring for a function or method should summarize its behavior and document its arguments and return values.
  • It should also list all the exceptions that can be raised and other optional arguments.

Example 5: Docstrings for Python functions

 def add_binary(a, b): ''' Returns the sum of two decimal numbers in binary digits. Parameters: a (int): A decimal integer b (int): Another decimal integer Returns: binary_sum (str): Binary string of the sum of a and b ''' binary_sum = bin(a+b)(2:) return binary_sum print(add_binary.__doc__)

Output

 Returns the sum of two decimal numbers in binary digits. Parameters: a (int): A decimal integer b (int): Another decimal integer Returns: binary_sum (str): Binary string of the sum of a and b

As you can see, we have included a short description of what the function does, the parameter it takes in and the value it returns. The string literal is embedded to the function add_binary as its __doc__ attribute.

3. Docstrings for Python Classes

  • The docstrings for classes should summarize its behavior and list the public methods and instance variables.
  • The subclasses, constructors, and methods should each have their own docstrings.

Example 6: Docstrings for Python class

Suppose we have a Person.py file with the following code:

 class Person: """ A class to represent a person.… Attributes ---------- name : str first name of the person surname : str family name of the person age : int age of the person Methods ------- info(additional=""): Prints the person's name and age. """ def __init__(self, name, surname, age): """ Constructs all the necessary attributes for the person object. Parameters ---------- name : str first name of the person surname : str family name of the person age : int age of the person """ self.name = name self.surname = surname self.age = age def info(self, additional=""): """ Prints the person's name and age. If the argument 'additional' is passed, then it is appended after the main info. Parameters ---------- additional : str, optional More info to be displayed (default is None) Returns ------- None """ print(f'My name is (self.name) (self.surname). I am (self.age) years old.' + additional)

Here, we can use the following code to access only the docstrings of the Person class:

 print(Person.__doc__)

Output

 A class to represent a person.… Attributes ---------- name : str first name of the person surname : str family name of the person age : int age of the person Methods ------- info(additional=""): Prints the person's name and age

Using the help() Function for Docstrings

We can also use the help() function to read the docstrings associated with various objects.

Example 7: Read Docstrings with the help() function

We can use the help() function on the class Person in Example 6 as:

 help(Person)

Output

 Help on class Person in module __main__: class Person(builtins.object) | Person(name, surname, age) | | A class to represent a person. | |… | | Attributes | ---------- | name : str | first name of the person | surname : str | family name of the person | age : int | age of the person | | Methods | ------- | info(additional=""): | Prints the person's name and age. | | Methods defined here: | | __init__(self, name, surname, age) | Constructs all the necessary attributes for the person object. | | Parameters | ---------- | name : str | first name of the person | surname : str | family name of the person | age : int | age of the person | | info(self, additional='') | Prints the person's name and age. | | If the argument 'additional' is passed, then it is appended after the main info. | | Parameters | ---------- | additional : str, optional | More info to be displayed (default is None) | | Returns | ------- | None | | ---------------------------------------------------------------------- | Data descriptors defined here: | | __dict__ | dictionary for instance variables (if defined) | | __weakref__ | list of weak references to the object (if defined)

Here, we can see that the help() function retrieves the docstrings of the Person class along with the methods associated with that class.

4. Docstrings for Python Scripts

  • The docstrings for Python script should document the script's functions and command-line syntax as a usable message.
  • It should serve as a quick reference to all the functions and arguments.

5. Docstrings for Python Packages

The docstrings for a Python package is written in the package's __init__.py file.

  • It should contain all the available modules and sub-packages exported by the package.

Docstring Formats

We can write docstring in many formats like the reStructured text (reST) format, Google format or the NumPy documentation format. To learn more, visit Popular Docstring Formats

हम Sphinx जैसे उपकरणों का उपयोग करके दस्तावेज़ों से दस्तावेज़ भी उत्पन्न कर सकते हैं। अधिक जानने के लिए, आधिकारिक स्फिंक्स प्रलेखन पर जाएं

दिलचस्प लेख...