Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Python for Geeks

You're reading from   Python for Geeks Build production-ready applications using advanced Python concepts and industry best practices

Arrow left icon
Product type Paperback
Published in Oct 2021
Publisher Packt
ISBN-13 9781801070119
Length 546 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Muhammad Asif Muhammad Asif
Author Profile Icon Muhammad Asif
Muhammad Asif
Arrow right icon
View More author details
Toc

Table of Contents (20) Chapters Close

Preface 1. Section 1: Python, beyond the Basics
2. Chapter 1: Optimal Python Development Life Cycle FREE CHAPTER 3. Chapter 2: Using Modularization to Handle Complex Projects 4. Chapter 3: Advanced Object-Oriented Python Programming 5. Section 2: Advanced Programming Concepts
6. Chapter 4: Python Libraries for Advanced Programming 7. Chapter 5: Testing and Automation with Python 8. Chapter 6: Advanced Tips and Tricks in Python 9. Section 3: Scaling beyond a Single Thread
10. Chapter 7: Multiprocessing, Multithreading, and Asynchronous Programming 11. Chapter 8: Scaling out Python Using Clusters 12. Chapter 9: Python Programming for the Cloud 13. Section 4: Using Python for Web, Cloud, and Network Use Cases
14. Chapter 10: Using Python for Web Development and REST API 15. Chapter 11: Using Python for Microservices Development 16. Chapter 12: Building Serverless Functions using Python 17. Chapter 13: Python and Machine Learning 18. Chapter 14: Using Python for Network Automation 19. Other Books You May Enjoy

Effectively documenting Python code

Finding an effective way to document code is always important. The challenge is to develop a comprehensive yet simple way to develop Python code. Let's first look into Python comments and then docstrings.

Python comments

In contrast with a docstring, Python comments are not visible to the runtime compiler. They are used as a note to explain the code. Comments start with a # sign in Python, as shown in the following screenshot:

Figure 1.4 – An example of a comment in Python

Figure 1.4 – An example of a comment in Python

Docstring

The main workhorse for documenting the code is the multiline comments block called a docstring. One of the features of the Python language is that DocStrings are associated with an object and are available for inspection. The guidelines for DocStrings are described in Python Enhancement Proposal (PEP) 257. According to the guidelines, their purpose is to provide an overview to the readers. They should have a good balance between being concise yet elaborative. DocStrings use a triple-double-quote string format: (""").

Here are some general guidelines when creating a docstring:

  • A docstring should be placed right after the function or the class definition.
  • A docstring should be given a one-line summary followed by a more detailed description.
  • Blank spaces should be strategically used to organize the comments but they should not be overused. You can use blank lines to organize code, but don't use them excessively.

In the following sections, let's take a look at more detailed concepts of docStrings.

Docstring styles

A Python docstring has the following slightly different styles:

  • Google
  • NumPy/SciPy
  • Epytext
  • Restructured

Docstring types

While developing the code, various types of documentation need to be produced, including the following:

  • Line-by-line commentary
  • Functional or class-level documentation
  • Algorithmic details

Let's discuss them, one by one.

Line-by-line commentary

One simple use of a docstring is to use it to create multiline comments, as shown here:

Figure 1.5 – An example of a line-by-line commentary-type docstring

Figure 1.5 – An example of a line-by-line commentary-type docstring

Functional or class-level documentation

A powerful use of a docstring is for functional or class-level documentation. If we place the docstring just after the definition of a function or a class, Python associates the docstring with the function or a class. This is placed in the __doc__ attribute of that particular function or class. We can print that out at runtime by either using the __doc__ attribute or by using the help function, as shown in the following example:

Figure 1.6 – An example of the help function

Figure 1.6 – An example of the help function

When using a docstring for documenting classes, the recommended structure is as follows:

  • A summary: usually a single line
  • First blank line
  • Any further explanation regarding the docstring
  • Second blank line

An example of using a docstring on the class level is shown here:

Figure 1.7 – An example of a class-level docstring

Figure 1.7 – An example of a class-level docstring

Algorithmic details

More and more often, Python projects use descriptive or predictive analytics and other complex logic. The details of the algorithm that is used need to be clearly specified with all the assumptions that were made. If an algorithm is implemented as a function, then the best place to write the summary of the logic of the algorithm is before the signature of the function.

You have been reading a chapter from
Python for Geeks
Published in: Oct 2021
Publisher: Packt
ISBN-13: 9781801070119
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime