Search code examples
pythonif-statementindentationwith-statement

Can I combine if and with statements in a single line?


I've a code block as follows:

if condition:
    with something as another_thing:
        // do my stuff here

I don't like this style due to multiple indentations. Can I combine if and with statements in a single line somehow, and reduce the indentation?


Solution

  • You can't, but you could put multiple statements on the same line. You can read more about it here.