Search code examples
pythonpython-3.xindentation

Is there a way to keep indentation when commenting out the control statement


In python, is there a way to keep the indentation of a block of code within the control statement when commenting out the control statement?

The use case would be a situation where you want to temporarily comment out an if-statement, but don't want to unindent the nested code. Am looking for a dummy control statement as follows:

Originally:

if condition is True:
    print('print')

Temporarily:

#if condition is True:
dummy_control_statement:
    print('print')

Solution

  • Here a couple of options that might work for you:

    1. if False and original-condition: (easily replace all False and with empty string to revert)
    2. if False: (if you prefer to comment out the original line of code)