如何获取Python中所有内置函数的列表

时间:2018-05-01 06:42:24

标签: python python-3.x function python-3.6 built-in

如何从Pythom提示中获取Python中所有内置函数的列表,因为我们从中获取关键字列表?

2 个答案:

答案 0 :(得分:3)

您可以使用以下内容获取所有内置名称:

>>> dir(__builtin__)

这包括__builtin__的所有内容。 如果您只想要函数名称,只需过滤它们:

import types

builtin_function_names = [name for name, obj in vars(__builtin__).items() 
                          if isinstance(obj, types.BuiltinFunctionType)]

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']

如果你想要函数对象,只需稍微更改你的代码:

builtin_functions = [obj for name, obj in vars(__builtin__).items() 
                     if isinstance(obj, types.BuiltinFunctionType)]

答案 1 :(得分:2)

>>> 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