Welcome to python code documentation!¶
In the following documentation, I have inserted four python files to show the DocStrings.
Python Comments Using Strings¶
"I am a single-line comment"
'''
I am a
multi-line comment!
'''
print("Hi there, Whats Up!")
Write single-line docstrings for a function¶
def multiplier(a, b):
"""Takes in two numbers, returns their product."""
return a*b
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__)
Docstrings for Python class¶
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)
Code Credit: https://www.programiz.com/python-programming/docstrings