Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

A Quick, Painless Tutorial On The Python Language

Download as pdf or txt
Download as pdf or txt
You are on page 1of 54

A Quick, Painless Tutorial on the Python Language

Norman Matloff University of California, Davis c 2003-2010, N. Matloff April 8, 2010

Contents
1 Overview 1.1 1.2 2 What Are Scripting Languages? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Why Python? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 5 5 5 5 6 6 6 6 7 8 9

How to Use This Tutorial 2.1 2.2 2.3 Background Needed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . What Parts to Read, When . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

A 5-Minute Introductory Example 3.1 3.2 3.3 3.4 3.5 Example Program Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Python Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Python Block Denition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Python Also Offers an Interactive Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Python As a Calculator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 11

A 10-Minute Introductory Example 4.1 4.2 4.3

Example Program Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Command-Line Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Introduction to File Manipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 13

Declaration (or Not), Scope, Functions, Etc.

5.1 5.2 6 7

Lack of Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Locals Vs. Globals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 14 14

A Couple of Built-In Functions Types of Variables/Values 7.1 7.2

String Versus Numerical Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 7.2.1 7.2.2 7.2.3 7.2.4 Lists (Quasi-Arrays) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Sorting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

7.3 7.4 8 9

Dictionaries (Hashes) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Function Denition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 21 21 23

Keyboard Input Use of name

10 Object-Oriented Programming

10.1 Example Program Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 10.2 The Keyword self . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 10.3 Instance Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 10.4 Class Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 10.5 Constructors and Destructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 10.6 Instance Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 10.7 Class Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 10.8 Derived Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 10.9 A Word on Class Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 11 Importance of Understanding Object References 12 Object Deletion 13 Object Comparison 27 28 28

14 Modules and Packages

29

14.1 Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 14.1.1 Example Program Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 14.1.2 How import Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 14.1.3 Compiled Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 14.1.4 Miscellaneous . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 14.1.5 A Note on Global Variables Within Modules . . . . . . . . . . . . . . . . . . . . . 31 14.2 Data Hiding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 14.3 Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 15 Exception Handling (Not Just for Exceptions!) 16 Docstrings 17 Miscellaneous 34 34 35

17.1 Running Python Scripts Without Explicitly Invoking the Interpreter . . . . . . . . . . . . . 35 17.2 Named Arguments in Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 17.3 Printing Without a Newline or Blanks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 17.4 Formatted String Manipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 18 Example of Data Structures in Python 37

18.1 Making Use of Python Idioms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 19 Functional Programming Features 39

19.1 Lambda Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 19.2 Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 19.3 Filtering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 19.4 List Comprehension . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 19.5 Reduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 A Debugging 42

A.1 Pythons Built-In Debugger, PDB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 A.1.1 The Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 A.1.2 Using PDB Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45

A.1.3 Using dict

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46

A.1.4 The type() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 A.2 Using PDB with Emacs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 A.3 Debugging with DDD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 A.3.1 Youll Need a Special PYDB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 A.3.2 DDD Launch and Program Loading . . . . . . . . . . . . . . . . . . . . . . . . . . 49 A.3.3 Breakpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 A.3.4 Running Your Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 A.3.5 Inspecting Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 A.3.6 Miscellaneous . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 A.4 Debugging with Winpdb . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 A.5 Debugging with Eclipse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 A.6 Some Python Internal Debugging Aids . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 A.6.1 The dict Attribute . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 A.6.2 The id() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 B Online Documentation 51

B.1 The dir() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 B.2 The help() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 B.3 PyDoc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 C Putting All Globals into a Class D Looking at the Python Virtual Machine 53 54

1
1.1

Overview
What Are Scripting Languages?

Languages like C and C++ allow a programmer to write code at a very detailed level which has good execution speed (especially in the case of C). But in most applications, execution speed is not important, and in many cases one would prefer to write at a higher level. For example, for text-manipulation applications, the basic unit in C/C++ is a character, while for languages like Perl and Python the basic units are lines of text and words within lines. One can work with lines and words in C/C++, but one must go to greater effort to accomplish the same thing. The term scripting language has never been formally dened, but here are the typical characteristics: Used often for system administration, Web programming, text processing, etc. Very casual with regard to typing of variables, e.g. little or no distinction between integer, oatingpoint or string variables. Arrays can mix elements of different types, such as integers and strings. Functions can return nonscalars, e.g. arrays. Nonscalars can be used as loop indexes. Etc. Lots of high-level operations intrinsic to the language, e.g. string concatenation and stack push/pop. Interpreted, rather than being compiled to the instruction set of the host machine.

1.2

Why Python?

The rst really popular scripting language was Perl. It is still in wide usage today, but the languages with momentum are Python and the Python-like Ruby. Many people, including me, greatly prefer Python to Perl, as it is much cleaner and more elegant. Python is very popular among the developers at Google. Advocates of Python, often called pythonistas, say that Python is so clear and so enjoyable to write in that one should use Python for all of ones programming work, not just for scripting work. They believe it is superior to C or C++.1 Personally, I believe that C++ is bloated and its pieces dont t together well; Java is nicer, but its strongly-typed nature is in my view a nuisance and an obstacle to clear programming. I was pleased to see that Eric Raymond, the prominent promoter of the open source movement, has also expressed the same views as mine regarding C++, Java and Python.

2
2.1

How to Use This Tutorial


Background Needed

Anyone with even a bit of programming experience should nd the material through Section 8 to be quite accessible. The material beginning with Section 10 will feel quite comfortable to anyone with background in an objectoriented programming (OOP) language such as C++ or Java. If you lack this background, you will still be
1

Again, an exception would be programs which really need fast execution speed.

able to read these sections, but will probably need to go through them more slowly than those who do know OOP; just focus on the examples, not the terminology. There will be a couple of places in which we describe things briey in a Unix context, so some Unix knowledge would be helpful, but it certainly is not required. Python is used on Windows and Macintosh platforms too, not just Unix.

2.2

Approach

Our approach here is different from that of most Python books, or even most Python Web tutorials. The usual approach is to painfully go over all details from the beginning. For example, the usual approach would be to state all possible forms that a Python integer can take on, and for that matter how many different command-line options one can launch Python with. I avoid this here. Again, the aim is to enable the reader to quickly acquire a Python foundation. He/she should then be able to delve directly into some special topic if and when the need arises.

2.3

What Parts to Read, When

I would suggest that you rst read through Section 8, and then give Python a bit of a try yourself. First experiment a bit in Pythons interactive mode (Section 3.4). Then try writing a few short programs yourself. These can be entirely new programs, or merely modications of the example programs presented below.2 This will give you a much more concrete feel of the language. If your main use of Python will be to write short scripts and you wont be using the Python library, this will probably be enough for you. However, most readers will need to go further, acquiring a basic knowledge of Pythons OOP features and Python modules/packages. So you should next read through Section 17. That would be a very solid foundation for you from which to make good use of Python. Eventually, you may start to notice that many Python programmers make use of Pythons functional programming features, and you may wish to understand what the others are doing or maybe use these features yourself. If so, Section 19 will get you started. Dont forget the appendices! The key ones are Sections A and B. I also have a number of tutorials on Python special programming, e.g. network programming, iterators/generators, etc. See http://heather.cs.ucdavis.edu/matloff/python.html.

3
3.1

A 5-Minute Introductory Example


Example Program Code

Here is a simple, quick example. Suppose I wish to nd the value of


2 The raw source le for this tutorial is downloadable at http://heather.cs.ucdavis.edu/matloff/Python/ PythonIntro.tex, so you dont have to type the programs yourself. You can either edit a copy of this le, saving only the lines of the program example you want, or use your mouse to do a copy-and-paste operation for the relevant lines. But if you do type these examples yourself, make sure to type exactly what appears here, especially the indenting. The latter is crucial, as will be discussed later.

g (x) =

x 1 x2

for x = 0.0, 0.1, ..., 0.9. I could nd these numbers by placing the following code,
for i in range(10): x = 0.1*i print x print x/(1-x*x)

in a le, say fme.py, and then running the program by typing


python fme.py

at the command-line prompt. The output will look like this:


0.0 0.0 0.1 0.10101010101 0.2 0.208333333333 0.3 0.32967032967 0.4 0.47619047619 0.5 0.666666666667 0.6 0.9375 0.7 1.37254901961 0.8 2.22222222222 0.9 4.73684210526

3.2

Python Lists

How does the program work? First, Pythons range() function is an example of the use of lists, i.e. Python arrays,3 even though not quite explicitly. Lists are absolutely fundamental to Python, so watch out in what follows for instances of the word list; resist the temptation to treat it as the English word list, instead always thinking about the Python construct list. Pythons range() function returns a list of consecutive integers, in this case the list [0,1,2,3,4,5,6,7,8,9]. Note that this is ofcial Python notation for listsa sequence of objects (these could be all kinds of things, not necessarily numbers), separated by commas and enclosed by brackets. So, the for statement above is equivalent to:
I loosely speak of them as arrays here, but as you will see, they are more exible than arrays in C/C++. On the other hand, true arrays can be accessed more quickly. In C/C++, the ith element of an array X is i words past the beginning of the array, so we can go right to it. This is not possible with Python lists, so the latter are slower to access. The NumPy add-on package for Python offers true arrays.
3

for i in [0,1,2,3,4,5,6,7,8,9]:

As you can guess, this will result in 10 iterations of the loop, with i rst being 0, then 1, etc. The code
for i in [2,3,6]:

would give us three iterations, with i taking on the values 2, 3 and 6. Python has a while construct too (though not an until). There is also a break statement like that of C/C++, used to leave loops prematurely. For example:
x = 5 while 1: x += 1 if x == 8: print x break

Also, if you want to just do nothing, there is pass, e.g.


class x(y): pass

just to make sure the class is nonempty.

3.3

Python Block Denition

Now focus your attention on that inoccuous-looking colon at the end of the for line, which denes the start of a block. Unlike languages like C/C++ or even Perl, which use braces to dene blocks, Python uses a combination of a colon and indenting to dene a block. I am using the colon to say to the Python interpreter, Hi, Python interpreter, how are you? I just wanted to let you know, by inserting this colon, that a block begins on the next line. Ive indented that line, and the two lines following it, further right than the current line, in order to tell you those three lines form a block. I chose 3-space indenting, but the amount wouldnt matter as long as I am consistent. If for example I were to write4
for i in range(10): print 0.1*i print g(0.1*i)

