Exploring with dir and help

Learn how to explore Python objects and get help using the dir() and help() functions.

Ali Berro

By Ali Berro

5 min read Section 5
From: Python Fundamentals: From Zero to Hero

Exploring with dir and help

For any Python programmer, it’s common to encounter many built-in functions, methods, and objects. Sometimes the question arises: “What can be done with this object?” or “How is this function used?” Python provides two powerful built-in functions to explore and understand code: dir() and help().

These functions are invaluable tools for learning and discovering what’s available in Python. Instead of memorizing everything or constantly looking up documentation online, these functions can be used directly in the Python interpreter to explore and learn.

The dir() function

The dir() function returns a list of names (attributes and methods) available in a given object. It’s like getting a directory listing of everything an object can do. When called without arguments, it returns the names in the current scope.

Using dir() without arguments

When dir() is called without any arguments, it shows all the names defined in the current scope:

x = 5
name = "Alice"
print(dir())

This will output a list of names including x, name, and many built-in names like __builtins__, __doc__, etc. Most of these are internal Python names that don’t need to be worried about right now.

Using dir() with an object

The most useful way to use dir() is to pass it an object to see what attributes and methods the object has:

x = "Hello"
print(dir(x))

This will show all the methods and attributes available for strings. Methods like upper(), lower(), replace(), and many others will be visible. We will explore these methods in more detail in the next section.

The help() function

While dir() tells what is available, help() tells how to use it. The help() function displays documentation (docstrings) for objects, functions, modules, or keywords.

Getting help on a function

To get help on a function, pass the function name to help():

help(print)
Help on built-in function print in module builtins:
print(...)
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.

This will display detailed documentation about the print() function, including its parameters, what it does, and how to use it.

Getting help on an object

You can also get help on objects:

x = 5
help(x)

This will show documentation for the integer type, including all available methods and what they do.

Getting help on a method

To get help on a specific method, you can use the object’s method directly:

text = "hello"
help(text.upper)

This will show documentation for the upper() method, explaining that it returns a copy of the string with all characters converted to uppercase.

Exercises

Exercise 1: Understanding dir()

What does the dir() function do when called without arguments?

A: It shows all built-in functions in Python

B: It shows all names (attributes and methods) available in the current scope

C: It shows all methods available for strings

D: It shows documentation for a function

Answer:

B: It shows all names (attributes and methods) available in the current scope

Exercise 2: True or False

Is the following statement true or false?

The dir() function can be used to see what methods are available for a specific object.

Answer:

True. The dir() function can be passed an object as an argument to see all the attributes and methods available for that object. For example, dir("hello") will show all methods available for strings, such as upper(), lower(), replace(), etc.

Exercise 3: Understanding help()

What is the main purpose of help()?

A: To list all available methods of an object

B: To display documentation and explain how to use functions, objects, or methods

C: To execute a function and see its output

D: To show all variables in the current scope

Answer:

B: To display documentation and explain how to use functions, objects, or methods

Exercise 4: Using help() on methods

How to get help documentation for the upper() method of a string object?

A: help(upper)

B: help("hello".upper)

C: help(string.upper)

D: Both A and B

Answer:

B: help("hello".upper)

To get help on a specific method, reference it from an object instance. help("hello".upper) will show documentation for the upper() method. Option A won’t work because upper alone is not defined in the scope. Option C would require importing a string module, which is not the standard way for built-in string methods.

Exercise 5: True or False

Is the following statement true or false?

The dir() and help() functions serve the same purpose and can be used interchangeably.

Answer:

False. The dir() and help() functions serve different purposes:

  • dir() shows what is available (lists attributes and methods)
  • help() shows how to use something (displays documentation)

They complement each other. Use dir() to discover what’s available, and then use help() to learn how to use those methods or functions.

Course Progress

Section 17 of 17

Back to Course