Understanding Python Language Features【32】: Characteristics of Python Language: Indentation Mechanism and Naming Conventions
1. Preliminary Insights
In our previous articles, after mastering the basic knowledge points, we can write relatively complex programs. However, as we delve deeper into programming, we also need to make our programs look more aesthetically pleasing. Just like a chef cooking, it is not enough to just satisfy hunger; the dish must also be visually appealing and flavorful. This article will help everyone gain a deeper understanding of some characteristics of the Python language, which will allow us to write more elegant programs.
Python is characterized by its “indentation defines structure, simplicity at its core, and modules promote reuse,” emphasizing readability and development efficiency. Compared to other programming languages, the code is more readable and clear.
2. Indentation Mechanism and Naming Conventions
2.1 Indentation Mechanism: Syntax-Level Code Structuring
One of the most notable features of Python is its enforced indentation, which treats indentation as part of the syntax rather than just for aesthetic purposes. This design intuitively reflects the logical relationships of code blocks (such as loops, conditions, function definitions, etc.) through indentation levels, forcing developers to write more structured code.
Rules: Statements within the same code block must maintain the same indentation (usually 4 spaces, using Tab is not recommended to avoid cross-platform compatibility issues); different indentation levels represent different code block scopes.
Example:

If the indentation is incorrect (for example, inconsistent indentation within the same code block), it will directly trigger an IndentationError, preventing execution.
2.2 Naming Conventions: Readability-First Identifier Rules
Identifiers in Python are used to name variables, functions, classes, modules, etc. Their naming rules balance syntax constraints and conventional styles, with the core principle being readability.
Syntax Constraints:
Composed of letters, numbers, and underscores, and cannot start with a number;
Case-sensitive (name and Name are different identifiers);
Cannot use keywords (such as if, def, class, etc.).
Conventional Naming Styles:
Variables / Functions / Modules: lowercase letters + underscores (snake_case), such as user_name, calculate_sum();
Classes: Each word starts with an uppercase letter (PascalCase), such as Student, DataProcessor;
Constants: all uppercase + underscores, such as MAX_SIZE, PI = 3.14159;
Single underscore prefix (_var): indicates a “private” variable (conventionally not accessed directly, not enforced);
Double underscore prefix (__var): triggers name mangling (to avoid subclass overriding, such as __name will be changed to _ClassName__name).
Example:

2.3 Application of __name__ in Python Programs
In Python, __name__ is a special built-in variable (magic variable) used to identify the name of the current module. Its value changes based on how the module is run (directly or imported), and it is a core tool for implementing the “module self-test” functionality.
1. Core Function of __name__
When a module is run directly, the value of __name__ is the string “__main__”;
When a module is imported by another module, the value of __name__ is the filename of that module (without the .py extension).
2. Typical Application: Module Self-Test
Using __name__, you can write code in a module that is “executed only when run directly” (such as testing logic), and not executed when imported, avoiding interference with other modules.
Example: Suppose there is a module calculator.py, with the following content:

When calculator.py is run directly (e.g., python calculator.py), __name__ is “__main__”, and the self-test code will execute, printing the test results;
When importing calculator in another module main.py:

At this time, the __name__ of the calculator module is “calculator”, and the if condition is not satisfied, so the self-test code is skipped.
In fact, the functionality of __name__ is very useful when you write complex programs or engineering projects. If you are just writing a simple program, its effect is very limited. However, if your program involves many custom module imports, the block of code where __name__ resides can be considered the main program, executing only the functions or classes/objects within the main program block. This makes the overall program very clear and enhances readability.
3. Conclusion
Today we learned about the characteristics of the Python language regarding the indentation mechanism and naming conventions. Python simplifies the expression of code structure through enforced indentation, reducing syntactic redundancy (such as the {} in other languages); while naming conventions enhance code readability and consistency through conventions, reflecting the design philosophy of “elegance, clarity, simplicity.” These two points together constitute the core advantages of Python being easy to learn and maintain.
Let us maintain our enthusiasm for learning and practice more. See you next time!

#python