Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a blank indentation in Python?

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")
like image 853
Cerno Avatar asked Nov 29 '18 11:11

Cerno


People also ask

Is there indentation in Python?

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.

Do blank lines get ignored for indentation in Python?

@Lo'oris Indentation is mandatory for code but not for blank lines. Therefore any spaces in a blank line are unnecessary.

How many spaces is a \t Python?

Python uses two spaces to indent code blocks. This style is popular among newer programmers because it is easy to learn and read.

What's an indented block in Python?

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.

How to indent statements in Python?

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.

What is indentationerror in Python?

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.

How many spaces are there for indentation in Python?

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.

Is indentation required for blank lines in code?

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


1 Answers

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.

like image 119
khelwood Avatar answered Sep 20 '22 04:09

khelwood