What is the use of help() and dir() in Python?
Both are built-in functions that help you explore Python from the console. help() shows the documentation of a function, class or module. dir() shows a list of the names (attributes and methods) that an object has.
def greet(name):
"""Print a greeting."""
print("Hi", name)
help(greet)
Output:
Tutorials Class - Output Window
Help on function greet in module __main__:
greet(name)
Print a greeting.
items = [3, 1, 2]
print("append" in dir(items))
print("upper" in dir(items))
Output:
Tutorials Class - Output Window
True
False
So dir() answers “what can I do with this object?” and help() answers “how do I use it?”. The text shown by help() comes from the docstring.
Learn more about the similar topics:
| Tutorials |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |