blob: e54359e79bebecdde521014d226f0be721b42a3c [file] [log] [blame]
Guido van Rossuma7925f11994-01-26 10:20:16 +00001Subject: FAQ: Python -- an object-oriented language
Guido van Rossum5333c5d1994-04-11 11:06:22 +00002Newsgroups: comp.lang.python,comp.answers,news.answers
3Followup-to: comp.lang.python
Guido van Rossuma7925f11994-01-26 10:20:16 +00004From: guido@cwi.nl (Guido van Rossum)
5Reply-to: guido@cwi.nl (Guido van Rossum)
6Approved: news-answers-request@MIT.Edu
7
8Archive-name: python-faq/part1
Guido van Rossumea2c6f71994-07-14 12:35:14 +00009Version: 1.10
10Last-modified: 14 July 1994
Guido van Rossuma7925f11994-01-26 10:20:16 +000011
12This article contains answers to Frequently Asked Questions about
13Python (an object-oriented interpreted programming language -- see
14the answer to question 1.1 for a short overview).
15
16Copyright 1993, 1994 Guido van Rossum. Unchanged electronic
17redistribution of this FAQ is allowed. Printed redistribution only
18with permission of the author. No warranties.
19
20Author's address:
21 Guido van Rossum
22 CWI, dept. CST
23 Kruislaan 413
24 P.O. Box 94079
25 1090 GB Amsterdam
26 The Netherlands
27Email: guido@cwi.nl
28
29The latest version of this FAQ is available by anonymous ftp from
Guido van Rossum7ce61c11994-06-13 15:13:56 +000030ftp.cwi.nl [192.16.191.128] in the directory /pub/python, with
Guido van Rossuma7925f11994-01-26 10:20:16 +000031filename python-FAQ. It will also be posted regularly to the
Guido van Rossum5333c5d1994-04-11 11:06:22 +000032newsgroups comp.answers and comp.lang.python.
Guido van Rossuma7925f11994-01-26 10:20:16 +000033
34Many FAQs, including this one, are available by anonymous ftp from
Guido van Rossumea2c6f71994-07-14 12:35:14 +000035rtfm.mit.edu [18.181.0.24] in the directory pub/usenet/news.answers.
Guido van Rossuma7925f11994-01-26 10:20:16 +000036The name under which a FAQ is archived appears in the Archive-name line
37at the top of the article. This FAQ is archived as python-faq/part1.
38
39There's a mail server on that machine which will send you files from
40the archive by e-mail if you have no ftp access. You send a e-mail
41message to mail-server@rtfm.mit.edu containing the single word help in
42the message body to receive instructions.
43
44This FAQ is divided in the following chapters:
45
46 1. General information and availability
47 2. Python in the real world
48 3. Building Python
49 4. Programming in Python
50 5. Extending Python
51 6. Python's design
52 7. Using Python on non-UNIX platforms
53
54To find the start of a particular chapter, search for the chapter number
55followed by a dot and a space at the beginning of a line (e.g. to
56find chapter 4 in vi, type /^4\. /).
57
58Here's an overview of the questions per chapter:
59
60 1. General information and availability
61 1.1. Q. What is Python?
62 1.2. Q. Why is it called Python?
63 1.3. Q. How do I obtain a copy of the Python source?
64 1.4. Q. How do I get documentation on Python?
Guido van Rossumc50158e1994-05-31 09:18:50 +000065 1.5. Q. Are there other ftp sites that mirror the Python distribution?
66 1.6. Q. Is there a newsgroup or mailing list devoted to Python?
67 1.7. Q. Is there a book on Python, or will there be one out soon?
68 1.8. Q. Are there any published articles about Python that I can quote?
69 1.9. Q. How does the Python version numbering scheme work?
70 1.10. Q. Are there other ftp sites that carry Python related material?
Guido van Rossum7ce61c11994-06-13 15:13:56 +000071 1.11. Q. Are there copyright restrictions on the use of Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +000072
73 2. Python in the real world
74 2.1. Q. How many people are using Python?
75 2.2. Q. Have any significant projects been done in Python?
76 2.3. Q. Are there any commercial projects going on using Python?
Guido van Rossum95f61a71994-01-26 17:23:37 +000077 2.4. Q. How stable is Python?
78 2.5. Q. What new developments are expected for Python in the future?
Guido van Rossuma7925f11994-01-26 10:20:16 +000079
80 3. Building Python
Guido van Rossum91f60831994-02-15 15:52:27 +000081 3.1. Q. Is there a test set?
82 3.2. Q. When running the test set, I get complaints about floating point
Guido van Rossuma7925f11994-01-26 10:20:16 +000083 operations, but when playing with floating point operations I cannot
84 find anything wrong with them.
Guido van Rossum91f60831994-02-15 15:52:27 +000085 3.3. Q. Link errors building Python with STDWIN on SGI IRIX.
86 3.4. Q. Link errors after rerunning the configure script.
87 3.5. Q. The python interpreter complains about options passed to a
Guido van Rossum24349991994-02-02 14:12:45 +000088 script (after the script name).
Guido van Rossum91f60831994-02-15 15:52:27 +000089 3.6. Q. When building on the SGI, make tries to run python to create
Guido van Rossum5333c5d1994-04-11 11:06:22 +000090 glmodule.c, but python hasn't been built or installed yet.
Guido van Rossum44b4cb11994-05-04 13:28:51 +000091 3.7. Q. Other trouble building Python 1.0.2 on platform X.
Guido van Rossuma7925f11994-01-26 10:20:16 +000092
93 4. Programming in Python
Guido van Rossum24349991994-02-02 14:12:45 +000094 4.1. Q. Is there a source code level debugger with breakpoints, step,
95 etc.?
96 4.2. Q. Can I create an object class with some methods implemented in
Guido van Rossuma7925f11994-01-26 10:20:16 +000097 C and others in Python (e.g. through inheritance)? (Also phrased as:
98 Can I use a built-in type as base class?)
Guido van Rossum24349991994-02-02 14:12:45 +000099 4.3. Q. Is there a curses/termcap package for Python?
100 4.4. Q. Is there an equivalent to C's onexit() in Python?
101 4.5. Q. When I define a function nested inside another function, the
Guido van Rossuma7925f11994-01-26 10:20:16 +0000102 nested function seemingly can't access the local variables of the
103 outer function. What is going on? How do I pass local data to a
104 nested function?
Guido van Rossum24349991994-02-02 14:12:45 +0000105 4.6. Q. How do I iterate over a sequence in reverse order?
106 4.7. Q. My program is too slow. How do I speed it up?
107 4.8. Q. When I have imported a module, then edit it, and import it
Guido van Rossuma7925f11994-01-26 10:20:16 +0000108 again (into the same Python process), the changes don't seem to take
109 place. What is going on?
Guido van Rossum24349991994-02-02 14:12:45 +0000110 4.9. Q. I have a module in which I want to execute some extra code when it
111 is run as a script. How do I find out whether I am running as a
112 script?
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000113 4.10. Q. I try to run a program from the Demo directory but it fails with
114 ImportError: No module named ...; what gives?
115 4.11. Q. What GUI toolkits exist for Python?
116 4.12. Q. Are there any interfaces to commercial database in Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000117
118 5. Extending Python
119 5.1. Q. Can I create my own functions in C?
120 5.2. Q. Can I create my own functions in C++?
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000121 5.3. Q. How can I execute arbitrary Python statements from C?
122 5.4. Q. How can I evaluate an arbitrary Python expression from C?
123 5.5. Q. How do I extract C values from a Python object?
124 5.6. Q. How do I use mkvalue() to create a tuple of arbitrary length?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000125
126 6. Python's design
127 6.1. Q. Why isn't there a generic copying operation for objects in
128 Python?
129 6.2. Q. Why isn't there a generic way to implement persistent objects
130 in Python? (Persistent == automatically saved to and restored from
131 disk.)
132 6.3. Q. Why isn't there a switch or case statement in Python?
Guido van Rossumc50158e1994-05-31 09:18:50 +0000133 6.4. Q. Why does Python use indentation for grouping of statements?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000134
135 7. Using Python on non-UNIX platforms
Guido van Rossum91f60831994-02-15 15:52:27 +0000136 7.1. Q. Is there a Mac version of Python?
137 7.2. Q. Is there a DOS version of Python?
138 7.3. Q. Is there a Windows version of Python?
139 7.4. Q. Is there a Windows NT version of Python?
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000140 7.5. Q. Is there an OS/2 version of Python?
141 7.6. Q. I have the PC version but it appears to be only a binary.
142 Where's the library?
143 7.7. Q. Where's the documentation for the Mac or PC version?
144 7.8. Q. The Mac (PC) version doesn't seem to have any facilities for
Guido van Rossum91f60831994-02-15 15:52:27 +0000145 creating or editing programs apart from entering it interactively, and
146 there seems to be no way to save code that was entered interactively.
147 How do I create a Python program on the Mac (PC)?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000148
149To find a particular question, search for the question number followed
150by a dot, a space, and a Q at the beginning of a line (e.g. to find
151question 4.2 in vi, type /^4\.2\. Q/).
152
153
1541. General information and availability
155=======================================
156
1571.1. Q. What is Python?
158
159A. Python is an interpreted, interactive, object-oriented programming
160language. It incorporates modules, exceptions, dynamic typing, very
161high level dynamic data types, and classes. Python combines
162remarkable power with very clear syntax. It has interfaces to many
163system calls and libraries, as well as to various window systems, and
164is extensible in C or C++. It is also usable as an extension language
165for applications that need a programmable interface. Finally, Python
166is portable: it runs on many brands of UNIX, on the Mac, and on
167MS-DOS.
168
169To find out more, the best thing to do is to start reading the
170tutorial from the documentation set (see a few questions further
171down).
172
1731.2. Q. Why is it called Python?
174
175A. Apart from being a computer wizard, I'm also a fan of "Monty
176Python's Flying Circus" (a BBC comedy series from the seventies, in
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000177the -- unlikely -- case you didn't know). It occurred to me one day
178that I needed a name that was short, unique, and slightly mysterious.
179And I happened to be reading some scripts from the series at the
180time... So then I decided to call my language Python. But Python is
181not a joke. And don't you associate it with dangerous reptiles
182either!
Guido van Rossuma7925f11994-01-26 10:20:16 +0000183
1841.3. Q. How do I obtain a copy of the Python source?
185
186A. The latest Python source distribution is always available by
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000187anonymous ftp from ftp.cwi.nl [192.16.191.128] in the directory
Guido van Rossum44b4cb11994-05-04 13:28:51 +0000188/pub/python, with filename python<version>.tar.gz. (Old versions may
189have an extension of .Z, indicating use of "compress" compression.)
190It is a gzip'ed tar file containing the complete C source, LaTeX
191documentation, Python library modules, example programs, and several
192useful pieces of freely distributable software. This will compile and
193run out of the box on most UNIX platforms. At the time of writing,
194<version> is 1.0.2. (See section 7 for non-UNIX information.)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000195
1961.4. Q. How do I get documentation on Python?
197
198A. The latest Python documentation set is always available by
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000199anonymous ftp from ftp.cwi.nl [192.16.191.128] in the directory
Guido van Rossum44b4cb11994-05-04 13:28:51 +0000200/pub/python, with filename pythondoc-ps<version>.tar.gz. It is a
201gzip'ed tar file containing PostScript files of the reference manual,
202the library manual, and the tutorial. At the time of writing
203<version> is 1.0.2. Note that the library manual is the most
204important one of the set, as much of Python's power stems from the
205standard or built-in types, functions and modules, all of which are
206described here. PostScript for a high-level description of Python is
207in the file nluug-paper.ps.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000208
Guido van Rossumc50158e1994-05-31 09:18:50 +00002091.5. Q. Are there other ftp sites that mirror the Python distribution?
210
211A. The following sites keep mirrors of the Python distribution:
Guido van Rossuma7925f11994-01-26 10:20:16 +0000212
213Site IP address Directory
214
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000215gatekeeper.dec.com 16.1.0.2 /pub/plan/python
Guido van Rossuma7925f11994-01-26 10:20:16 +0000216ftp.uu.net 192.48.96.9 /languages/python
217ftp.wustl.edu 128.252.135.4 /graphics/graphics/sgi-stuff/python
Guido van Rossumc50158e1994-05-31 09:18:50 +0000218ftp.funet.fi 128.214.6.100 /pub/languages/python
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000219ftp.fu-berlin.de 130.133.4.50 /pub/unix/languages/python (*python* only)
Guido van Rossumc50158e1994-05-31 09:18:50 +0000220ftp.sunet.se 130.238.127.3 /pub/lang/python
Guido van Rossumea2c6f71994-07-14 12:35:14 +0000221unix.hensa.ac.uk 129.12.43.16 /uunet/languages/python
Guido van Rossuma7925f11994-01-26 10:20:16 +0000222
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000223Or try archie on e.g. python1.0 to locate the nearest copy of that
224version...
Guido van Rossuma7925f11994-01-26 10:20:16 +0000225
Guido van Rossumc50158e1994-05-31 09:18:50 +00002261.6. Q. Is there a newsgroup or mailing list devoted to Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000227
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000228A. There is a newsgroup, comp.lang.python, and a mailing list. The
229newsgroup and mailing list are gatewayed into each other -- if you can
230read news it is not necessary to subscribe to the mailing list. Send
231e-mail to python-list-request@cwi.nl to (un)subscribe to the mailing
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000232list. Once you're on, send e-mail tp python-list@cwi.nl to send mail
233to the entire mailing list and newsgroup.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000234
Guido van Rossumc50158e1994-05-31 09:18:50 +00002351.7. Q. Is there a book on Python, or will there be one out soon?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000236
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000237A. Unfortunately, not yet. Mark Lutz and I are planning to write one,
238but we are still a in very preliminary stage. If you would like to
239beat us at it and get rich from book royalties, go ahead!
Guido van Rossuma7925f11994-01-26 10:20:16 +0000240
Guido van Rossumc50158e1994-05-31 09:18:50 +00002411.8. Q. Are there any published articles about Python that I can quote?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000242
243A. So far the only refereed and published article that describes
244Python in some detail is:
245
246 Guido van Rossum and Jelke de Boer, "Interactively Testing Remote
247 Servers Using the Python Programming Language", CWI Quarterly, Volume
248 4, Issue 4 (December 1991), Amsterdam, pp 283-303.
249
250LaTeX source for this paper is available as part of the Python source
251distribution.
252
253A more recent high-level description of Python is:
254
255 Guido van Rossum, "An Introduction to Python for UNIX/C
256 Programmers", in the proceedings of the NLUUG najaarsconferentie
257 1993 (dutch UNIX users group meeting november 1993).
258
259PostScript for this paper and for the slides used for the accompanying
260presentation can be found in the ftp directory mentioned a few
261questions earlier, with filenames nluug-paper.ps and nluug-slides.ps,
262respectively.
263
Guido van Rossumc50158e1994-05-31 09:18:50 +00002641.9. Q. How does the Python version numbering scheme work?
Guido van Rossum95f61a71994-01-26 17:23:37 +0000265
266A. Python versions are numbered A.B.C. A is the major version number
267-- it is only incremented for major changes in functionality or source
268structure. B is the minor version number, incremented for less
269earth-shattering changes to a release. C is the patchlevel -- it is
270incremented for each new release. Note that in the past, patches have
271added significant changes; in fact the changeover from 0.9.9 to 1.0.0
272was the first time that either A or B changed!
273
Guido van Rossumc50158e1994-05-31 09:18:50 +00002741.10. Q. Are there other ftp sites that carry Python related material?
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000275
276A. An interesting ftp site for Python users is ftp.markv.com
277(192.122.251.1); the directory pub/python contains a growing
278collection of interesting Python scripts. To submit a script for
279inclusion, place it together with a readme file (with extension
280.readme) in the publicly writable directory /incoming/python. This
281service is maintained by Lance Ellinghouse <lance@markv.com>.
282
Guido van Rossum7ce61c11994-06-13 15:13:56 +00002831.11. Q. Are there copyright restrictions on the use of Python?
284
285A. Hardly. You can do anything you want with the source, as long as
286you leave the copyrights in, display those copyrights in any
287documentation about Python that you produce, don't use the author's
288institute's name in publicity without prior written permission, and
289don't hold them responsible for anything (read the actual copyright
290for a precise legal wording).
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000291
Guido van Rossuma7925f11994-01-26 10:20:16 +0000292
2932. Python in the real world
294===========================
295
2962.1. Q. How many people are using Python?
297
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000298A. I don't know, but the maximum number of simultaneous subscriptions
299to the Python mailing list before it was gatewayed into the newsgroup
300was about 180 (several of which were local redistribution lists). I
301believe that many active Python users don't bother to subscribe to the
302list, and now that there's a newsgroup the mailing list subscription
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000303is even less meaningful. I see new names on the newsgroup all the
304time and my best guess is that there are currently at least several
305thousands of users.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000306
3072.2. Q. Have any significant projects been done in Python?
308
309A. Here at CWI (the home of Python), we have written a 20,000 line
310authoring environment for transportable hypermedia presentations, a
Guido van Rossum5333c5d1994-04-11 11:06:22 +00003115,000 line multimedia teleconferencing tool, as well as many many
312smaller programs.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000313
314The University of Virginia uses Python to control a virtual reality
315engine. Contact: Matt Conway <conway@virginia.edu>.
316
317See also the next question.
318
3192.3. Q. Are there any commercial projects going on using Python?
320
321A. Several companies have revealed to me that they are planning or
322considering to use Python in a future product. The furthest is
323Sunrise Software, who already have a product out using Python -- they
324use Python for a GUI management application and an SNMP network
325manangement application. Contact: <info@sunrise.com>.
326
327Individuals at many other companies are using Python for
328internal development (witness their contributions to the Python
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000329mailing list or newsgroup).
Guido van Rossuma7925f11994-01-26 10:20:16 +0000330
331Python has also been elected as an extension language by MADE, a
332consortium supported by the European Committee's ESPRIT program and
333consisting of Bull, CWI and some other European companies. Contact:
334Ivan Herman <ivan@cwi.nl>.
335
Guido van Rossum95f61a71994-01-26 17:23:37 +00003362.4. Q. How stable is Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000337
Guido van Rossum44b4cb11994-05-04 13:28:51 +0000338A. Very stable. While the current version number (1.0.2) would
Guido van Rossuma7925f11994-01-26 10:20:16 +0000339suggest it is in the early stages of development, in fact new, stable
Guido van Rossum95f61a71994-01-26 17:23:37 +0000340releases (numbered 0.9.x) have been coming out roughly every 3 to 6
341months for the past four years.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000342
Guido van Rossum95f61a71994-01-26 17:23:37 +00003432.5. Q. What new developments are expected for Python in the future?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000344
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000345A. A proposal is being worked out to change the semantics of operator
346overloading (__add__, __mul__ etc.) to make them more useful for
347implementing types that don't resemble numbers. Additions will be
348__call__ (to call an instance as if it were a function) and __eq__,
349_lt__ etc. (to override individual comparisons). A (new) pthreads
350interface has been submitted which will be integrated in the next
351release. The X11/Motif interface will be improved. There are ideas
352about built-in help using strings placed into function objects, and
353possibly a switch statement.
354
355There will be better ports to the Mac, DOS, Windows, Windows NT, and
356OS/2. The Windows ports will support dynalically loaded modules using
357DLLs. The Mac port may also support dynamic loading of modules.
358There may be a Windows version of STDWIN.
359
360Also planned is improved support for embedding Python in other
361applications, e.g. by renaming most global symbols to have a "Py"
362prefix and providing more documentation and threading support.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000363
364
3653. Building Python
366==================
367
Guido van Rossum91f60831994-02-15 15:52:27 +00003683.1. Q. Is there a test set?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000369
370A. Yes, simply do "import testall" (or "import autotest" if you aren't
371interested in the output). The standard modules whose name begins
372with "test" together comprise the test. The test set doesn't test
373*all* features of Python but it goes a long way to confirm that a new
374port is actually working. The Makefile contains an entry "make test"
375which runs the autotest module.
376
Guido van Rossum91f60831994-02-15 15:52:27 +00003773.2. Q. When running the test set, I get complaints about floating point
Guido van Rossuma7925f11994-01-26 10:20:16 +0000378operations, but when playing with floating point operations I cannot
379find anything wrong with them.
380
381A. The test set makes occasional unwarranted assumptions about the
382semantics of C floating point operations. Until someone donates a
383better floating point test set, you will have to comment out the
384offending floating point tests and execute similar tests manually.
385
Guido van Rossum91f60831994-02-15 15:52:27 +00003863.3. Q. Link errors building Python with STDWIN on SGI IRIX.
Guido van Rossum24349991994-02-02 14:12:45 +0000387
388A. Rebuild STDWIN, specifying "CC=cc -cckr" in the Makefile.
389
Guido van Rossum91f60831994-02-15 15:52:27 +00003903.4. Q. Link errors after rerunning the configure script.
Guido van Rossum24349991994-02-02 14:12:45 +0000391
392A. It is generally necessary to run "make clean" after a configuration
393change.
394
Guido van Rossum91f60831994-02-15 15:52:27 +00003953.5. Q. The python interpreter complains about options passed to a
Guido van Rossum24349991994-02-02 14:12:45 +0000396script (after the script name).
397
398A. You are probably linking with GNU getopt, e.g. through -liberty.
399Don't. (If you are using this because you link with -lreadline, use
400the readline distributed with Python instead.)
401
Guido van Rossum91f60831994-02-15 15:52:27 +00004023.6. Q. When building on the SGI, make tries to run python to create
Guido van Rossum24349991994-02-02 14:12:45 +0000403glmodule.c, but python hasn't been built or installed yet.
404
405A. Comment out the line mentioning glmodule.c in Setup and build a
406python without gl first; install it or make sure it is in your $PATH,
407then edit the Setup file again to turn on the gl module, and make
408again. You don't need to do "make clean"; you do need to run "make
409Makefile" in the Modules subdirectory (or just run "make" at the
410toplevel).
411
Guido van Rossum44b4cb11994-05-04 13:28:51 +00004123.7. Q. Other trouble building Python 1.0.2 on platform X.
Guido van Rossum95f61a71994-01-26 17:23:37 +0000413
414A. Please email the details to <guido@cwi.nl> and I'll look into it.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000415
416
4174. Programming in Python
418========================
419
Guido van Rossum24349991994-02-02 14:12:45 +00004204.1. Q. Is there a source code level debugger with breakpoints, step,
421etc.?
422
423A. Yes. Check out module pdb; pdb.help() prints the documentation (or
424you can read it as Lib/pdb.doc). If you use the STDWIN option,
425there's also a windowing interface, wdb. You can write your own
426debugger by using the code for pdb or wdb as an example.
427
4284.2. Q. Can I create an object class with some methods implemented in
Guido van Rossuma7925f11994-01-26 10:20:16 +0000429C and others in Python (e.g. through inheritance)? (Also phrased as:
430Can I use a built-in type as base class?)
431
432A. No, but you can easily create a Python class which serves as a
433wrapper around a built-in object, e.g. (for dictionaries):
434
435 # A user-defined class behaving almost identical
436 # to a built-in dictionary.
437 class UserDict:
438 def __init__(self): self.data = {}
439 def __repr__(self): return repr(self.data)
440 def __cmp__(self, dict):
441 if type(dict) == type(self.data):
442 return cmp(self.data, dict)
443 else:
444 return cmp(self.data, dict.data)
445 def __len__(self): return len(self.data)
446 def __getitem__(self, key): return self.data[key]
447 def __setitem__(self, key, item): self.data[key] = item
448 def __delitem__(self, key): del self.data[key]
449 def keys(self): return self.data.keys()
450 def items(self): return self.data.items()
451 def values(self): return self.data.values()
452 def has_key(self, key): return self.data.has_key(key)
453
Guido van Rossum24349991994-02-02 14:12:45 +00004544.3. Q. Is there a curses/termcap package for Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000455
456A. No, but you can use the "alfa" (== character cell) version of
457STDWIN. (STDWIN == Standard Windows, a portable windowing system
Guido van Rossum91f60831994-02-15 15:52:27 +0000458interface by the same author, URL: ftp://ftp.cwi.nl/pub/stdwin.)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000459This will also prepare your program for porting to windowing
460environments such as X11 or the Macintosh.
461
Guido van Rossum24349991994-02-02 14:12:45 +00004624.4. Q. Is there an equivalent to C's onexit() in Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000463
464A. Yes, if you import sys and assign a function to sys.exitfunc, it
465will be called when your program exits, is killed by an unhandled
466exception, or (on UNIX) receives a SIGHUP or SIGTERM signal.
467
Guido van Rossum24349991994-02-02 14:12:45 +00004684.5. Q. When I define a function nested inside another function, the
Guido van Rossuma7925f11994-01-26 10:20:16 +0000469nested function seemingly can't access the local variables of the
470outer function. What is going on? How do I pass local data to a
471nested function?
472
473A. Python does not have arbitrarily nested scopes. When you need to
474create a function that needs to access some data which you have
475available locally, create a new class to hold the data and return a
476method of an instance of that class, e.g.:
477
478 class MultiplierClass:
479 def __init__(self, factor):
480 self.factor = factor
481 def multiplier(self, argument):
482 return argument * self.factor
483
484 def generate_multiplier(factor):
485 return MultiplierClass(factor).multiplier
486
487 twice = generate_multiplier(2)
488 print twice(10)
489 # Output: 20
490
Guido van Rossum24349991994-02-02 14:12:45 +00004914.6. Q. How do I iterate over a sequence in reverse order?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000492
493A. If it is a list, the fastest solution is
494
495 list.reverse()
496 try:
497 for x in list:
498 "do something with x"
499 finally:
500 list.reverse()
501
502This has the disadvantage that while you are in the loop, the list
503is temporarily reversed. If you don't like this, you can make a copy.
504This appears expensive but is actually faster than other solutions:
505
506 rev = list[:]
507 rev.reverse()
508 for x in rev:
509 <do something with x>
510
511If it isn't a list, a more general but slower solution is:
512
513 i = len(list)
514 while i > 0:
515 i = i-1
516 x = list[i]
517 <do something with x>
518
519A more elegant solution, is to define a class which acts as a sequence
520and yields the elements in reverse order (solution due to Steve
521Majewski):
522
523 class Rev:
524 def __init__(self, seq):
525 self.forw = seq
526 def __len__(self):
527 return len(self.forw)
528 def __getitem__(self, i):
529 return self.forw[-(i + 1)]
530
531You can now simply write:
532
533 for x in Rev(list):
534 <do something with x>
535
536Unfortunately, this solution is slowest of all, due the the method
537call overhead...
538
Guido van Rossum24349991994-02-02 14:12:45 +00005394.7. Q. My program is too slow. How do I speed it up?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000540
541A. That's a tough one, in general. There are many tricks to speed up
542Python code; I would consider rewriting parts in C only as a last
543resort. One thing to notice is that function and (especially) method
544calls are rather expensive; if you have designed a purely OO interface
545with lots of tiny functions that don't do much more than get or set an
546instance variable or call another method, you may consider using a
547more direct way, e.g. directly accessing instance variables. Also see
548the standard module "profile" (described in the file
549"python/lib/profile.doc") which makes it possible to find out where
550your program is spending most of its time (if you have some patience
551-- the profiling itself can slow your program down by an order of
552magnitude).
553
Guido van Rossum24349991994-02-02 14:12:45 +00005544.8. Q. When I have imported a module, then edit it, and import it
Guido van Rossuma7925f11994-01-26 10:20:16 +0000555again (into the same Python process), the changes don't seem to take
556place. What is going on?
557
558A. For efficiency reasons, Python only reads the module file on the
559first time a module is imported (otherwise a program consisting of
560many modules, each of which imports the same basic module, would read
561the basic module over and over again). To force a changed module
562being read again, do this:
563
564 import modname
565 reload(modname)
566
567Warning: this technique is not 100% fool-proof. In particular,
568modules containing statements like
569
570 from modname import some_objects
571
572will continue to work with the old version of the objects imported
573thus.
574
Guido van Rossum24349991994-02-02 14:12:45 +00005754.9. Q. I have a module in which I want to execute some extra code when it
576is run as a script. How do I find out whether I am running as a
577script?
578
579A. A module can find out its own module name by alooking at the
580(predefined) global variable __name__. If this has the value
581'__main__' you are running as a script. E.g. if you put the following
582on the last line of your module, main() is called only when your
583module is running as a script:
584
585 if __name__ == '__main__': main()
586
Guido van Rossum7ce61c11994-06-13 15:13:56 +00005874.10. Q. I try to run a program from the Demo directory but it fails with
588ImportError: No module named ...; what gives?
589
590A. This is probably an optional module which hasn't been configured on
591your system. This especially happens with modules like "stdwin",
592"gl", "Xt" or "Xm". For stdwin and many other modules, see
593Modules/Setup.in for info on how to add these modules to your Python,
594if it is possible at all. Sometimes you will have to ftp and build
595another package first (e.g. stdwin). Sometimes the module only works
596on specific platforms (e.g. gl only works on SGI machines). For
597X-related modules (Xt and Xm) you will have to do more work: they are
598currently not part of the standard Python distribution. You will have
599to ftp the file "extensions.tar.gz" file from a Python ftp repository
600(e.g. ftp.cwi.nl) and follow the instructions there. Note: the X
601related modules are still somewhat flakey, so don't try this unless
602you know a bit or two about building X applications on your platform.
603
6044.11. Q. What GUI toolkits exist for Python?
605
606A. Depending on what platform(s) you are aiming at, there are several.
607
608- The standard Python distribution comes with an interface to STDWIN,
609a platform-independent low-level windowing interface (you have to ftp
610the source for stdwin separately, e.g. from ftp.cwi.nl in pub/stdwin
611or gatekeeper.dec.com in pub/misc/stdwin). STDWIN runs under X11 or
612the Mac; a Windows port is about to be released.
613
614- For SGI only, there's an interface to the complete GL (Graphics
615Library -- low level but very good 3D capabilities) as well as to
616FORMS (a buttons-and-sliders-etc package built on top of GL by Mark
617Overmars -- ftp'able from ftp.cs.ruu.nl in pub/SGI/FORMS).
618
619- There's an interface to X11, including the Athena and Motif widget
620sets (and a few individual widgets, like Mosaic's HTML widget and
621SGI's GL widget) in the Extensions set, which is separately ftp'able
622from ftp.cwi.nl as pub/python/extensions.tar.gz.
623
624- There's an interface to SUIT, the U of Virginia's Simple User
625Interface Toolkit; it can be ftp'ed from uvacs.cs.virginia.edu as
626pub/suit/python/SUIT_python.tar.Z. A PC binary of Python 1.0.2
627compiled with DJGPP and with SUIT support built-in has been made
628available by Antonio Costa on ftp site asterix.inescn.pt, directory
629pub/PC/python, file pyt102su.exe (a self-extracting archive).
630
631- There's a neat interface to the Tcl/Tk widget set; ftp it from
632ftp.cwi.nl as pub/python/tkinter.tar.gz.
633
634- There's an interface to WAFE, a Tcl interface to the X11 Motif and
635Athena widget sets. Last I heard about it it was included in the
636WAFE 1.0 prerelease, ftp'able from ftp.wu-wien.ac.at as
637pub/src/X11/wafe/wafe-1.0.tar.gz-prerelease.
638
6394.12. Q. Are there any interfaces to commercial database in Python?
640
641A. There's an interface to SYBASE by John Redford
642<jredford@lehman.com>.
643
644There's also an interface to metalbase by Lance Ellinghouse
645<lance@markv.com>.
646
Guido van Rossuma7925f11994-01-26 10:20:16 +0000647
6485. Extending Python
649===================
650
6515.1. Q. Can I create my own functions in C?
652
653A. Yes, you can create built-in modules containing functions,
Guido van Rossum24349991994-02-02 14:12:45 +0000654variables, exceptions and even new types in C. This is explained in
655the document "Extending and Embedding the Python Interpreter" (the
656LaTeX file Doc/ext.tex). Also read the chapter on dynamic loading.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000657
6585.2. Q. Can I create my own functions in C++?
659
660A. Yes, using the C-compatibility features found in C++. Basically
661you place extern "C" { ... } around the Python include files and put
662extern "C" before each function that is going to be called by the
663Python interpreter. Global or static C++ objects with constructors
664are probably not a good idea.
665
Guido van Rossum7ce61c11994-06-13 15:13:56 +00006665.3. Q. How can I execute arbitrary Python statements from C?
667
668A. The highest-level function to do this is run_command() which takes
669a single string argument which is executed in the context of module
670__main__ and returns 0 for success and -1 when an exception occurred
671(including SyntaxError). If you want more control, use run_string();
672see the source for run_command() in Python/pythonrun.c.
673
6745.4. Q. How can I evaluate an arbitrary Python expression from C?
675
676A. Call the function run_string() from the previous question with the
677start symbol eval_input; it then parses an expression, evaluates it
678and returns its value. See exec_eval() in Python/bltinmodule.c.
679
6805.5. Q. How do I extract C values from a Python object?
681
682A. That depends on the object's type. If it's a tuple,
683gettuplesize(o) returns its length and gettupleitem(o, i) returns its
684i'th item; similar for lists with getlistsize(o) and getlistitem(o,
685i). For strings, getstringsize(o) returns its length and
686getstringvalue(o) a pointer to its value (note that Python strings may
687contain null bytes so strlen() is not safe). To test which type an
688object is, first make sure it isn't NULL, and then use
689is_stringobject(o), is_tupleobject(o), is_listobject(o) etc.
690
6915.6. Q. How do I use mkvalue() to create a tuple of arbitrary length?
692
693A. You can't. Use t = newtupleobject(n) instead, and fill it with
694objects using settupleitem(t, i, o) -- note that this "eats" a
695reference count of o. Similar for lists with newlistobject(n) and
696setlistitem(l, i, o).
697
Guido van Rossuma7925f11994-01-26 10:20:16 +0000698
6996. Python's design
700==================
701
7026.1. Q. Why isn't there a generic copying operation for objects in
703Python?
704
705A. Hmm. Maybe there should be one, but it's difficult to assign a
706useful meaning to copying of open files, sockets and windows, or
707recursive data structures. As long as you design all your classes
708yourself you are of course free to define a standard base class that
709defines an overridable copying operation for all the objects you care
710about. (One practical point: it would have to be a built-in function,
711not a standard method name, since not all built-in object types have
712methods; e.g. strings, integers and tuples don't.)
713
7146.2. Q. Why isn't there a generic way to implement persistent objects
715in Python? (Persistent == automatically saved to and restored from
716disk.)
717
718A. Hmm, hmm. Basically for the same reasons as why there is no
719generic copying operation.
720
7216.3. Q. Why isn't there a switch or case statement in Python?
722
723A. You can do this easily enough with a sequence of
724if... elif... elif... else. There have been some proposals for switch
725statement syntax, but there is no concensus (yet) on whether and how
726to do range tests.
727
Guido van Rossumc50158e1994-05-31 09:18:50 +00007286.4. Q. Why does Python use indentation for grouping of statements?
729
730A. Basically I believe that using indentation for grouping is
731extremely elegant and contributes a lot to the clarity of the average
732Python program. Most people learn to love this feature after a while.
733Some arguments for it:
734
735- Since there are no begin/end brackets there cannot be a disagreement
736between grouping perceived by the parser and the human reader. I
737remember long ago seeing a C fragment like this:
738
739 if (x <= y)
740 x++;
741 y--;
742 z++;
743
744and staring a long time at it wondering why y was being decremented
745even for x > y... (And I wasn't a C newbie then either.)
746
747- Since there are no begin/end brackets there can be no conflicting
748coding styles. In C there are loads of different ways to place the
749braces (including the choice whether to place braces around single
750statements in certain cases, for consistency). If you're used to
751reading (and writing) code that uses one style, you will feel at least
752slightly uneasy when reading (or being required to write) another
753style.
754
755- Many coding styles place begin/end brackets on a line by themself.
756This makes programs considerably longer and wastes valuable screen
757space, making it harder to get a good overview over a program.
758Ideally, a function should fit on one basic tty screen (say, 20
759lines). 20 lines of Python are worth a LOT more than 20 lines of C.
760This is not solely due to the lack of begin/end brackets (the lack of
761declarations also helps, and the powerful operations of course), but
762it certainly helps!
763
Guido van Rossuma7925f11994-01-26 10:20:16 +0000764
7657. Using Python on non-UNIX platforms
766=====================================
767
Guido van Rossum91f60831994-02-15 15:52:27 +00007687.1. Q. Is there a Mac version of Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000769
Guido van Rossum91f60831994-02-15 15:52:27 +0000770A. Yes. It is on most ftp sites carrying Python as python.sea.hqx --
771this is a self-extracting archive containing the application binary as
772well as the Lib modules.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000773
Guido van Rossum91f60831994-02-15 15:52:27 +00007747.2. Q. Is there a DOS version of Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000775
Guido van Rossum91f60831994-02-15 15:52:27 +0000776A. Yes. More than one, actually: 16python.exe runs in standard DOS
777mode on 186 CPUs or higher; 32python.exe uses a DOS extender and only
778runs on a 386 or higher CPUs. Although 16python.exe does not pass the
779test set because test_grammar is too big for the parser, it actually
780has about 270 kbyte of allocatable heap space, which is sufficient for
781fairly large programs. 32python.exe is distributed as a tar file
782containing the required DOS extended and 387 emulator. Both are on
783most ftp sites carrying Python.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000784
Guido van Rossum91f60831994-02-15 15:52:27 +00007857.3. Q. Is there a Windows version of Python?
786
787A. Yes. Use qwpython.exe. The only problem with it: ^C
788unconditionally kills the entire program -- it does not raise
789KeyboardInterrupt. You can also run 16python.exe or 32python.exe in a
790"DOS box", but qwpython.exe appears to be slightly faster.
791
7927.4. Q. Is there a Windows NT version of Python?
793
794A. Yes. Use ntpython.exe. This is for Intel CPUs. If you want a
795Windows user interface, use qwpython.exe.
796
Guido van Rossum7ce61c11994-06-13 15:13:56 +00007977.5. Q. Is there an OS/2 version of Python?
798
799A. Yes. You can ftp it (from ftp.cwi.nl in pub/python, or from the
800mirror sites) as pyth_os2.zip. This contains both an executable and
801Makefiles for those fortunate enough to have a C compiler.
802
8037.6. Q. I have the PC version but it appears to be only a binary.
804Where's the library?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000805
806A. You still need to copy the files from the distribution directory
Guido van Rossum91f60831994-02-15 15:52:27 +0000807"python/Lib" to your system. If you don't have the full distribution,
Guido van Rossum44b4cb11994-05-04 13:28:51 +0000808you can get the file pythonlib1.0.2.tar.gz from most ftp sites carrying
Guido van Rossum91f60831994-02-15 15:52:27 +0000809Python; this is a subset of the distribution containing just those
810file.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000811
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000812Once you have installed the library, you need to point sys.path to it.
813Assuming the library is in C:\misc\python\lib, the following commands
814will point your Python interpreter to it (note the doubled backslashes
815-- you can also use single forward slashes instead):
816
817 >>> import sys
818 >>> sys.path.insert(0, 'C:\\misc\\python\\lib')
819 >>>
820
821For a more permanent effect, set the environment variable PYTHONPATH,
822as follows (talking to a DOS prompt):
823
824 C> SET PYTHONPATH=C:\misc\python\lib
825
Guido van Rossum7ce61c11994-06-13 15:13:56 +00008267.7. Q. Where's the documentation for the Mac or PC version?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000827
828A. There isn't any. The documentation for the Unix version also
Guido van Rossum91f60831994-02-15 15:52:27 +0000829applies to the Mac and PC versions. Where applicable, differences
Guido van Rossuma7925f11994-01-26 10:20:16 +0000830are indicated in the text.
831
Guido van Rossum7ce61c11994-06-13 15:13:56 +00008327.8. Q. The Mac (PC) version doesn't seem to have any facilities for
Guido van Rossum91f60831994-02-15 15:52:27 +0000833creating or editing programs apart from entering it interactively, and
834there seems to be no way to save code that was entered interactively.
835How do I create a Python program on the Mac (PC)?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000836
Guido van Rossum91f60831994-02-15 15:52:27 +0000837A. Use an external editor. On the Mac, I am quite happy with the Desk
838Accessory called Sigma Edit; this doesn't require Multifinder or
839System 7. I work like this: start the interpreter; edit a module file
840using Sigma Edit; import and test it in the interpreter; edit again in
841Sigma Edit; then use the built-in function reload() to re-read the
842imported module; etc.
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000843
844Regarding the same question for the PC, Kurt Wm. Hemr writes: "While
845anyone with a pulse could certainly figure out how to do the same on
846MS-Windows, I would recommend the NotGNU Emacs clone for MS-Windows.
847Not only can you easily resave and "reload()" from Python after making
848changes, but since WinNot auto-copies to the clipboard any text you
849select, you can simply select the entire procedure (function) which
850you changed in WinNot, switch to QWPython, and shift-ins to reenter
851the changed program unit."