如何获得Python中所有内置函数的列表? [英] How do I get a list of all the built-in functions in Python?
问题描述
我正在尝试汇总一个如何获取Python中所有内置函数列表的规范示例.该文档很好,但是我想用一种可证明的方法来演示它.
I'm trying to put together a canonical example of how to get a list of all the builtin functions in Python. The documentation is good, but I want to demonstrate it with a provable approach.
在这里,我实质上是将内置函数定义为默认名称空间的成员,这些成员可用且与打算在模块中使用的函数的样式特征一致,即:它们提供了一些有用的信息功能,并以小写字母开头.
Here, I'm essentially defining the built-in functions as the members of the default namespace that are usable and consistent with the stylistic characteristics of a function that is intended for use in a module, that is: they provide some useful functionality and begin with a lowercase letter of the alphabet.
我在这里所做的好处是,我正在演示列表推导的过滤器部分,但这似乎有点肮脏,并且应该有一种更直接的方法.到目前为止,这是我正在做的事情:
The upside of what I'm doing here is that I'm demonstrating the filter part of list comprehensions, but it seems a bit of a dirty hack and like there should be a more straight-forward way of doing this. Here's what I'm doing so far:
import string
alc = string.ascii_lowercase
bif = [i for i in dir(__builtins__) if
any(i.startswith(j) for j in alc)]
哪个给我:
['abs','all','any','apply','basestring','bin','bool','buffer','bytearray','bytes','callable','chr','classmethod','cmp',强制",编译",复杂",版权",信用","delattr",'dict','dir','divmod','enumerate','eval','execfile','exit','文件','过滤器','浮动','格式','冻结集合','getattr','globals','hasattr','hash','help','hex','id','input','int','intern','isinstance','issubclass','iter','len','license',列表",本地人",长",地图",最大",内存视图",最小",下一个",对象",八度",打开","ord",战利品",打印",属性",退出",'范围','原始输入','减少','重新加载','重复','反转','回合','set','setattr','slice','sorted','staticmethod,'str','sum','super','tuple','type','unichr','unicode','vars','xrange','zip']
['abs', 'all', 'any', 'apply', 'basestring', 'bin', 'bool', 'buffer', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'cmp', 'coerce', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'execfile', 'exit', 'file', 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'intern', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'long', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit', 'range', 'raw_input', 'reduce', 'reload', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'unichr', 'unicode', 'vars', 'xrange', 'zip']
我相信它们都是可以调用的,就像这样:
I believe that they are all callable, as with this:
bi2 = [i for i in dir(__builtins__) if
any(i.startswith(j) for j in alc)
and callable(getattr(__builtins__, i, None))]
set(bif).symmetric_difference(bi2)
我得到:
set([])
因此,有更好的方法来列出内置的Python函数吗?到目前为止,Google和stackoverflow搜索都使我失败了.
So is there a better way to list the builtin Python functions? Google and stackoverflow searches have failed me so far.
我正在寻找一种可证明且可重复的实验教学方法.谢谢!
I am looking for a demonstrable and repeatable method for experimental instruction. Thanks!
推荐答案
import __builtin__
import inspect
[name for name, function in sorted(vars(__builtin__).items())
if inspect.isbuiltin(function) or inspect.isfunction(function)]
文档中也有该列表.
这篇关于如何获得Python中所有内置函数的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!