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 verymuch helpful for daily use like for converting float to intwe use int function actually the int function is present insidethe __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 insidethe __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 0section=1 # Let's take by default section as 1def __init__(self,name,rollno,section):self.name=nameself.rollno=rollnoself.section=sectiondef 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/tprint('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 0section=1 # Let's take by default section as 1def __init__(self,name,rollno,section):self.name=nameself.rollno=rollnoself.section=sectiondef 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 alsoobjects in python for the below function just i am assigning twoattributes one is author and other is created data.If we use__dict__ on function we can see that the dictionary is writtenthat 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__)
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__
.