Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Define docstrings in Python

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/

The post Define Docstrings in Python appeared first on krypted.com.



This post first appeared on Krypted.com | Tiny Deathstars Of Foulness, please read the originial post: here

Share the post

Define docstrings in Python

×

Subscribe to Krypted.com | Tiny Deathstars Of Foulness

Get updates delivered right to your inbox!

Thank you for your subscription

×