Quickly comment out block of code in Python

While debugging a python module I needed to remove sections of code for quick functionality testing. For those not familiar with python the hash (#) character is used to comment out a single line of code, so how do you comment out a large block of code ?

One solution could be to employ a macro on your editor to hash each line in a section of the code or a simple if statement could be used, for example :-

Show Plain Text
Python code
  1. if 0:
  2.   <your_block_of_code>

However, the easiest way I've found is to use three quotes on a line either side of your block of code, with this solution you don't need to indent or de-indent your code as with the if statement method, for example :-

Show Plain Text
Python code
  1. """
  2. <your_block_of_code>
  3. """

If you know of a better better way feel free to post a comment...

Filed under: Notes  Tags: Python

3 Responses to “Quickly comment out block of code in Python”

thanks

very interesting, thanks

thank you, thank you, there was nothing in the documentation about this.

Sorry, comments have been closed for this post.
(default) 11 queries took 8 ms
NrQueryErrorAffectedNum. rowsTook (ms)
1DESCRIBE `posts`17171
2DESCRIBE `comments`11111
3DESCRIBE `tags`221
4DESCRIBE `categories`221
5DESCRIBE `posts_tags`221
6DESCRIBE `categories_posts`221
7SELECT `Post`.`id`, `Post`.`url`, `Post`.`title`, `Post`.`icon`, `Post`.`metadesc`, `Post`.`metakeys`, `Post`.`categories`, `Post`.`tease`, `Post`.`body`, `Post`.`private_body`, `Post`.`created`, `Post`.`modified`, `Post`.`status`, `Post`.`allow_comments`, `Post`.`tags`, `Post`.`hitcount`, `Post`.`hitcount_rss` FROM `posts` AS `Post` WHERE `Post`.`url` = 'quickly-comment-out-block-of-code-in-python' LIMIT 1111
8SELECT `Comment`.`id`, `Comment`.`post_id`, `Comment`.`body`, `Comment`.`author`, `Comment`.`url`, `Comment`.`email`, `Comment`.`ip`, `Comment`.`status`, `Comment`.`junk_score`, `Comment`.`created`, `Comment`.`modified` FROM `comments` AS `Comment` WHERE `Comment`.`status` = 2 AND `Comment`.`post_id` = (55) 331
9SELECT `Tag`.`id`, `Tag`.`tag`, `PostsTag`.`post_id`, `PostsTag`.`tag_id` FROM `tags` AS `Tag` JOIN `posts_tags` AS `PostsTag` ON (`PostsTag`.`post_id` = 55 AND `PostsTag`.`tag_id` = `Tag`.`id`) 110
10SELECT `Category`.`id`, `Category`.`category`, `CategoriesPost`.`post_id`, `CategoriesPost`.`category_id` FROM `categories` AS `Category` JOIN `categories_posts` AS `CategoriesPost` ON (`CategoriesPost`.`post_id` = 55 AND `CategoriesPost`.`category_id` = `Category`.`id`) 110
11UPDATE `posts` AS `Post` SET `Post`.`hitcount` = Post.hitcount + 1 WHERE `Post`.`id` = 5510