ReviseAlgo Logo

Built-in Functions

dir and help

Introspection functions for exploring objects and documentation

Interview: Essential debugging and exploration tools — expected knowledge for Python developers

Last Updated: June 12, 2026 7 min read

dir() and help() are introspection functions that help you explore objects at runtime. dir() lists all attributes and methods of an object, while help() displays formatted documentation. These are invaluable for debugging, learning new libraries, and understanding unfamiliar codebases.

dir() Function

  • No arguments: dir() — lists names in the current local scope
  • With object: dir(obj) — lists all attributes, methods, and dunder methods
  • Returns list of strings: Easy to filter with list comprehensions or startswith()
  • Custom __dir__: Classes can override to customize what dir() returns

help() Function

  • With object: help(obj) — displays formatted docstring and signature
  • With module: help(json) — shows module documentation
  • Interactive mode: help() enters interactive help mode — type any topic
  • Uses docstrings: Displays __doc__ attribute formatted for readability

Other Introspection Tools

  • vars(): Returns __dict__ — the object's writable attributes as a dict
  • getattr/setattr/hasattr: Access, set, and check attributes dynamically by name
  • inspect module: inspect.signature(func), inspect.getsource(func)
  • __doc__: Direct access to an object's docstring as a string

Practical Debugging with dir()

When working with unfamiliar objects, use [x for x in dir(obj) if not x.startswith('_')] to see public methods only. Combine with help(obj.method) to learn how to use each method. This is faster than searching documentation.

Use Cases

Interactive debugging — quickly discovering methods on unfamiliar objects

Building documentation generators and API reference tools

Dynamic attribute access for frameworks (ORMs, serializers)

Learning new libraries interactively in the Python REPL

Inspecting function signatures for decorators and wrappers

Common Mistakes

Not filtering dir() output — too many dunder methods make it hard to find useful methods

Using type(x) instead of isinstance() for type checking — always prefer isinstance

Forgetting that help() paginates in the terminal — use help(obj) | cat or capture output

Not knowing about getattr(obj, name, default) — safer than obj.name when attribute might not exist

Over-using eval/exec when getattr/setattr can achieve the same dynamic behavior safely