def my_add(x,y):
'''Takes two inputs and returns the sum'''
return x+y
print(my_add(5,7)) #12
print(my_add.__doc__)
By using __doc__
we are getting the output for second print command.
Takes two inputs and returns the sum
We should include about the functions, arguments, return value, error if any and additional notes in our Docstrings. Here is one sample.
def my_add(x,y):
""" Adds to inputs
Args:
x (int) : first integer to be addded
y (int) : second integer to be added
Raises:
TypeError: can only concatenate str (not "int") to str
Returns:
Sum of two inputs.
Notes:
Visit www.plus2net.com for more info
"""
return x+y
print(my_add.__doc__)
class my_tutorial():
'''This is about the tutorial to learn the language.'''
my_subject='Python' # class attributes
#instance attributes
def __init__(self,name,mark):
self.name= name
self.mark= mark
def discount(self):
'''This is about the discount offers.'''
if(self.mark > 50):
return "Discount is avilable"
else:
return "No Discount"
Alex=my_tutorial('Alex',40) # object is defined
print(Alex.discount()) # No Discount # Discount method is used
print(Alex.my_subject) # Python # Class attribute is used
print(my_tutorial.__doc__)
print(my_tutorial.discount.__doc__)
#using the object
print(Alex.__doc__)
print(Alex.discount.__doc__)
Output is here
No Discount
Python
This is about the tutorial to learn the language.
This is about the discount offers.
This is about the tutorial to learn the language.
This is about the discount offers.
'''This is module level docstrings'''
class my_tutorial():
'''This is about the tutorial to learn the language.'''
my_subject='Python' # class attributes
#instance attributes
def __init__(self,name,mark):
self.name= name
self.mark= mark
def discount(self):
'''This is about the discount offers.'''
if(self.mark > 50):
return "Discount is avilable"
else:
return "No Discount"
This module my_module.py we will import and print the docstrings of module, class and object. Our file docstrings_model.py is here
import my_module
print(my_module.__doc__)
print(my_module.my_tutorial.__doc__)
print(my_module.my_tutorial.discount.__doc__)
Output is here
This is module level docstrings
This is about the tutorial to learn the language.
This is about the discount offers.
help('my_module')
About help()
def my_add(x,y):
'''Takes two inputs
and returns
the sum of the inputs.'''
return x+y
#print(my_add(5,7)) #12
print(my_add.__doc__)
Using double quotes for docstrings.
"""Takes two inputs
and returns
the sum of the inputs."""
import pandas as pd
print(pd.__doc__)
For tkinter
import tkinter as tk
print(tk.__doc__)
from google.colab import drive
drive.mount('/content/drive') #, force_remount=True
Change to the working directory
%cd /content/drive/MyDrive/Colab Notebooks/
!ls # listing of files
Import the module ( my_module.ipynb file here ) pip install import_ipynb
before using .ipynb file as module.
import import_ipynb
import my_module
print(my_module.__doc__)
print(my_module.my_tutorial.__doc__)
print(my_module.my_tutorial.discount.__doc__)
Output is here.
This is module level docstring
This is about the tutorial to learn the language
This is about the discount offers.
Global, local and non-local variables
Builtin FunctionsAuthor
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.