Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

8.3 The dir() Function

The built-in function dir() is used to find out which names a module defines. It returns a sorted list of strings:

    >>> import fibo, sys
    >>> dir(fibo)
    ['__name__', 'fib', 'fib2']
    >>> dir(sys)
    ['__displayhook__', '__doc__', '__excepthook__',
     '__name__', '__stderr__', '__stdin__', '__stdout__',
     '_getframe', 'argv', 'builtin_module_names',
     'byteorder', 'copyright', 'displayhook', 'exc_info',
     'exc_type', 'excepthook', 'exec_prefix', 'executable',
     'exit', 'getdefaultencoding', 'getdlopenflags',
     'getrecursionlimit', 'getrefcount', 'hexversion',
     'maxint', 'maxunicode', 'modules', 'path', 'platform',
     'prefix', 'ps1', 'ps2', 'setcheckinterval',
     'setdlopenflags', 'setprofile', 'setrecursionlimit',
     'settrace', 'stderr', 'stdin', 'stdout', 'version',
     'version_info', 'warnoptions']

Without arguments, dir() lists the names you have defined currently:

    >>> a = [1, 2, 3, 4, 5]
    >>> import fibo, sys
    >>> fib = fibo.fib
    >>> dir()
    ['__name__', 'a', 'fib', 'fibo', 'sys']

Note that it lists all types of names: variables, modules, functions, etc.

dir() does not list the names of built-in functions and variables. If you want a list of those, they are defined in the standard module ‘__builtin__’:

    >>> import __builtin__
    >>> dir(__builtin__)
    ['ArithmeticError', 'AssertionError', 'AttributeError',
     'DeprecationWarning', 'EOFError', 'Ellipsis',
     'EnvironmentError', 'Exception', 'FloatingPointError',
     'IOError', 'ImportError', 'IndentationError',
     'IndexError', 'KeyError', 'KeyboardInterrupt',
     'LookupError', 'MemoryError', 'NameError', 'None',
     'NotImplemented', 'NotImplementedError', 'OSError',
     'OverflowError', 'OverflowWarning', 'ReferenceError',
     'RuntimeError', 'RuntimeWarning', 'StandardError',
     'StopIteration', 'SyntaxError', 'SyntaxWarning',
     'SystemError', 'SystemExit', 'TabError', 'TypeError',
     'UnboundLocalError', 'UnicodeError', 'UserWarning',
     'ValueError', 'Warning', 'ZeroDivisionError', '_',
     '__debug__', '__doc__', '__import__', '__name__',
     'abs', 'apply', 'buffer', 'callable', 'chr',
     'classmethod', 'cmp', 'coerce', 'compile', 'complex',
     'copyright', 'credits', 'delattr', 'dict', 'dir',
     'divmod', 'eval', 'execfile', 'exit', 'file', 'filter',
     'float', 'getattr', 'globals', 'hasattr', 'hash',
     'help', 'hex', 'id', 'input', 'int', 'intern',
     'isinstance', 'issubclass', 'iter', 'len', 'license',
     'list', 'locals', 'long', 'map', 'max', 'min',
     'object', 'oct', 'open', 'ord', 'pow', 'property',
     'quit', 'range', 'raw_input', 'reduce', 'reload',
     'repr', 'round', 'setattr', 'slice', 'staticmethod',
     'str', 'super', 'tuple', 'type', 'unichr', 'unicode',
     'vars', 'xrange', 'zip']

 
 
  Published under the terms of the Python License Design by Interspire