Table of Contents | |
Understanding Python help()
Function
Python help() is a built-in function that provides access to the Python documentation system. It’s an interactive tool that you can use to get information about modules, classes, functions, and other Python objects. When you call help() with an object as an argument, it displays the object’s documentation, including a description of what the object does, its parameters (if it’s a function or method), its attributes (if it’s a class or module), and other relevant details. If you call Python help() without arguments, it starts an interactive help session in your console.
Syntax of Python help()
help(object)
Explanation
help()
: Built-in function that provides access to the Python documentation.object
(optional): Object you want to get help on (e.g., a function, class, module, or variable). It is used as input forhelp()
function. If omitted, an interactive help session starts.
Example of Python help()
help(print)
Explanation
help(print)
: Calls thehelp()
function with the built-inprint
function as an argument, displaying its documentation.
Output
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.
help()
Parameters
Python help() function can take one optional parameter, which is the object you want to get help on. This object can be anything: a built-in function, a module, a class, an instance of a class, or even a string representing the name of something you want to learn about. If you don’t provide any parameters, help()
starts an interactive help session where you can type in names of objects or topics to get more information.
Syntax
help(object)
Example
help(len)
Explanation
help(len)
: Callshelp()
with the built-in functionlen
as an argument, displaying its documentation.
Output
Help on built-in function len in module builtins:
len(obj, /)
Return the number of items in a container.
help()
Return Value
Python help() function doesn’t return a value you typically store in a variable. Instead, it directly prints the help information to the console. If you call help() with an object, it displays the object’s documentation and returns None. If you call help() without any arguments to start an interactive session, it doesn’t return any value but waits for your input within the help utility.
Syntax
help(object) # Returns None after displaying help
Explanation
help()
: Function displays help information and returnsNone
.object
(optional): Object you want help on. If this parameter is not used then interactive help session is started. It is the input forhelp()
function.
Example
result = help(list)
print(result)
Explanation
result = help(list)
: Callshelp()
on the built-inlist
type, which prints the help information and returnsNone
.print(result)
: Prints the value ofresult
, which isNone
.
Output
Help on class list in module builtins:
class list(object)
| list(iterable=(), /)
|
| Built-in mutable sequence.
|
| If no argument is given, the constructor creates a new empty list.
| The argument must be an iterable if specified.
|
| Methods defined here:
|
| __add__(self, value, /)
| Return self+value.
….(Many lines of output not shown here)…
| sort(self, /, *, key=None, reverse=False)
| Sort the list in ascending order and return None.
|
| The sort is in-place (i.e. the list itself is modified) and stable (i.e. the
| order of two equal elements is maintained).
|
| If a key function is given, apply it once to each list item and sort them,
| ascending or descending, according to their function values.
|
| The reverse flag can be set to sort the list in descending order.
|
| ———————————————————————-
| Static methods defined here:
|
| __new__(*args, **kwargs) from builtins.type
| Create and return a new object. See help(type) for accurate signature.
None
Interactive Help Session
When you call Python help() without arguments, an interactive help session starts in the console. In this session, you can type the name of a module, class, function, or keyword, and Python will display its documentation. You can also type topics like “keywords”, “symbols”, or “modules” to get general help. To exit the interactive help session, you type “quit”.
Syntax
help()
Explanation
help()
: Calling this function without arguments starts an interactive help session.
Example
# Start an interactive help session by running:
# help()
# Then, at the help> prompt, type a module name, class name, function name, or 'quit' to exit.
Explanation
# help()
: If uncommented and executed, would start the interactive help session.# Then, at the help> prompt, type a module name, class name, function name, or 'quit' to exit.
: An instruction inside the code.
Output
This will start an interactive session in the console and will not print any output until you give some input inside the interactive session.
Help with Print Function in Python
If you want help with the built-in print() function, you can pass it to Python help(). This will display the print() function’s documentation, showing its parameters, their default values, and a brief description of how to use it. This is useful when you need a quick reminder of the print() function’s capabilities, like how to change the separator or the end character.
Syntax
help(print)
Explanation
help()
: Function provides help on the given object.print
: Built-in function you’re getting help on. It is passed as input forhelp()
function.
Example
help(print)
Explanation
help(print)
: Callshelp()
on theprint
function to display its documentation.
Output
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.
Help on User Defined Class in Python
You can get help on your classes using Python help() if you’ve provided docstrings in your class definition. A docstring is a string literal that occurs as the first statement in a module, function, class, or method definition and describes what that component does. When you call help() on an instance of your class or the class itself, Python displays the class’s docstring along with the docstrings of its methods.
Syntax
class MyClass:
"""This is a docstring for MyClass."""
def my_method(self):
"""This is a docstring for my_method."""
pass
help(MyClass)
Explanation
class MyClass:
: Defines a class namedMyClass
."""This is a docstring for MyClass."""
: This is the docstring forMyClass
.def my_method(self):
: Defines a methodmy_method
."""This is a docstring for my_method."""
: This is the docstring formy_method
.pass
: Indicates thatmy_method
has no implementation here.help(MyClass)
: Callshelp()
onMyClass
to display its documentation.
Example
class MyClass:
"""This is a docstring for MyClass."""
def my_method(self):
"""This is a docstring for my_method."""
pass
help(MyClass)
Explanation
class MyClass:
: Defines a class namedMyClass
."""This is a docstring for MyClass."""
: Docstring forMyClass
, describing the class.def my_method(self):
: Defines a methodmy_method
withinMyClass
."""This is a docstring for my_method."""
: Docstring formy_method
.pass
: Placeholder in the method definition.help(MyClass)
: Callshelp()
onMyClass
to display its documentation, including docstrings.
Output
Help on class MyClass in module __main__:
class MyClass(builtins.object)
| This is a docstring for MyClass.
|
| Methods defined here:
|
| my_method(self)
| This is a docstring for my_method.
|
| ———————————————————————-
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)
If no Help or Info is Present
If you call Python help() on an object that doesn’t have any documentation (like a docstring), help() will still provide some basic information about the object, such as its type and possibly its signature if it’s a function or method. However, the output won’t be as detailed as when a docstring is present. In such cases, help() might show the object’s definition line and indicate that no further documentation is available.
Syntax
help(object)
Example
class MyClass:
pass
obj = MyClass()
help(obj)
Explanation
class MyClass:
: Defines a class namedMyClass
without a docstring.pass
: Indicates thatMyClass
has no methods or attributes defined yet.obj = MyClass()
: Creates an instance ofMyClass
.help(obj)
: Callshelp()
onobj
to display information about it.
Output
Help on MyClass in module __main__:
class MyClass(builtins.object)
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)
If a String is Given as an Argument
When you pass a string to Python help(), Python tries to find an object with that name in the current scope or the built-in modules. If it finds a match, it displays the help information for that object. If no object is found with that name, help() typically sends a message indicating that no documentation was found for the given string.
Syntax
help(“string_name”)
Explanation
help()
: Function looks for an object matching “string_name” and displays its help."string_name"
: Name of the object you want help on, passed as a string. It is used as input forhelp()
function.
Example
help("print")
Explanation
help("print")
: Callshelp()
with the string “print”, causing it to display help for the built-inprint
function.
Output
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.
Python help()
Function Docstring
Docstrings (documentation strings) are used to document Python code; you can access them using Python help(). When you call help() on an object with a docstring, Python will display that docstring as part of the help information. This is a standard way to provide usage instructions and details about classes, functions, modules, and other objects directly within the code.
Syntax
def my_function():
"""This is a docstring."""
pass
help(my_function)
Explanation
def my_function():
: Defines a function namedmy_function
."""This is a docstring."""
: This is the docstring formy_function
, which describes what the function does.pass
: A placeholder indicating that the function doesn’t perform any action here.help(my_function)
: Callshelp()
onmy_function
to display its documentation, including the docstring.
Example
def my_function():
"""This function adds two numbers."""
pass
help(my_function)
Explanation
def my_function():
: Defines a function namedmy_function
."""This function adds two numbers."""
: Docstring formy_function
.pass
: Placeholder in the function definition.help(my_function)
: Callshelp()
onmy_function
to display its docstring and other information.
Output
Help on function my_function in module __main__:
my_function()
This function adds two numbers.
Conclusion
Python help() function is a valuable tool for learning about Python objects and getting quick access to documentation. It can be used interactively without arguments to explore modules, functions, and classes, or it can be called with a specific object to get detailed information about that object. Understanding how to use Python help() effectively can significantly speed up your development process by providing immediate access to documentation and usage instructions directly within the Python environment.