Comment out a block of code in Python

There may be many situations while testing or debugging python programs where we want a certain statement in the code or a block of statements in the code not to be executed. For this purpose we comment out the block of code. In this article, we will see how we can comment out a block of code in python using python comment. First we will see what a block of code means in python and then we will see how to comment them out.

What is a block of code?

A block of code is a group of statements in source code in any programming language which are supposed to be executed once a condition is met or a function is called.

In python, blocks of code are created by applying proper indentation for the statements to create a block of code and separate them from other blocks of code.

A block of code in python may consist of statements belonging to a control statements, a function or a class.

For Example, following is a block of code in if-else control statement.

number =int(input())
#This an example of block of code where a block consists of control statement(If else in this case)
if (number%2==0):
    print("number is even")
else:
    print("number is odd")

A block of code inside a function may be as follows. The following function adds a number and its square to a python dictionary as key value pair.

def add_square_to_dict(x,mydict):
    #This is an example of block of code where the block consists of an entire function
    a=x*x
    mydict[str(x)]=a
    return mydict

A block of code inside a class may be as follows.

class number:
    #This is an example of block of code where the block consists of an entire class

    def __init__(self,value):
        self.value =value
    def increment(self):
        self.value=self.value+1

Generally, a block of code extends to multiple lines. So to comment out a block of code in python, we will have to use multiline comments. First we will see the working of multiline comments and then we will try to comment out a block of code .

Working of multiline comments in python

Multiline comments are not intrinsically included as a construct in python. But single line comments and multiline strings can be used to implement multi line comments in python.

We can implement multi line comments using single line comment by inserting a # sign whenever a line break is encountered. In this way, the multiline comments are just depicted as a sequence of single line comments as shown below.

def add_square(x,y):
    a=x*x
    b=y*y
    #This is a multi line comment
    #implemented using # sign
    return a+b

We can also use multiline strings as multi line comments if we do not assign them to any variable.When the string isn’t assigned to any variable, they are parsed and evaluated by the interpreter but no byte code is generated because no address can be assigned to the strings. This affects the string working just as a comment. In this method, multi line comments can be declared using triple quotes as shown below.

def add_square(x,y):
    a=x*x
    b=y*y
    """This is a multiline comment
    implemented with the help of 
    triple quoted strings"""
    return a+b

Comment out a block of code in python using # sign

We can comment out a block of code in python by placing a # sign at the start of each statement in that particular block of code as follows.

number =int(input())
#if (number%2==0):
 #   print("number is even")
#else:
#    print("number is odd")

Comment out blocks of code in python using multiline strings

We can comment out a block of code using multiline string method as follows.

number =int(input())
"""if (number%2==0):
   print("number is even")
else:
  print("number is odd")
"""

Although this method works but we should not use multiline strings as comments to comment out block of code. The reason is that multiline comments are used as docstrings for documenting the source code.

Conclusion

We can comment out a block of code in python using # symbol but we should refrain ourselves from using multiline strings to comment out the block of code. Stay tuned for more informative articles.

Leave a Reply

Your email address will not be published. Required fields are marked *