Next / Previous / Contents / TCC Help System / NM Tech homepage

14.5. dir(): Display a namespace's names

The purpose of the dir() function is to find out what names are defined in a given namespace, and return a list of those names. If called without arguments, it returns a list of the names defined in the local namespace. This function can be very handy for finding out what items are in a module or class.

Certain special names are found in most or all namespaces:

In this example sequence, we'll show you what is in the global namespace just after starting up Python. Then we'll import the math module and display its names.

>>> dir()
['__builtins__', '__doc__', '__name__']
>>> x=5; forkTail='Tyrannus'
>>> dir()
['__builtins__', '__doc__', '__name__', 'forkTail', 'x']
>>> print __doc__
None
>>> print __name__
__main__
>>> import math
>>> print math.__name__
math
>>> dir(math)
['__doc__', '__file__', '__name__', 'acos', 'asin', 'atan', 'atan2', 'ceil'
, 'cos', 'cosh', 'degrees', 'e', 'exp', 'fabs', 'floor', 'fmod', 'frexp', '
hypot', 'ldexp', 'log', 'log10', 'modf', 'pi', 'pow', 'radians', 'sin', 'si
nh', 'sqrt', 'tan', 'tanh']
>>> print math.__doc__
This module is always available.  It provides access to the
mathematical functions defined by the C standard.
>>> print math.log10.__doc__
log10(x) -> the base 10 logarithm of x.
>>> print __builtins__
<module '__builtin__' (built-in)>
>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'D
eprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception'
, 'False', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError
', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'KeyEr
ror', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError', 'None
', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'Pe
ndingDeprecationWarning', 'ReferenceError', 'RuntimeError', 'RuntimeWarning
', 'StandardError', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'Syste
mError', 'SystemExit', 'TabError', 'True', 'TypeError', 'UnboundLocalError'
, 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTrans
lateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'Zero
DivisionError', '_', '__debug__', '__doc__', '__import__', '__name__', 'abs
', 'all', 'any', 'apply', 'basestring', 'bool', 'buffer', 'callable', 'chr'
, 'classmethod', 'cmp', 'coerce', 'compile', 'complex', 'copyright', 'credi
ts', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'execfile', '
exit', 'file', 'filter', 'float', 'frozenset', 'getattr', 'globals', 'hasat
tr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'intern', 'isinstance', '
issubclass', 'iter', 'len', 'license', 'list', 'locals', 'long', 'map', 'ma
x', 'min', 'object', 'oct', 'open', 'ord', 'pow', 'property', 'quit', 'rang
e', 'raw_input', 'reduce', 'reload', 'repr', 'reversed', 'round', 'set', 's
etattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple',
 'type', 'unichr', 'unicode', 'vars', 'xrange', 'zip']