the Python interpreter would give me an error message, telling me that I have a syntax error.5 I am only allowed to indent further-right within a given block if I have a sub-block within that block, e.g.
4 5

Here g() is a function I dened earlier, not shown. Keep this in mind. New Python users are often bafed by a syntax error arising in this situation.

for i in range(10): if i%2 == 1: print 0.1*i print g(0.1*i)

Here I am printing out only the cases in which the variable i is an odd number; % is the mod operator as in C/C++.6 Again, note the colon at the end of the if line, and the fact that the two print lines are indented further right than the if line. Note also that, again unlike C/C++/Perl, there are no semicolons at the end of Python source code statements. A new line means a new statement. If you need a very long line, you can use the backslash character for continuation, e.g.
x = y + \ z

3.4

Python Also Offers an Interactive Mode

A really nice feature of Python is its ability to run in interactive mode. You usually wont do this, but its a great way to do a quick tryout of some feature, to really see how it works. Whenever youre not sure whether something works, your motto should be, When in doubt, try it out!, and interactive mode makes this quick and easy. Well also be doing a lot of this in this tutorial, with interactive mode being an easy way to do a quick illustration of a feature. Instead of executing this program from the command line in batch mode as we did above, we could enter and run the code in interactive mode:
% python >>> for i in range(10): ... x = 0.1*i ... print x ... print x/(1-x*x) ... 0.0 0.0 0.1 0.10101010101 0.2 0.208333333333 0.3 0.32967032967 0.4 0.47619047619 0.5 0.666666666667 0.6 0.9375 0.7 1.37254901961
6 Most of the usual C operators are in Python, including the relational ones such as the == seen here. The 0x notation for hex is there, as is the FORTRAN ** for exponentiation. Also, the if construct can be paired with else as usual, and you can abbreviate else if as elif. The boolean operators are and, or and not. Youll see examples as we move along. By the way, watch out for Python statements like print a or b or c, in which the rst true (i.e. nonzero) expression is printed and the others ignored; this is a common Python idiom.

0.8 2.22222222222 0.9 4.73684210526 >>>

Here I started Python, and it gave me its >>> interactive prompt. Then I just started typing in the code, line by line. Whenever I was inside a block, it gave me a special prompt, ..., for that purpose. When I typed a blank line at the end of my code, the Python interpreter realized I was done, and ran the code.7 While in interactive mode, one can go up and down the command history by using the arrow keys, thus saving typing. To exit interactive Python, hit ctrl-d. Automatic printing: By the way, in interactive mode, just referencing or producing an object, or even an expression, without assigning it, will cause its value to print out, even without a print statement. For example:
>>> for i in range(4): ... 3*i ... 0 3 6 9

Again, this is true for general objects, not just expressions, e.g.:
>>> open(x) <open file x, mode r at 0xb7eaf3c8>

Here we opened the le x, which produces a le object. Since we did not assign to a variable, say f, for reference later in the code, i.e. we did not do the more typical
f = open(x)

the object was printed out. Wed get that same information this way:
>>> f = open(x) >>> f <open file x, mode r at 0xb7f2a3c8>

3.5

Python As a Calculator

Among other things, this means you can use Python as a quick calculator (which I do a lot). If for example I needed to know what 5% above $88.88 is, I could type
Interactive mode allows us to execute only single Python statements or evaluate single Python expressions. In our case here, we typed in and executed a single for statement. Interactive mode is not designed for us to type in an entire program. Technically we could work around this by beginning with something like if 1:, making our program one large if statement, but of course it would not be convenient to type in a long program anyway.
7

10

% python >>> 1.05*88.88 93.323999999999998

Among other things, one can do quick conversions between decimal and hex:
>>> 0x12 18 >>> hex(18) 0x12

If I need math functions, I must import the Python math library rst. This is analogous to what we do in C/C++, where we must have a #include line for the library in our source code and must link in the machine code for the library. Then we must refer to the functions in the context of the math library. For example, the functions sqrt() and sin() must be prexed by math:8
>>> import math >>> math.sqrt(88) 9.3808315196468595 >>> math.sin(2.5) 0.59847214410395655

4
4.1

A 10-Minute Introductory Example


Example Program Code

This program reads a text le, specied on the command line, and prints out the number of lines and words in the le:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

# reads in the text file whose name is specified on the command line, # and reports the number of lines and words import sys def checkline(): global l global wordcount w = l.split() wordcount += len(w) wordcount = 0 f = open(sys.argv[1]) flines = f.readlines() linecount = len(flines) for l in flines: checkline() print linecount, wordcount

Say for example the program is in the le tme.py, and we have a text le x with contents

This is an
8

A method for avoiding the prex is shown in Sec. 14.1.2.

11

example of a text file.

(There are ve lines in all, the rst and last of which are blank.) If we run this program on this le, the result is:
python tme.py x 5 8

On the surface, the layout of the code here looks like that of a C/C++ program: First an import statement, analogous to #include (with the corresponding linking at compile time) as stated above; second the denition of a function; and then the main program. This is basically a good way to look at it, but keep in mind that the Python interpreter will execute everything in order, starting at the top. In executing the import statement, for instance, that might actually result in some code being executed, if the module being imported has some free-standing code. More on this later. Execution of the def statement wont execute any code for now, but the act of dening the function is considered execution. Here are some features in this program which were not in the rst example: use of command-line arguments le-manipulation mechanisms more on lists function denition library importation introduction to scope I will discuss these features in the next few sections.

4.2

Command-Line Arguments

First, lets explain sys.argv. Python includes a module (i.e. library) named sys, one of whose member variables is argv. The latter is a Python list, analogous to argv in C/C++.9 Element 0 of the list is the script name, in this case tme.py, and so on, just as in C/C++. In our example here, in which we run our program on the le x, sys.argv[1] will be the string x (strings in Python are generally specied with single quote marks). Since sys is not loaded automatically, we needed the import line. Both in C/C++ and Python, those command-line arguments are of course strings. If those strings are supposed to represent numbers, we could convert them. If we had, say, an integer argument, in C/C++ we would do the conversion using atoi(); in Python, wed use int().10 For oating-point, in Python wed use oat().11
There is no need for an analog of argc, though. Python, being an object-oriented language, treats lists as objects, The length of a list is thus incorporated into that object. So, if we need to know the number of elements in argv, we can get it via len(argv). 10 We would also use it like C/C++s oor(), in applications that need such an operation. 11 In C/C++, we could use atof() if it were available, or sscanf().
9

12

4.3

Introduction to File Manipulation

The function open() is similar to the one in C/C++. Our line


f = open(sys.argv[1])

created an object of le class, and assigned it to f . The readlines() function of the le class returns a list (keep in mind, list is an ofcial Python term) consisting of the lines in the le. Each line is a string, and that string is one element of the list. Since the le here consisted of ve lines, the value returned by calling readlines() is the ve-element list
[,This is an,example of a,text file,]

(Though not visible here, there is an end-of-line character in each string.)

5
5.1

Declaration (or Not), Scope, Functions, Etc.


Lack of Declaration

Variables are not declared in Python. A variable is created when the rst assignment to it is executed. For example, in the program tme.py above, the variable ines does not exist until the statement
flines = f.readlines()

is executed. By the way, a variable which has not been assigned a value yet has the value None (and this can be assigned to a variable, tested for in an if statement, etc.).

5.2

Locals Vs. Globals

