There’s no end to how many block comments you can have, in a row or otherwise. Any Python statement that begins with a hashtag will be treated as a comment by the compiler. The most straight-forward way to comment out a block of code in Python is to use the # character. In such a case, it’s good to use a block comment. That way we don’t lose what we’ve already written.įurthermore, while it’s wise to keep comments short, sometimes we need more than one line to say what we need. In such an instance, it can be convenient to comment out an entire block of code. For instance, if we need to troubleshoot a part of the code and we’d like to see what happens if some block doesn’t execute. There are times when commenting out an entire block of code can be useful. The function call at the bottom is outside the aforementioned block of code. This block of code begins with a comment, followed by two more statements. In this example, we’ve identified the block of code that lies below the print_upper_case() function. Print_upper_case("taylor") # outside of the block # these statements make up a single block Example 1: Identifying a Block of Code in Python def print_upper_case(name): In Python, blocks of code rest on the same indentation level. This can include several statements as well as comments. In general, a block of code refers to multiple likes of code that are grouped together. This is especially true with a large program, spanning multiple files. Why? Chances are, when you come back to the program in a month or two, you won’t remember exactly how everything works. You don’t want your coworkers tearing out their hair trying to figure out what some badly named variable does.Īnd even if you are working alone, writing software in solo-mode, it’s a good idea to put some comments in the program. And without comments to help decipher the code, it can be challenging for new developers to get up to speed. Poor choices early on can lead to code that is difficult to maintain. For that team to function correctly, everyone needs to be on the same page. This means a larger codebase and a larger team. Why Writing Comments is ImportantĪs a project grows, it becomes necessary to expand. This allows us to test alternative logic or troubleshoot our programs. By blocking out a line of code, we can prevent it from being compiled. We can also use comments to remove parts of a program for testing purposes.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |