How can I retrieve names of all builtins
for my current python
distribution during runtime?
views:
37answers:
1
+1
A:
I am not sure if this suffices, but you can fire up the interpreter and do the following
>>> 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']
And you can see the dir value
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'atexit']
And from the modules, you can import builtin as module
pyfunc
2010-10-08 18:34:28
However, if I call it inside a module, I don't get what I want. Why it is different? I actually I need to call it inside a module, but ther e I don't get all those names, but rather ['__class__', '__contains__', '__delattr__', '__delitem__', '__doc__', '__eq__', , '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__','__sizeof__','__str__', '__subclasshook__','clear','copy','fromkeys','get', 'items','keys','pop','popitem','setdefault','update','values']
gruszczy
2010-10-08 18:40:36
Ok, it seems, I can `import builtins` in python 3k and simply run `dir` on it. Thanks a lot :-)
gruszczy
2010-10-08 18:47:08
@gruszczy : Correct, The dir provides the namespace and in your case it is providing the namespace from where you are calling.
pyfunc
2010-10-08 18:49:48