I, too, remember sometimes wanting such structure. This is what comes to mind (in C code that allows this mis-indentation):
glBegin(GL_TRIANGLES);
drawVertices();
glEnd();
Note that we have a begin
and an end
, and from here on I'm going to assume that the same happens in your case: you want to denote the beginning and ending of something. Another situation would be opening and closing a file, or even the example in your question. Python has a specific feature for this: context managers. Python's documentation even has an example with exactly this:
(this is not recommended as a real way of generating HTML!):
from contextlib import contextmanager
@contextmanager
def tag(name):
print("<%s>" % name)
yield
print("</%s>" % name)
>>> with tag("h1"):
... print("foo")
...
<h1>
foo
</h1>
I have to mention that context managers aren't simply a way to restructure your code, they can actually act on exceptions raised from the enclosed code, and execute some code regardless of the exception (e.g. to ensure that a file is closed). With the simple examples using @contextmanager
this does not happen because its default behavior is to simply re-raise the exception so no surprises happen.
Other than this, the people you work with will not be happy about the false indentation. If you insist on it, sure, if True
is an option.