Special Variables in Python

Python includes several special variables — these are predefined identifiers that begin and end with double underscores (__)
, commonly called “dunder”
variables (short for double underscore).
These variables have special purposes and are automatically created and used by Python to manage how modules, classes, and programs behave.
1. What Are Special Variables?
Special variables are built-in attributes that Python defines to:
- Control program execution flow,
- Provide metadata about a scripts or modules,
- Support advanced features like object introspection and inheritance.
You’ll often see them in code surrounded by double underscores —
.
Common examples include:
__name__
__file__
__doc__
__init__
These are not meant to be created or changed casually — Python uses them internally, but developers can use some (like __name__
) for useful control logic.
The Most Important __name__
— (Identifying How a Script Is Run)
One of the most important special variables is __name__
. It helps Python whether a file is being executed directly or imported as a module into another file.
Example:
# file: example.py
if __name__ == "__main__":
print("This script is running directly!")
else:
print("This script has been imported as a module.")
When run directly::
This script is running directly!
When imported from another file:
import example # Output: This script has been imported as a module.
Output:
This script has been imported as a module.
Why Is __name__
Useful?
It’s especially handy when writing reusable code:
- You can keep test or demo code inside the if name == “main”: block.
- That code/block runs only when the file executes directly, not when imported.
Practical Example::
def greet():
print("Hello from my module!")
if __name__ == "__main__":
greet() # Runs only if executed directly
✅ When you run python my_module.py, it prints “Hello from my module!” ✅ When you import my_module elsewhere, it doesn’t print anything automatically (function available for use).
Other Common Special Variables :
__name__
- Identifies how the file is executed (main vs. imported).__file__
- Stores the path of the current Python file.__doc__
- Holds the module’s documentation string (if provided).__package__
- Indicates the package that a module belongs to.__annotations__
- Holds/stores function or variable type hints.
Usage Examples::
print(__file__) # Shows the current script’s location
print(__doc__) # Displays the docstring if one exists/provide
Key Takeaways
- Special variables begin and end with double underscores (
__var__
). - They give Python internal metadata about files, objects, and execution state.
- The most common and practical for controlling script behavior
__name__
. - Use if
__name__ == "__main__":
to make your scripts both runnable and importable (i.e., to create dual-purpose scripts).
Tip:
“Dunder”
variables are part of Python’s internal language design—
use them carefully, understand their purpose, and avoid redefining them unless absolutely necessary.
Understanding special variables helps you write more professional and reusable Python code. They’re especially important when building modules and packages for others to use.
To continue building your skills, your next step is to learn about Constant in Python.
Hope this tutorial was helpful.
Thanks for reading!