How to check the built-in function in Python


As a new programmer, you may find yourself wondering how to check if a certain built-in function exists in Python. After all, there are so many functions available, and it can be difficult to keep track of them all! Luckily, there is a simple way to do this. In this blog post, we’ll show you how to use the dir() function to check for the existence of a built-in function in Python.

The dir() Function

The dir() function is a built-in function in Python that returns a list of the attributes and methods of an object. This is useful for finding out what attributes and methods are available for an object.

# To check the built in function in python we can use dir(). If called without an argument, return the names in the current scope. Else, return an alphabetized list of names comprising (some of) the attribute of the given object, and of attributes reachable from it. >>> dir(__builtins__)
Code language: Python (python)

For example, let’s say we have a list named my_list. We can use dir() to check what attributes and methods are available for this list:

my_list = [1, 2, 3] print(dir(my_list)) # Output: ['add', 'class', 'class_getitem', 'contains', 'delattr', 'delitem', 'dir', 'doc', 'eq', 'format', 'ge', 'getattribute', 'getitem', 'gt', 'hash', 'iadd', 'imul', 'init', 'init_subclass', 'iter', 'le', 'len', 'lt', 'mul', 'ne', 'new', 'reduce', 'reduce_ex', 'repr', 'reversed', 'rmul', 'setattr', 'setitem', 'sizeof', 'str', 'subclasshook', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
Code language: Python (python)

From the output above, we can see that my_list has many built-in attributes and methods available.

Using dir() to Check if a Built-in Function Exists

Now that we know how to use dir(), let’s put it to good use! We can use dir() to check if a certain built-in function exists in Python. For example, let’s say we want to know if the len() function exists. We can do this by passing “len” into the dir() function:

print(dir("len")) #Output: ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'removeprefix', 'removesuffix', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
Code language: Python (python)

From the output above, we can see that the len() function does exist! If the len() function did not exist, then “len” would not be in the list returned by dir().

To know all the functions in a module we can assign the functions list to a variable, and then print the variable.

>>> import math >>> dir(math) # Output ['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt', 'lcm', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'nextafter', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc', 'ulp']
Code language: CSS (css)

Checking the built-in function by help() function.

Python has a lot of built-in functions. Sometimes, you may want to know what these functions are so that you can use them in your program. To do this, you will first want to open your Python interpreter. You can do this by opening your terminal and typing “python.”

Once you have your Python interpreter open, you will want to type “help()” and then hit the tab key. This will show you all of the available functions in Python. If you know the name of the function that you want to use, you can type it after the “help(” text. For example, if we wanted to learn more about the “max” function, we would type “help(“max”).

>>> help(max) Help on built-in function max in module __builtin__: max(...) max(iterable[, key=func]) -> value max(a, b, c, ...[, key=func]) -> value With a single iterable argument, return its largest item. With two or more arguments, return the largest argument.
Code language: JavaScript (javascript)

If you want to exit out of the help section, you can type “quit.”

The dir() function is a built-in function in Python that returns a list of the attributes and methods of an object. This is useful for finding out what attributes and methods are available for an object. It can also be used to check if a certain built-in function exists in Python. Simply pass the name of the built-in function into the dir() function as an argument. If the output includes the name of the built-in function, then it exists; if not, then it does not exist.

Andy Avery

I really enjoy helping people with their tech problems to make life easier, ​and that’s what I’ve been doing professionally for the past decade.

Recent Posts