Close Navigation
Learn more about IBKR accounts
Python Function Tutorial – Part VIII

Python Function Tutorial – Part VIII

Posted May 22, 2020
Jay Parmar
QuantInsti

See Python functions with variable length arguments to get up-to-date in this tutorial.

DocStrings

Python has a nifty feature called documentation string, usually referred to by its shorter name docstrings. This is an important but not required tool that should be used every time we write a program since it helps to document the program better and makes it easier to understand.

Docstrings are written within triple single/double quotes just after definition header. They are written on the first logical line of a python function. Docstrings are not limited to functions only; they also apply to modules and classes. The convention followed for a docstring is a multi-line string where the first line starts with a capital letter and ends with a dot. The second line is blank followed by any detailed explanation starting from the third line. It is strongly advised to follow this convention for all docstrings. Let’s see this in practice with the help of an example:

def power(x, y):
“””Equivalent to x**y or built-in pow() with two arguments.
x and y should be numerical values else an appropriate error will be thrown for incompatible types.
Parameters:
x (int or float): Base value for the power operation.
y (int or float): Power to which base value should be raised.
Returns:
int or float: It returns x raised to the power of y.
“””
try:
return x ** y
except Exception as e:
print(e)

Preview(opens in a new tab)

The Python function power defined above returns the raised value of the argument x powered to y. The thing of our interest is the docstring written within ''' which documents the function. We can access a docstring of any function using the __doc__ attribute (notice the double underscores) of that function. The docstring for the power function can be accessed with the following code: 

print(power.__doc__)

And the output is shown below:

Equivalent to x**y or built-in pow() with two arguments.
x and y should be numerical values else an appropriate error will be thrown for incompatible types.
Parameters:
x (int or float): Base value for the power operation.
y (int or float): Power to which base value should be raised.
Returns:
int or float: It returns x raised to the power of y.

We have already seen the indirect usage of docstrings in previous sections. When we use a python function help in Python, it will show up the docstring. What it does is fetch the __doc__ attribute of that function and displays it in a neat manner. If we ask for the help on the user defined power using the print(help(power)), Python will return the same output as shown above that we got using the print(power.__doc__).

In the next installment, the author will provide code for Nested Python functions and non-local variable.

Visit https://www.quantinsti.com/ for ready-to-use Python functions as applied in trading and data analysis.

Disclosure: Interactive Brokers

Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

This material is from QuantInsti and is being posted with its permission. The views expressed in this material are solely those of the author and/or QuantInsti and Interactive Brokers is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.

IBKR Campus Newsletters

This website uses cookies to collect usage information in order to offer a better browsing experience. By browsing this site or by clicking on the "ACCEPT COOKIES" button you accept our Cookie Policy.