Special Variables in Python

Special Variables in Python
Lesson 17: Special Variables in Python

Python includes several special variables — these are predefined identifiers that begin and end with double underscores (__), often 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 for you to:

  • Control program execution flow,
  • Provide metadata about a script or module,
  • Support advanced features like object introspection and inheritance.

You’ll often see them in code surrounded by double underscores such as:

__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.

Example: __name__ — Identifying How a Script Is Run

One of the most important special variables is __name__. It helps Python know 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.")

Output:

This script is running directly!

But if you import it from another file:

import example

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 block runs only when the file is executed directly, not when imported.

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.

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 function or variable type hints.

Example :

print(__file__)  # Shows the current script’s location
print(__doc__)   # Displays the docstring if one exists

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 one is __name__.
  • Use if __name__ == "__main__": to make your scripts both runnable and importable.

Tip:

“Dunder” variables are part of Python’s internal language design use them with care, understand their role, and avoid redefining them unless absolutely necessary.

Now that you have understanding of Special Variables in Python. Your next step is to learn about Memory and Identity in Python in detail.