-
Integers, Floats and Strings 3
-
Lecture1.1
-
Lecture1.2
-
Lecture1.3
-
-
If Statements 3
-
Lecture2.1
-
Lecture2.2
-
Lecture2.3
-
-
Lists, Sets and Tuples 5
-
Lecture3.1
-
Lecture3.2
-
Lecture3.3
-
Lecture3.4
-
Lecture3.5
-
-
Loops 3
-
Lecture4.1
-
Lecture4.2
-
Lecture4.3
-
-
Functions 3
-
Lecture5.1
-
Lecture5.2
-
Lecture5.3
-
-
Dictionaries 3
-
Lecture6.1
-
Lecture6.2
-
Lecture6.3
-
-
Assertions 2
-
Lecture7.1
-
Lecture7.2
-
-
Classes 3
-
Lecture8.1
-
Lecture8.2
-
Lecture8.3
-
-
Matplotlib 2
-
Lecture9.1
-
Lecture9.2
-
Assertions and Docstrings
Docstrings¶
Docstrings define the function and the inputs and are very useful if you want to understand what something is doing. If you use the question mark followed by the function it will show you the docstring. For example, let’s see what the max function has.
In [8]:
#If you use the ? command you can see the docstring of a function, for example let's look at the max function
? max
We can define our docstring with an explanation of the function as well as any arguments like below and use the assertion to make sure everything makes sense.
In [9]:
#Docstrings define the function and the inputs, and are done with triple quotes at the beginning of a function
def add(a, b):
"""This functions adds a and b together.
Args:
a (int): The first number.
b (int): The second number.
"""
assert type(a)==int and type(b)==int, "Arguments must be integers"
return a+b
add(1, 2)
Out[9]:
3
Now if use a question mark followed by the function then we get back that docstring.
In [10]:
? add
Prev
Introduction
Next
Basic Classes