blob: d7c735f56ac3c8d7ff2c0420eeed53e42bd0123e [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 Rossum5333c5d1994-04-11 11:06:22 +00009Version: 1.6
10Last-modified: 11 April 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
30ftp.cwi.nl [192.16.184.180] in the directory /pub/python, with
31filename 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
35rtfm.mit.edu [18.70.0.209] in the directory pub/usenet/news.answers.
36The 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?
65 1.5. Q. Is there a newsgroup or mailing list devoted to Python?
66 1.6. Q. Is there a book on Python, or will there be one out soon?
67 1.7. Q. Are there any published articles about Python that I can quote?
Guido van Rossum95f61a71994-01-26 17:23:37 +000068 1.8. Q. How does the Python version numbering scheme work?
Guido van Rossum5333c5d1994-04-11 11:06:22 +000069 1.9. Q. Are there other ftp sites that carry Python related material?
Guido van Rossuma7925f11994-01-26 10:20:16 +000070
71 2. Python in the real world
72 2.1. Q. How many people are using Python?
73 2.2. Q. Have any significant projects been done in Python?
74 2.3. Q. Are there any commercial projects going on using Python?
Guido van Rossum95f61a71994-01-26 17:23:37 +000075 2.4. Q. How stable is Python?
76 2.5. Q. What new developments are expected for Python in the future?
Guido van Rossuma7925f11994-01-26 10:20:16 +000077
78 3. Building Python
Guido van Rossum91f60831994-02-15 15:52:27 +000079 3.1. Q. Is there a test set?
80 3.2. Q. When running the test set, I get complaints about floating point
Guido van Rossuma7925f11994-01-26 10:20:16 +000081 operations, but when playing with floating point operations I cannot
82 find anything wrong with them.
Guido van Rossum91f60831994-02-15 15:52:27 +000083 3.3. Q. Link errors building Python with STDWIN on SGI IRIX.
84 3.4. Q. Link errors after rerunning the configure script.
85 3.5. Q. The python interpreter complains about options passed to a
Guido van Rossum24349991994-02-02 14:12:45 +000086 script (after the script name).
Guido van Rossum91f60831994-02-15 15:52:27 +000087 3.6. Q. When building on the SGI, make tries to run python to create
Guido van Rossum5333c5d1994-04-11 11:06:22 +000088 glmodule.c, but python hasn't been built or installed yet.
Guido van Rossum91f60831994-02-15 15:52:27 +000089 3.7. Q. Other trouble building Python 1.0.1 on platform X.
Guido van Rossuma7925f11994-01-26 10:20:16 +000090
91 4. Programming in Python
Guido van Rossum24349991994-02-02 14:12:45 +000092 4.1. Q. Is there a source code level debugger with breakpoints, step,
93 etc.?
94 4.2. Q. Can I create an object class with some methods implemented in
Guido van Rossuma7925f11994-01-26 10:20:16 +000095 C and others in Python (e.g. through inheritance)? (Also phrased as:
96 Can I use a built-in type as base class?)
Guido van Rossum24349991994-02-02 14:12:45 +000097 4.3. Q. Is there a curses/termcap package for Python?
98 4.4. Q. Is there an equivalent to C's onexit() in Python?
99 4.5. Q. When I define a function nested inside another function, the
Guido van Rossuma7925f11994-01-26 10:20:16 +0000100 nested function seemingly can't access the local variables of the
101 outer function. What is going on? How do I pass local data to a
102 nested function?
Guido van Rossum24349991994-02-02 14:12:45 +0000103 4.6. Q. How do I iterate over a sequence in reverse order?
104 4.7. Q. My program is too slow. How do I speed it up?
105 4.8. Q. When I have imported a module, then edit it, and import it
Guido van Rossuma7925f11994-01-26 10:20:16 +0000106 again (into the same Python process), the changes don't seem to take
107 place. What is going on?
Guido van Rossum24349991994-02-02 14:12:45 +0000108 4.9. Q. I have a module in which I want to execute some extra code when it
109 is run as a script. How do I find out whether I am running as a
110 script?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000111
112 5. Extending Python
113 5.1. Q. Can I create my own functions in C?
114 5.2. Q. Can I create my own functions in C++?
115
116 6. Python's design
117 6.1. Q. Why isn't there a generic copying operation for objects in
118 Python?
119 6.2. Q. Why isn't there a generic way to implement persistent objects
120 in Python? (Persistent == automatically saved to and restored from
121 disk.)
122 6.3. Q. Why isn't there a switch or case statement in Python?
123
124 7. Using Python on non-UNIX platforms
Guido van Rossum91f60831994-02-15 15:52:27 +0000125 7.1. Q. Is there a Mac version of Python?
126 7.2. Q. Is there a DOS version of Python?
127 7.3. Q. Is there a Windows version of Python?
128 7.4. Q. Is there a Windows NT version of Python?
129 7.5. Q. I have the DOS or Windows version but it appears to be only a
130 binary. Where's the library?
131 7.6. Q. Where's the documentation for the Mac or PC version?
132 7.7. Q. The Mac (PC) version doesn't seem to have any facilities for
133 creating or editing programs apart from entering it interactively, and
134 there seems to be no way to save code that was entered interactively.
135 How do I create a Python program on the Mac (PC)?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000136
137To find a particular question, search for the question number followed
138by a dot, a space, and a Q at the beginning of a line (e.g. to find
139question 4.2 in vi, type /^4\.2\. Q/).
140
141
1421. General information and availability
143=======================================
144
1451.1. Q. What is Python?
146
147A. Python is an interpreted, interactive, object-oriented programming
148language. It incorporates modules, exceptions, dynamic typing, very
149high level dynamic data types, and classes. Python combines
150remarkable power with very clear syntax. It has interfaces to many
151system calls and libraries, as well as to various window systems, and
152is extensible in C or C++. It is also usable as an extension language
153for applications that need a programmable interface. Finally, Python
154is portable: it runs on many brands of UNIX, on the Mac, and on
155MS-DOS.
156
157To find out more, the best thing to do is to start reading the
158tutorial from the documentation set (see a few questions further
159down).
160
1611.2. Q. Why is it called Python?
162
163A. Apart from being a computer wizard, I'm also a fan of "Monty
164Python's Flying Circus" (a BBC comedy series from the seventies, in
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000165the -- unlikely -- case you didn't know). It occurred to me one day
166that I needed a name that was short, unique, and slightly mysterious.
167And I happened to be reading some scripts from the series at the
168time... So then I decided to call my language Python. But Python is
169not a joke. And don't you associate it with dangerous reptiles
170either!
Guido van Rossuma7925f11994-01-26 10:20:16 +0000171
1721.3. Q. How do I obtain a copy of the Python source?
173
174A. The latest Python source distribution is always available by
175anonymous ftp from ftp.cwi.nl [192.16.184.180] in the directory
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000176/pub/python, with filename python<version>.tar.Z. (In the future and
177on some archive sites the extension will be .gz, indicating use of
178gzip compression.) It is a compressed (gzip'ed if the extension is
179.gz) tar file containing the complete C source, LaTeX documentation,
180Python library modules, example programs, and several useful pieces of
181freely distributable software. This will compile and run out of the
182box on most UNIX platforms. At the time of writing, <version> is
1831.0.1. (See section 7 for non-UNIX information.)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000184
1851.4. Q. How do I get documentation on Python?
186
187A. The latest Python documentation set is always available by
188anonymous ftp from ftp.cwi.nl [192.16.184.180] in the directory
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000189/pub/python, with filename pythondoc-ps<version>.tar.Z. (Or .gz --
190see the previous question.) It is a compressed (or gzip'ed) tar file
191containing PostScript files of the reference manual, the library
192manual, and the tutorial. At the time of writing <version> is 1.0.1.
193Note that the library manual is the most important one of the set, as
194much of Python's power stems from the standard or built-in types,
195functions and modules, all of which are described here. PostScript
196for a high-level description of Python is in the file nluug-paper.ps.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000197
198The following sites keep mirrors of the Python distribution:
199
200Site IP address Directory
201
202gatekeeper.dec.com 16.1.0.2 /pub/plan/python/cwi
203ftp.uu.net 192.48.96.9 /languages/python
204ftp.wustl.edu 128.252.135.4 /graphics/graphics/sgi-stuff/python
205ftp.funet.fi 128.214.6.100 /pub/languages/python (old?)
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000206ftp.fu-berlin.de 130.133.4.50 /pub/unix/languages/python (*python* only)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000207
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000208Or try archie on e.g. python1.0 to locate the nearest copy of that
209version...
Guido van Rossuma7925f11994-01-26 10:20:16 +0000210
2111.5. Q. Is there a newsgroup or mailing list devoted to Python?
212
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000213A. There is a newsgroup, comp.lang.python, and a mailing list. The
214newsgroup and mailing list are gatewayed into each other -- if you can
215read news it is not necessary to subscribe to the mailing list. Send
216e-mail to python-list-request@cwi.nl to (un)subscribe to the mailing
217list.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000218
2191.6. Q. Is there a book on Python, or will there be one out soon?
220
221A. Unfortunately, not yet. I would like to write one but my
222obligations at CWI include too much other work to make much progress
223on it. Several parties have expressed interest in sponsoring or
224helping the production of a book or reference manual, but so far there
225are no firm plans. If you volunteer help, by all means drop me a
226note!
227
2281.7. Q. Are there any published articles about Python that I can quote?
229
230A. So far the only refereed and published article that describes
231Python in some detail is:
232
233 Guido van Rossum and Jelke de Boer, "Interactively Testing Remote
234 Servers Using the Python Programming Language", CWI Quarterly, Volume
235 4, Issue 4 (December 1991), Amsterdam, pp 283-303.
236
237LaTeX source for this paper is available as part of the Python source
238distribution.
239
240A more recent high-level description of Python is:
241
242 Guido van Rossum, "An Introduction to Python for UNIX/C
243 Programmers", in the proceedings of the NLUUG najaarsconferentie
244 1993 (dutch UNIX users group meeting november 1993).
245
246PostScript for this paper and for the slides used for the accompanying
247presentation can be found in the ftp directory mentioned a few
248questions earlier, with filenames nluug-paper.ps and nluug-slides.ps,
249respectively.
250
Guido van Rossum95f61a71994-01-26 17:23:37 +00002511.8. Q. How does the Python version numbering scheme work?
252
253A. Python versions are numbered A.B.C. A is the major version number
254-- it is only incremented for major changes in functionality or source
255structure. B is the minor version number, incremented for less
256earth-shattering changes to a release. C is the patchlevel -- it is
257incremented for each new release. Note that in the past, patches have
258added significant changes; in fact the changeover from 0.9.9 to 1.0.0
259was the first time that either A or B changed!
260
Guido van Rossum5333c5d1994-04-11 11:06:22 +00002611.9. Q. Are there other ftp sites that carry Python related material?
262
263A. An interesting ftp site for Python users is ftp.markv.com
264(192.122.251.1); the directory pub/python contains a growing
265collection of interesting Python scripts. To submit a script for
266inclusion, place it together with a readme file (with extension
267.readme) in the publicly writable directory /incoming/python. This
268service is maintained by Lance Ellinghouse <lance@markv.com>.
269
270
Guido van Rossuma7925f11994-01-26 10:20:16 +0000271
2722. Python in the real world
273===========================
274
2752.1. Q. How many people are using Python?
276
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000277A. I don't know, but the maximum number of simultaneous subscriptions
278to the Python mailing list before it was gatewayed into the newsgroup
279was about 180 (several of which were local redistribution lists). I
280believe that many active Python users don't bother to subscribe to the
281list, and now that there's a newsgroup the mailing list subscription
282is even less meaningful.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000283
2842.2. Q. Have any significant projects been done in Python?
285
286A. Here at CWI (the home of Python), we have written a 20,000 line
287authoring environment for transportable hypermedia presentations, a
Guido van Rossum5333c5d1994-04-11 11:06:22 +00002885,000 line multimedia teleconferencing tool, as well as many many
289smaller programs.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000290
291The University of Virginia uses Python to control a virtual reality
292engine. Contact: Matt Conway <conway@virginia.edu>.
293
294See also the next question.
295
2962.3. Q. Are there any commercial projects going on using Python?
297
298A. Several companies have revealed to me that they are planning or
299considering to use Python in a future product. The furthest is
300Sunrise Software, who already have a product out using Python -- they
301use Python for a GUI management application and an SNMP network
302manangement application. Contact: <info@sunrise.com>.
303
304Individuals at many other companies are using Python for
305internal development (witness their contributions to the Python
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000306mailing list or newsgroup).
Guido van Rossuma7925f11994-01-26 10:20:16 +0000307
308Python has also been elected as an extension language by MADE, a
309consortium supported by the European Committee's ESPRIT program and
310consisting of Bull, CWI and some other European companies. Contact:
311Ivan Herman <ivan@cwi.nl>.
312
Guido van Rossum95f61a71994-01-26 17:23:37 +00003132.4. Q. How stable is Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000314
Guido van Rossum91f60831994-02-15 15:52:27 +0000315A. Very stable. While the current version number (1.0.1) would
Guido van Rossuma7925f11994-01-26 10:20:16 +0000316suggest it is in the early stages of development, in fact new, stable
Guido van Rossum95f61a71994-01-26 17:23:37 +0000317releases (numbered 0.9.x) have been coming out roughly every 3 to 6
318months for the past four years.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000319
Guido van Rossum95f61a71994-01-26 17:23:37 +00003202.5. Q. What new developments are expected for Python in the future?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000321
Guido van Rossum95f61a71994-01-26 17:23:37 +0000322A. Without warranty that any of this will actually be realized: I am
Guido van Rossuma7925f11994-01-26 10:20:16 +0000323currently thinking about mechanisms for built-in on-line help and a
Guido van Rossum99bf2501994-01-26 17:30:41 +0000324switch/case statement. There have been some discussions on
325hierarchical module names which might solve the problem of the
326development of large packages. A pthreads interface has been
327contributed which I would like to merge into the latest release. The
328X interface needs improving. There are also some people
329(independently) working on a windowing interface based on STDWIN but
330with the power and ease of use of the average modern widget set. I
331still hope to get some help in producing a Windows version. It would
332be nice if there were a window-based class browser (Someone at CWI has
333contributed one using Motif but it needs some work). Also: improved
334support for embedding Python in other applications, e.g. by renaming
335most global symbols to have a "Py" prefix and providing more
336documentation and threading support.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000337
338
3393. Building Python
340==================
341
Guido van Rossum91f60831994-02-15 15:52:27 +00003423.1. Q. Is there a test set?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000343
344A. Yes, simply do "import testall" (or "import autotest" if you aren't
345interested in the output). The standard modules whose name begins
346with "test" together comprise the test. The test set doesn't test
347*all* features of Python but it goes a long way to confirm that a new
348port is actually working. The Makefile contains an entry "make test"
349which runs the autotest module.
350
Guido van Rossum91f60831994-02-15 15:52:27 +00003513.2. Q. When running the test set, I get complaints about floating point
Guido van Rossuma7925f11994-01-26 10:20:16 +0000352operations, but when playing with floating point operations I cannot
353find anything wrong with them.
354
355A. The test set makes occasional unwarranted assumptions about the
356semantics of C floating point operations. Until someone donates a
357better floating point test set, you will have to comment out the
358offending floating point tests and execute similar tests manually.
359
Guido van Rossum91f60831994-02-15 15:52:27 +00003603.3. Q. Link errors building Python with STDWIN on SGI IRIX.
Guido van Rossum24349991994-02-02 14:12:45 +0000361
362A. Rebuild STDWIN, specifying "CC=cc -cckr" in the Makefile.
363
Guido van Rossum91f60831994-02-15 15:52:27 +00003643.4. Q. Link errors after rerunning the configure script.
Guido van Rossum24349991994-02-02 14:12:45 +0000365
366A. It is generally necessary to run "make clean" after a configuration
367change.
368
Guido van Rossum91f60831994-02-15 15:52:27 +00003693.5. Q. The python interpreter complains about options passed to a
Guido van Rossum24349991994-02-02 14:12:45 +0000370script (after the script name).
371
372A. You are probably linking with GNU getopt, e.g. through -liberty.
373Don't. (If you are using this because you link with -lreadline, use
374the readline distributed with Python instead.)
375
Guido van Rossum91f60831994-02-15 15:52:27 +00003763.6. Q. When building on the SGI, make tries to run python to create
Guido van Rossum24349991994-02-02 14:12:45 +0000377glmodule.c, but python hasn't been built or installed yet.
378
379A. Comment out the line mentioning glmodule.c in Setup and build a
380python without gl first; install it or make sure it is in your $PATH,
381then edit the Setup file again to turn on the gl module, and make
382again. You don't need to do "make clean"; you do need to run "make
383Makefile" in the Modules subdirectory (or just run "make" at the
384toplevel).
385
Guido van Rossum91f60831994-02-15 15:52:27 +00003863.7. Q. Other trouble building Python 1.0.1 on platform X.
Guido van Rossum95f61a71994-01-26 17:23:37 +0000387
388A. Please email the details to <guido@cwi.nl> and I'll look into it.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000389
390
3914. Programming in Python
392========================
393
Guido van Rossum24349991994-02-02 14:12:45 +00003944.1. Q. Is there a source code level debugger with breakpoints, step,
395etc.?
396
397A. Yes. Check out module pdb; pdb.help() prints the documentation (or
398you can read it as Lib/pdb.doc). If you use the STDWIN option,
399there's also a windowing interface, wdb. You can write your own
400debugger by using the code for pdb or wdb as an example.
401
4024.2. Q. Can I create an object class with some methods implemented in
Guido van Rossuma7925f11994-01-26 10:20:16 +0000403C and others in Python (e.g. through inheritance)? (Also phrased as:
404Can I use a built-in type as base class?)
405
406A. No, but you can easily create a Python class which serves as a
407wrapper around a built-in object, e.g. (for dictionaries):
408
409 # A user-defined class behaving almost identical
410 # to a built-in dictionary.
411 class UserDict:
412 def __init__(self): self.data = {}
413 def __repr__(self): return repr(self.data)
414 def __cmp__(self, dict):
415 if type(dict) == type(self.data):
416 return cmp(self.data, dict)
417 else:
418 return cmp(self.data, dict.data)
419 def __len__(self): return len(self.data)
420 def __getitem__(self, key): return self.data[key]
421 def __setitem__(self, key, item): self.data[key] = item
422 def __delitem__(self, key): del self.data[key]
423 def keys(self): return self.data.keys()
424 def items(self): return self.data.items()
425 def values(self): return self.data.values()
426 def has_key(self, key): return self.data.has_key(key)
427
Guido van Rossum24349991994-02-02 14:12:45 +00004284.3. Q. Is there a curses/termcap package for Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000429
430A. No, but you can use the "alfa" (== character cell) version of
431STDWIN. (STDWIN == Standard Windows, a portable windowing system
Guido van Rossum91f60831994-02-15 15:52:27 +0000432interface by the same author, URL: ftp://ftp.cwi.nl/pub/stdwin.)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000433This will also prepare your program for porting to windowing
434environments such as X11 or the Macintosh.
435
Guido van Rossum24349991994-02-02 14:12:45 +00004364.4. Q. Is there an equivalent to C's onexit() in Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000437
438A. Yes, if you import sys and assign a function to sys.exitfunc, it
439will be called when your program exits, is killed by an unhandled
440exception, or (on UNIX) receives a SIGHUP or SIGTERM signal.
441
Guido van Rossum24349991994-02-02 14:12:45 +00004424.5. Q. When I define a function nested inside another function, the
Guido van Rossuma7925f11994-01-26 10:20:16 +0000443nested function seemingly can't access the local variables of the
444outer function. What is going on? How do I pass local data to a
445nested function?
446
447A. Python does not have arbitrarily nested scopes. When you need to
448create a function that needs to access some data which you have
449available locally, create a new class to hold the data and return a
450method of an instance of that class, e.g.:
451
452 class MultiplierClass:
453 def __init__(self, factor):
454 self.factor = factor
455 def multiplier(self, argument):
456 return argument * self.factor
457
458 def generate_multiplier(factor):
459 return MultiplierClass(factor).multiplier
460
461 twice = generate_multiplier(2)
462 print twice(10)
463 # Output: 20
464
Guido van Rossum24349991994-02-02 14:12:45 +00004654.6. Q. How do I iterate over a sequence in reverse order?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000466
467A. If it is a list, the fastest solution is
468
469 list.reverse()
470 try:
471 for x in list:
472 "do something with x"
473 finally:
474 list.reverse()
475
476This has the disadvantage that while you are in the loop, the list
477is temporarily reversed. If you don't like this, you can make a copy.
478This appears expensive but is actually faster than other solutions:
479
480 rev = list[:]
481 rev.reverse()
482 for x in rev:
483 <do something with x>
484
485If it isn't a list, a more general but slower solution is:
486
487 i = len(list)
488 while i > 0:
489 i = i-1
490 x = list[i]
491 <do something with x>
492
493A more elegant solution, is to define a class which acts as a sequence
494and yields the elements in reverse order (solution due to Steve
495Majewski):
496
497 class Rev:
498 def __init__(self, seq):
499 self.forw = seq
500 def __len__(self):
501 return len(self.forw)
502 def __getitem__(self, i):
503 return self.forw[-(i + 1)]
504
505You can now simply write:
506
507 for x in Rev(list):
508 <do something with x>
509
510Unfortunately, this solution is slowest of all, due the the method
511call overhead...
512
Guido van Rossum24349991994-02-02 14:12:45 +00005134.7. Q. My program is too slow. How do I speed it up?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000514
515A. That's a tough one, in general. There are many tricks to speed up
516Python code; I would consider rewriting parts in C only as a last
517resort. One thing to notice is that function and (especially) method
518calls are rather expensive; if you have designed a purely OO interface
519with lots of tiny functions that don't do much more than get or set an
520instance variable or call another method, you may consider using a
521more direct way, e.g. directly accessing instance variables. Also see
522the standard module "profile" (described in the file
523"python/lib/profile.doc") which makes it possible to find out where
524your program is spending most of its time (if you have some patience
525-- the profiling itself can slow your program down by an order of
526magnitude).
527
Guido van Rossum24349991994-02-02 14:12:45 +00005284.8. Q. When I have imported a module, then edit it, and import it
Guido van Rossuma7925f11994-01-26 10:20:16 +0000529again (into the same Python process), the changes don't seem to take
530place. What is going on?
531
532A. For efficiency reasons, Python only reads the module file on the
533first time a module is imported (otherwise a program consisting of
534many modules, each of which imports the same basic module, would read
535the basic module over and over again). To force a changed module
536being read again, do this:
537
538 import modname
539 reload(modname)
540
541Warning: this technique is not 100% fool-proof. In particular,
542modules containing statements like
543
544 from modname import some_objects
545
546will continue to work with the old version of the objects imported
547thus.
548
Guido van Rossum24349991994-02-02 14:12:45 +00005494.9. Q. I have a module in which I want to execute some extra code when it
550is run as a script. How do I find out whether I am running as a
551script?
552
553A. A module can find out its own module name by alooking at the
554(predefined) global variable __name__. If this has the value
555'__main__' you are running as a script. E.g. if you put the following
556on the last line of your module, main() is called only when your
557module is running as a script:
558
559 if __name__ == '__main__': main()
560
Guido van Rossuma7925f11994-01-26 10:20:16 +0000561
5625. Extending Python
563===================
564
5655.1. Q. Can I create my own functions in C?
566
567A. Yes, you can create built-in modules containing functions,
Guido van Rossum24349991994-02-02 14:12:45 +0000568variables, exceptions and even new types in C. This is explained in
569the document "Extending and Embedding the Python Interpreter" (the
570LaTeX file Doc/ext.tex). Also read the chapter on dynamic loading.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000571
5725.2. Q. Can I create my own functions in C++?
573
574A. Yes, using the C-compatibility features found in C++. Basically
575you place extern "C" { ... } around the Python include files and put
576extern "C" before each function that is going to be called by the
577Python interpreter. Global or static C++ objects with constructors
578are probably not a good idea.
579
580
5816. Python's design
582==================
583
5846.1. Q. Why isn't there a generic copying operation for objects in
585Python?
586
587A. Hmm. Maybe there should be one, but it's difficult to assign a
588useful meaning to copying of open files, sockets and windows, or
589recursive data structures. As long as you design all your classes
590yourself you are of course free to define a standard base class that
591defines an overridable copying operation for all the objects you care
592about. (One practical point: it would have to be a built-in function,
593not a standard method name, since not all built-in object types have
594methods; e.g. strings, integers and tuples don't.)
595
5966.2. Q. Why isn't there a generic way to implement persistent objects
597in Python? (Persistent == automatically saved to and restored from
598disk.)
599
600A. Hmm, hmm. Basically for the same reasons as why there is no
601generic copying operation.
602
6036.3. Q. Why isn't there a switch or case statement in Python?
604
605A. You can do this easily enough with a sequence of
606if... elif... elif... else. There have been some proposals for switch
607statement syntax, but there is no concensus (yet) on whether and how
608to do range tests.
609
610
6117. Using Python on non-UNIX platforms
612=====================================
613
Guido van Rossum91f60831994-02-15 15:52:27 +00006147.1. Q. Is there a Mac version of Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000615
Guido van Rossum91f60831994-02-15 15:52:27 +0000616A. Yes. It is on most ftp sites carrying Python as python.sea.hqx --
617this is a self-extracting archive containing the application binary as
618well as the Lib modules.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000619
Guido van Rossum91f60831994-02-15 15:52:27 +00006207.2. Q. Is there a DOS version of Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000621
Guido van Rossum91f60831994-02-15 15:52:27 +0000622A. Yes. More than one, actually: 16python.exe runs in standard DOS
623mode on 186 CPUs or higher; 32python.exe uses a DOS extender and only
624runs on a 386 or higher CPUs. Although 16python.exe does not pass the
625test set because test_grammar is too big for the parser, it actually
626has about 270 kbyte of allocatable heap space, which is sufficient for
627fairly large programs. 32python.exe is distributed as a tar file
628containing the required DOS extended and 387 emulator. Both are on
629most ftp sites carrying Python.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000630
Guido van Rossum91f60831994-02-15 15:52:27 +00006317.3. Q. Is there a Windows version of Python?
632
633A. Yes. Use qwpython.exe. The only problem with it: ^C
634unconditionally kills the entire program -- it does not raise
635KeyboardInterrupt. You can also run 16python.exe or 32python.exe in a
636"DOS box", but qwpython.exe appears to be slightly faster.
637
6387.4. Q. Is there a Windows NT version of Python?
639
640A. Yes. Use ntpython.exe. This is for Intel CPUs. If you want a
641Windows user interface, use qwpython.exe.
642
6437.5. Q. I have the DOS or Windows version but it appears to be only a
644binary. Where's the library?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000645
646A. You still need to copy the files from the distribution directory
Guido van Rossum91f60831994-02-15 15:52:27 +0000647"python/Lib" to your system. If you don't have the full distribution,
648you can get the file pythonlib1.0.1.tar.Z from most ftp sites carrying
649Python; this is a subset of the distribution containing just those
650file.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000651
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000652Once you have installed the library, you need to point sys.path to it.
653Assuming the library is in C:\misc\python\lib, the following commands
654will point your Python interpreter to it (note the doubled backslashes
655-- you can also use single forward slashes instead):
656
657 >>> import sys
658 >>> sys.path.insert(0, 'C:\\misc\\python\\lib')
659 >>>
660
661For a more permanent effect, set the environment variable PYTHONPATH,
662as follows (talking to a DOS prompt):
663
664 C> SET PYTHONPATH=C:\misc\python\lib
665
Guido van Rossum91f60831994-02-15 15:52:27 +00006667.6. Q. Where's the documentation for the Mac or PC version?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000667
668A. There isn't any. The documentation for the Unix version also
Guido van Rossum91f60831994-02-15 15:52:27 +0000669applies to the Mac and PC versions. Where applicable, differences
Guido van Rossuma7925f11994-01-26 10:20:16 +0000670are indicated in the text.
671
Guido van Rossum91f60831994-02-15 15:52:27 +00006727.7. Q. The Mac (PC) version doesn't seem to have any facilities for
673creating or editing programs apart from entering it interactively, and
674there seems to be no way to save code that was entered interactively.
675How do I create a Python program on the Mac (PC)?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000676
Guido van Rossum91f60831994-02-15 15:52:27 +0000677A. Use an external editor. On the Mac, I am quite happy with the Desk
678Accessory called Sigma Edit; this doesn't require Multifinder or
679System 7. I work like this: start the interpreter; edit a module file
680using Sigma Edit; import and test it in the interpreter; edit again in
681Sigma Edit; then use the built-in function reload() to re-read the
682imported module; etc.
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000683
684Regarding the same question for the PC, Kurt Wm. Hemr writes: "While
685anyone with a pulse could certainly figure out how to do the same on
686MS-Windows, I would recommend the NotGNU Emacs clone for MS-Windows.
687Not only can you easily resave and "reload()" from Python after making
688changes, but since WinNot auto-copies to the clipboard any text you
689select, you can simply select the entire procedure (function) which
690you changed in WinNot, switch to QWPython, and shift-ins to reenter
691the changed program unit."