Python Function Tutorial – Part VIII

Articles From: QuantInsti
Website: 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 and not by Interactive Brokers does NOT constitute a recommendation by Interactive Brokers 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 permission from QuantInsti. The views expressed in this material are solely those of the author and/or QuantInsti and IBKR 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 sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation to buy, sell or hold such security. 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.

In accordance with EU regulation: The statements in this document shall not be considered as an objective or independent explanation of the matters. Please note that this document (a) has not been prepared in accordance with legal requirements designed to promote the independence of investment research, and (b) is not subject to any prohibition on dealing ahead of the dissemination or publication of investment research.

Any trading symbols displayed are for illustrative purposes only and are not intended to portray recommendations.