Indentation in Python — Solution Approach

META

Activist
SUPREME
MEMBER
Joined
Mar 1, 2026
Messages
118
Reaction score
978
In the vast majority of programming languages, if you remove all indentation from the entire source code and then apply auto-formatting, the program will remain fully functional and will also be formatted in a consistent style.

At first glance, such an operation seems impossible in Python.

At least, I couldn’t find a way to instantly detect accidentally shifted indentation in Python. So I had to come up with my own solution.


---

Introduction

Dear reader!

If you agree with at least one of the following statements:

1. Programming is a high art.


2. When programming in a language, you should use its full power and variety to minimize the source code as much as possible.


3. Your code should demonstrate a high level of expertise so that no one can call it “dumb”.



If so — please don’t read this article. Don’t waste your time on it.

This article discusses a rather artificial and arguably absurd aspect of programming.


---

Dear reader!

If instead you agree with the following:

1. Programming is routine, repetitive work, similar to digging an endlessly winding trench.


2. You should use a minimal and optimal subset of a language so that even a junior developer can easily understand your code.


3. Your program should be somewhat “simple” so that after deployment, you can hand it over to a junior developer for maintenance.



If all three apply to you, then you might relate to the problem I’m addressing.


---

Main drawbacks of Python

1. Python is not optimized for resource usage, making it unsuitable for resource-critical applications such as mobile apps or low-level software.


2. Python is slow and effectively single-threaded due to the Global Interpreter Lock (GIL).


3. Python relies entirely on indentation for code blocks. Because of this:



Readability may suffer

Full auto-formatting is difficult

Accidental indentation errors can occur and be hard to detect



---

The first issue is inherent and acceptable.

The second is mitigated by Python’s excellent interoperability with C/C++.

But for the third issue, I propose a solution.


---

The problem

In most languages, auto-formatting fixes indentation.

In Python, this seems impossible.

In real-world development, code is constantly moved, refactored, and modified.

A small accidental shift in indentation can:

Keep the program running

But silently change its logic


These bugs are extremely hard to detect.


---

Solution: Explicit block endings

Python blocks are formed by:

class

def

for / while

if

try

with


I propose introducing explicit “closing statements” for each block.


---

class / def

Problem: nested functions

Solution: add return at the end


---

for / while

Add continue at the end:

for ...:
...
continue

This ensures indentation errors cause runtime issues instead of silent logic bugs.


---

if

Use:

if ...:
...
elif ...:
...
elif 1: # instead of else
...
elif 0: pass # end of block

This makes block boundaries explicit and prevents silent errors.


---

try

Same idea:

try:
...
except ...:
...
except 1:
...
except 0: pass

For finally, move logic into a function:

def my_func():
...
return

finally:
my_func()


---

with

No natural closing statement exists.

Options:

Move logic into a function

Or avoid using with entirely



---

Benefits

Using these conventions:

You can auto-format code safely

Block boundaries become explicit

Accidental indentation errors are easier to detect



---

Conclusion

If you:

Structure blocks explicitly

Use C/C++ where needed

Avoid Python in resource-critical systems


Then Python has almost no serious drawbacks.


---

Advantages of Python

Simplicity

Fast development cycle

Powerful ecosystem


Together, these make Python nearly ideal under the right conditions.
 
Top Bottom