The quickest way to find help for Python commands: The help() command
As mentioned in my previous post, we’ve just completed the latest round of our Python Basics Course for Data Scientists. Also this time we got a lot of new and very useful input. Among some typo corrections there was yet another handy Matplotlib “hack” for opening plot figures in stand-alone windows from Jupyter notebooks. This was contributed by Miguel Fernandez (/), who co-moderated the course.
Another contribution, also by Miguel, was actually a reminder of one of the most basic Python features that I haven’t thought of for a long time: Python’s help()
ꜛ function:
help(command or object)
I think I have become a bit lazy over the time by putting every command I wanted to look up into a web search engine, while the answer for, e.g., a quick syntax lock up was just a fingertip away. For instance, if you just want to look up all builtin modules of lists, this is the excerpt of the corresponding console output of help(list)
:
help(list)
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.
[...]
Toggle full console 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.
|
| __contains__(self, key, /)
| Return key in self.
|
| __delitem__(self, key, /)
| Delete self[key].
|
| __eq__(self, value, /)
| Return self==value.
|
| __ge__(self, value, /)
| Return self>=value.
|
| __getattribute__(self, name, /)
| Return getattr(self, name).
|
| __getitem__(...)
| x.__getitem__(y) <==> x[y]
|
| __gt__(self, value, /)
| Return self>value.
|
| __iadd__(self, value, /)
| Implement self+=value.
|
| __imul__(self, value, /)
| Implement self*=value.
|
| __init__(self, /, *args, **kwargs)
| Initialize self. See help(type(self)) for accurate signature.
|
| __iter__(self, /)
| Implement iter(self).
|
| __le__(self, value, /)
| Return self<=value.
|
| __len__(self, /)
| Return len(self).
|
| __lt__(self, value, /)
| Return self<value.
|
| __mul__(self, value, /)
| Return self*value.
|
| __ne__(self, value, /)
| Return self!=value.
|
| __repr__(self, /)
| Return repr(self).
|
| __reversed__(self, /)
| Return a reverse iterator over the list.
|
| __rmul__(self, value, /)
| Return value*self.
|
| __setitem__(self, key, value, /)
| Set self[key] to value.
|
| __sizeof__(self, /)
| Return the size of the list in memory, in bytes.
|
| append(self, object, /)
| Append object to the end of the list.
|
| clear(self, /)
| Remove all items from list.
|
| copy(self, /)
| Return a shallow copy of the list.
|
| count(self, value, /)
| Return number of occurrences of value.
|
| extend(self, iterable, /)
| Extend list by appending elements from the iterable.
|
| index(self, value, start=0, stop=9223372036854775807, /)
| Return first index of value.
|
| Raises ValueError if the value is not present.
|
| insert(self, index, object, /)
| Insert object before index.
|
| pop(self, index=-1, /)
| Remove and return item at index (default last).
|
| Raises IndexError if list is empty or index is out of range.
|
| remove(self, value, /)
| Remove first occurrence of value.
|
| Raises ValueError if the value is not present.
|
| reverse(self, /)
| Reverse *IN PLACE*.
|
| 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 in descending order.
|
| ----------------------------------------------------
| Class methods defined here:
|
| __class_getitem__(...) from builtins.type
| See PEP 585
|
| ----------------------------------------------------
| Static methods defined here:
|
| __new__(*args, **kwargs) from builtins.type
| Create and return a new object. See help(type) for accurate signature.
|
| ----------------------------------------------------
| Data and other attributes defined here:
|
`
| __hash__ = None
The output lists all methods and functions, that are available for lists. If you want to look up a specific method, just refine your help request:
help(list.append)
Help on method_descriptor:
append(self, object, /)
Append object to the end of the list.
help(list.insert)
Help on method_descriptor:
insert(self, index, object, /)
Insert object before index.
To recap, e.g., how to use append()
or insert()
to add additional entries to a pre-defined list, this was probably the quickest way to get an answer:
L = [1, 2, 3]
L.append(5)
print(f"L: {L}")
L: [1, 2, 3, 5]
L.insert(1, 99)
print(f"L: {L}")
L: [1, 99, 2, 3, 5]
The help()
function also works with your own defined functions and classes by reading the docstring therein:
def my_sum(x,y):
""" This my own sum function.
INPUT(S):
- x: the first number. must be a number.
- y: the first number. must be a number.
RETURN(S):
- z: the sum of x and y
"""
z = x+y
return z
help(my_sum)
Help on function my_sum in module __main__:
my_sum(x, y)
This my own sum function.
INPUT(S):
- x: the first number. must be a number.
- y: the first number. must be a number.
RETURN(S):
- z: the sum of x and y
The conclusion of all of this might be, that we should stop to search everything on the web, the answer is often faster available by using, e.g., local resources. For example, for help()
command you don’t even have to leave your Python environment and it fully works without an internet connection.
comments