π§ 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 :
