Dir () function
You can use the built-in Dir function to list the identifier for a module definition. Identifiers have functions, classes, and variables.
When you provide a module name for Dir (), it returns a list of the name of the module definition. If no argument is supplied, it returns a list of names defined in the current module.
Using the Dir function
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Example 8.4 using the Dir function
[[email protected] python]# python
Python 2.6.6 (r266:84292, Sep 4 2013, 07:46:00)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "Help", "copyright", "credits" or "license" for more information.
>>> Import Sys
>>> dir (SYS)
[' __displayhook__ ', ' __doc__ ', ' __excepthook__ ', ' __name__ ', ' __package__ ', ' __stderr __ ', ' __stdin__ ', ' __stdout__ ', ' _clear_type_cache ', ' _current_frames ',, ' _getframe ', ' api_version ', ' argv ', ' builtin_module_names ', ' byteorder ', ' call_tracing ', ' Callstats ', ' copyright ', ' displayhook ', ' dont_write_bytecode ', ' exc_clear ', ' exc_info ', ' exc_type ', ' excepthook ', ' exec_prefix ' ', ' executable ', ' exit ', ' flags ', ' Float_info ', ' getcheckinterval ', ' getdefaultencoding ', ' getdlopenflags ', ' Getfilesystemencoding ', ' getprofile ', ' getrecursionlimit ', ' getrefcount ',, ' getsizeof ', ' gettrace ', ' hexversion ', ' maxint ', ' maxsize ', ' maxunicode ', ' Meta_path ', ' Modules ', ' path ', ' path_hooks ', ' path_importer_cache ', ' platform ', ', ' prefix ', ' PS1 ', ' ps2 ', ' Py3kwarniNg ', ' setcheckinterval ', ' setdlopenflags ', ' setprofile ', ' setrecursionlimit ', ' Settrace ', ' stderr ', ' stdin ', ' ' stdout ', ' Subversion ', ' version ', ' Version_info ', ' Warnoptions ']
>>> a=5
>>> dir ()
[' __builtins__ ', ' __doc__ ', ' __name__ ', ' __package__ ', ' a ', ' sys ']
>>> del A
>>> dir ()
[' __builtins__ ', ' __doc__ ', ' __name__ ', ' __package__ ', ' sys ']
>>>
How it works
First, let's take a look at using dir on the input sys module. We see that it contains a large list of attributes.
Next, we don't use it to pass parameters to the Dir function--by default, it returns the list of properties for the current module. Note that the input module is also part of the list.
To observe the function of Dir, we define a new variable A and assign it a value, and then we examine dir, and we observe that the same value is added to the list. We use the DEL statement to delete the variable/attribute in the current module, which is once again reflected in the output of Dir.
A note about Del--this statement is used to delete a variable/name after it is run. In this example, Del A, you will no longer be able to use the variable a--it as if it had never existed.
Dir function of the module