7

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")
10
  • I guess there is no solution for your problem. A good IDE maybe can help you to indent multiple lines at the same time. Commented Nov 29, 2018 at 11:43
  • 2
    Interesting question. if True: seems to be the closest you'll get to this behaviour but some other statement might work better Commented Nov 29, 2018 at 11:45
  • 1
    well solution seems obvious to me: do not comment code at all, if you are using debugger then you can change condition from x == 0 to __debug__ or x == 0 Commented Nov 29, 2018 at 11:48
  • 3
    if True: # x==0: seems like the easiest approach. Commented Nov 29, 2018 at 11:50
  • 2
    You could also use if 1: if you want to type fewer characters. But as @khelwood says if 1: # x==0: would be neater. Commented Nov 29, 2018 at 11:51

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.