View all of the built in functions that take in a particular data type in Python

Question:

How would I be able to view all of built in functions take in a particular object(list, str, int, float ….) as its parameter for example some functions that pass list are max(), min(), len() ..... I can view all of the methods by simply dir(object) like dir(list). How would I be able to do it for functions?

Asked By: Teoman Selcuk

||

Answers:

I think I have the answer to your question!
You can use the help() function to view the documentation of any built-in function in Python. To see all the built-in functions that take a particular object as a parameter, you can use the dir() function to get a list of all the attributes and methods of that object, and then filter out the ones that are not functions using a list comprehension.

For example, to see all the built-in functions that take a list as a parameter, you can do the following:

>>> dir(list) # get a list of all attributes and methods of the list object
[‘add‘, ‘class‘, ‘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’]

>>> [func for func in dir(list) if callable(getattr(list, func))] # filter out non-functions
[‘add‘, ‘class‘, ‘contains‘, ‘delattr‘, ‘delitem‘, ‘dir‘, ‘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’]

Answered By: Promaster

You can use the built-in help() function to view the documentation for any built-in function or module. For example, to see all the built-in functions that can take a list as an argument, you can enter the following command in a Python interpreter or script:

help(list)

This will display the documentation for the list type, including a list of all the built-in functions that can take a list as an argument.

Alternatively, you can use the dir() function to get a list of all the names defined in the built-in namespace. To see all the built-in functions that can take a list as an argument, you can use the following code:

import builtins
for name in dir(builtins):
  obj = getattr(builtins, name)
  if type(obj) is type(list) and name != 'list':
    print(name)

This code imports the builtins module (which contains all the built-in functions and types) and then iterates over all the names defined in it. For each name, it checks whether the corresponding object is a type that is a subclass of list (excluding the list type itself), and if so, prints the name. This will give you a list of all the built-in functions that can take a list as an argument.

Answered By: Vishal
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.