dir and help

Introspection functions

Interview Relevant: Debugging tools

dir and help

Explore objects and get documentation.

Code Examples

Introspection with dir and help.

python
1# dir - list attributes and methods
2dir([])  # All list methods
3dir(str) # All string methods
4
5# Filter for non-dunder
6[x for x in dir([]) if not x.startswith("_")]
7
8# help - get documentation
9help(len)
10help(str.split)
11help([].append)
12
13# Useful for exploration
14s = "hello"
15dir(s)  # See available methods
16help(s.upper)  # Learn how to use
17
18# __doc__ for docstrings
19print(len.__doc__)
20
21# vars - get __dict__
22class MyClass:
23    x = 1
24vars(MyClass)

AI Tutor

Ask about the topic

Sign in Required

Please sign in to use the AI tutor

Sign In