How to get the list of all built in functions in Python from Python prompt/command line as we get the list of keywords from it?
-
3I think you are looking for this https://stackoverflow.com/questions/8370206/how-to-get-a-list-of-built-in-modules-in-python – nikhalster May 01 '18 at 06:46
-
I guess no. I wasn't looking for the modules. – Harley C Brown May 01 '18 at 06:56
-
[Relevant.](https://stackoverflow.com/questions/22864221/is-the-list-of-python-reserved-words-and-builtins-available-in-a-library/22864250) – user2357112 May 01 '18 at 07:02
2 Answers
UPDATE:
There can be some confusion about __builtins__
or __builtin__
.
The What’s New In Python 3.0 suggests to use builtins
Renamed module
__builtin__
tobuiltins
(removing the underscores, adding an ‘s’). The__builtins__
variable found in most global namespaces is unchanged. To modify a builtin, you should usebuiltins
, not__builtins__
!
This may be good if you work with a different Python implementation as the docs indicate:
As an implementation detail, most modules have the name
__builtins__
made available as part of their globals. The value of__builtins__
is normally either this module or the value of this module’s__dict__
attribute. Since this is an implementation detail, it may not be used by alternate implementations of Python.
You can get all builtin names with:
>>> import builtins
>>> dir(builtins)
This includes everything from builtins
.
If you strictly only want the function names, just filter for them:
import types
builtin_function_names = [name for name, obj in vars(builtins).items()
if isinstance(obj, types.BuiltinFunctionType)]
Resulting list in Python 3.6:
['__build_class__',
'__import__',
'abs',
'all',
'any',
'ascii',
'bin',
'callable',
'chr',
'compile',
'delattr',
'dir',
'divmod',
'eval',
'exec',
'format',
'getattr',
'globals',
'hasattr',
'hash',
'hex',
'id',
'isinstance',
'issubclass',
'iter',
'len',
'locals',
'max',
'min',
'next',
'oct',
'ord',
'pow',
'print',
'repr',
'round',
'setattr',
'sorted',
'sum',
'vars',
'open']
If you want the functions objects, just change your code a little bit by selecting the 'obj' from the dictionary:
builtin_functions = [obj for name, obj in vars(builtins).items()
if isinstance(obj, types.BuiltinFunctionType)]

- 82,630
- 20
- 166
- 161
-
-
-
There are just the names. See my updated answer for how to get the function objects. i.e the variables. – Mike Müller May 01 '18 at 07:44
-
The answer as given doesn't give all the built-ins that are in the documentation (for example, help(), bool(), and many more). It is not a bug in this code, but it is worth being cautious as python doesn't use the BuiltinFunctionType for all its built-in functions. – rafraf Feb 08 '22 at 10:47
-
I managed to get a complete list (ugly) by including all callables in builtin that were not Exception classes... – rafraf Feb 08 '22 at 11:11
-
More accurately, including all callables in builtins module that were not BaseException sub classes and weren't _sitebuiltins._Printer (avoid copyright, credits, etc). My list matches the documentation except for also including __build_class__ and __loader__ – rafraf Feb 08 '22 at 11:43
-
You wrote, "there can be some confusion about `__builtins__` or `__builtin__`. As of python 3.10, `__builtin__` is not defined. We are allowed to type `__builtins__`. I wish that people could type in any subsequence of a name from the current scope. If two or more names have the same subsequence (e.g. `AError` is a subsequence of `ArithmeticError` and `AssertionError`). In a future version of python we could raise an exception with the message `NameError: name 'AError' is ambigous. It could be:\n ArithmeticError\n AssertionError\`. `__builtin__` is a subsequence of `__builtins__`. – Toothpick Anemone Mar 24 '23 at 16:55
>>> for e in __builtins__.__dict__:
... print(e)
...
__name__
__doc__
__package__
__loader__
__spec__
__build_class__
__import__
abs
all
any
ascii
bin
callable
chr
compile
delattr
dir
divmod
eval
exec
format
getattr
globals
hasattr
hash
hex
id
input
isinstance
issubclass
iter
len
locals
max
min
next
oct
ord
pow
print
repr
round
setattr
sorted
sum
vars
None
Ellipsis
NotImplemented
False
True
bool
memoryview
bytearray
bytes
classmethod
complex
dict
enumerate
filter
float
frozenset
property
int
list
map
object
range
reversed
set
slice
staticmethod
str
super
tuple
type
zip
__debug__
BaseException
Exception
TypeError
StopAsyncIteration
StopIteration
GeneratorExit
SystemExit
KeyboardInterrupt
ImportError
ModuleNotFoundError
OSError
EnvironmentError
IOError
WindowsError
EOFError
RuntimeError
RecursionError
NotImplementedError
NameError
UnboundLocalError
AttributeError
SyntaxError
IndentationError
TabError
LookupError
IndexError
KeyError
ValueError
UnicodeError
UnicodeEncodeError
UnicodeDecodeError
UnicodeTranslateError
AssertionError
ArithmeticError
FloatingPointError
OverflowError
ZeroDivisionError
SystemError
ReferenceError
BufferError
MemoryError
Warning
UserWarning
DeprecationWarning
PendingDeprecationWarning
SyntaxWarning
RuntimeWarning
FutureWarning
ImportWarning
UnicodeWarning
BytesWarning
ResourceWarning
ConnectionError
BlockingIOError
BrokenPipeError
ChildProcessError
ConnectionAbortedError
ConnectionRefusedError
ConnectionResetError
FileExistsError
FileNotFoundError
IsADirectoryError
NotADirectoryError
InterruptedError
PermissionError
ProcessLookupError
TimeoutError
open
quit
exit
copyright
credits
license
help

- 2,398
- 1
- 10
- 19