How can we see all the objects and functions in a module?


#1

Question

In the context of this exercise, how can we see all the objects and functions in a module?

Answer

Python 3 provides some useful functions that let us view information about the attributes of a module.

You can use the dir() function, which returns a list of all the attributes of a specified module.

import math
print(dir(math))

# This would output all the attributes of the math module
# ['__doc__', '__file__', '__loader__', '__name__', '__package__', 
# '__spec__', 'acos', 'acosh', …]

You can also use the help() function, usually run in the Python shell, and you can provide it with an argument, which can be the name of a module, function, class, or other object. It will then provide an interactive help page about it.

>>>help(math)