Pages

Wednesday, May 5, 2010

The dir() function


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 in that module.
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 applied to it, it returns the list of names defined in the current module.
Using the dir function
Example 8.4. Using the dir function
$ python
>>> import sys
>>> dir(sys) # get list of attributes for sys module
['__displayhook__', '__doc__', '__excepthook__', '__name__', '__stderr__',
'__stdin__', '__stdout__', '_getframe', 'api_version', 'argv',
'builtin_module_names', 'byteorder', 'call_tracing', 'callstats',
'copyright', 'displayhook', 'exc_clear', 'exc_info', 'exc_type',
'excepthook', 'exec_prefix', 'executable', 'exit', 'getcheckinterval',
'getdefaultencoding', 'getdlopenflags', 'getfilesystemencoding',
'getrecursionlimit', 'getrefcount', 'hexversion', 'maxint', 'maxunicode',
'meta_path','modules', 'path', 'path_hooks', 'path_importer_cache',
'platform', 'prefix', 'ps1', 'ps2', 'setcheckinterval', 'setdlopenflags',
'setprofile', 'setrecursionlimit', 'settrace', 'stderr', 'stdin', 'stdout',
'version', 'version_info', 'warnoptions']
>>> dir() # get list of attributes for current module
['__builtins__', '__doc__', '__name__', 'sys']
>>>
>>> a = 5 # create a new variable 'a'
>>> dir()
['__builtins__', '__doc__', '__name__', 'a', 'sys']
>>>
>>> del a # delete/remove a name
>>>
>>> dir()
['__builtins__', '__doc__', '__name__', 'sys']
>>>
How It Works
First, we see the usage of dir on the imported sys module. We can see the huge list of attributes that itcontains.
Next, we use the dir function without passing parameters to it - by default, it returns the list of attributes
for the current module. Notice that the list of imported modules is also part of this list.
In order to observe the dir in action, we define a new variable a and assign it a value and then check
dir and we observe that there is an additional value in the list of the same name. We remove the variable/
attribute of the current module using the del statement and the change is reflected again in the output
of the dir function.
A note on del - this statement is used to delete a variable/name and after the statement has run, in this
case del a, you can no longer access the variable a - it is as if it never existed before at all.

from..import


Here is a version utilising the from..import syntax.
#!/usr/bin/python
# Filename: mymodule_demo2.py
from mymodule import sayhi, version
# Alternative:

# from mymodule import *
sayhi()
print 'Version', version
The output of mymodule_demo2.py is same as the output of mymodule_demo.py.

Making your own Modules


Creating your own modules is easy, you've been doing it all along! Every Python program is also a module.
You just have to make sure it has a .py extension. The following example should make it clear.
Creating your own Modules
Example 8.3. How to create your own module

#!/usr/bin/python
# Filename: mymodule.py
def sayhi():
print 'Hi, this is mymodule speaking.'
version = '0.1'
# End of mymodule.py
The above was a sample module. As you can see, there is nothing particularly special about compared to
our usual Python program. We will next see how to use this module in our other Python programs.
Remember that the module should be placed in the same directory as the program that we import it in, or
the module should be in one of the directories listed in sys.path .
#!/usr/bin/python
# Filename: mymodule_demo.py
import mymodule
mymodule.sayhi()
print 'Version', mymodule.version
Output
$ python mymodule_demo.py
Hi, this is mymodule speaking.
Version 0.1
How It Works
Notice that we use the same dotted notation to access members of the module. Python makes good reuse
of the same notation to give the distinctive 'Pythonic' feel to it so that we don't have to keep learning
new ways to do things.

A module's __name__

Every module has a name and statements in a module can find out the name of its module. This is especially handy in one particular situation - As mentioned previously, when a module is imported for the
first time, the main block in that module is run. What if we want to run the block only if the program
was used by itself and not when it was imported from another module? This can be achieved using the
__name__ attribute of the module.
Using a module's __name__
Example 8.2. Using a module's __name__
#!/usr/bin/python
# Filename: using_name.py
if __name__ == '__main__':
print 'This program is being run by itself'
else:
print 'I am being imported from another module'
Output
$ python using_name.py
This program is being run by itself
$ python
>>> import using_name
I am being imported from another module
>>>
How It Works
Every Python module has it's __name__ defined and if this is '__main__', it implies that the module
is being run standalone by the user and we can do corresponding appropriate actions.

The from..import statement


If you want to directly import the argv variable into your program (to avoid typing the sys. everytime
for it), then you can use the from sys import argv statement. If you want to import all the names
used in the sys module, then you can use the from sys import * statement. This works for any
module. In general, avoid using the from..import statement and use the import statement instead
since your program will be much more readable and will avoid any name clashes that way.

Byte-compiled .pyc files


Importing a module is a relatively costly affair, so Python does some tricks to make it faster. One way is
to create byte-compiled files with the extension .pyc which is related to the intermediate form that Python
transforms the program into (remember the intro section on how Python works ?). This .pyc file
is useful when you import the module the next time from a different program - it will be much faster
since part of the processing required in importing a module is already done. Also, these byte-compiled
files are platform-independent. So, now you know what those .pyc files really are.

Using the sys module


sys
print 'The command line arguments are:'
for i in sys.argv:
print i
print '\n\nThe PYTHONPATH is', sys.path, '\n'
Output
$ python using_sys.py we are arguments
The command line arguments are:
using_sys.py
we
are
arguments
The PYTHONPATH is ['/home/swaroop/byte/code', '/usr/lib/python23.zip',
'/usr/lib/python2.3', '/usr/lib/python2.3/plat-linux2',
'/usr/lib/python2.3/lib-tk', '/usr/lib/python2.3/lib-dynload',
'/usr/lib/python2.3/site-packages', '/usr/lib/python2.3/site-packages/gtk-2.0']
How It Works

First, we import the sys module using the import statement. Basically, this translates to us telling Python
that we want to use this module. The sys module contains functionality related to the Python interpreter
and its environment.
When Python executes the import sys statement, it looks for the sys.py module in one of the directores
listed in its sys.path variable. If the file is found, then the statements in the main block of that
module is run and then the module is made available for you to use. Note that the initialization is done
only the first time that we import a module. Also, 'sys' is short for 'system'.
The argv variable in the sys module is referred to using the dotted notation - sys.argv - one of the
advantages of this approach is that the name does not clash with any argv variable used in your program.
Also, it indicates clearly that this name is part of the sys module.
The sys.argv variable is a list of strings (lists are explained in detail in later sections). Specifically,
the sys.argv contains the list of command line arguments i.e. the arguments passed to your program
using the command line.
If you are using an IDE to write and run these programs, look for a way to specify command line arguments
to the program in the menus.
Here, when we execute python using_sys.py we are arguments, we run the module using_
sys.py with the python command and the other things that follow are arguments passed to the
program. Python stores it in the sys.argv variable for us.
Remember, the name of the script running is always the first argument in the sys.argv list. So, in this
case we will have 'using_sys.py' as sys.argv[0], 'we' as sys.argv[1], 'are' as
sys.argv[2] and 'arguments' as sys.argv[3] . Notice that Python starts counting from 0
and not 1.
The sys.path contains the list of directory names where modules are imported from. Observe that the
first string in sys.path is empty - this empty string indicates that the current directory is also part of
the sys.path which is same as the PYTHONPATH environment variable. This means that you can directly
import modules located in the current directory. Otherwise, you will have to place your module in
one of the directories listed in sys.path .