No Result
View All Result
DevRescue
  • Home
  • Python
  • Lists
  • Movies
  • Finance
  • Opinion
  • About
  • Contact Us
  • Home
  • Python
  • Lists
  • Movies
  • Finance
  • Opinion
  • About
  • Contact Us
DevRescue
Home Blog Python

Python Multiline Comment Syntax

by Khaleel O.
January 7, 2024
in Python
Reading Time: 3 mins read
A A

Let’s explain Python multiline comment syntax and identify some uses. Let’s go! 🔥🔥🔥

python multiline comment syntax
basic usage

In Python, a multiline comment is created by enclosing the comment text within triple quotes. These can be either triple single quotes (''') or triple double quotes ("""). Anything inside these triple quotes is considered a comment and is not executed by Python. Here’s an example:

'''
This is a multiline comment
in Python. It can span multiple lines.
'''

"""
Another example of a
multiline comment.
"""

These are often used for documentation strings (docstrings) as well, but they can be used anywhere in the code to add explanatory text or temporarily disable blocks of code.

Multiline comments as docstrings can be used to describe the functionality of Python classes, functions, modules, and methods. They’re written in triple quotes and are located just below the definition of a function, class, or module. Here are three examples, ranging from simple to more complex:

def add_numbers(a, b):
    """
    Add two numbers and return the result.
    
    Parameters:
    a (int): The first number.
    b (int): The second number.
    
    Returns:
    int: The sum of a and b.
    """
    return a + b

This example shows a docstring for a class, including its constructor and method:

class Calculator:
    """
    A simple calculator class to perform basic arithmetic operations.
    
    Attributes:
    history (list): Stores a history of calculations.
    """

    def __init__(self):
        """
        Initializes the calculator with an empty history.
        """
        self.history = []
    
    def add(self, a, b):
        """
        Adds two numbers and stores the result in history.
        
        Parameters:
        a (int): The first number.
        b (int): The second number.
        
        Returns:
        int: The sum of a and b.
        """
        result = a + b
        self.history.append(("add", a, b, result))
        return result

This is a more complex example of a module docstring, documenting the purpose of the module and its contents:

"""
This module provides advanced mathematical operations and utilities.

It includes classes and functions for handling complex arithmetic operations,
algebraic equations, and numerical analysis. It is designed to be a comprehensive
toolkit for scientific computing.

Classes:
- ComplexNumber: Represents and manipulates complex numbers.
- AlgebraSolver: Solves various algebraic equations.

Functions:
- integrate(func, lower, upper): Numerically integrates a function over a given range.
- differentiate(func, at_point): Calculates the derivative of a function at a given point.
"""

# ... implementation of classes and functions ...

In each of these examples, the docstring provides a clear description of what the function, class, or module does, its parameters, return values, and any other relevant information. This is crucial for anyone who will use or maintain the code in the future.

Once you define your multiline comment docstring, you can then access it using .__doc__ function:

def add_numbers(a, b):
    """
    Add two numbers and return the result.
    
    Parameters:
    a (int): The first number.
    b (int): The second number.
    
    Returns:
    int: The sum of a and b.
    """
    return a + b

#using the .__doc__ built-in function
print(add_numbers.__doc__)

#Will print the following:
#    Add two numbers and return the result.
#    
#    Parameters:
#    a (int): The first number.
#    b (int): The second number.
#    
#    Returns:
#    int: The sum of a and b.

If you are using Python at the terminal you can use the help(example_function) to access the docstring.

Thanks for reading our Python multiline comment syntax tutorial! Be sure to check out our other great tutorials HERE. 👌🏻👌🏻👌🏻

Tags: commentdocstringdocumentationmultilinepython
Previous Post

Python Switch Statement Alternatives with Examples

Next Post

Python DNS Lookup IPv4 IPv6 in 4 Ways

Khaleel O.

Khaleel O.

I love to share, educate and help developers. I have 14+ years experience in IT. Currently transitioning from Systems Administration to DevOps. Avid reader, intellectual and dreamer. Enter Freely, Go safely, And leave something of the happiness you bring.

Related Posts

Python

Python Fibonacci Recursive Solution

by Khaleel O.
January 16, 2024
0
0

Let's do a Python Fibonacci Recursive Solution. Let's go! 🔥🔥🔥 The Fibonacci sequence is a series of numbers in which...

Read moreDetails
Python

Python Slice String List Tuple

by Khaleel O.
January 16, 2024
0
0

Let's do a Python Slice string list tuple how-to tutorial. Let's go! 🔥🔥🔥 In Python, a slice is a feature...

Read moreDetails
Python

Python Blowfish Encryption Example

by Khaleel O.
January 14, 2024
0
0

Let's do a Python Blowfish Encryption example. Let's go! 🔥 🔥 Blowfish is a symmetric-key block cipher algorithm designed for...

Read moreDetails
Python

Python Deque Methods

by Khaleel O.
January 14, 2024
0
0

In this post we'll list Python Deque Methods. Ready? Let's go! 🔥🔥🔥 A deque (double-ended queue) in Python is a...

Read moreDetails

DevRescue © 2021 All Rights Reserved. Privacy Policy. Cookie Policy

Manage your privacy

To provide the best experiences, we and our partners use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us and our partners to process personal data such as browsing behavior or unique IDs on this site and show (non-) personalized ads. Not consenting or withdrawing consent, may adversely affect certain features and functions.

Click below to consent to the above or make granular choices. Your choices will be applied to this site only. You can change your settings at any time, including withdrawing your consent, by using the toggles on the Cookie Policy, or by clicking on the manage consent button at the bottom of the screen.

Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Statistics

Marketing

Features
Always active

Always active
Manage options Manage services Manage {vendor_count} vendors Read more about these purposes
Manage options
{title} {title} {title}
Manage your privacy
To provide the best experiences, DevRescue.com will use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Statistics

Marketing

Features
Always active

Always active
Manage options Manage services Manage {vendor_count} vendors Read more about these purposes
Manage options
{title} {title} {title}
No Result
View All Result
  • Home
  • Python
  • Lists
  • Movies
  • Finance
  • Opinion
  • About
  • Contact Us

DevRescue © 2022 All Rights Reserved Privacy Policy