Python dir(), help(), and __name__ – Built-in Tools for Introspection
Introduction – Why Use Python Introspection Tools?
Python provides powerful built-in tools for introspection—the ability to examine objects, modules, and programs at runtime. Three essential tools every Python developer should master are:
dir()– Lists available attributes and methods of an objecthelp()– Provides documentation and usage help__name__– A special variable that controls module behavior
These tools enhance debugging, documentation, and modular code reuse.
In this guide, you’ll learn:
- How to use
dir()to inspect Python objects - How to use
help()to explore documentation - What
__name__ == "__main__"means and why it’s used - Best practices for using these features in real-world projects
dir() – List Attributes of an Object or Module
print(dir(str))
Output (Partial):
['capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', ...]
Use Case: Quickly explore available methods on data types, classes, modules, etc.
help() – Get Help on Objects, Functions, Modules
help(str.upper)
Output:
Help on built-in function upper:
upper() method of builtins.str instance
Return a copy of the string converted to uppercase.
Tip: Use help() in the interactive shell to discover usage syntax and docstrings.
__name__ Variable – Module Execution Context
In every Python script, the built-in __name__ variable tells Python whether a module is being run directly or imported.
Example:
# demo.py
def greet():
print("Hello!")
if __name__ == "__main__":
greet()
Explanation:
- When run directly:
__name__ == "__main__"→greet()is executed. - When imported:
__name__ == "demo"→greet()is not executed.
Common Use Case – Importable vs Executable Scripts
# math_utils.py
def add(a, b):
return a + b
if __name__ == "__main__":
print(add(2, 3)) # This only runs when executed directly
This allows:
- Safe importing from the module without triggering code execution
- Reusability in multiple projects
Summary of Use Cases
| Tool | Purpose |
|---|---|
dir() | List attributes/methods of an object/module |
help() | View documentation for a class/function |
__name__ | Identify how a module is being used |
Summary – Recap & Next Steps
Python provides introspection tools like dir() and help() for exploring objects, and the __name__ variable for controlling module execution. These are essential for debugging, documentation, and modular coding.
Key Takeaways:
- Use
dir()to list attributes and methods of any object. - Use
help()to access built-in documentation from the interpreter. - Use
__name__ == "__main__"to write reusable and executable modules.
Real-World Relevance:
These features are used in Python REPL, libraries, scripts, and debugging sessions across all environments—from simple scripts to large-scale applications.
FAQ Section – Python dir(), help(), __name__
What does dir() do in Python?
dir() returns a list of all valid attributes and methods associated with an object. It helps inspect what you can do with a module, class, or variable.
How is help() different from dir()?
While dir() shows attribute names, help() displays detailed documentation and usage instructions, including docstrings for functions, modules, and objects.
What is the purpose of __name__ in Python?
__name__ is a special built-in variable that tells you whether a script is being run directly (__name__ == "__main__") or imported as a module.
Why use if __name__ == "__main__" in a script?
It ensures that certain code only runs when the script is executed directly, and not when it’s imported. This is commonly used for test code or script entry points.
Can I use dir() and help() with custom functions or classes?
Yes! Both tools work with user-defined functions, objects, and classes—great for exploring your own code or debugging unfamiliar ones.
Share Now :