Python does not really have global variables in the sense of C/C++, in which the scope of a variable is an entire program. We will discuss this further in Section 14.1.5, but for now assume our source code consists of just a single .py le; in that case, Python does have global variables pretty much like in C/C++. Python tries to infer the scope of a variable from its position in the code. If a function includes any code which assigns to a variable, then that variable is assumed to be local. So, in the code for checkline(), Python would assume that l and wordcount are local to checkline() if we dont inform it otherwise. We do the latter with the global keyword. Use of global variables simplies the presentation here, and I personally believe that the unctuous criticism of global variables is unwarranted. (See http://heather.cs.ucdavis.edu/matloff/ globals.html.) In fact, in one of the major types of programming, threads, use of globals is basically mandatory. You may wish, however, to at least group together all your globals into a class, as I do. See Appendix C. 13

A Couple of Built-In Functions

The function len() returns the number of elements in a list, in this case, the number of lines in the le (since readlines() returned a list in which each element consisted of one line of the le). The method split() is a member of the string class.12 It splits a string into a list of words, for example.13 So, for instance, in checkline() when l is This is an then the list w will be equal to [This,is,an]. (In the case of the rst line, which is blank, w will be equal to the empty list, [].)

Types of Variables/Values

As is typical in scripting languages, type in the sense of C/C++ int or oat is not declared in Python. However, the Python interpreter does internally keep track of the type of all objects. Thus Python variables dont have types, but their values do. In other words, a variable X might be bound to an integer at one point in your program and then be rebound to a class instance at another point. In other words, Python uses dynamic typing. Pythons types include notions of scalars, sequences (lists or tuples) and dictionaries (associative arrays, discussed in Sec. 7.3), classes, function, etc.

7.1

String Versus Numerical Values

Unlike Perl, Python does distinguish between numbers and their string representations. The functions eval() and str() can be used to convert back and forth. For example:
>>> 2 + 1.5 Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: unsupported operand type(s) for +: int and str >>> 2 + eval(1.5) 3.5 >>> str(2 + eval(1.5)) 3.5

There are also int() to convert from strings to integers, and oat(), to convert from strings to oating-point values:
>>> n = int(32) >>> n 32 >>> x = float(5.28) >>> x 5.2800000000000002

See also Section 17.4.


12 13

Member functions of classes are referred to as methods. The default is to use blank characters as the splitting criterion, but other characters or strings can be used.

14

7.2

Sequences

Lists are actually special cases of sequences, which are all array-like but with some differences. Note though, the commonalities; all of the following (some to be explained below) apply to any sequence type: the use of brackets to denote individual elements (e.g. x[i]) the built-in len() function to give the number of elements in the sequence14 slicing operations, i.e. the extraction of subsequences use of + and * operators for concatenation and replication 7.2.1 Lists (Quasi-Arrays)

As stated earlier, lists are denoted by brackets and commas. For instance, the statement
x = [4,5,12]

would set x to the specied 3-element array. Lists may grow dynamically, using the list class append() or extend() functions. For example, if after the abovfe statement we were to execute
x.append(-2)

x would now be equal to [4,5,12,-2]. A number of other operations are available for lists, a few of which are illustrated in the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

>>> x = [5,12,13,200] >>> x [5, 12, 13, 200] >>> x.append(-2) >>> x [5, 12, 13, 200, -2] >>> del x[2] >>> x [5, 12, 200, -2] >>> z = x[1:3] # array "slicing": elements 1 through 3-1 = 2 >>> z [12, 200] >>> yy = [3,4,5,12,13] >>> yy[3:] # all elements starting with index 3 [12, 13] >>> yy[:3] # all elements up to but excluding index 3 [3, 4, 5] >>> yy[-1] # means "1 item from the right end" 13 >>> x.insert(2,28) # insert 28 at position 2 >>> x [5, 12, 28, 200, -2] >>> 28 in x # tests for membership; 1 for true, 0 for false
14

This function is applicable to dictionaries too.

15

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

1 >>> 0 >>> 2 >>> >>> [5, >>> >>> [5, >>> >>> [1, >>> >>> >>> [1, >>> >>> 1 >>> [2,

13 in x x.index(28) # finds the index within the list of the given value

x.remove(200) # different from "delete," since its indexed by value x 12, 28, -2] w = x + [1,"ghi"] # concatenation of two or more lists w 12, 28, -2, 1, ghi] qz = 3*[1,2,3] # list replication qz 2, 3, 1, 2, 3, 1, 2, 3] x = [1,2,3] x.extend([4,5]) x 2, 3, 4, 5] y = x.pop(0) # deletes and returns 0th element y x 3, 4, 5]

We also saw the in operator in an earlier example, used in a for loop. A list could include mixed elements of different types, including other lists themselves. The Python idiom includes a number of common Python tricks involving sequences, e.g. the following quick, elegant way to swap two variables x and y:
>>> >>> >>> >>> 12 >>> 5 x = 5 y = 12 [x,y] = [y,x] x y

Multidimensional lists can be implemented as lists of lists. For example:


>>> x = [] >>> x.append([1,2]) >>> x [[1, 2]] >>> x.append([3,4]) >>> x [[1, 2], [3, 4]] >>> x[1][1] 4

But be careful! Look what can go wrong:


>>> x = 4*[0] >>> y = 4*[x] >>> y [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] >>> y[0][2] 0 >>> y[0][2] = 1 >>> y [[0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 1, 0]]

16

The problem is that that assignment to y was really a list of four references to the same thing (x). When the object pointed to by x changed, then all four rows of y changed. The Python Wikibook (http://en.wikibooks.org/wiki/Python_Programming/Lists) suggests a solution, in the form of list comprehensions, which we cover in Section 19.4:
>>> z = [[0]*4 for i in range(5)] >>> z [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] >>> z[0][2] = 1 >>> z [[0, 0, 1, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

7.2.2

Tuples

Tuples are like lists, but are immutable, i.e. unchangeable. They are enclosed by parentheses or nothing at all, rather than brackets. The parentheses are mandatory if there is an ambiguity without them, e.g. in function arguments. A comma must be used in the case of empty or single tuple, e.g. (,) and (5,). The same operations can be used, except those which would change the tuple. So for example
x = (1,2,abc) print x[1] # prints 2 print len(x) # prints 3 x.pop() # illegal, due to immutability

A nice function is zip(), which strings together corresponding components of several lists, producing tuples, e.g.
>>> zip([1,2],[a,b],[168,168]) [(1, a, 168), (2, b, 168)]

7.2.3

Strings

Strings are essentially tuples of character elements. But they are quoted instead of surrounded by parentheses, and have more exibility than tuples of character elements would have. For example:
1 2 3 4 5 6 7 8 9 10

>>> x = abcde >>> x[2] c >>> x[2] = q # illegal, since strings are immmutable Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: object doesnt support item assignment >>> x = x[0:2] + q + x[3:5] >>> x abqde

(You may wonder why that last assignment


>>> x = x[0:2] + q + x[3:5]

17

does not violate immmutability. The reason is that x is really a pointer, and we are simply pointing it to a new string created from old ones. See Section 11.) As noted, strings are more than simply tuples of characters:
>>> x.index(d) # as expected 3 >>> d in x # as expected 1 >>> x.index(de) # pleasant surprise 3

As can be seen, the index() function from the str class has been overloaded, making it more exible. There are many other handy functions in the str class. For example, we saw the split() function earlier. The opposite of this function is join(). One applies it to a string, with a sequence of strings as an argument. The result is the concatenation of the strings in the sequence, with the original string between each of them:15
>>> ---.join([abc,de,xyz]) abc---de---xyz >>> q = \n.join((abc,de,xyz)) >>> q abc\nde\nxyz >>> print q abc de xyz

Here are some more:


>>> x = abc >>> x.upper() ABC >>> abc.upper() ABC >>> abc.center(5) # center the string within a 5-character set abc >>> abc de f.replace( ,+) abc+de+f

A very rich set of functions for string manipulation is also available in the re (regular expression) module. The str class is built-in for newer versions of Python. With an older version, you will need a statement
import string

That latter class does still exist, and the newer str class does not quite duplicate it. 7.2.4 Sorting

The Python function sort() can be applied to any sequence. For nonscalars, one provides a compare function, which returns a negative, zero or positive value, signying <, = or >. As an illustration, lets sort an array of arrays, using the second elements as keys:
The example here shows the new usage of join(), now that string methods are built-in to Python. See discussion of new versus old below.
15

18

>>> x = [[1,4],[5,2]] >>> x [[1, 4], [5, 2]] >>> x.sort() >>> x [[1, 4], [5, 2]] >>> def g(u,v): ... return u[1]-v[1] ... >>> x.sort(g) >>> x [[5, 2], [1, 4]]

(This would be more easily done using lambda functions. See Section 19.1.) There is a Python library module, bisect, which does binary search and related sorting.

7.3

Dictionaries (Hashes)

Dictionaries are associative arrays. The technical meaning of this will be discussed below, but from a pure programming point of view, this means that one can set up arrays with non-integer indices. The statement
x = {abc:12,sailing:away}

sets x to what amounts to a 2-element array with x[abc] being 12 and x[sailing] equal to away. We say that abc and sailing are keys, and 12 and away are values. Keys can be any immmutable object, i.e. numbers, tuples or strings.16 Use of tuples as keys is quite common in Python applications, and you should keep in mind that this valuable tool is available. Internally, x here would be stored as a 4-element array, and the execution of a statement like
w = x[sailing]

would require the Python interpreter to search through that array for the key sailing. A linear search would be slow, so internal storage is organized as a hash table. This is why Perls analog of Pythons dictionary concept is actually called a hash. Here are examples of usage of some of the member functions of the dictionary class:
1 2 3 4 5 6 7 8 9 10 11 12

>>> x = {abc:12,sailing:away} >>> x[abc] 12 >>> y = x.keys() >>> y [abc, sailing] >>> z = x.values() >>> z [12, away] x[uv] = 2 >>> x {abc: 12, uv: 2, sailing: away}

Now one sees a reason why Python distinguishes between tuples and lists. Allowing mutable keys would be an implementation nightmare, and probably lead to error-prone programming.

16

19

Note how we added a new element to x near the end. The keys need not be tuples. For example:
>>> x {abc: 12, uv: 2, sailing: away} >>> f = open(z) >>> x[f] = 88 >>> x {<open file z, mode r at 0xb7e6f338>: 88, abc: 12, uv: 2, sailing: away}

Deletion of an element from a dictionary can be done via pop(), e.g.


>>> x.pop(abc) 12 >>> x {<open file x, mode r at 0xb7e6f338>: 88, uv: 2, sailing: away}

The in operator works on dictionary keys, e.g.


>>> uv in x True >>> 2 in x False

7.4

Function Denition

Obviously the keyword def is used to dene a function. Note once again that the colon and indenting are used to dene a block which serves as the function body. A function can return a value, using the return keyword, e.g.
return 8888

However, the function does not have a type even if it does return something, and the object returned could be anythingan integer, a list, or whatever. Functions are rst-class objects, i.e. can be assigned just like variables. Function names are variables; we just temporarily assign a set of code to a name. Consider:
>>> ... ... >>> 9 >>> >>> 9 >>> ... ... >>> 27 >>> >>> def square(x): return x*x square(3) gy = square gy(3) # now gy points to that code too # define code, and point the variable square to it

def cube(x): return x**3 cube(3) square = cube square(3) # point the variable square to the cubing code

20

27 >>> square = 8.8 >>> square 8.8000000000000007 # dont be shocked by the 7 >>> gy(3) # gy still points to the squaring code 9

Keyboard Input

The raw input() function will display a prompt and read in what is typed. For example,
name = raw_input(enter a name: )

would display enter a name:, then read in a response, then store that response in name. Note that the user input is returned in string form, and needs to be converted if the input consists of numbers. If you dont want the prompt, dont specify one:
>>> y = raw_input() 3 >>> y 3

Alternatively, you can directly specify stdin:


>>> import sys >>> z = sys.stdin.readlines() abc de f >>> z [abc\n, de\n, f\n]

After typing f, I hit ctrl-d to close the stdin le.)

Use of name

In some cases, it is important to know whether a module is being executed on its own, or via import. This can be determined through Pythons built-in variable name , as follows. Whatever the Python interpreter is running is called the top-level program. If for instance you type
% python x.py

then the code in x.py is the top-level program. If you are running Python interactively, then the code you type in is the top-level program. The top-level program is known to the interpreter as main , and the module currently being run is referred to as name . So, to test whether a given module is running on its own, versus having been imported by 21

other code, we check whether name is main . If the answer is yes, you are in the top level, and your code was not imported; otherwise it was. For example, lets add a statement
print __name__

to our very rst code example, from Section 3.1, in the le fme.py:
print __name__ for i in range(10): x = 0.1*i print x print x/(1-x*x)

Lets run the program twice. First, we run it on its own:


% python fme.py __main__ 0.0 0.0 0.1 0.10101010101 0.2 0.208333333333 0.3 0.32967032967 ... [remainder of output not shown]

Now look what happens if we run it from within Pythons interactive interpreter:
>>> __name__ __main__ >>> import fme fme 0.0 0.0 0.1 0.10101010101 0.2 0.208333333333 0.3 0.32967032967 ... [remainder of output not shown]

Our modules statement


print __name__

printed out main the rst time, but printed out fme the second time. It is customary to collect ones main program (in the C sense) into a function, typically named main(). So, lets change our example above to fme2.py:

22

def main(): for i in range(10): x = 0.1*i print x print x/(1-x*x) if __name__ == __main__: main()

The advantage of this is that when we import this module, the code wont be executed right away. Instead, fme2.main() must be called, either by the importing module or by the interactive Python interpreter. Here is an example of the latter:
>>> import fme2 >>> fme2.main() 0.0 0.0 0.1 0.10101010101 0.2 0.208333333333 0.3 0.32967032967 0.4 0.47619047619 ...

Among other things, this will be a vital point in using debugging tools (Section A). So get in the habit of always setting up access to main() in this manner in your programs.

10

Object-Oriented Programming

In contrast to Perl, Python has been object-oriented from the beginning, and thus has a much nicer, cleaner, clearer interface for OOP.

23

10.1

Example Program Code

As an illustration, we will develop a class which deals with text les. Here are the contents of the le tfe.py:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

class textfile: ntfiles = 0 # count of number of textfile objects def __init__(self,fname): textfile.ntfiles += 1 self.name = fname # name self.fh = open(fname) # handle for the file self.lines = self.fh.readlines() self.nlines = len(self.lines) # number of lines self.nwords = 0 # number of words self.wordcount() def wordcount(self): "finds the number of words in the file" for l in self.lines: w = l.split() self.nwords += len(w) def grep(self,target): "prints out all lines containing target" for l in self.lines: if l.find(target) >= 0: print l a = textfile(x) b = textfile(y) print "the number of text files open is", textfile.ntfiles print "here is some information about them (name, lines, words):" for f in [a,b]: print f.name,f.nlines,f.nwords a.grep(example)

In addition to the le x I used in Section 4 above, I had the 2-line le y. Here is what happened when I ran the program:
% python tfe.py the number of text files opened is 2 here is some information about them (name, lines, words): x 5 8 y 2 5 example of a

10.2

The Keyword self

Lets take a look at the class textle. The rst thing to note is the prevalence of the keyword self, meaning the current instance of the class, analogous to this in C++ and Java.17 So, self is a pointer to the current instance of the class.

10.3

Instance Variables

In general OOP terminology, an instance variable x of a class is a member variable for which each instance of the class has a separate value of that variable, In the C++ or Java world, you know this as a variable which
Actually self is not a keyword. Unlike C++/Java, you do not HAVE TO call this variable self. If in your denition of init() you were to name the rst argument me, and then write me instead of self throughout the denition of the class, that would work ne. However, you would invoke the wrath of purist pythonistas all over the world. So dont do it.
17

24

is not declared static. The term instance variable is the generic OOP term, non-language specic. To see how these work in Python, recall rst that a variable in Python is created when something is assigned to it. So, an instance variable in an instance of a Python class does not exist until it is assigned to. For example, when
self.name = fname

is executed, the member variable name for the current instance of the class is created, and is assigned the indicated value.

10.4

Class Variables

A class variable say v, has a common value in all instances of the class. Again in the C++ or Java world, you know this as a static variable. It is designated as such by having some reference to v in code which is in the class but not in any method of the class. An example is the code
ntfiles = 0 # count of number of textfile objects

above.18 Note that a class variable v of a class u is referred to as u.v within methods of the class and in code outside the class. For code inside the class but not within a method, it is referred to as simply v. Take a moment now to go through our example program above, and see examples of this with our ntles variable.

10.5

Constructors and Destructors

The constructor for a class must be named init() . The argument self is mandatory, and you can add others, as Ive done in this case with a lename. The destructor is del() . Note that it is only invoked when garbage collection is done, i.e. when all variables pointing to the object are gone.

10.6

Instance Methods

The method wordcount() is an instance method, i.e. it applies specically to the given object of this class. Again, in C++/Java terminology, this is a non-static method. Unlike C++ and Java, where this is an implicit argument to instance methods, Python wisely makes the relation explicit; the argument self is required.

10.7

Class Methods

Before Version 2.2, Python had no formal provision for class methods, i.e. methods which do not apply to specic objects of the class. Now Python has two (slightly differing) ways to do this, using the functions
By the way, though we placed that code at the beginning of the class, it could be at the end of the class, or between two methods, as long as it is not inside a method. In the latter situation ntles would be considered a local variable in the method, not what we want at all.
18

25

staticmethod() and classmethod(). I will present use of the former, in the following enhancement to the code in Section 10.1 within the class textle:
class textfile: ... def totfiles(): print "the total number of text files is", textfile.ntfiles totfiles = staticmethod(totfiles) ... # here we are in "main" ... textfile.totfiles() ...

Note that class methods do not have the self argument. (Nor should they, right?) Note also that this method could be called even if there are not yet any instances of the class textle. In the example here, 0 would be printed out, since no les had yet been counted.19

10.8

Derived Classes

Inheritance is very much a part of the Python philosophy. A statement like


class b(a):

starts the denition of a subclass b of a class a. Multiple inheritance, etc. can also be done. Note that when the constructor for a derived class is called, the constructor for the base class is not automatically called. If you wish the latter constructor to be invoked, you must invoke it yourself, e.g.
class b(a): def __init__(self,xinit): # constructor for class b self.x = xinit # define and initialize an instance variable x a.__init__(self) # call base class constructor

The ofcial Python tutorial notes, [In the C++ sense] all methods in Python are effectively virtual. If you wish to extend, rather than override a method in the base class, you can refer to the latter by prepending the base name, as in our example a. init (self) above.

10.9

A Word on Class Implementation

A Python class instance is implemented internally as a dictionary. For example, in our program tfe.py above, the object b is implemented as a dictionary.
Note carefully that this is different from the Python value None. Even if we have not yet created instances of the class textle, the code ntles = 0 would still have been executed when we rst started execution of the program. As mentioned earlier, the Python interpreter executes the le from the rst line onward. When it reaches the line class textle: it then executes any free-standing code in the denition of the class.
19

26

Among other things, this means that you can add member variables to an instance of a class on the y, long after the instance is created. We are simply adding another key and value to the dictionary. In our main program, for example, we could have a statement like
b.name = zzz

11

Importance of Understanding Object References

A variable which has been assigned a mutable value is actually a pointer to the given object. For example, consider this code:
>>> >>> >>> >>> 5 >>> >>> >>> [1, >>> >>> [1, x = [1,2,3] # x is mutable y = x # x and y now both point to [1,2,3] x[2] = 5 # the mutable object pointed to by x now "mutes" y[2] # this means y[2] changes to 5 too! x = [1,2] y = x y 2] x = [3,4] y 2]

In the rst few lines, x and y are references to a list, a mutable object. The statement
x[2] = 5

then changes one aspect of that object, but x still points to that object. On the other hand, the code
x = [3,4]

now changes x itself, having it point to a different object, while y is still pointing to the rst object. If in the above example we wished to simply copy the list referenced by x to y, we could use slicing, e.g.
y = x[:]

Then y and x would point to different objects; x would point to the same object as before, but the statement for y would create a new object, which y would point to. Even though those two objects have the same values for the time being, if the object pointed to by x changes, ys object wont change. As you can imagine, this gets delicate when we have complex objects. See Pythons copy module for functions that will do object copying to various depths. An important similar issue arises with arguments in function calls. Any argument which is a variable which points to a mutable object can change the value of that object from within the function, e.g.:

27

>>> def f(a): ... a = 2*a # numbers are immutable ... >>> x = 5 >>> f(x) >>> x 5 >>> def g(a): ... a[0] = 2*a[0] # lists are mutable ... >>> y = [5] >>> g(y) >>> y [10]

Function names are references to objects too. What we think of as the name of the function is actually just a pointera mutable oneto the code for that function. For example,
>>> ... ... >>> ... ... >>> 1 >>> 2 >>> >>> 2 >>> 1 def f(): print 1 def g(): print 2 f() g() [f,g] = [g,f] f() g()

12

Object Deletion

Objects can be deleted from Pythons memory by using del, e.g.


>>> del x

NOTE CAREFULLY THAT THIS IS DIFFERENT FROM DELETION FROM A LIST OR DICTIONARY. If you use remove() or pop(), for instance, you are simply removing the pointer to the object from the given data structure, but as long as there is at least one reference, i.e. a pointer, to an object, that object still takes up space in memory. This can be a major issue in long-running programs. If you are not careful to delete objects, or if they are not simply garbage-collected when their scope disappears, you can accumulate more and more of them, and have a very serious memory problem. If you see your machine running ever more slowly while a program is running, you should immediately suspect this.

13

Object Comparison

One can use the < operator to compare sequences, e.g. 28

if x < y:

for lists x and y. The comparison is lexicographic. For example,


>>> [12,tuv] < [12,xyz] True >>> [5,xyz] > [12,tuv] False

Of course, since strings are sequences, we can compare them too:


>>> abc < tuv True >>> xyz < tuv False >>> xyz != tuv True

Note the effects of this on, for example, the max() function:
>>> max([[1, 2], [0], [12, 15], [3, 4, 5], [8, 72]]) [12, 15] >>> max([8,72]) 72

We can set up comparisons for non-sequence objects, e.g. class instances, by dening a cmp() function in the class. The denition starts with
def __cmp__(self,other):

It must be dened to return a negative, zero or positive value, depending on whether self is less than, equal to or greater than other. Very sophisticated sorting can be done if one combines Pythons sort() function with a specialized cmp() function.

14

Modules and Packages

Youve often heard that it is good software engineering practice to write your code in modular fashion, i.e. to break it up into components, top-down style, and to make your code reusable, i.e. to write it in such generality that you or someone else might make use of it in some other programs. Unlike a lot of follow-like-sheep software engineering shiboleths, this one is actually correct! :-)

14.1

Modules

A module is a set of classes, library functions and so on, all in one le. Unlike Perl, there are no special actions to be taken to make a le a module. Any le whose name has a .py sufx is a module!20
20

Make sure the base part of the le name begins with a letter, not, say, a digit.

29

14.1.1

Example Program Code

As our illustration, lets take the textle class from our example above. We could place it in a separate le tf.py, with contents
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

# file tf.py class textfile: ntfiles = 0 # count of number of textfile objects def __init__(self,fname): textfile.ntfiles += 1 self.name = fname # name self.fh = open(fname) # handle for the file self.lines = self.fh.readlines() self.nlines = len(self.lines) # number of lines self.nwords = 0 # number of words self.wordcount() def wordcount(self): "finds the number of words in the file" for l in self.lines: w = l.split() self.nwords += len(w) def grep(self,target): "prints out all lines containing target" for l in self.lines: if l.find(target) >= 0: print l

Note that even though our module here consists of just a single class, we could have several classes, plus global variables,21 executable code not part of any function, etc.) Our test program le, tftest.py, might now look like this:
1 2 3 4 5 6 7 8 9 10 11

# file tftest.py import tf a = tf.textfile(x) b = tf.textfile(y) print "the number of text files open is", tf.textfile.ntfiles print "here is some information about them (name, lines, words):" for f in [a,b]: print f.name,f.nlines,f.nwords a.grep(example)

14.1.2

How import Works

The Python interpreter, upon seeing the statement import tf, would load the contents of the le tf.py.22 Any executable code in tf.py is then executed, in this case
ntfiles = 0 # count of number of textfile objects

(The modules executable code might not only be within classes. See what happens when we do import fme2 in an example in Section 9 below.)
Though they would be global only to the module, not to a program which imports the module. See Section 14.1.5. In our context here, we would probably place the two les in the same directory, but we will address the issue of search path later.
22 21

30

Later, when the interpreter sees the reference to tf.textle, it would look for an item named textle within the module tf, i.e. within the le tf.py, and then proceed accordingly. An alternative approach would be:
1 2 3 4 5 6 7 8 9

from tf import textfile a = textfile(x) b = textfile(y) print "the number of text files open is", textfile.ntfiles print "here is some information about them (name, lines, words):" for f in [a,b]: print f.name,f.nlines,f.nwords a.grep(example)

This saves typing, since we type only textle instead of tf.textle, making for less cluttered code. But arguably it is less safe (what if tftest.py were to have some other item named textle?) and less clear (textles origin in tf might serve to clarify things in large programs). The statement
from tf import *

would import everything in tf.py in this manner. In any event, by separating out the textle class, we have helped to modularize our code, and possibly set it up for reuse. 14.1.3 Compiled Code

Like the case of Java, the Python interpreter compiles any code it executes to byte code for the Python virtual machine. If the code is imported, then the compiled code is saved in a le with sufx .pyc, so it wont have to be recompiled again later. Since modules are objects, the names of the variables, functions, classes etc. of a module are attributes of that module. Thus they are retained in the .pyc le, and will be visible, for instance, when you run the dir() function on that module (Section B.1). 14.1.4 Miscellaneous

A modules (free-standing, i.e. not part of a function) code executes immediately when the module is imported. Modules are objects. They can be used as arguments to functions, return values from functions, etc. The list sys.modules shows all modules ever imported into the currently running program. 14.1.5 A Note on Global Variables Within Modules

Python does not truly allow global variables in the sense that C/C++ do. An imported Python module will not have direct access to the globals in the module which imports it, nor vice versa. 31

For instance, consider these two les, x.py,


# x.py import y def f(): global x x = 6 def main(): global x x = 3 f() y.g() if __name__ == __main__: main()

and y.py:
# y.py def g(): global x x += 1

The variable x in x.py is visible throughout the module x.py, but not in y.py. In fact, execution of the line
x += 1

in the latter will cause an error message to appear, global name x is not dened. Indeed, a global variable in a module is merely an attribute (i.e. a member entity) of that module, similar to a class variables role within a class. When module B is imported by module A, Bs namespace is copied to As. If module B has a global variable X, then module A will create a variable of that name, whose initial value is whatever module B had for its variable of that name at the time of importing. But changes to X in one of the modules will NOT be reected in the other. Say X does change in B, but we want code in A to be able to get the latest value of X in B. We can do that by including a function, say named GetX() in B. Assuming that A imported everything from B, then A will get a function GetX() which is a copy of Bs function of that name, and whose sole purpose is to return the value of X. Unless B changes that function (which is possible, e.g. functions may be assigned), the functions in the two modules will always be the same, and thus A can use its function to get the value of X in B.

14.2

Data Hiding

Python has no strong form of data hiding comparable to the private and other such constructs in C++. It does offer a small provision of this sort, though: If you prepend an underscore to a variables name in a module, it will not be imported if the from form of import is used. For example, if in the module tf.py in Section 14.1.1 were to contain a variable z, then a statement 32

from tf import *

would mean that z is accesible as just z rather than tf.z. If on the other hand we named this variable z, then the above statement would not make this variable accessible as z; we would need to use tf. z. Of course, the variable would still be visible from outside the module, but by requiring the tf. prex we would avoid confusion with similarly-named variables in the importing module. A double underscore results in mangling, with another underscore plus the name of the module prepended.

14.3

Packages

As mentioned earlier, one might place more than one class in a given module, if the classes are closely related. A generalization of this arises when one has several modules that are related. Their contents may not be so closely related that we would simply pool them all into one giant module, but still they may have a close enough relationship that you want to group them in some other way. This is where the notion of a package comes in. For instance, you may write some libraries dealing with some Internet software youve written. You might have one module web.py with classes youve written for programs which do Web access, and another module em.py which is for e-mail software. Instead of combining them into one big module, you could keep them as separate les put in the same directory, say net. To make this directory a package, simply place a le init .py in that directory. The le can be blank, or in more sophisticated usage can be used for some startup operations. In order to import these modules, you would use statements like
import net.web

This tells the Python interpreter to look for a le web.py within a directory net. The latter, or more precisely, the parent of the latter, must be in your Python search path. If for example the full path name for net were
/u/v/net

then the directory /u/v would need to be in your Python search path. If you are on a Unix system and using the C shell, for instance, you could type
setenv PYTHONPATH /u/v

If you have several special directories like this, string them all together, using colons as delimiters:
setenv PYTHONPATH /u/v:/aa/bb/cc

The current path is contained in sys.path. Again, it consists of a list of strings, one string for each directory, separated by colons. It can be printed out or changed by your code, just like any other variable.23 Package directories often have subdirectories, subsubdirectories and so on. Each one must contain a init .py le.
23

Remember, you do have to import sys rst.

33

15

Exception Handling (Not Just for Exceptions!)

By the way, Pythons built-in and library functions have no C-style error return code to check to see whether they succeeded. Instead, you use Pythons try/except exception-handling mechanism, e.g.
try: f = open(sys.argv[1]) except: print open failed:,sys.argv[1]

Heres another example:


try: i = 5 y = x[i] except: print no such index:, i

But the Python idiom also uses this for code which is not acting in an exception context. Say for example we want to nd the index of the number 8 in the list z and if there is no such number, to rst add it to the list. We could do it this way:
try: place = x.index(8) except: x.append(8) place = len(x)

As seen above, you use try to check for an exception; you use raise to raise one.

16

Docstrings

There is a double-quoted string, nds the number of words in the le, at the beginning of wordcount(). This is called a docstring. It serves as a kind of comment, but at runtime, so that it can be used by debuggers and the like. Also, it enables users who have only the compiled form of the method, say as a commercial product, access to a comment. Here is an example of how to access it, using tf.py from above:
>>> import tf >>> tf.textfile.wordcount.__doc__ finds the number of words in the file

A docstring typically spans several lines. To create this kind of string, use triple quote marks. The method grep() is another instance method, this one with an argument besides self. By the way, method arguments in Python can only be pass-by-value, in the sense of C: Functions have side effects with respect to the parameter if the latter is a pointer. (Python does not have formal pointers, but it does have references; see Section 11.)

34

Note also that grep() makes use of one of Pythons many string operations, nd(). It searches for the argument string within the object string, returning the index of the rst occurrence of the argument string within the object string, or returning -1 if none is found.24

17
17.1

Miscellaneous
Running Python Scripts Without Explicitly Invoking the Interpreter

Say you have a Python script x.py. So far, we have discussed running it via the command25
% python x.py

But if you state the location of the Python interpreter in the rst line of x.py, e.g.
#! /usr/bin/python

and use the Unix chmod command to make x.py executable, then you can run x.py by merely typing
% x.py

This is necessary, for instance, if you are invoking the program from a Web page. Better yet, you can have Unix search your environment for the location of Python, by putting this as your rst line in x.py:
#! /usr/bin/env python

This is more portable, as different platforms may place Python in different directories.

17.2

Named Arguments in Functions

Consider this little example:


1 2 3 4 5 6 7 8 9 10

def f(u,v=2): return u+v def main(): x = 2; y = 3; print f(x,y) # prints 5 print f(x) # prints 4 if __name__ == __main__: main()

Strings are also treatable as lists of characters. For example, geometry can be treated as an 8-element list, and applying nd() for the substring met would return 3. 25 This section will be Unix-specic.

24

35

Here, the argument v is called a named argument, with default value 2. The ordinary argument u is called a mandatory argument, as it must be specied while v need not be. Another term for u is positional argument, as its value is inferred by its position in the order of declaration of the functions arguments. Mandatory arguments must be declared before named arguments.

17.3

Printing Without a Newline or Blanks

A print statement automatically prints a newline character. To suppress it, add a trailing comma. For example:
print 5, print 12 # nothing printed out yet # 5 12 now printed out, with end-of-line

The print statement automatically separates items with blanks. To suppress blanks, use the string-concatenation operator, +, and possibly the str() function, e.g.
x = a y = 3 print x+str(y)

# prints a3

By the way, str(None) is None.

17.4

Formatted String Manipulation

Python supports C-style printf(), e.g.


print "the factors of 15 are %d and %d" % (3,5)

prints out
the factors of 15 are 3 and 5

Note the importance of writing (3,5) rather than 3,5. In the latter case, the % operator would think that its operand was merely 3, whereas it needs a 2-element tuple. Recall that parentheses enclosing a tuple can be omitted as long as there is no ambiguity, but that is not the case here. This is nice, but it is far more powerful than just for printing, but for general string manipulation. In
print "the factors of 15 are %d and %d" % (3,5)

the portion
"the factors of 15 are %d and %d" % (3,5)

is a string operation, producing a new string; the print simply prints that new string. For example: 36

>>> x = "%d years old" % 12

The variable x now is the string 12 years old. This is another very common idiom, quite powerful.26

18

Example of Data Structures in Python

Below is a Python class for implementing a binary tree. The comments should make the program selfexplanatory (no pun intended).27
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38

# bintree.py, a module for handling sorted binary trees; values to be # stored can be general, as long as an ordering relation exists # here, only have routines to insert and print, but could add delete, # etc. class treenode: def __init__(self,v): self.value = v; self.left = None; self.right = None; def ins(self,nd): # inserts the node nd into tree rooted at self m = nd.value if m < self.value: if self.left == None: self.left = nd else: self.left.ins(nd) else: if self.right == None: self.right = nd else: self.right.ins(nd) def prnt(self): # prints the subtree rooted at self if self.value == None: return if self.left != None: self.left.prnt() print self.value if self.right != None: self.right.prnt() class tree: def __init__(self): self.root = None def insrt(self,m): newnode = treenode(m) if self.root == None: self.root = newnode return self.root.ins(newnode)

And here is a test:


1 2 3 26 27

# trybt1.py: test of bintree.py # usage: python trybt.py numbers_to_insert

Some C/C++ programmers might recognize the similarity to sprintf() from the C library. But did you get the pun?

37

4 5 6 7 8 9 10 11 12 13

import sys import bintree def main(): tr = bintree.tree() for n in sys.argv[1:]: tr.insrt(int(n)) tr.root.prnt() if __name__ == __main__: main()

The good thing about Python is that we can use the same code again for nonnumerical objects, as long as they are comparable. (Recall Section 13.) So, we can do the same thing with strings, using the tree and treenode classes AS IS, NO CHANGE, e.g.
# trybt2.py: # usage: test of bintree.py

python trybt.py strings_to_insert

import sys import bintree def main(): tr = bintree.tree() for s in sys.argv[1:]: tr.insrt(s) tr.root.prnt() if __name__ == __main__: main()

% python trybt2.py abc tuv 12 12 abc tuv

Or even
# trybt3.py: import bintree def main(): tr = bintree.tree() tr.insrt([12,xyz]) tr.insrt([15,xyz]) tr.insrt([12,tuv]) tr.insrt([2,y]) tr.insrt([20,aaa]) tr.root.prnt() if __name__ == __main__: main() test of bintree.py

% python trybt3.py [2, y] [12, tuv] [12, xyz] [15, xyz] [20, aaa]

38

18.1

Making Use of Python Idioms

In the example in Section 10.1, it is worth calling special attention to the line
for f in [a,b]:

where a and b are objects of type textle. This illustrates the fact that the elements within a list do not have to be scalars.28 Much more importantly, it illustrates that really effective use of Python means staying away from classic C-style loops and expressions with array elements. This is what makes for much cleaner, clearer and elegant code. It is where Python really shines. You should almost never use C/C++ style for loopsi.e. where an index (say j), is tested against an upper bound (say j < 10), and incremented at the end of each iteration (say j++). Indeed, you can often avoid explicit loops, and should do so whenever possible. For example, the code
self.lines = self.fh.readlines() self.nlines = len(self.lines)

in that same program is much cleaner than what we would have in, say, C. In the latter, we would need to set up a loop, which would read in the le one line at a time, incrementing a variable nlines in each iteration of the loop.29 Another great way to avoid loops is to use Pythons functional programming features, described in Section 19. Making use of Python idioms is often referred to by the pythonistas as the pythonic way to do things.

19

Functional Programming Features

These features provide concise ways of doing things which, though certainly doable via more basic constructs, compactify your code and thus make it easier to write and read. They may also make your code run much faster. Moreover, it may help us avoid bugs, since a lot of the infracture wed need to write ourselves, which would be bug-prone, is automatically taken care of us by the functional programming constructs. Except for the rst feature here (lambda functions), these features eliminate the need for explicit loops and explicit references to list elements. As mentioned in Section 18.1, this makes for cleaner, clearer code.

19.1

Lambda Functions

Lambda functions provide a way of dening short functions. They help you avoid cluttering up your code with a lot of one-liners which are called only once. For example:
>>> g = lambda u:u*u >>> g(4) 16
28 29

Nor do they all have to be of the same type at all. One can have diverse items within the same list. By the way, note the reference to an object within an object, self.fh.

39

Note carefully that this is NOT a typical usage of lambda functions; it was only to illustrate the syntax. Usually a lambda functions would not be dened in a free-standing manner as above; instead, it would be dened inside other functions such as map() and lter(), as seen next. Here is a more realistic illustration, redoing the sort example from Section 7.2.4:
>>> x = [[1,4],[5,2]] >>> x [[1, 4], [5, 2]] >>> x.sort() >>> x [[1, 4], [5, 2]] >>> x.sort(lambda u,v: u[1]-v[1]) >>> x [[5, 2], [1, 4]]

A bit of explanation is necessary. If you look at the online help for sort(), youll nd that the denition to be
sort(...) L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*; cmp(x, y) -> -1, 0, 1

You see that the rst argument is a named argument (recall Section 17.2), cmp. That is our compare function, which we dened above as lambda u,v: u[1]-v[1]. The general form of a lambda function is
lambda arg 1, arg 2, ...: expression

So, multiple arguments are permissible, but the function body itself must be an expression.

19.2

Mapping

The map() function converts one sequence to another, by applying the same function to each element of the sequence. For example:
>>> z = map(len,["abc","clouds","rain"]) >>> z [3, 6, 4]

So, we have avoided writing an explicit for loop, resulting in code which is a little cleaner, easier to write and read.30 In the example above we used a built-in function, len(). We could also use our own functions; frequently these are conveniently expressed as lambda functions, e.g.:
30 Side note: Note again that if we had not assigned to z, the list [3,6,4] would have been printed out anyway. In interactive mode, any Python non-assignment statement prints the value of the result.

40

>>> >>> >>> [1,

x = [1,2,3] y = map(lambda z: z*z, x) y 4, 9]

The condition that a lambda functions body consist only of an expression is rather limiting, for instance not allowing if-then-else constructs. If you really wish to have the latter, you could use a workaround. For example, to implement something like
if u > 2: u = 5

we could work as follows:


>>> >>> >>> [1, x = [1,2,3] g = lambda u: (u > 2) * 5 + (u <= 2) * u map(g,x) 2, 5]

Clearly, this is not feasible except for simple situations. For more complex cases, we would use a nonlambda function. For example, here is a revised version of the program in Section 4.1:
1 2 3 4 5 6 7 8 9 10 11 12 13

import sys def checkline(l): global wordcount w = l.split() wordcount += len(w) wordcount = 0 f = open(sys.argv[1]) flines = f.readlines() linecount = len(flines) map(checkline,flines) # replaces the old for loop print linecount, wordcount

Note that l is now an argument to checkline(). Of course, this could be reduced even further, with the heart of the main program above being changed to
map(checkline,open(sys.argv[1]).readlines)

But this is getting pretty hard to read and debug.

19.3

Filtering

The lter() function works like map(), except that it culls out the sequence elements which satisfy a certain condition. The function which lter() is applied to must be boolean-valued, i.e. return the desired true or false value. For example:
>>> >>> >>> [5, x = [5,12,-2,13] y = filter(lambda z: z > 0, x) y 12, 13]

Again, this allows us to avoid writing a for loop and an if statement. 41

19.4

List Comprehension

This allows you to compactify a for loop which produces a list. For example:
>>> x = [(1,-1), (12,5), (8,16)] >>> y = [(v,u) for (u,v) in x] >>> y [(-1, 1), (5, 12), (16, 8)]

This is more compact than rst initializing y to [], then having a for loop in which we call y.append(). It gets even better when done in nested form. Say for instance we have a list of lists which we want to concatenate together, ignoring the rst element in each. Heres how we could do it using list comprehensions:
>>> y [[0, 2, 22], [1, 5, 12], [2, 3, 33]] >>> [a for b in y for a in b[1:]] [2, 22, 5, 12, 3, 33]

19.5

Reduction

The reduce() function is used for applying the sum or other arithmetic-like operation to a list. For example,
>>> x = reduce(lambda x,y: x+y, range(5)) >>> x 10

Here range(5) is of course [0,1,2,3,4]. What reduce() does is it rst adds the rst two elements of [0,1,2,3,4], i.e. with 0 playing the role of x and 1, playing the role of y. That gives a sum of 1. Then that sum, 1, plays the role of x and the next element of [0,1,2,3,4], 2, plays the role of y, yielding a sum of 3, etc. Eventually reduce() nishes its work and returns a value of 10. Once again, this allowed us to avoid a for loop, plus a statement in which we initialize x to 0 before the for loop.

Debugging

Do NOT debug by simply adding and subtracting print statements. Use a debugging tool! If you are not a regular user of a debugging tool, then you are causing yourself unnecessary grief and wasted time; see my debugging slide show, at http://heather.cs.ucdavis.edu/matloff/debug.html.

A.1

Pythons Built-In Debugger, PDB

The built-in debugger for Python, PDB, is rather primitive, but its very important to understand how it works, for two reasons: PDB is used indirectly by more sophisticated debugging tools. A good knowledge of PDB will enhance your ability to use those other tools. 42

I will show you here how to increase PDBs usefulness even as a standalone debugger. I will present PDB below in a sequence of increasingly-useful forms: The basic form. The basic form enhanced by strategic use of macros. The basic form in conjunction with the Emacs text editor. The basic form in conjunction with the DDD GUI for debuggers. A.1.1 The Basics

You should be able to nd PDB in the lib subdirectory of your Python package. On a Unix system, for example, that is probably in something like /usr/lib/python2.2. /usr/local/lib/python2.4, etc. To debug a script x.py, type
% /usr/lib/python2.2/pdb.py x.py

(If x.py had had command-line arguments, they would be placed after x.py on the command line.) Of course, since you will use PDB a lot, it would be better to make an alias for it. For example, under Unix in the C-shell:
alias pdb /usr/lib/python2.2/pdb.py

so that you can write more simply


% pdb x.py

Once you are in PDB, set your rst breakpoint, say at line 12:
b 12

You can make it conditional, e.g.


b 12, z > 5

Hit c (continue), which you will get you into x.py and then stop at the breakpoint. Then continue as usual, with the main operations being like those of GDB: b (break) to set a breakpoint tbreak to set a one-time breakpoint 43

ignore to specify that a certain breakpoint will be ignored the next k times, where k is specied in the command l (list) to list some lines of source code n (next) to step to the next line, not stopping in function code if the current line is a function call s (subroutine) same as n, except that the function is entered in the case of a call c (continue) to continue until the next break point w (where) to get a stack report u (up) to move up a level in the stack, e.g. to query a local variable there d (down) to move down a level in the stack r (return) continue execution until the current function returns j (jump) to jump to another line without the intervening code being executed h (help) to get (minimal) online help (e.g. h b to get help on the b command, and simply h to get a list of all commands); type h pdb to get a tutorial on PDB31 q (quit) to exit PDB Upon entering PDB, you will get its (Pdb) prompt. If you have a multi-le program, breakpoints can be specied in the form module name:line number. For instance, suppose your main module is x.py, and it imports y.py. You set a breakpoint at line 8 of the latter as follows:
(Pdb) b y:8

Note, though, that you cant do this until y has actually been imported by x.32 When you are running PDB, you are running Python in its interactive mode. Therefore, you can issue any Python command at the PDB prompt. You can set variables, call functions, etc. This can be highly useful. For example, although PDB includes the p command for printing out the values of variables and expressions, it usually isnt necessary. To see why, recall that whenever you run Python in interactive mode, simply typing the name of a variable or expression will result in printing it outexactly what p would have done, without typing the p. So, if x.py contains a variable ww and you run PDB, instead of typing
(Pdb) p ww

you can simply type


31 32

The tutorial is run through a pager. Hit the space bar to go to the next page, and the q key to quit. Note also that if the module is implemented in C, you of course will not be able to break there.

44

ww

and the value of ww will be printed to the screen.33 If your program has a complicated data structure, you could write a function to print to the screen all or part of that structure. Then, since PDB allows you to issue any Python command at the PDB prompt, you could simply call this function at that prompt, thus getting more sophisticated, application-specic printing. After your program either nishes under PDB or runs into an execution error, you can re-run it without exiting PDBimportant, since you dont want to lose your breakpointsby simply hitting c. And yes, if youve changed your source code since then, the change will be reected in PDB.34 If you give PDB a single-step command like n when you are on a Python line which does multiple operations, you will need to issue the n command multiple times (or set a temporary breakpoint to skip over this). For example,
for i in range(10):

does two operations. It rst calls range(), and then sets i, so you would have to issue n twice. And how about this one?
y = [(y,x) for (x,y) in x]

If x has, say, 10 elements, then you would have to issue the n command 10 times! Here you would denitely want to set a temporary breakpoint to get around it. A.1.2 Using PDB Macros

PDBs undeniably bare-bones nature can be remedied quite a bit by making good use of the alias command, which I strongly suggest. For example, type
alias c c;;l

This means that each time you continue, when you next stop at a breakpoint you automatically get a listing of the neighboring code. This will really do a lot to make up for PDBs lack of a GUI. In fact, this is so important that you should put it in your PDB startup le, which in Unix is $HOME/.pdbrc.35 That way the alias is always available. You could do the same for the n and s commands:
However, if the name of the variable is the same as that of a PDB command (or its abbreviation), the latter will take precedence. If for instance you have a variable n, then typing n will result in PDBs n[ext] command being executed, rather than there being a printout of the value of the variable n. To get the latter, you would have to type p n. 34 PDB is, as seen above, just a Python program itself. When you restart, it will re-import your source code. By the way, the reason your breakpoints are retained is that of course they are variables in PDB. Specically, they are stored in member variable named breaks in the the Pdb class in pdb.py. That variable is set up as a dictionary, with the keys being names of your .py source les, and the items being the lists of breakpoints. 35 Python will also check for such a le in your current directory.
33

45

alias c c;;l alias n n;;l alias s s;;l

There is an unalias command too, to cancel an alias. You can write other macros which are specic to the particular program you are debugging. For example, lets again suppose you have a variable named ww in x.py, and you wish to check its value each time the debugger pauses, say at breakpoints. Then change the above alias to
alias c c;;l;;ww

A.1.3

Using dict

In Section A.6.1 below, well show that if o is an object of some class, then printing o. dict will print all the member variables of this object. Again, you could combine this with PDBs alias capability, e.g.
alias c c;;l;;o.__dict__

Actually, it would be simpler and more general to use


alias c c;;l;;self

This way you get information on the member variables no matter what class you are in. On the other hand, this apparently does not produce information on member variables in the parent class. A.1.4 The type() Function

In reading someone elses code, or even ones own, one might not be clear what type of object a variable currently references. For this, the type() function is sometimes handy. Here are some examples of its use:
>>> x = [5,12,13] >>> type(x) <type list> >>> type(3) <type int> >>> def f(y): return y*y ... >>> f(5) 25 >>> type(f) <type function>

A.2

Using PDB with Emacs

Emacs is a combination text editor and tools collection. Many software engineers swear by it. It is available for Windows, Macs and Unix/Linux; it is included in most Linux distributions. But even if you are not an Emacs acionado, you may nd it to be an excellent way to use PDB. You can split Emacs into two 46

windows, one for editing your program and the other for PDB. As you step through your code in the second window, you can see yourself progress through the code in the rst. To get started, say on your le x.py, go to a command window (whatever you have under your operating system), and type either
emacs x.py

or
emacs -nw x.py

The former will create a new Emacs window, where you will have mouse operations available, while the latter will run Emacs in text-only operations in the current window. Ill call the former GUI mode. Then type M-x pdb, where for most systems M, which stands for meta, means the Escape (or Alt) key rather than the letter M. Youll be asked how to run PDB; answer in the manner you would run PDB externally to Emacs (but with a full path name), e.g.
/usr/local/lib/python2.4/pdb.py x.py 3 8

where the 3 and 8 in this example are your programs command-line arguments. At that point Emacs will split into two windows, as described earlier. You can set breakpoints directly in the PDB window as usual, or by hitting C-x space at the desired line in your programs window; here and below, C- means hitting the control key and holding it while you type the next key. At that point, run PDB as usual. If you change your program and are using the GUI version of Emacs, hit IM-Python | Rescan to make the new version of your program known to PDB. In addition to coordinating PDB with your error, note that another advantage of Emacs in this context is that Emacs will be in Python mode, which gives you some extra editing commands specic to Python. Ill describe them below. In terms of general editing commands, plug Emacs tutorial or Emacs commands into your favorite Web search engine, and youll see tons of resources. Here Ill give you just enough to get started. First, there is the notion of a buffer. Each le you are editing36 has its own buffer. Each other action you take produces a buffer too. For instance, if you invoke one of Emacs online help commands, a buffer is created for it (which you can edit, save, etc. if you wish). An example relevant here is PDB. When you do M-x pdb, that produces a buffer for it. So, at any given time, you may have several buffers. You also may have several windows, though for simplicity well assume just two windows here. In the following table, we show commands for both the text-only and the GUI versions of Emacs. Of course, you can use the text-based commands in the GUI too.
36

There may be several at once, e.g. if your program consists of two or more source les.

47

action cursor movement undo cut paste search for string mark region go to other window enlarge window repeat folowing command n times list buffers go to a buffer exit Emacs

text arrow keys, PageUp/Down C-x u C-space (cursor move) C-w C-y C-s C-@ C-x o (1 line at a time) C-x M-x n C-x C-b C-x b C-x C-c

GUI mouse, left scrollbar Edit | Undo select region | Edit | Cut Edit | Paste Edit | Search select region click window drag bar M-x n Buffers Buffers File | Exit Emacs

In using PDB, keep in mind that the name of your PDB buffer will begin with gud, e.g. gud-x.py. You can get a list of special Python operations in Emacs by typing C-h d and then requesting info in pythonmode. One nice thing right off the bat is that Emacs python-mode adds a special touch to auto-indenting: It will automatically indent further right after a def or class line. Here are some operations: action comment-out region go to start of def or class go to end of def or class go one block outward shift region right shift region left text C-space (cursor move) C-c # ESC C-a ESC C-e C-c C-u mark region, C-c C-r mark region, C-c C-l GUI select region | Python | Comment ESC C-a ESC C-e C-c C-u mark region, Python | Shift right mark region, Python | Shift left

A.3

Debugging with DDD

DDD, available on many Unix systems (and freely downloadable if your system doesnt have it), is a GUI for many debuggers, such GDB (for C/C++), JDB (for Java), Perls built-in Perl debugger, and so on. It can be used on PDB for Python, and thus make your usage of PDB more enjoyable and productive. A.3.1 Youll Need a Special PYDB

The rst use of DDD on PDB was designed by Richard Wolff. He modied pdb.py slightly for this purpose, calling the new PDB pydb.py. It was designed for Python 1.5, but he has kindly provided an update for me. Youll need the les
http://heather.cs.ucdavis.edu/matloff/Python/DDD/pydb.py http://heather.cs.ucdavis.edu/matloff/Python/DDD/pydbcmd.py http://heather.cs.ucdavis.edu/matloff/Python/DDD/pydbsupt.py

Place the les somewhere in your search path, say /usr/bin. Make sure that you give them execute permission, and that bdb.py from the Python library is in your PYTHONPATH.

48

A.3.2

DDD Launch and Program Loading

To start, say for debugging fme2.py in Section 9, rst make sure that main() is set up as described in that section. When you invoke DDD, tell it to use PYDB:
ddd --debugger /usr/bin/pydb.py

Then in DDDs Console, i.e. the PDB command subwindow (near the bottom), type
(pdb) file fme2.py

Later, when you make a change to your source code, again issue the command
(pdb) file fme2.py

Your breakpoints from the last run will be retained. Select Program | Run as usual to set your command-line arguments, and then run. (You may get a DDD: No Source) popup error window, but just click OK and ignore it.) A.3.3 Breakpoints

To set a breakpoint, right-click somewhere in blank space on the line in your source le and choose Set Breakpoint (or Set Temporary Breakpoint or Continue to Until Here, as the case may be). A.3.4 Running Your Code

To run, click on Program | Run, ll in your programs command line arguments if any in the Run with Arguments box, and click Run in that pop-up window. You will be asked to hit Continue, which you could do by clicking Program | Run |, but is more conveniently done by clicking Continue in the little command summary window. (But dont use Run there.) You can then click on Next, Step, Cont etc. The marker for the current execution line is shaped like an I, though rather faint when the mouse pointer is not in the source code section of the DDD window.. By the way, do not refer to sys.argv in freestanding code within a class. When your program is rst loaded, any freestanding code will be executed, and since the command-line arguments wont have been loaded yet, so you will get an index out of range error. Avoid this by putting code involving sys.argv either inside a function in the class, or outside the class entirely. A.3.5 Inspecting Variables

You can inspect the value of a variable by moving the mouse pointer to any instance of the variable in the source code window. 49

As mentioned in Section A.1.3, if o is an object of some class, then printing o. dict will print all the member variables of this object. In DDD, you can do this even more easily, as follows. Simply put that expression in a comment, e.g.
# o.__dict__

and then whenever you wish to inspect the member variables of o, simply move the mouse pointer to that expression in the comment! Make good use of DDDs feature which allows a variable to be displayed continuously. Simply right-click on any instance of the variable, and then choose Display. A.3.6 Miscellaneous

DDD, developed originally for C/C++, is not always a perfect match to Python. But since what DDD actually does is relay your clicked commands to PDB, as you can see in DDDs Console, whatever DDD cant do for you, you can type PDB commands directly into the Console.

A.4

Debugging with Winpdb

The Winpdb debugger (www.digitalpeers.com/pythondebugger/),37 is very good. Among other things, it can be used to debug threaded code, curses-based code and so on, which many debuggers cant. Winpdb is a GUI front end to the text-based RPDB2, which is in the same package. I have a tutorial on both at http://heather.cs.ucdavis.edu/matloff/winpdb.html.

A.5

Debugging with Eclipse

I personally do not like integrated development environments (IDEs). They tend to be very slow to load, often do not allow me to use my favorite text editor,38 and in my view they do not add much functionality. However, if you are a fan of IDEs, here are some suggestions: However, if you like IDEs, I do suggest Eclipse, which I have a tutorial for at http://heather.cs. ucdavis.edu/matloff/eclipse.html. My tutorial is more complete than most, enabling you to avoid the gotchas and have smooth sailing.

A.6

Some Python Internal Debugging Aids

There are various built-in functions in Python that you may nd helpful during the debugging process.
No, its not just for Microsoft Windows machines, in spite of the name. I use vim, but the main point is that I want to use the same editor for all my workprogramming, writing, e-mail, Web site development, etc.
38 37

50

A.6.1

The dict Attribute

Recall that class instances are implemented as dictionaries. If you have a class instance i, you can view the dictionary which is its implementation via i. dict . This will show you the values of all the member variables of the class. A.6.2 The id() Function

Sometimes it is helpful to know the actual memory address of an object. For example, you may have two variables which you think point to the same object, but are not sure. The id() method will give you the address of the object. For example:
>>> x = [1,2,3] >>> id(x) -1084935956 >>> id(x[1]) 137809316

(Dont worry about the negative address, which just reects the fact that the address was so high that, viewed as a 2s-complement integer, it is negative.)

B
B.1

Online Documentation
The dir() Function

There is a very handy function dir() which can be used to get a quick review of what a given object or function is composed of. You should use it often. To illustrate, in the example in Section 10.1 suppose we stop at the line
print "the number of text files open is", textfile.ntfiles

Then we might check a couple of things with dir(), say:


(Pdb) dir() [a, b] (Pdb) dir(textfile) [__doc__, __init__, __module__, grep, wordcount, ntfiles]

When you rst start up Python, various items are loaded. Lets see:
>>> dir() [__builtins__, __doc__, __name__] >>> dir(__builtins__) [ArithmeticError, AssertionError, AttributeError, DeprecationWarning, EOFError, Ellipsis, EnvironmentError, Exception, False, FloatingPointError, FutureWarning, IOError, ImportError, IndentationError, IndexError, KeyError, KeyboardInterrupt, LookupError, MemoryError, NameError, None,

51

NotImplemented, NotImplementedError, OSError, OverflowError, OverflowWarning, PendingDeprecationWarning, ReferenceError, RuntimeError, RuntimeWarning, StandardError, StopIteration, SyntaxError, SyntaxWarning, SystemError, SystemExit, TabError, True, TypeError, UnboundLocalError, UnicodeDecodeError, UnicodeEncodeError, UnicodeError, UnicodeTranslateError, UserWarning, ValueError, Warning, ZeroDivisionError, _, __debug__, __doc__, __import__, __name__, abs, apply, basestring, bool, buffer, callable, chr, classmethod, cmp, coerce, compile, complex, copyright, credits, delattr, dict, dir, divmod, enumerate, eval, execfile, exit, file, filter, float, frozenset, 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, reversed, round, set, setattr, slice, sorted, staticmethod, str, sum, super, tuple, type, unichr, unicode, vars, xrange, zip]

Well, there is a list of all the builtin functions and other attributes for you! Want to know what functions and other attributes are associated with dictionaries?
>>> dir(dict) [__class__, __cmp__, __contains__, __delattr__, __delitem__, __doc__, __eq__, __ge__, __getattribute__, __getitem__, __gt__, __hash__, __init__, __iter__, __le__, __len__, __lt__, __ne__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __setitem__, __str__, clear, copy, fromkeys, get, has_key, items, iteritems, iterkeys, itervalues, keys, pop, popitem, setdefault, update, values]

Suppose we want to nd out what methods and attributes are associated with strings. As mentioned in Section 7.2.3, strings are now a built-in class in Python, so we cant just type
>>> dir(string)

But we can use any string object:


>>> dir() [__add__, __class__, __contains__, __delattr__, __doc__, __eq__, __ge__, __getattribute__, __getitem__, __getnewargs__, __getslice__, __gt__, __hash__, __init__, __le__, __len__, __lt__, __mod__, __mul__, __ne__, __new__, __reduce__, __reduce_ex__, __repr__, __rmod__, __rmul__, __setattr__, __str__, capitalize, center, count, decode, encode, endswith, expandtabs, find, index, isalnum, isalpha, isdigit, islower, isspace, istitle, isupper, join, ljust, lower, lstrip, replace, rfind, rindex, rjust, rsplit, rstrip, split, splitlines, startswith, strip, swapcase, title, translate, upper, zfill]

B.2

The help() Function

For example, lets nd out about the pop() method for lists:

52

>>> help(list.pop) Help on method_descriptor: pop(...) L.pop([index]) -> item -- remove and return item at index (default last) (END)

And the center() method for strings:


>>> help(.center) Help on function center: center(s, width) center(s, width) -> string Return a center version of s, in a field of the specified width. padded with spaces as needed. The string is never truncated.

Hit q to exit the help pager. You can also get information by using pydoc at the Unix command line, e.g.
% pydoc string.center [...same as above]

B.3

PyDoc

The above methods of obtaining help were for use in Pythons interactive mode. Outside of that mode, in an OS shell, you can get the same information from PyDoc. For example,
pydoc sys

will give you all the information about the sys module. For modules outside the ordinary Python distribution, make sure they are in your Python search path, and be sure show the dot sequence, e.g.
pydoc u.v

Putting All Globals into a Class

As mentioned in Section 5, instead of using the keyword global, we may nd it clearer or more organized to group all our global variables into a class. Here, in the le tmeg.py, is how we would do this to modify the example in that section, tme.py:
1 2

# reads in the text file whose name is specified on the command line, # and reports the number of lines and words

53

3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

import sys def checkline(): glb.linecount += 1 w = glb.l.split() glb.wordcount += len(w) class glb: linecount = 0 wordcount = 0 l = [] f = open(sys.argv[1]) for glb.l in f.readlines(): checkline() print glb.linecount, glb.wordcount

Note that when the program is rst loaded, the class glb will be executed, even before main() starts.

Looking at the Python Virtual Machine

One can inspect the Python virtual machine code for a program. For the program srvr.py in http:// heather.cs.ucdavis.edu/matloff/Python/PyThreads.pdf, I once did the following: Running Python in interactive mode, I rst imported the module dis (disassembler). I then imported the program, by typing import srvr (I rst needed to add the usual if being imported.) I then ran
>>> dis.dis(srvr)

name == main code, so that the program wouldnt execute upon

How do you read the code? You can get a list of Python virtual machine instructions in Python: the Complete Reference, by Martin C. Brown, pub. by Osborne, 2001. But if you have background in assembly language, you can probably guess what the code is doing anyway.

54

You might also like