在这里,我基本上将内置函数定义为默认命名空间的成员,具有可用性和与模块中使用的函数风格特征一致的特点,即:它们提供了一些有用的功能并以字母表中的小写字母开头。
我所做的好处是展示了列表推导式的过滤部分,但它似乎有点不太正当的哈克,应该有更直接的方法来实现这一点。到目前为止,以下是我所做的:
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', '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']
我相信它们都是可调用的,就像这样:
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内置函数呢?到目前为止,我的谷歌和stackoverflow搜索都失败了。
我正在寻找一种可演示且可重复的实验指导方法。谢谢!