2010-10-08 55 views
3

如何在运行时检索当前python分配的所有builtins的名称?python:检索所有内置函数的名称

+0

你指的是哪个buildins?一个对象的方法和属性,标准库中的模块?标准功能? – synthesizerpatel 2010-10-08 18:32:58

+0

我的意思是:http://docs.python.org/dev/py3k/library/functions.html,但现在我意识到,我也需要内建类型和例外。 – gruszczy 2010-10-08 18:37:02

+0

由于它在文档中,您还需要了解哪些内容? – 2010-10-08 22:49:00

回答

4

我不知道这是否就足够了,但你可以火​​起来的解释并执行以下操作

>>> dir(__builtins__) 
['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BufferError', 'BytesWarning', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError', 'None', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'ReferenceError', 'RuntimeError', 'RuntimeWarning', 'StandardError', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'ZeroDivisionError', '_', '__debug__', '__doc__', '__import__', '__name__', '__package__', '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', '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'] 

而且你可以看到DIR值

>>> dir() 
['__builtins__', '__doc__', '__name__', '__package__', 'atexit'] 

而且从模块,你可以导入内置模块作为

+1

但是,如果我在模块内调用它,我不会得到我想要的。为什么它不同?实际上,我需要在模块中调用它,但是我没有得到所有这些名称,而是['__class__','__contains__','__delattr__','__delitem__','__doc__','__eq__', ,'__hash__','__init__','__iter__','__le__','__len__','__lt__','__ne__','__new__','__reduce__','__reduce_ex__','__repr__','__setattr__',' __'__ str__','__subclasshook __','clear','copy','fromkeys','get','items','keys','pop','popitem','setdefault' ,'update','values'] – gruszczy 2010-10-08 18:40:36

+0

好吧,看来,我可以'在python 3k中导入builtins',并简单地在其上运行'dir'。非常感谢:-) – gruszczy 2010-10-08 18:47:08

+0

@gruszczy:正确,dir提供了命名空间,在你的情况下它提供了你调用的命名空间。 – pyfunc 2010-10-08 18:49:48