Datasheet

Chapter 1: A Python Primer
14
Exploring sys.path
If you ever want to see your system s Python search path, all you have to do is bring up the interactive
interpreter, import the
sys module, and type sys.path . The full Python module search path will be
returned, as shown in the following example:
> > > import sys
> > > sys.path
[‘C:\\Python25’, ‘C:\\Python25\\Lib\\idlelib’, ‘C:\\Program Files\\PythonNet’,
‘c:\\scripts\\python’, ‘c:\\python25’, ‘C:\\Python25\\pyunit-1.4.1’,
‘c:\\python25\\pamie’, ‘C:\\WINDOWS\\system32\\python25.zip’, ‘C:\\Python25\\DLLs’,
‘C:\\Python25\\lib’, ‘C:\\Python25\\lib\\plat-win’, ‘C:\\Python25\\lib\\lib-tk’,
‘C:\\Python25\\lib\\site-packages’, ‘C:\\Python25\\lib\\site-packages\\win32’,
‘C:\\Python25\\lib\\site-packages\\win32\\lib’, ‘C:\\Python25\\lib\\site-
packages\\
Pythonwin’, ‘C:\\Python25\\lib\\site-packages\\wx-2.8-msw-ansi’]
> > >
Classes
Python is a language that can support both procedural programming and object - oriented programming.
Here is an example of a Python class:
> > > class name1():
def setmyname(self, myname):
self.name = myname
> > > jimname = name1()
> > > jimname.setmyname(“Jim”)
> > > print jimname.name
Jim
> > >
Note some points about Python s implementation of class programming as demonstrated in the
preceding example:
If we were inheriting from other classes, those class names would have been inside the
parentheses of the
class name1(): definition.
In this case, there is one class method,
setmyname . If we wanted to create a constructor for the
class, it would be named
__init__ .
To create an instance of a class, you simply assign a variable to the class definition, as in
jimname = name1() .
Attributes are accessed with familiar dot notation ( instance
variable.attribute ) such as
jimname.name .
c01.indd 14c01.indd 14 6/2/08 12:03:12 PM6/2/08 12:03:12 PM