krypted.com

Tiny Deathstars of Foulness

Bryson mentioned Docstrings in the latest episode of the MacAdmins Podcast. But how do you use them? Documentation strings (or docstrings for short) are an easy way to document Python objects (classes, functions, methods, and modules in-line with your code. Docstrings are created using three double-quotes in the first statement of the definition and are meant to describe in a human readable way what the object does.

Let’s look at an example for hello_krypted:

def hello_krypted():
"""This simply echos Hello Krypted.

But there's so much potential to do more!
"""

Docstrings can then be accessed using the __doc__ attribute on objects (e.g. via print):

>>> print hello_krypted.__doc__
This simply echos Hello Krypted.

But you can say hello in person!

For more on docstrings, check out the official docs at https://www.python.org/dev/peps/pep-0257/

June 25th, 2018

Posted In: Programming, Python

Tags: ,

  • David Buxton

    > Docstrings are created using three double-quotes

    By the way you don’t _have_ to use a triple quotation mark. Any string (not assigned to a name), if it is the first statement in the module, class or method, will be used as a docstring.