blob: 11f38a532965626d106c9c08d4cd83376a6705dc [file] [log] [blame]
Guido van Rossuma7925f11994-01-26 10:20:16 +00001Subject: FAQ: Python -- an object-oriented language
2Newsgroups: comp.lang.misc,comp.answers,news.answers
3Followup-to: comp.lang.misc
4From: 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 Rossum24349991994-02-02 14:12:45 +00009Version: 1.4
10Last-modified: 2 Feb 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
32newsgroups comp.answers and comp.lang.misc.
33
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 Rossuma7925f11994-01-26 10:20:16 +000069
70 2. Python in the real world
71 2.1. Q. How many people are using Python?
72 2.2. Q. Have any significant projects been done in Python?
73 2.3. Q. Are there any commercial projects going on using Python?
Guido van Rossum95f61a71994-01-26 17:23:37 +000074 2.4. Q. How stable is Python?
75 2.5. Q. What new developments are expected for Python in the future?
Guido van Rossuma7925f11994-01-26 10:20:16 +000076
77 3. Building Python
78 3.1. Q. I have trouble building the md5 module and/or finding the file
79 md5.c.
80 3.2. Q. Is there a test set?
81 3.3. Q. When running the test set, I get complaints about floating point
82 operations, but when playing with floating point operations I cannot
83 find anything wrong with them.
84 3.4. Q. I get an OverflowError on evaluating 2*2. What is going on?
Guido van Rossum95f61a71994-01-26 17:23:37 +000085 3.5. Q. Trouble building Python 0.9.9 or earlier on platform X.
Guido van Rossum24349991994-02-02 14:12:45 +000086 3.6. Q. Link errors building Python with STDWIN on SGI Irix.
87 3.7. Q. Link errors for dlopen, dlsym, dlerror from import.o.
88 3.8. Q. Link errors after rerunning the configure script.
89 3.9. Q. The python interpreter complains about options passed to a
90 script (after the script name).
91 3.10. Q. When building on the SGI, make tries to run python to create
92 glmodule.c, but python hasn't been built or installed yet.
93 3.11. Q. Intermittent core dumps and complaints about perfectly valid
94 argument lists to built-in functions (e.g. posix.stat(filename) says
95 "no arguments needed").
96 3.12. Q. Trouble building Python 1.0.0 on NeXT.
97 3.13. Q. Other trouble building Python 1.0.0 on platform X.
Guido van Rossuma7925f11994-01-26 10:20:16 +000098
99 4. Programming in Python
Guido van Rossum24349991994-02-02 14:12:45 +0000100 4.1. Q. Is there a source code level debugger with breakpoints, step,
101 etc.?
102 4.2. Q. Can I create an object class with some methods implemented in
Guido van Rossuma7925f11994-01-26 10:20:16 +0000103 C and others in Python (e.g. through inheritance)? (Also phrased as:
104 Can I use a built-in type as base class?)
Guido van Rossum24349991994-02-02 14:12:45 +0000105 4.3. Q. Is there a curses/termcap package for Python?
106 4.4. Q. Is there an equivalent to C's onexit() in Python?
107 4.5. Q. When I define a function nested inside another function, the
Guido van Rossuma7925f11994-01-26 10:20:16 +0000108 nested function seemingly can't access the local variables of the
109 outer function. What is going on? How do I pass local data to a
110 nested function?
Guido van Rossum24349991994-02-02 14:12:45 +0000111 4.6. Q. How do I iterate over a sequence in reverse order?
112 4.7. Q. My program is too slow. How do I speed it up?
113 4.8. Q. When I have imported a module, then edit it, and import it
Guido van Rossuma7925f11994-01-26 10:20:16 +0000114 again (into the same Python process), the changes don't seem to take
115 place. What is going on?
Guido van Rossum24349991994-02-02 14:12:45 +0000116 4.9. Q. I have a module in which I want to execute some extra code when it
117 is run as a script. How do I find out whether I am running as a
118 script?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000119
120 5. Extending Python
121 5.1. Q. Can I create my own functions in C?
122 5.2. Q. Can I create my own functions in C++?
123
124 6. Python's design
125 6.1. Q. Why isn't there a generic copying operation for objects in
126 Python?
127 6.2. Q. Why isn't there a generic way to implement persistent objects
128 in Python? (Persistent == automatically saved to and restored from
129 disk.)
130 6.3. Q. Why isn't there a switch or case statement in Python?
131
132 7. Using Python on non-UNIX platforms
Guido van Rossum95f61a71994-01-26 17:23:37 +0000133 7.1. Q. Where's the DOS version of 1.0.0?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000134 7.2. Q. Is there a Windows version of Python?
135 7.3. Q. I have the Mac or DOS version but it appears to be only a binary.
136 Where's the library?
137 7.4. Q. Where's the documentation for the Mac or DOS version?
138 7.5. Q. The Mac version doesn't seem to have any facilities for creating or
139 editing programs apart from entering it interactively, and there seems
140 to be no way to save code that was entered interactively. How do I
141 create a Python program on the Mac?
142
143To find a particular question, search for the question number followed
144by a dot, a space, and a Q at the beginning of a line (e.g. to find
145question 4.2 in vi, type /^4\.2\. Q/).
146
147
1481. General information and availability
149=======================================
150
1511.1. Q. What is Python?
152
153A. Python is an interpreted, interactive, object-oriented programming
154language. It incorporates modules, exceptions, dynamic typing, very
155high level dynamic data types, and classes. Python combines
156remarkable power with very clear syntax. It has interfaces to many
157system calls and libraries, as well as to various window systems, and
158is extensible in C or C++. It is also usable as an extension language
159for applications that need a programmable interface. Finally, Python
160is portable: it runs on many brands of UNIX, on the Mac, and on
161MS-DOS.
162
163To find out more, the best thing to do is to start reading the
164tutorial from the documentation set (see a few questions further
165down).
166
1671.2. Q. Why is it called Python?
168
169A. Apart from being a computer wizard, I'm also a fan of "Monty
170Python's Flying Circus" (a BBC comedy series from the seventies, in
171case you didn't know). It occurred to me one day that I needed a name
172that was short, unique, and slightly mysterious. And I happened to be
173reading some scripts from the series at the time... So then I decided
174to call my language Python. But Python is not a joke. And don't you
175associate it with dangerous reptiles either!
176
1771.3. Q. How do I obtain a copy of the Python source?
178
179A. The latest Python source distribution is always available by
180anonymous ftp from ftp.cwi.nl [192.16.184.180] in the directory
181/pub/python, with filename python<version>.tar.Z. It is a compressed
182tar file containing the complete C source, LaTeX documentation, Python
183library modules, example programs, and several useful pieces of freely
184distributable software. This will compile and run out of the box on
Guido van Rossum95f61a71994-01-26 17:23:37 +0000185most UNIX platforms. Currently <version> is 1.0.0. (See section 7
Guido van Rossuma7925f11994-01-26 10:20:16 +0000186for non-UNIX information.)
187
1881.4. Q. How do I get documentation on Python?
189
190A. The latest Python documentation set is always available by
191anonymous ftp from ftp.cwi.nl [192.16.184.180] in the directory
192/pub/python, with filename pythondoc-ps<version>.tar.Z. It is a
193compressed tar file containing PostScript files of the reference
194manual, the library manual, and the tutorial. Currently <version> is
Guido van Rossum95f61a71994-01-26 17:23:37 +00001951.0.0. (Note that the library manual is the most important one of the
Guido van Rossuma7925f11994-01-26 10:20:16 +0000196set, as much of Python's power stems from the standard or built-in
197types, functions and modules, all of which are described here.)
198PostScript for a high-level description of Python is in the file
199nluug-paper.ps.
200
201The following sites keep mirrors of the Python distribution:
202
203Site IP address Directory
204
205gatekeeper.dec.com 16.1.0.2 /pub/plan/python/cwi
206ftp.uu.net 192.48.96.9 /languages/python
207ftp.wustl.edu 128.252.135.4 /graphics/graphics/sgi-stuff/python
208ftp.funet.fi 128.214.6.100 /pub/languages/python (old?)
209ftp.fu-berlin.de 130.133.4.50 /pub/unix/languages/python (python* only)
210
Guido van Rossum95f61a71994-01-26 17:23:37 +0000211Or try archie on e.g. python1.0.0.tar.Z to locate the nearest copy of
Guido van Rossuma7925f11994-01-26 10:20:16 +0000212that version...
213
2141.5. Q. Is there a newsgroup or mailing list devoted to Python?
215
216A. There is no Python newsgroup yet; if you want to post to the net
217about Python, use comp.lang.misc. There is a mailing list devoted to
218Python; send e-mail to python-list-request@cwi.nl to (un)subscribe.
219There are plans to start the discussion about creation of
220comp.lang.python as soon as version 1.0.0 has been released.
221
2221.6. Q. Is there a book on Python, or will there be one out soon?
223
224A. Unfortunately, not yet. I would like to write one but my
225obligations at CWI include too much other work to make much progress
226on it. Several parties have expressed interest in sponsoring or
227helping the production of a book or reference manual, but so far there
228are no firm plans. If you volunteer help, by all means drop me a
229note!
230
2311.7. Q. Are there any published articles about Python that I can quote?
232
233A. So far the only refereed and published article that describes
234Python in some detail is:
235
236 Guido van Rossum and Jelke de Boer, "Interactively Testing Remote
237 Servers Using the Python Programming Language", CWI Quarterly, Volume
238 4, Issue 4 (December 1991), Amsterdam, pp 283-303.
239
240LaTeX source for this paper is available as part of the Python source
241distribution.
242
243A more recent high-level description of Python is:
244
245 Guido van Rossum, "An Introduction to Python for UNIX/C
246 Programmers", in the proceedings of the NLUUG najaarsconferentie
247 1993 (dutch UNIX users group meeting november 1993).
248
249PostScript for this paper and for the slides used for the accompanying
250presentation can be found in the ftp directory mentioned a few
251questions earlier, with filenames nluug-paper.ps and nluug-slides.ps,
252respectively.
253
Guido van Rossum95f61a71994-01-26 17:23:37 +00002541.8. Q. How does the Python version numbering scheme work?
255
256A. Python versions are numbered A.B.C. A is the major version number
257-- it is only incremented for major changes in functionality or source
258structure. B is the minor version number, incremented for less
259earth-shattering changes to a release. C is the patchlevel -- it is
260incremented for each new release. Note that in the past, patches have
261added significant changes; in fact the changeover from 0.9.9 to 1.0.0
262was the first time that either A or B changed!
263
Guido van Rossuma7925f11994-01-26 10:20:16 +0000264
2652. Python in the real world
266===========================
267
2682.1. Q. How many people are using Python?
269
270A. I don't know, but at the last count there were at least 130
271addresses on the Python mailing list (several of which are local
272redistribution lists). I suspect that many users don't bother
273to subscribe to the list.
274
2752.2. Q. Have any significant projects been done in Python?
276
277A. Here at CWI (the home of Python), we have written a 20,000 line
278authoring environment for transportable hypermedia presentations, a
279multimedia teleconferencing tool, as well as many smaller programs.
280
281The University of Virginia uses Python to control a virtual reality
282engine. Contact: Matt Conway <conway@virginia.edu>.
283
284See also the next question.
285
2862.3. Q. Are there any commercial projects going on using Python?
287
288A. Several companies have revealed to me that they are planning or
289considering to use Python in a future product. The furthest is
290Sunrise Software, who already have a product out using Python -- they
291use Python for a GUI management application and an SNMP network
292manangement application. Contact: <info@sunrise.com>.
293
294Individuals at many other companies are using Python for
295internal development (witness their contributions to the Python
296mailing list).
297
298Python has also been elected as an extension language by MADE, a
299consortium supported by the European Committee's ESPRIT program and
300consisting of Bull, CWI and some other European companies. Contact:
301Ivan Herman <ivan@cwi.nl>.
302
Guido van Rossum95f61a71994-01-26 17:23:37 +00003032.4. Q. How stable is Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000304
Guido van Rossum95f61a71994-01-26 17:23:37 +0000305A. Very stable. While the current version number (1.0.0) would
Guido van Rossuma7925f11994-01-26 10:20:16 +0000306suggest it is in the early stages of development, in fact new, stable
Guido van Rossum95f61a71994-01-26 17:23:37 +0000307releases (numbered 0.9.x) have been coming out roughly every 3 to 6
308months for the past four years.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000309
Guido van Rossum95f61a71994-01-26 17:23:37 +00003102.5. Q. What new developments are expected for Python in the future?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000311
Guido van Rossum95f61a71994-01-26 17:23:37 +0000312A. Without warranty that any of this will actually be realized: I am
Guido van Rossuma7925f11994-01-26 10:20:16 +0000313currently thinking about mechanisms for built-in on-line help and a
Guido van Rossum99bf2501994-01-26 17:30:41 +0000314switch/case statement. There have been some discussions on
315hierarchical module names which might solve the problem of the
316development of large packages. A pthreads interface has been
317contributed which I would like to merge into the latest release. The
318X interface needs improving. There are also some people
319(independently) working on a windowing interface based on STDWIN but
320with the power and ease of use of the average modern widget set. I
321still hope to get some help in producing a Windows version. It would
322be nice if there were a window-based class browser (Someone at CWI has
323contributed one using Motif but it needs some work). Also: improved
324support for embedding Python in other applications, e.g. by renaming
325most global symbols to have a "Py" prefix and providing more
326documentation and threading support.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000327
328
3293. Building Python
330==================
331
3323.1. Q. I have trouble building the md5 module and/or finding the file
333md5.c.
334
335A. Apparently the md5 module was based on an older version of RSA's
336md5 implementation. The ftp site rsa.com mentioned in the Makefile
337where this version was found is no longer accessible, and the version
338from RFC 1321 (md5c.c) is slightly different. This will be fixed in
339the 1.0 release; write me if you need the fixes now.
340
3413.2. Q. Is there a test set?
342
343A. Yes, simply do "import testall" (or "import autotest" if you aren't
344interested in the output). The standard modules whose name begins
345with "test" together comprise the test. The test set doesn't test
346*all* features of Python but it goes a long way to confirm that a new
347port is actually working. The Makefile contains an entry "make test"
348which runs the autotest module.
349
3503.3. Q. When running the test set, I get complaints about floating point
351operations, but when playing with floating point operations I cannot
352find anything wrong with them.
353
354A. The test set makes occasional unwarranted assumptions about the
355semantics of C floating point operations. Until someone donates a
356better floating point test set, you will have to comment out the
357offending floating point tests and execute similar tests manually.
358
3593.4. Q. I get an OverflowError on evaluating 2*2. What is going on?
360
361A. Your machine probably has 64 bit long integers (e.g. DEC alpha or
362HP snake architectures). There are some dependencies on word length
363in file intobject.c. This will be corrected in the 1.0 release; until
364then, on a 64 bit machine, just comment out the check for overflow
365from int_mul:
366
367 #if 0
368 if (x > 0x7fffffff || x < (double) (long) 0x80000000)
369 return err_ovf("integer multiplication");
370 #endif
371
372You should also include <limits.h> and replace the constant 32 by
373LONG_BIT in int_[lr]shift.
374
Guido van Rossum95f61a71994-01-26 17:23:37 +00003753.5. Q. Trouble building Python 0.9.9 or earlier on platform X.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000376
Guido van Rossum95f61a71994-01-26 17:23:37 +0000377Please convert to Python 1.0.0 -- it is much more portable.
378
Guido van Rossum24349991994-02-02 14:12:45 +00003793.6. Q. Link errors building Python with STDWIN on SGI IRIX.
380
381A. Rebuild STDWIN, specifying "CC=cc -cckr" in the Makefile.
382
3833.7. Q. Link errors for dlopen, dlsym, dlerror from import.o.
384
385A. You are probably using the GNU loader which doesn't understand
386dynamic linking. Manually comment out #define HAVE_DLFCN_H from
387config.h. (Should be fixed in 1.0.1.)
388
3893.8. Q. Link errors after rerunning the configure script.
390
391A. It is generally necessary to run "make clean" after a configuration
392change.
393
3943.9. Q. The python interpreter complains about options passed to a
395script (after the script name).
396
397A. You are probably linking with GNU getopt, e.g. through -liberty.
398Don't. (If you are using this because you link with -lreadline, use
399the readline distributed with Python instead.)
400
4013.10. Q. When building on the SGI, make tries to run python to create
402glmodule.c, but python hasn't been built or installed yet.
403
404A. Comment out the line mentioning glmodule.c in Setup and build a
405python without gl first; install it or make sure it is in your $PATH,
406then edit the Setup file again to turn on the gl module, and make
407again. You don't need to do "make clean"; you do need to run "make
408Makefile" in the Modules subdirectory (or just run "make" at the
409toplevel).
410
4113.11. Q. Intermittent core dumps and complaints about perfectly valid
412argument lists to built-in functions (e.g. posix.stat(filename) says
413"no arguments needed").
414
415A. You are probably using <stdarg.h> instead of <varargs.h>, or the
416other way around. It may also be that your <stdarg.h> or <varargs.h>
417does not match the code generated by your compiler. In simple cases,
418it may help to turn comment out the #define HAVE_STDARG_H from the
419generated config.h.
420
4213.12. Q. Trouble building Python 1.0.0 on NeXT.
422
423A. Manually add #define _POSIX_SOURCE to config.h. (Should be fixed
424in 1.0.1.)
425
4263.13. Q. Other trouble building Python 1.0.0 on platform X.
Guido van Rossum95f61a71994-01-26 17:23:37 +0000427
428A. Please email the details to <guido@cwi.nl> and I'll look into it.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000429
430
4314. Programming in Python
432========================
433
Guido van Rossum24349991994-02-02 14:12:45 +00004344.1. Q. Is there a source code level debugger with breakpoints, step,
435etc.?
436
437A. Yes. Check out module pdb; pdb.help() prints the documentation (or
438you can read it as Lib/pdb.doc). If you use the STDWIN option,
439there's also a windowing interface, wdb. You can write your own
440debugger by using the code for pdb or wdb as an example.
441
4424.2. Q. Can I create an object class with some methods implemented in
Guido van Rossuma7925f11994-01-26 10:20:16 +0000443C and others in Python (e.g. through inheritance)? (Also phrased as:
444Can I use a built-in type as base class?)
445
446A. No, but you can easily create a Python class which serves as a
447wrapper around a built-in object, e.g. (for dictionaries):
448
449 # A user-defined class behaving almost identical
450 # to a built-in dictionary.
451 class UserDict:
452 def __init__(self): self.data = {}
453 def __repr__(self): return repr(self.data)
454 def __cmp__(self, dict):
455 if type(dict) == type(self.data):
456 return cmp(self.data, dict)
457 else:
458 return cmp(self.data, dict.data)
459 def __len__(self): return len(self.data)
460 def __getitem__(self, key): return self.data[key]
461 def __setitem__(self, key, item): self.data[key] = item
462 def __delitem__(self, key): del self.data[key]
463 def keys(self): return self.data.keys()
464 def items(self): return self.data.items()
465 def values(self): return self.data.values()
466 def has_key(self, key): return self.data.has_key(key)
467
Guido van Rossum24349991994-02-02 14:12:45 +00004684.3. Q. Is there a curses/termcap package for Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000469
470A. No, but you can use the "alfa" (== character cell) version of
471STDWIN. (STDWIN == Standard Windows, a portable windowing system
472interface by the same author, URL ftp://ftp.cwi.nl/pub/stdwin.)
473This will also prepare your program for porting to windowing
474environments such as X11 or the Macintosh.
475
Guido van Rossum24349991994-02-02 14:12:45 +00004764.4. Q. Is there an equivalent to C's onexit() in Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000477
478A. Yes, if you import sys and assign a function to sys.exitfunc, it
479will be called when your program exits, is killed by an unhandled
480exception, or (on UNIX) receives a SIGHUP or SIGTERM signal.
481
Guido van Rossum24349991994-02-02 14:12:45 +00004824.5. Q. When I define a function nested inside another function, the
Guido van Rossuma7925f11994-01-26 10:20:16 +0000483nested function seemingly can't access the local variables of the
484outer function. What is going on? How do I pass local data to a
485nested function?
486
487A. Python does not have arbitrarily nested scopes. When you need to
488create a function that needs to access some data which you have
489available locally, create a new class to hold the data and return a
490method of an instance of that class, e.g.:
491
492 class MultiplierClass:
493 def __init__(self, factor):
494 self.factor = factor
495 def multiplier(self, argument):
496 return argument * self.factor
497
498 def generate_multiplier(factor):
499 return MultiplierClass(factor).multiplier
500
501 twice = generate_multiplier(2)
502 print twice(10)
503 # Output: 20
504
Guido van Rossum24349991994-02-02 14:12:45 +00005054.6. Q. How do I iterate over a sequence in reverse order?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000506
507A. If it is a list, the fastest solution is
508
509 list.reverse()
510 try:
511 for x in list:
512 "do something with x"
513 finally:
514 list.reverse()
515
516This has the disadvantage that while you are in the loop, the list
517is temporarily reversed. If you don't like this, you can make a copy.
518This appears expensive but is actually faster than other solutions:
519
520 rev = list[:]
521 rev.reverse()
522 for x in rev:
523 <do something with x>
524
525If it isn't a list, a more general but slower solution is:
526
527 i = len(list)
528 while i > 0:
529 i = i-1
530 x = list[i]
531 <do something with x>
532
533A more elegant solution, is to define a class which acts as a sequence
534and yields the elements in reverse order (solution due to Steve
535Majewski):
536
537 class Rev:
538 def __init__(self, seq):
539 self.forw = seq
540 def __len__(self):
541 return len(self.forw)
542 def __getitem__(self, i):
543 return self.forw[-(i + 1)]
544
545You can now simply write:
546
547 for x in Rev(list):
548 <do something with x>
549
550Unfortunately, this solution is slowest of all, due the the method
551call overhead...
552
Guido van Rossum24349991994-02-02 14:12:45 +00005534.7. Q. My program is too slow. How do I speed it up?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000554
555A. That's a tough one, in general. There are many tricks to speed up
556Python code; I would consider rewriting parts in C only as a last
557resort. One thing to notice is that function and (especially) method
558calls are rather expensive; if you have designed a purely OO interface
559with lots of tiny functions that don't do much more than get or set an
560instance variable or call another method, you may consider using a
561more direct way, e.g. directly accessing instance variables. Also see
562the standard module "profile" (described in the file
563"python/lib/profile.doc") which makes it possible to find out where
564your program is spending most of its time (if you have some patience
565-- the profiling itself can slow your program down by an order of
566magnitude).
567
Guido van Rossum24349991994-02-02 14:12:45 +00005684.8. Q. When I have imported a module, then edit it, and import it
Guido van Rossuma7925f11994-01-26 10:20:16 +0000569again (into the same Python process), the changes don't seem to take
570place. What is going on?
571
572A. For efficiency reasons, Python only reads the module file on the
573first time a module is imported (otherwise a program consisting of
574many modules, each of which imports the same basic module, would read
575the basic module over and over again). To force a changed module
576being read again, do this:
577
578 import modname
579 reload(modname)
580
581Warning: this technique is not 100% fool-proof. In particular,
582modules containing statements like
583
584 from modname import some_objects
585
586will continue to work with the old version of the objects imported
587thus.
588
Guido van Rossum24349991994-02-02 14:12:45 +00005894.9. Q. I have a module in which I want to execute some extra code when it
590is run as a script. How do I find out whether I am running as a
591script?
592
593A. A module can find out its own module name by alooking at the
594(predefined) global variable __name__. If this has the value
595'__main__' you are running as a script. E.g. if you put the following
596on the last line of your module, main() is called only when your
597module is running as a script:
598
599 if __name__ == '__main__': main()
600
Guido van Rossuma7925f11994-01-26 10:20:16 +0000601
6025. Extending Python
603===================
604
6055.1. Q. Can I create my own functions in C?
606
607A. Yes, you can create built-in modules containing functions,
Guido van Rossum24349991994-02-02 14:12:45 +0000608variables, exceptions and even new types in C. This is explained in
609the document "Extending and Embedding the Python Interpreter" (the
610LaTeX file Doc/ext.tex). Also read the chapter on dynamic loading.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000611
6125.2. Q. Can I create my own functions in C++?
613
614A. Yes, using the C-compatibility features found in C++. Basically
615you place extern "C" { ... } around the Python include files and put
616extern "C" before each function that is going to be called by the
617Python interpreter. Global or static C++ objects with constructors
618are probably not a good idea.
619
620
6216. Python's design
622==================
623
6246.1. Q. Why isn't there a generic copying operation for objects in
625Python?
626
627A. Hmm. Maybe there should be one, but it's difficult to assign a
628useful meaning to copying of open files, sockets and windows, or
629recursive data structures. As long as you design all your classes
630yourself you are of course free to define a standard base class that
631defines an overridable copying operation for all the objects you care
632about. (One practical point: it would have to be a built-in function,
633not a standard method name, since not all built-in object types have
634methods; e.g. strings, integers and tuples don't.)
635
6366.2. Q. Why isn't there a generic way to implement persistent objects
637in Python? (Persistent == automatically saved to and restored from
638disk.)
639
640A. Hmm, hmm. Basically for the same reasons as why there is no
641generic copying operation.
642
6436.3. Q. Why isn't there a switch or case statement in Python?
644
645A. You can do this easily enough with a sequence of
646if... elif... elif... else. There have been some proposals for switch
647statement syntax, but there is no concensus (yet) on whether and how
648to do range tests.
649
650
6517. Using Python on non-UNIX platforms
652=====================================
653
Guido van Rossum95f61a71994-01-26 17:23:37 +00006547.1. Q. Where's the DOS version of 1.0.0?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000655
Guido van Rossum95f61a71994-01-26 17:23:37 +0000656A. I hope it will be coming soon. Our institute has finally acquired
657a 486 PC with a decent C compiler, now all I have to do is to learn
658how to use it :-) Until then, you will have to make do with the 0.9.8
659version (which isn't so bad, actually).
Guido van Rossuma7925f11994-01-26 10:20:16 +0000660
6617.2. Q. Is there a Windows version of Python?
662
663A. Not yet. Several Windows hackers with C compilers are working on a
664port though, so maybe we'll have one soon.
665
6667.3. Q. I have the Mac or DOS version but it appears to be only a binary.
667Where's the library?
668
669A. You still need to copy the files from the distribution directory
670"python/lib" to your system. If you don't have the full distribution,
Guido van Rossum95f61a71994-01-26 17:23:37 +0000671you can ftp the file pythonlib1.0.0.tar.Z from site ftp.cwi.nl,
Guido van Rossuma7925f11994-01-26 10:20:16 +0000672directory /pub/python; this is a subset of the distribution containing
673just those file.
674
6757.4. Q. Where's the documentation for the Mac or DOS version?
676
677A. There isn't any. The documentation for the Unix version also
678applies to the Mac and DOS versions. Where applicable, differences
679are indicated in the text.
680
6817.5. Q. The Mac version doesn't seem to have any facilities for creating or
682editing programs apart from entering it interactively, and there seems
683to be no way to save code that was entered interactively. How do I
684create a Python program on the Mac?
685
686A. Use an external editor. I am quite happy with the Desk Accessory
687called Sigma Edit; this doesn't require Multifinder or System 7. I
688work like this: start the interpreter; edit a module file using Sigma
689Edit; import and test it in the interpreter; edit again in Sigma Edit;
690then use the built-in function reload() to re-read the imported
691module; etc.