How to find help in Python3

In this shot, we will go over the smart ways to find built-in functions on a particular data type without the use of Google.

dir()

One easy way to find all of the attributes of a particular data type is by applying the dir function. If we call dir() without any parameters, it returns a list of all the globally available attributes. If you pass a particular object, then dir() lists the attributes present inside that object.

Let’s apply dir() on the list and analyze the output.

print('Globally available attributes: ',dir())
print('Attributes available in list: ',dir(list))
print('Globally available builtin functions are: ',dir(__builtins__))
'''
__builtins__ object contains many functions that are very
much helpful for daily use like for converting float to int
we use int function actually the int function is present inside
the __builtins__ object when we write int(some_float_value)
then python automatically converts it into __builtins__.
int(some_float_value), similarly list(),tuple(),
len(),max(),..... are present inside __builtins_ object.
In the output we can see all the attributes present inside
the __builtins__ object.
'''

The dir(list) function call returns all the attributes that are present inside the list data type. dir(object) returns the names of attributes that are present inside the object. Here, the object can be anything, such as data type, function, class, etc.

help()

In Python, everything is an object. Data types, functions, and classes are all objects. You can use help() on an attribute if you want to know more information. An attribute can be any object, such as a datatype, predefined functions, user-defined functions, predefined classes, and user-defined classes.

print(help(list.count))
print(help(list.sort))
print(help(list))
class Student:
name='' # Let's take default name as ''
rollno=0 # Let's take default roll no is 0
section=1 # Let's take by default section as 1
def __init__(self,name,rollno,section):
self.name=name
self.rollno=rollno
self.section=section
def student_name(self):
print('Student name is ',self.name)
print(help(Student))

The above code returns a detailed description of the functions count and sort, as well as detailed information about the class list and the user-defined class, Student.

In the output, we can easily see what arguments we can pass, what the return type is, what the default arguments are of a function, what attributes are present inside a class, and more. We need to remember one thing: if we want to know about any attribute that is present inside a particular data type or object, then we need to call the help function.

For example, we can call help(data_type.attribute_name) or help(object.attribute_name). If we want to apply help on global objects, we can direct the call as help(object).

_annotations_ and _doc_

If we want to know even more information about the arguments, then we can use the __ annotations __ and __ doc __ attributes of the function.

The built-in <class 'builtin_function_or_method'> type does not have the __ annotations __ attribute. We can use __ annotations __ on any user-defined functions, provided the programmer wrote the annotations in code. If they didn’t, then an empty dictionary will be returned.

Let’s see an example.

def acceleration(d: 'Distance in meters',t: 'Time in seconds')->'Return the acceleration value in m/s':
'''
This function calculates the acceleration by taking distance and time as parameters.
'''
return d/t
print('acceleration.__annotations__ = ',acceleration.__annotations__)
print('acceleration.__doc__ = ',acceleration.__doc__)

_dict_

Annotations and docstrings are very helpful when we borrow functions from other people’s code or when we try to extend the work of others. In many situations, we don’t bother with the implementation of the function, we are simply interested in knowing what arguments to pass when calling a function, and what the return type is. This information about annotations and docstrings is very helpful.

We can also use the __dict__ attribute instead of the dir function to learn about all the attributes of an object. However, __dict__ returns a dictionary instead of the list that contains the property name and property value as key-value pairs of the dictionary.

The below code is an example of the __dict__ property:

print('list.__dict__ = ',list.__dict__)
class Student:
name='' # Let's take default name as ''
rollno=0 # Let's take default roll no is 0
section=1 # Let's take by default section as 1
def __init__(self,name,rollno,section):
self.name=name
self.rollno=rollno
self.section=section
def student_name(self):
print('Student name is ',self.name)
print('Student.__dict__ = ',Student.__dict__)
s1=Student('Harsha','03','15')
print('s1.__dict__ = ',s1.__dict__)
'''
Below is a small code to illustarte that functions are also
objects in python for the below function just i am assigning two
attributes one is author and other is created data.If we use
__dict__ on function we can see that the dictionary is written
that contains all the aatributes of function along with the values
'''
def test_func(a,b):
return 'test'
test_func.author='Harsha'
test_func.date='28-08-2021'
print('__dict__ attribute on user defined function test_func : ',test_func.__dict__)

Takeaways

Let’s say you participate in a hackathon, or, while writing a company selection exam, you suddenly forget the functions that help solve a problem. You can use the dir and help functions to search for all the available built-in functions in the builtins module, as well as in specific objects like list, tuple, etc.

In other cases, like if you are using someone else’s code and you want to know all the available attributes that are present inside a built-in or user-defined object, then you can use the functions dir() and help() and the attributes __annotations__, __doc__, and __dict__.

Free Resources