2024-04-25 17:23:26 发布
网友
当我们从Pythom提示符中获得关键字列表时,如何从Pythom提示符中获取所有内置函数的列表?在
>>> 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
您可以通过以下方式获取所有内置名称:
>>> dir(__builtin__)
这包括__builtin__中的所有内容。 如果只需要函数名,只需筛选它们:
__builtin__
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']
如果需要functions对象,只需稍微更改一下代码:
builtin_functions = [obj for name, obj in vars(__builtin__).items() if isinstance(obj, types.BuiltinFunctionType)]
您可以通过以下方式获取所有内置名称:
这包括
^{pr2}$__builtin__
中的所有内容。 如果只需要函数名,只需筛选它们:Python 3.6中的结果列表:
如果需要functions对象,只需稍微更改一下代码:
相关问题 更多 >
编程相关推荐