You can use the built-in dir function to list the identifiers that a module defines. The identifiers are the functions, classes and variables defined. When you supply a module name to the dir() function, it returns the list of the names defined in that module. When no argument is supplied to it, it returns the list of names defined in the current module.
Example 8.4. Using the dir function
$ python Python 2.2.2 (#1, Feb 24 2003, 19:13:11) [GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-4)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> 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'] >>> dir() ['__builtins__', '__doc__', '__name__', 'sys'] >>> a = 5 >>> dir() ['__builtins__', '__doc__', '__name__', 'a', 'sys'] >>> del a >>> dir() ['__builtins__', '__doc__', '__name__', 'sys']
Notice how the assigning of a variable automatically adds that identifier name to the list returned by the dir() function. When we delete the variable i.e. undeclare the variable, then it is automatically removed from the list returned by the dir() function. We delete a variable using the del statement. After the statement del a, you can no longer access the variable a (unless you define it again, of course) - it was as if it never existed in the current program.