Over the course of development it sometimes becomes necessary to temporarily comment out blocks of code for testing purposes.
Sometimes these comments require re-indentation of code fragments that could become difficult to take back later without introducing errors.
I was wondering whether there was a "blank indentation" operator, sort of the opposite of "pass"
Example:
def func(x):
if x == 0:
print("Test")
For testing, I am commenting out the "if" temporarily, which breaks indentation:
def func(x):
# if x == 0:
print("Test")
I want to avoid re-indentation like this, since it's only a temporary change and it could mess up more complex code:
def func(x):
# if x == 0:
print("Test")
Question: Is there something like this?
def func(x):
# if x == 0:
force_indent:
print("Test")
Of course, I could do the following, I was just wondering whether there was some sort of idiom or better way to do this:
def func(x):
# if x == 0:
if True:
print("Test")
Python uses indentation to highlight the blocks of code. Whitespace is used for indentation in Python. All statements with the same distance to the right belong to the same block of code. If a block has to be more deeply nested, it is simply indented further to the right.
@Lo'oris Indentation is mandatory for code but not for blank lines. Therefore any spaces in a blank line are unnecessary.
Python uses two spaces to indent code blocks. This style is popular among newer programmers because it is easy to learn and read.
expected an indented block This means that a function must have at least one line of code. It also means that a conditional must have at least one line of code to run if the condition is true.
When writing python code, we have to define a group of statements for functions and loops. This is done by properly indenting the statements for that block. The leading whitespaces (space and tabs) at the start of a line is used to determine the indentation level of the line.
Let’s look at some examples of the IndentationError in the Python code. We can’t have indentation in the first line of the code. That’s why IndentationError is thrown. The code lines inside the if block have different indentation level, hence the IndentationError.
Unlike many other programming languages, where flower braces {} or other mechanism is used to define scope or a block of code, Python does not have those. Instead, it uses indentation. You can use any number of spaces for indentation. But, you have to provide same number of spaces for statement in a code block.
5 3 @Lo'oris Indentation is mandatory for code but not for blank lines. Therefore any spaces in a blank line are unnecessary. – wm_eddie
The easiest approach, it seems to me, is to insert True: #
into your if
statement.
So
if x==0:
becomes
if True: # x==0
Your indentation can stay the same, and the old condition is still clear and easy to go back to.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With