πŸ“¦ Python Modules & Package Management
Estimated reading: 3 minutes 41 views

🧭 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 object
  • help() – 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

ToolPurpose
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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Python dir(), help(), __name__ variable

Or Copy Link

CONTENTS
Scroll to Top