blob: fb44640004006070410ed89fcc7313568502dd50 [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 Rossum796b2591995-01-20 23:05:52 +00009Version: 1.19--
Guido van Rossuma6c707c1995-01-02 17:32:28 +000010Last-modified: 2 January 1995
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
Guido van Rossuma6c707c1995-01-02 17:32:28 +000016Copyright 1993-1995 Guido van Rossum. Unchanged electronic
Guido van Rossuma7925f11994-01-26 10:20:16 +000017redistribution of this FAQ is allowed. Printed redistribution only
18with permission of the author. No warranties.
19
20Author's address:
Guido van Rossuma6c707c1995-01-02 17:32:28 +000021 Guido van Rossum
22 CWI, dept. CST
23 Kruislaan 413
24 P.O. Box 94079
25 1090 GB Amsterdam
26 The Netherlands
Guido van Rossum796b2591995-01-20 23:05:52 +000027Email: <guido@cwi.nl>
Guido van Rossuma7925f11994-01-26 10:20:16 +000028
29The latest version of this FAQ is available by anonymous ftp from
Guido van Rossuma6c707c1995-01-02 17:32:28 +000030<URL:ftp://ftp.cwi.nl/pub/python/python-FAQ>. It will also be posted
31regularly to the newsgroups comp.answers <URL:news:comp.answers> and
32comp.lang.python <URL:news:comp.lang.python>.
Guido van Rossuma7925f11994-01-26 10:20:16 +000033
Guido van Rossuma6c707c1995-01-02 17:32:28 +000034Many FAQs, including this one, are available by anonymous ftp
35<URL:ftp://rtfm.mit.edu/pub/usenet/news.answers/>. The name under
36which a FAQ is archived appears in the Archive-name line at the top of
37the article. This FAQ is archived as python-faq/part1
38<URL:ftp://rtfm.mit.edu/pub/usenet/news.answers/python-faq/part1>.
Guido van Rossuma7925f11994-01-26 10:20:16 +000039
40There's a mail server on that machine which will send you files from
41the archive by e-mail if you have no ftp access. You send a e-mail
Guido van Rossuma6c707c1995-01-02 17:32:28 +000042message to <mail-server@rtfm.mit.edu> containing the single word help
43in the message body to receive instructions.
Guido van Rossuma7925f11994-01-26 10:20:16 +000044
45This FAQ is divided in the following chapters:
46
47 1. General information and availability
48 2. Python in the real world
49 3. Building Python
50 4. Programming in Python
51 5. Extending Python
52 6. Python's design
53 7. Using Python on non-UNIX platforms
54
55To find the start of a particular chapter, search for the chapter number
56followed by a dot and a space at the beginning of a line (e.g. to
57find chapter 4 in vi, type /^4\. /).
58
59Here's an overview of the questions per chapter:
60
61 1. General information and availability
62 1.1. Q. What is Python?
63 1.2. Q. Why is it called Python?
64 1.3. Q. How do I obtain a copy of the Python source?
65 1.4. Q. How do I get documentation on Python?
Guido van Rossumc50158e1994-05-31 09:18:50 +000066 1.5. Q. Are there other ftp sites that mirror the Python distribution?
67 1.6. Q. Is there a newsgroup or mailing list devoted to Python?
Guido van Rossuma6c707c1995-01-02 17:32:28 +000068 1.7. Q. Is there a WWW page devoted to Python?
69 1.8. Q. Is there a book on Python, or will there be one out soon?
70 1.9. Q. Are there any published articles about Python that I can quote?
71 1.10. Q. Are there short introductory papers or talks on Python?
72 1.11. Q. How does the Python version numbering scheme work?
73 1.12. Q. Are there other ftp sites that carry Python related material?
74 1.13. Q. Are there copyright restrictions on the use of Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +000075
76 2. Python in the real world
77 2.1. Q. How many people are using Python?
78 2.2. Q. Have any significant projects been done in Python?
79 2.3. Q. Are there any commercial projects going on using Python?
Guido van Rossum95f61a71994-01-26 17:23:37 +000080 2.4. Q. How stable is Python?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000081 2.5. Q. When will the next version be released?
82 2.6. Q. What new developments are expected for Python in the future?
83 2.7. Q. Is it reasonable to propose incompatible changes to Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +000084
85 3. Building Python
Guido van Rossum91f60831994-02-15 15:52:27 +000086 3.1. Q. Is there a test set?
87 3.2. Q. When running the test set, I get complaints about floating point
Guido van Rossuma7925f11994-01-26 10:20:16 +000088 operations, but when playing with floating point operations I cannot
89 find anything wrong with them.
Guido van Rossum9351fdb1994-11-10 23:03:51 +000090 3.3. Q. Link errors building Python with STDWIN 0.9.8. on SGI IRIX.
91 3.4. Q. Link errors building Python with STDWIN 0.9.9.
92 3.5. Q. Link errors after rerunning the configure script.
93 3.6. Q. The python interpreter complains about options passed to a
Guido van Rossum24349991994-02-02 14:12:45 +000094 script (after the script name).
Guido van Rossum9351fdb1994-11-10 23:03:51 +000095 3.7. Q. When building on the SGI, make tries to run python to create
Guido van Rossum5333c5d1994-04-11 11:06:22 +000096 glmodule.c, but python hasn't been built or installed yet.
Guido van Rossum9351fdb1994-11-10 23:03:51 +000097 3.8. Q. Python built with gcc for the DEC Alpha doesn't work.
98 3.9. Q. I use VPATH but some targets are built in the source directory.
99 3.10. Q. Trouble building or linking with the GNU readline library.
100 3.11. Q. Trouble building Python on Linux.
101 3.12. Q. Trouble with prototypes on Ultrix.
102 3.13. Q. Trouble with posix.listdir on NeXTSTEP 3.2.
103 3.14. Q. Other trouble building Python on platform X.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000104
105 4. Programming in Python
Guido van Rossum24349991994-02-02 14:12:45 +0000106 4.1. Q. Is there a source code level debugger with breakpoints, step,
107 etc.?
108 4.2. Q. Can I create an object class with some methods implemented in
Guido van Rossuma7925f11994-01-26 10:20:16 +0000109 C and others in Python (e.g. through inheritance)? (Also phrased as:
110 Can I use a built-in type as base class?)
Guido van Rossum24349991994-02-02 14:12:45 +0000111 4.3. Q. Is there a curses/termcap package for Python?
112 4.4. Q. Is there an equivalent to C's onexit() in Python?
113 4.5. Q. When I define a function nested inside another function, the
Guido van Rossuma7925f11994-01-26 10:20:16 +0000114 nested function seemingly can't access the local variables of the
115 outer function. What is going on? How do I pass local data to a
116 nested function?
Guido van Rossum24349991994-02-02 14:12:45 +0000117 4.6. Q. How do I iterate over a sequence in reverse order?
118 4.7. Q. My program is too slow. How do I speed it up?
119 4.8. Q. When I have imported a module, then edit it, and import it
Guido van Rossuma7925f11994-01-26 10:20:16 +0000120 again (into the same Python process), the changes don't seem to take
121 place. What is going on?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000122 4.9. Q. How do I find the current module name?
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000123 4.10. Q. I have a module in which I want to execute some extra code
124 when it is run as a script. How do I find out whether I am running as
125 a script?
126 4.11. Q. I try to run a program from the Demo directory but it fails
127 with ImportError: No module named ...; what gives?
Guido van Rossum061f1821994-10-06 16:03:45 +0000128 4.12. Q. I have successfully built Python with STDWIN but it can't
129 find some modules (e.g. stdwinevents).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000130 4.13. Q. What GUI toolkits exist for Python?
Guido van Rossum061f1821994-10-06 16:03:45 +0000131 4.14. Q. Are there any interfaces to database packages in Python?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000132 4.15. Q. Is it possible to write obfuscated one-liners in Python?
133 4.16. Q. Is there an equivalent of C's "?:" ternary operator?
134 4.17. Q. My class defines __del__ but it is not called when I delete the
135 object.
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000136 4.18. Q. How do I change the shell environment for programs called
137 using os.popen() or os.system()? Changing os.environ doesn't work.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000138 4.19. Q. What is a class?
139 4.20. Q. What is a method?
140 4.21. Q. What is self?
141 4.22. Q. What is a unbound method?
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000142 4.23. Q. How do I call a method defined in a base class from a derived
143 class that overrides it?
144 4.24. Q. How do I call a method from a base class without using the
145 name of the base class?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000146 4.25. Q. How can I organize my code to make it easier to change the base
147 class?
148 4.26. Q. How can I find the methods or attributes of an object?
Guido van Rossum061f1821994-10-06 16:03:45 +0000149 4.27. Q. I can't seem to use os.read() on a pipe created with os.popen().
150 4.28. Q. How can I create a stand-alone binary from a Python script?
151 4.29. Q. Is there a special lib for writing CGI scripts in Python?
152 4.30. Q. What other WWW tools are there for Python?
Guido van Rossum796b2591995-01-20 23:05:52 +0000153 4.31. Q. How do I run a subprocess with pipes connected to both input
154 and output?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000155
156 5. Extending Python
157 5.1. Q. Can I create my own functions in C?
158 5.2. Q. Can I create my own functions in C++?
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000159 5.3. Q. How can I execute arbitrary Python statements from C?
160 5.4. Q. How can I evaluate an arbitrary Python expression from C?
161 5.5. Q. How do I extract C values from a Python object?
162 5.6. Q. How do I use mkvalue() to create a tuple of arbitrary length?
Guido van Rossum796b2591995-01-20 23:05:52 +0000163 5.7. Q. What happened to mktuple(), featured in an example in the
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000164 Extensions manual?
165 5.8. Q. How do I call an object's method from C?
166 5.9. Q. How do I catch the output from print_error()?
167 5.10. Q. How do I access a module written in Python from C?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000168
169 6. Python's design
170 6.1. Q. Why isn't there a generic copying operation for objects in
171 Python?
172 6.2. Q. Why isn't there a generic way to implement persistent objects
173 in Python? (Persistent == automatically saved to and restored from
174 disk.)
175 6.3. Q. Why isn't there a switch or case statement in Python?
Guido van Rossumc50158e1994-05-31 09:18:50 +0000176 6.4. Q. Why does Python use indentation for grouping of statements?
Guido van Rossum3de27361994-07-25 14:19:33 +0000177 6.5. Q. Why are Python strings immutable?
178 6.6. Q. Why don't strings have methods like index() or sort(), like
179 lists?
180 6.7. Q. Why does Python use methods for some functionality
181 (e.g. list.index()) but functions for other (e.g. len(list))?
182 6.8. Q. Why can't I derive a class from built-in types (e.g. lists or
183 files)?
184 6.9. Q. Why must 'self' be declared and used explicitly in method
185 definitions and calls?
Guido van Rossum061f1821994-10-06 16:03:45 +0000186 6.10. Q. Can't you emulate threads in the interpreter instead of
187 relying on an OS-specific thread implementation?
188 6.11. Q. Why can't lambda forms contain statements?
189 6.12. Q. Why is there no more efficient way of iterating over a dictionary
190 than first constructing the list of keys()?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000191
192 7. Using Python on non-UNIX platforms
Guido van Rossum91f60831994-02-15 15:52:27 +0000193 7.1. Q. Is there a Mac version of Python?
194 7.2. Q. Is there a DOS version of Python?
195 7.3. Q. Is there a Windows version of Python?
196 7.4. Q. Is there a Windows NT version of Python?
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000197 7.5. Q. Is there an OS/2 version of Python?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000198 7.6. Q. Is there a VMS version of Python?
199 7.7. Q. What about IBM mainframes, or other esoteric non-UNIX
200 platforms?
201 7.8. Q. Where are the source or Makefiles for the non-UNIX versions?
202 7.9. Q. What is the status and support for the non-UNIX versions?
203 7.10. Q. I have the PC version but it appears to be only a binary.
204 Where's the library?
205 7.11. Q. Where's the documentation for the Mac or PC version?
206 7.12. Q. The Mac (PC) version doesn't seem to have any facilities for
207 creating or editing programs apart from entering it interactively, and
208 there seems to be no way to save code that was entered interactively.
209 How do I create a Python program on the Mac (PC)?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000210
211To find a particular question, search for the question number followed
212by a dot, a space, and a Q at the beginning of a line (e.g. to find
213question 4.2 in vi, type /^4\.2\. Q/).
214
215
2161. General information and availability
217=======================================
218
2191.1. Q. What is Python?
220
221A. Python is an interpreted, interactive, object-oriented programming
222language. It incorporates modules, exceptions, dynamic typing, very
223high level dynamic data types, and classes. Python combines
224remarkable power with very clear syntax. It has interfaces to many
225system calls and libraries, as well as to various window systems, and
226is extensible in C or C++. It is also usable as an extension language
227for applications that need a programmable interface. Finally, Python
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000228is portable: it runs on many brands of UNIX, on the Mac, and on PCs
229under MS-DOS, Windows, Windows NT, and OS/2.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000230
231To find out more, the best thing to do is to start reading the
232tutorial from the documentation set (see a few questions further
233down).
234
2351.2. Q. Why is it called Python?
236
Guido van Rossum796b2591995-01-20 23:05:52 +0000237A. Apart from being a computer scientist, I'm also a fan of "Monty
Guido van Rossuma7925f11994-01-26 10:20:16 +0000238Python's Flying Circus" (a BBC comedy series from the seventies, in
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000239the -- unlikely -- case you didn't know). It occurred to me one day
240that I needed a name that was short, unique, and slightly mysterious.
241And I happened to be reading some scripts from the series at the
242time... So then I decided to call my language Python. But Python is
243not a joke. And don't you associate it with dangerous reptiles
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000244either! (If you need an icon, use an image of the 16-ton weight from
245the TV series or of a can of SPAM :-)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000246
2471.3. Q. How do I obtain a copy of the Python source?
248
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000249A. The latest complete Python source distribution is always available
250by anonymous ftp, e.g.
251<URL:ftp://ftp.cwi.nl/pub/python/python1.1.tar.gz>. It is a gzipped
252tar file containing the complete C source, LaTeX documentation, Python
253library modules, example programs, and several useful pieces of freely
254distributable software. This will compile and run out of the box on
255most UNIX platforms. (See section 7 for non-UNIX information.)
256
257Occasionally a set of patches is issued which has to be applied using
258the patch program. These patches are placed in the same directory,
259e.g. <URL:ftp://ftp.cwi.nl/pub/python/patch1.1.1>.
260
261An index of said ftp directory can be found in the file INDEX. An
262HTML version of the index can be found in the file index.html,
263<URL:ftp://ftp.cwi.nl/pub/python/index.html>.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000264
2651.4. Q. How do I get documentation on Python?
266
267A. The latest Python documentation set is always available by
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000268anonymous ftp, e.g.
269<URL:ftp://ftp.cwi.nl/pub/python/pythondoc-ps1.1.tar.gz>. It is a
270gzipped tar file containing PostScript files of the reference manual,
Guido van Rossum3de27361994-07-25 14:19:33 +0000271the library manual, and the tutorial. Note that the library manual is
272the most important one of the set, as much of Python's power stems
273from the standard or built-in types, functions and modules, all of
274which are described here. PostScript for a high-level description of
275Python is in the file nluug-paper.ps.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000276
Guido van Rossumc50158e1994-05-31 09:18:50 +00002771.5. Q. Are there other ftp sites that mirror the Python distribution?
278
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000279A. The following anonymous ftp sites keep mirrors of the Python
280distribution:
Guido van Rossuma7925f11994-01-26 10:20:16 +0000281
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000282 <URL:ftp://gatekeeper.dec.com/pub/plan/python/>
283 <URL:ftp://ftp.uu.net/languages/python/>
284 <URL:ftp://ftp.wustl.edu/graphics/graphics/sgi-stuff/python/>
285 <URL:ftp://ftp.funet.fi/pub/languages/python/>
286 <URL:ftp://ftp.sunet.se/pub/lang/python/>
287 <URL:ftp://unix.hensa.ac.uk/uunet/languages/python/>
288 <URL:ftp://ftp.sterlng.com/programming/languages/python/>
289 <URL:ftp://ftp.ibp.fr/pub/python/>
Guido van Rossuma7925f11994-01-26 10:20:16 +0000290
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000291Or try archie on the string "python".
Guido van Rossuma7925f11994-01-26 10:20:16 +0000292
Guido van Rossumc50158e1994-05-31 09:18:50 +00002931.6. Q. Is there a newsgroup or mailing list devoted to Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000294
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000295A. There is a newsgroup, comp.lang.python <URL:news:comp.lang.python>,
296and a mailing list. The newsgroup and mailing list are gatewayed into
297each other -- if you can read news it's unnecessary to subscribe to
298the mailing list. Send e-mail to <python-list-request@cwi.nl> to
Guido van Rossum796b2591995-01-20 23:05:52 +0000299(un)subscribe to the mailing list. Hypermail archives of
Guido van Rossum061f1821994-10-06 16:03:45 +0000300(nearly) everything posted to the mailing list (and thus the
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000301newsgroup) are available on our WWW server,
302<URL:http://www.cwi.nl/~guido/hypermail/index.html>. The raw archives
303are also available by ftp, e.g.
304<URL:ftp://ftp.cwi.nl/pub/python/mailinglist.gz>. The uncompressed
305versions of these files can be read with the standard UNIX Mail
306program ("Mail -f file") or with nn ("nn file"). To read them using
307MH, you could use "inc -file file".
Guido van Rossuma7925f11994-01-26 10:20:16 +0000308
Guido van Rossuma6c707c1995-01-02 17:32:28 +00003091.7. Q. Is there a WWW page devoted to Python?
310
311A. The official Python home page is
312<URL:http://www.cwi.nl/~guido/Python.html>. Michael McLay at NIST
313maintains a Python page at <URL:http://www.eeel.nist.gov/python/>.
314
3151.8. Q. Is there a book on Python, or will there be one out soon?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000316
Guido van Rossum061f1821994-10-06 16:03:45 +0000317A. I am writing one. Addison-Wesley is interested. Optimistically,
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000318it will be ready for the press by mid-1995. Mark Lutz is working on
319one too -- more details as they emerge.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000320
Guido van Rossuma6c707c1995-01-02 17:32:28 +00003211.9. Q. Are there any published articles about Python that I can quote?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000322
323A. So far the only refereed and published article that describes
324Python in some detail is:
325
326 Guido van Rossum and Jelke de Boer, "Interactively Testing Remote
327 Servers Using the Python Programming Language", CWI Quarterly, Volume
328 4, Issue 4 (December 1991), Amsterdam, pp 283-303.
329
330LaTeX source for this paper is available as part of the Python source
331distribution.
332
Guido van Rossuma6c707c1995-01-02 17:32:28 +00003331.10. Q. Are there short introductory papers or talks on Python?
334
335A. A recent high-level description of Python is:
Guido van Rossuma7925f11994-01-26 10:20:16 +0000336
337 Guido van Rossum, "An Introduction to Python for UNIX/C
338 Programmers", in the proceedings of the NLUUG najaarsconferentie
Guido van Rossum061f1821994-10-06 16:03:45 +0000339 1993 (dutch UNIX users group meeting November 1993).
Guido van Rossuma7925f11994-01-26 10:20:16 +0000340
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000341PostScript for this paper and for the slides used for the accompanying
342presentation is available by ftp as
343<URL:ftp://ftp.cwi.nl/pub/python/nluug-paper.ps> and
344<URL:ftp://ftp.cwi.nl/pub/python/nluug-slides.ps>, respectively.
345
346Slides for a talk on Python that I gave at the Usenix Symposium on
347Very High Level Languages in Santa Fe, NM, USA in October 1995 are
348available as <URL:ftp://ftp.cwi.nl/pub/python/vhll-slides.ps>.
349
3501.11. Q. How does the Python version numbering scheme work?
Guido van Rossum95f61a71994-01-26 17:23:37 +0000351
352A. Python versions are numbered A.B.C. A is the major version number
353-- it is only incremented for major changes in functionality or source
354structure. B is the minor version number, incremented for less
355earth-shattering changes to a release. C is the patchlevel -- it is
356incremented for each new release. Note that in the past, patches have
357added significant changes; in fact the changeover from 0.9.9 to 1.0.0
358was the first time that either A or B changed!
359
Guido van Rossuma6c707c1995-01-02 17:32:28 +00003601.12. Q. Are there other ftp sites that carry Python related material?
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000361
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000362A. An interesting ftp site for Python users is ftp.markv.com; the
363directory pub/python contains a growing collection of interesting
364Python scripts <URL:ftp://ftp.markv.com/pub/python/>. To submit a
365script for inclusion, place it together with a readme file (with
366extension .readme) in the publicly writable directory
367/incoming/python. This service is maintained by Lance Ellinghaus
368<lance@markv.com>. (I've heard complaints about this service not
369being very responsive -- try at your own risk.)
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000370
Guido van Rossuma6c707c1995-01-02 17:32:28 +00003711.13. Q. Are there copyright restrictions on the use of Python?
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000372
373A. Hardly. You can do anything you want with the source, as long as
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000374you leave the copyrights in, and display those copyrights in any
375documentation about Python that you produce. Also, don't use the
376author's institute's name in publicity without prior written
377permission, and don't hold them responsible for anything (read the
378actual copyright for a precise legal wording).
379
380In particular, if you honor the copyright rules, it's OK to use Python
381for commercial use, to sell copies of Python in source or binary form,
382or to sell products that enhance Python or incorporate Python (or part
383of it) in some form. I would still like to know about all commercial
384use of Python!
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000385
Guido van Rossuma7925f11994-01-26 10:20:16 +0000386
3872. Python in the real world
388===========================
389
3902.1. Q. How many people are using Python?
391
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000392A. I don't know, but the maximum number of simultaneous subscriptions
393to the Python mailing list before it was gatewayed into the newsgroup
394was about 180 (several of which were local redistribution lists). I
395believe that many active Python users don't bother to subscribe to the
396list, and now that there's a newsgroup the mailing list subscription
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000397is even less meaningful. I see new names on the newsgroup all the
398time and my best guess is that there are currently at least several
399thousands of users.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000400
4012.2. Q. Have any significant projects been done in Python?
402
403A. Here at CWI (the home of Python), we have written a 20,000 line
404authoring environment for transportable hypermedia presentations, a
Guido van Rossum5333c5d1994-04-11 11:06:22 +00004055,000 line multimedia teleconferencing tool, as well as many many
406smaller programs.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000407
408The University of Virginia uses Python to control a virtual reality
409engine. Contact: Matt Conway <conway@virginia.edu>.
410
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000411The ILU project at Xerox PARC can generate Python glue for ILU
412interfaces. See <URL:ftp://ftp.parc.xerox.com/pub/ilu/ilu.html>.
413
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000414If you have done a significant project in Python that you'd like to be
415included in the list above, send me email!
Guido van Rossuma7925f11994-01-26 10:20:16 +0000416
4172.3. Q. Are there any commercial projects going on using Python?
418
419A. Several companies have revealed to me that they are planning or
Guido van Rossum796b2591995-01-20 23:05:52 +0000420considering use of Python in a future product.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000421
422The furthest is Sunrise Software, who already have a product out using
423Python -- they use Python for a GUI management application and an SNMP
Guido van Rossum061f1821994-10-06 16:03:45 +0000424network management application. Contact: <info@sunrise.com>.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000425
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000426Infoseek uses Python to implement their commercial WWW information
427retrieval service <URL:http://www.infoseek.com/>. Contact:
428<info@infoseek.com>.
Guido van Rossumf8c76d01994-08-17 12:19:53 +0000429
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000430Michael Powers of daVinci Time & Space is "writing tons-o-python for
431interactive television entertainment." Contact: <powers@dvts.com>.
432
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000433Paul Everitt of Connecting Minds is planning a Lotus Notes gateway.
434Contact: <Paul.Everitt@cminds.com>. Or see their WWW server
435<URL:http://www.cminds.com/>.
436
Guido van Rossuma7925f11994-01-26 10:20:16 +0000437Individuals at many other companies are using Python for
438internal development (witness their contributions to the Python
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000439mailing list or newsgroup).
Guido van Rossuma7925f11994-01-26 10:20:16 +0000440
441Python has also been elected as an extension language by MADE, a
442consortium supported by the European Committee's ESPRIT program and
443consisting of Bull, CWI and some other European companies. Contact:
444Ivan Herman <ivan@cwi.nl>.
445
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000446If you'd like to be included in the list above, send me email!
447
Guido van Rossum95f61a71994-01-26 17:23:37 +00004482.4. Q. How stable is Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000449
Guido van Rossum3de27361994-07-25 14:19:33 +0000450A. Very stable. While the current version number would suggest it is
451in the early stages of development, in fact new, stable releases
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000452(numbered 0.9.x through 1.1.x) have been coming out roughly every 3 to
4536 months for the past four years.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000454
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00004552.5. Q. When will the next version be released?
456
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000457A. I am planning to release 1.2 in January 1995. It will contain
458hooks into the implementation of the import command, a (still limited)
459form of persistent objects, and the usual complement of bug fixes
460(including many fixed memory leaks and thread problems).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000461
4622.6. Q. What new developments are expected for Python in the future?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000463
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000464A. There will be better ports to the Mac, DOS, Windows, Windows NT, and
Guido van Rossumf8c76d01994-08-17 12:19:53 +0000465OS/2. The Windows ports will support dynamically loaded modules using
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000466DLLs.
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000467
468Also planned is improved support for embedding Python in other
469applications, e.g. by renaming most global symbols to have a "Py"
470prefix and providing more documentation and threading support.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000471
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000472Some proposals were discussed at the recent Python workshop:
473
474 - persistent objects
475
476 - safe execution of untrusted Python code
477
478 - extensions to the import statement for managing packages
479 (groups of related modules)
480
481 - automatic generation of C/C++ interface glue
482
483 - interfaces to OMG IDL (== Interface Definition Language by
484 the Object Management Group)
485
486 - a portable GUI API (Graphical User Interface Application
487 Programmers Interface)
488
489 - module customization tools
490
491 - standardized documentation strings on module, class and
492 function objects
493
494 - the formation of a Python Steering Committee
495
496 - another Python Workshop
497
498For more info, have a look at the WWW page for the last Python
499Workshop <URL:http://www.eeel.nist.gov/python/workshop11-94/>.
500
501
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00005022.7. Q. Is it reasonable to propose incompatible changes to Python?
Guido van Rossum3de27361994-07-25 14:19:33 +0000503
504A. In general, no. There are already millions of lines of Python code
505around the world, so any changes in the language that invalidates more
506than a very small fraction of existing programs has to be frowned
507upon. Even if you can provide a conversion program, there still is
508the problem of updating all documentation. Providing a gradual
509upgrade path is the only way if a feature has to be changed.
510
Guido van Rossuma7925f11994-01-26 10:20:16 +0000511
5123. Building Python
513==================
514
Guido van Rossum91f60831994-02-15 15:52:27 +00005153.1. Q. Is there a test set?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000516
517A. Yes, simply do "import testall" (or "import autotest" if you aren't
518interested in the output). The standard modules whose name begins
519with "test" together comprise the test. The test set doesn't test
520*all* features of Python but it goes a long way to confirm that a new
521port is actually working. The Makefile contains an entry "make test"
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000522which runs the autotest module. NOTE: if "make test" fails, run the
523tests manually ("import testall") to see what goes wrong before
524reporting the error.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000525
Guido van Rossum91f60831994-02-15 15:52:27 +00005263.2. Q. When running the test set, I get complaints about floating point
Guido van Rossuma7925f11994-01-26 10:20:16 +0000527operations, but when playing with floating point operations I cannot
528find anything wrong with them.
529
530A. The test set makes occasional unwarranted assumptions about the
531semantics of C floating point operations. Until someone donates a
532better floating point test set, you will have to comment out the
533offending floating point tests and execute similar tests manually.
534
Guido van Rossum061f1821994-10-06 16:03:45 +00005353.3. Q. Link errors building Python with STDWIN 0.9.8. on SGI IRIX.
Guido van Rossum24349991994-02-02 14:12:45 +0000536
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000537A. Get STDWIN 0.9.9 <URL:ftp://ftp.cwi.nl/pub/stdwin/stdwin0.9.9.tar.gz>.
Guido van Rossum061f1821994-10-06 16:03:45 +0000538
Guido van Rossum9351fdb1994-11-10 23:03:51 +00005393.4. Q. Link errors building Python with STDWIN 0.9.9.
Guido van Rossum061f1821994-10-06 16:03:45 +0000540
Guido van Rossum796b2591995-01-20 23:05:52 +0000541A. The linker probably complains that it can't find routines like
542'tecreate', 'tenew' etc. The STDWIN 0.9.9 distribution requires that
543you add TWO libraries from stdwin to the line for stdwin in the
544Setupfile. Use something like this (all on one line!):
Guido van Rossum061f1821994-10-06 16:03:45 +0000545
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000546 stdwin stdwinmodule.c -I$(STDWIN)/H $(STDWIN)/Packs/textedit/libtextedit.a $(STDWIN)/Ports/x11/libstdwin.a -lX11
Guido van Rossum24349991994-02-02 14:12:45 +0000547
Guido van Rossum9351fdb1994-11-10 23:03:51 +00005483.5. Q. Link errors after rerunning the configure script.
Guido van Rossum24349991994-02-02 14:12:45 +0000549
550A. It is generally necessary to run "make clean" after a configuration
551change.
552
Guido van Rossum9351fdb1994-11-10 23:03:51 +00005533.6. Q. The python interpreter complains about options passed to a
Guido van Rossum24349991994-02-02 14:12:45 +0000554script (after the script name).
555
556A. You are probably linking with GNU getopt, e.g. through -liberty.
Guido van Rossumf8c76d01994-08-17 12:19:53 +0000557Don't. The reason for the complaint is that GNU getopt, unlike System
558V getopt and other getopt implementations, doesn't consider a
559non-option to be the end of the option list. A quick (and compatible)
560fix for scripts is to add "--" to the interpreter, like this:
561
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000562 #! /usr/local/bin/python --
Guido van Rossumf8c76d01994-08-17 12:19:53 +0000563
564You can also use this interactively:
565
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000566 python -- script.py [options]
567
568Note that a working getopt implementation is provided in the Python
569distribution (in Python/getopt.c) but not automatically used.
Guido van Rossum24349991994-02-02 14:12:45 +0000570
Guido van Rossum9351fdb1994-11-10 23:03:51 +00005713.7. Q. When building on the SGI, make tries to run python to create
Guido van Rossum24349991994-02-02 14:12:45 +0000572glmodule.c, but python hasn't been built or installed yet.
573
574A. Comment out the line mentioning glmodule.c in Setup and build a
575python without gl first; install it or make sure it is in your $PATH,
576then edit the Setup file again to turn on the gl module, and make
577again. You don't need to do "make clean"; you do need to run "make
578Makefile" in the Modules subdirectory (or just run "make" at the
579toplevel).
580
Guido van Rossum9351fdb1994-11-10 23:03:51 +00005813.8. Q. Python built with gcc for the DEC Alpha doesn't work.
Guido van Rossum3de27361994-07-25 14:19:33 +0000582
583People have reported problems with both gcc 2.5.8 and 2.6.0. The DEC
584OSF/1 cc compiler does not have these problems so it's probably gcc's
585fault. One person reported that the problem went away when using -g
586instead of -O so this may be an option if you insist on using gcc. If
587someone tracks it down more completely I'd like to hear about it!
588
Guido van Rossum9351fdb1994-11-10 23:03:51 +00005893.9. Q. I use VPATH but some targets are built in the source directory.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000590
591A. On some systems (e.g. Sun), if the target already exists in the
592source directory, it is created there instead of in the build
593directory. This is usually because you have previously built without
594VPATH. Try running "make clobber" in the source directory.
595
Guido van Rossum9351fdb1994-11-10 23:03:51 +00005963.10. Q. Trouble building or linking with the GNU readline library.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000597
598A. Consider using readline 2.0. From the Python 1.1 README:
599
600- You can use the GNU readline library to improve the interactive user
601interface: this gives you line editing and command history when
Guido van Rossum796b2591995-01-20 23:05:52 +0000602calling python interactively. You need to configure and build the GNU
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000603readline library before running the configure script. Its sources are
604no longer distributed with Python; you can ftp them from any GNU
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000605mirror site, or from its home site
606<URL:ftp://slc2.ins.cwru.edu/pub/dist/readline-2.0.tar.gz> (or a
607higher version number -- using version 1.x is not recommended). Pass
608the Python configure script the option --with-readline=DIRECTORY where
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000609DIRECTORY is the absolute pathname of the directory where you've built
610the readline library. Some hints on building and using the readline
611library:
612
613- On SGI IRIX 5, you may have to add the following
614to rldefs.h:
615
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000616 #ifndef sigmask
617 #define sigmask(sig) (1L << ((sig)-1))
618 #endif
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000619
620- On most systems, you will have to add #include "rldefs.h" to the
621top of several source files, and if you use the VPATH feature, you
622will have to add dependencies of the form foo.o: foo.c to the
623Makefile for several values of foo.
624
625- The readline library requires use of the termcap library. A
626known problem with this is that it contains entry points which
Guido van Rossum061f1821994-10-06 16:03:45 +0000627cause conflicts with the STDWIN and SGI GL libraries. The STDWIN
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000628conflict can be solved (and will be, in the next release of
Guido van Rossum061f1821994-10-06 16:03:45 +0000629STDWIN) by adding a line saying '#define werase w_erase' to the
630stdwin.h file (in the STDWIN distribution, subdirectory H). The
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000631GL conflict has been solved in the Python configure script by a
632hack that forces use of the static version of the termcap library.
633
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000634- Check the newsgroup gnu.bash.bugs <URL:news:gnu.bash.bugs> for
635specific problems with the readline library (I don't get this group
636here but I've been told that it is the place for readline bugs.)
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000637
Guido van Rossum9351fdb1994-11-10 23:03:51 +00006383.11. Q. Trouble building Python on Linux.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000639
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000640A. (Adapted from Bennet Todd:)
641
642It seems to work for some people but not for others.
643
644This might depend on which Linux release you're using. Older Linux
645releases had (understandably) fewer gracious hack to improve Unix
646compatibility; really old Linux was Posix 1003.1 compatible, without
647nearly as much Unix compatibility as one might have wished. Current
648releases of Linux build most current free software, either BSD or
649System V, with little or no trouble.
650
651Besides that, there are quite a few different releases currently,
652including MCC, Slackware, SLS, and Debian, and probably more. The
653differences between their compilation environments are far smaller
654than they used to be, but they're still updated with different
655frequencies. It's likely that the current Slackware works fine, as it
656currently seems to be the most popular. But that's just a guess.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000657
Guido van Rossum9351fdb1994-11-10 23:03:51 +00006583.12. Q. Trouble with prototypes on Ultrix.
Guido van Rossum72eb83c1994-10-07 11:33:28 +0000659
660A. Ultrix cc seems broken -- use gcc, or edit config.h to #undef
661HAVE_PROTOTYPES.
662
Guido van Rossum9351fdb1994-11-10 23:03:51 +00006633.13. Q. Trouble with posix.listdir on NeXTSTEP 3.2.
664
Guido van Rossum796b2591995-01-20 23:05:52 +0000665A. If you built 1.2, you probably forgot to pass -posix as mentioned
666in the README file -- this links with the right version of the
667libraries. If you built an earlier version, that's also your problem
668(edit Makefile to add it to OPT), but you also need to remove this
669NeXT-specific section from import.c:
Guido van Rossum9351fdb1994-11-10 23:03:51 +0000670
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000671 #if defined(NeXT) || defined(WITH_RLD)
672 #define DYNAMIC_LINK
673 #define USE_RLD
674 #endif
Guido van Rossum9351fdb1994-11-10 23:03:51 +0000675
6763.14. Q. Other trouble building Python on platform X.
Guido van Rossum95f61a71994-01-26 17:23:37 +0000677
Guido van Rossum796b2591995-01-20 23:05:52 +0000678A. Please email the details to <guido@cwi.nl> and I'll look into it.
679Please provide as many details as possible. In particular, if you
680don't tell me what type of computer and what operating system (and
681version) you are using it will be difficult for me to figure out what
682is the matter. If you get a specific error message, please email it
683to me too.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000684
685
6864. Programming in Python
687========================
688
Guido van Rossum24349991994-02-02 14:12:45 +00006894.1. Q. Is there a source code level debugger with breakpoints, step,
690etc.?
691
692A. Yes. Check out module pdb; pdb.help() prints the documentation (or
693you can read it as Lib/pdb.doc). If you use the STDWIN option,
694there's also a windowing interface, wdb. You can write your own
695debugger by using the code for pdb or wdb as an example.
696
6974.2. Q. Can I create an object class with some methods implemented in
Guido van Rossuma7925f11994-01-26 10:20:16 +0000698C and others in Python (e.g. through inheritance)? (Also phrased as:
699Can I use a built-in type as base class?)
700
701A. No, but you can easily create a Python class which serves as a
702wrapper around a built-in object, e.g. (for dictionaries):
703
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000704 # A user-defined class behaving almost identical
705 # to a built-in dictionary.
706 class UserDict:
707 def __init__(self): self.data = {}
708 def __repr__(self): return repr(self.data)
709 def __cmp__(self, dict):
710 if type(dict) == type(self.data):
711 return cmp(self.data, dict)
712 else:
713 return cmp(self.data, dict.data)
714 def __len__(self): return len(self.data)
715 def __getitem__(self, key): return self.data[key]
716 def __setitem__(self, key, item): self.data[key] = item
717 def __delitem__(self, key): del self.data[key]
718 def keys(self): return self.data.keys()
719 def items(self): return self.data.items()
720 def values(self): return self.data.values()
721 def has_key(self, key): return self.data.has_key(key)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000722
Guido van Rossum24349991994-02-02 14:12:45 +00007234.3. Q. Is there a curses/termcap package for Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000724
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000725A. Yes -- Lance Ellinghaus has written a module that interfaces to
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000726System V's "ncurses". If you know a little curses and some Python,
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000727it's straightforward to use. It is part of the standard Python
728distribution, but not configured by default -- you must enable it by
729editing Modules/Setup. It requires a System V curses implementation.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000730
731You could also consider using the "alfa" (== character cell) version
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000732of STDWIN. (Standard Window System Interface, a portable windowing
733system interface by myself <URL:ftp://ftp.cwi.nl/pub/stdwin/>.) This
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000734will also prepare your program for porting to windowing environments
735such as X11 or the Macintosh.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000736
Guido van Rossum24349991994-02-02 14:12:45 +00007374.4. Q. Is there an equivalent to C's onexit() in Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000738
739A. Yes, if you import sys and assign a function to sys.exitfunc, it
740will be called when your program exits, is killed by an unhandled
741exception, or (on UNIX) receives a SIGHUP or SIGTERM signal.
742
Guido van Rossum24349991994-02-02 14:12:45 +00007434.5. Q. When I define a function nested inside another function, the
Guido van Rossuma7925f11994-01-26 10:20:16 +0000744nested function seemingly can't access the local variables of the
745outer function. What is going on? How do I pass local data to a
746nested function?
747
748A. Python does not have arbitrarily nested scopes. When you need to
749create a function that needs to access some data which you have
750available locally, create a new class to hold the data and return a
751method of an instance of that class, e.g.:
752
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000753 class MultiplierClass:
754 def __init__(self, factor):
755 self.factor = factor
756 def multiplier(self, argument):
757 return argument * self.factor
Guido van Rossuma7925f11994-01-26 10:20:16 +0000758
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000759 def generate_multiplier(factor):
760 return MultiplierClass(factor).multiplier
Guido van Rossuma7925f11994-01-26 10:20:16 +0000761
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000762 twice = generate_multiplier(2)
763 print twice(10)
764 # Output: 20
765
766An alternative solution uses default arguments, e.g.:
767
768 def generate_multiplier(factor):
769 def multiplier(arg, fact = factor):
770 return arg*fact
771 return multiplier
772
773 twice = generate_multiplier(2)
774 print twice(10)
775 # Output: 20
Guido van Rossuma7925f11994-01-26 10:20:16 +0000776
Guido van Rossum24349991994-02-02 14:12:45 +00007774.6. Q. How do I iterate over a sequence in reverse order?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000778
779A. If it is a list, the fastest solution is
780
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000781 list.reverse()
782 try:
783 for x in list:
784 "do something with x"
785 finally:
786 list.reverse()
Guido van Rossuma7925f11994-01-26 10:20:16 +0000787
788This has the disadvantage that while you are in the loop, the list
789is temporarily reversed. If you don't like this, you can make a copy.
790This appears expensive but is actually faster than other solutions:
791
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000792 rev = list[:]
793 rev.reverse()
794 for x in rev:
795 <do something with x>
Guido van Rossuma7925f11994-01-26 10:20:16 +0000796
797If it isn't a list, a more general but slower solution is:
798
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000799 i = len(list)
800 while i > 0:
801 i = i-1
802 x = list[i]
803 <do something with x>
Guido van Rossuma7925f11994-01-26 10:20:16 +0000804
805A more elegant solution, is to define a class which acts as a sequence
806and yields the elements in reverse order (solution due to Steve
807Majewski):
808
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000809 class Rev:
810 def __init__(self, seq):
811 self.forw = seq
812 def __len__(self):
813 return len(self.forw)
814 def __getitem__(self, i):
815 return self.forw[-(i + 1)]
Guido van Rossuma7925f11994-01-26 10:20:16 +0000816
817You can now simply write:
818
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000819 for x in Rev(list):
820 <do something with x>
Guido van Rossuma7925f11994-01-26 10:20:16 +0000821
Guido van Rossum061f1821994-10-06 16:03:45 +0000822Unfortunately, this solution is slowest of all, due to the method
Guido van Rossuma7925f11994-01-26 10:20:16 +0000823call overhead...
824
Guido van Rossum24349991994-02-02 14:12:45 +00008254.7. Q. My program is too slow. How do I speed it up?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000826
827A. That's a tough one, in general. There are many tricks to speed up
828Python code; I would consider rewriting parts in C only as a last
829resort. One thing to notice is that function and (especially) method
830calls are rather expensive; if you have designed a purely OO interface
831with lots of tiny functions that don't do much more than get or set an
832instance variable or call another method, you may consider using a
833more direct way, e.g. directly accessing instance variables. Also see
834the standard module "profile" (described in the file
835"python/lib/profile.doc") which makes it possible to find out where
836your program is spending most of its time (if you have some patience
837-- the profiling itself can slow your program down by an order of
838magnitude).
839
Guido van Rossum24349991994-02-02 14:12:45 +00008404.8. Q. When I have imported a module, then edit it, and import it
Guido van Rossuma7925f11994-01-26 10:20:16 +0000841again (into the same Python process), the changes don't seem to take
842place. What is going on?
843
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000844A. For reasons of efficiency as well as consistency, Python only reads
Guido van Rossum796b2591995-01-20 23:05:52 +0000845the module file on the first time a module is imported. (Otherwise a
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000846program consisting of many modules, each of which imports the same
Guido van Rossum796b2591995-01-20 23:05:52 +0000847basic module, would read the basic module over and over again.) To
848force rereading of a changed module, do this:
Guido van Rossuma7925f11994-01-26 10:20:16 +0000849
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000850 import modname
851 reload(modname)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000852
853Warning: this technique is not 100% fool-proof. In particular,
854modules containing statements like
855
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000856 from modname import some_objects
Guido van Rossuma7925f11994-01-26 10:20:16 +0000857
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000858will continue to work with the old version of the imported objects.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000859
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00008604.9. Q. How do I find the current module name?
Guido van Rossum24349991994-02-02 14:12:45 +0000861
Guido van Rossum061f1821994-10-06 16:03:45 +0000862A. A module can find out its own module name by looking at the
Guido van Rossum24349991994-02-02 14:12:45 +0000863(predefined) global variable __name__. If this has the value
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000864'__main__' you are running as a script.
865
Guido van Rossuma6c707c1995-01-02 17:32:28 +00008664.10. Q. I have a module in which I want to execute some extra code
867when it is run as a script. How do I find out whether I am running as
868a script?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000869
870A. See the previous question. E.g. if you put the following on the
871last line of your module, main() is called only when your module is
872running as a script:
Guido van Rossum24349991994-02-02 14:12:45 +0000873
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000874 if __name__ == '__main__': main()
Guido van Rossum24349991994-02-02 14:12:45 +0000875
Guido van Rossuma6c707c1995-01-02 17:32:28 +00008764.11. Q. I try to run a program from the Demo directory but it fails
877with ImportError: No module named ...; what gives?
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000878
Guido van Rossum061f1821994-10-06 16:03:45 +0000879A. This is probably an optional module (written in C!) which hasn't
880been configured on your system. This especially happens with modules
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000881like "Tkinter", "stdwin", "gl", "Xt" or "Xm". For Tkinter, STDWIN and
882many other modules, see Modules/Setup.in for info on how to add these
883modules to your Python, if it is possible at all. Sometimes you will
884have to ftp and build another package first (e.g. STDWIN). Sometimes
885the module only works on specific platforms (e.g. gl only works on SGI
886machines).
887
888NOTE: if the complaint is about "Tkinter" (upper case T) and you have
889already configured module "tkinter" (lower case t), the solution is
890*not* to rename tkinter to Tkinter or vice versa. There is probably
Guido van Rossum796b2591995-01-20 23:05:52 +0000891something wrong with your module search path. Check out the value of
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000892sys.path.
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000893
Guido van Rossum061f1821994-10-06 16:03:45 +0000894For X-related modules (Xt and Xm) you will have to do more work: they
895are currently not part of the standard Python distribution. You will
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000896have to ftp the Extensions tar file, e.g.
897<URL:ftp://ftp.cwi.nl/pub/python/extensions.tar.gz> and follow the
898instructions there. Note: the X related modules are still somewhat
899flakey, so don't try this unless you alread know a bit or two about
900building X applications on your platform.
Guido van Rossum061f1821994-10-06 16:03:45 +0000901
902See also the next question.
903
9044.12. Q. I have successfully built Python with STDWIN but it can't
905find some modules (e.g. stdwinevents).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000906
907A. There's a subdirectory of the library directory named 'stdwin'
908which should be in the default module search path. There's a line in
909Modules/Setup(.in) that you have to enable for this purpose --
Guido van Rossum061f1821994-10-06 16:03:45 +0000910unfortunately in the latest release it's not near the other
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000911STDWIN-related lines so it's easy to miss it.
912
9134.13. Q. What GUI toolkits exist for Python?
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000914
915A. Depending on what platform(s) you are aiming at, there are several.
916
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000917- There's a neat object-oriented interface to the Tcl/Tk widget set,
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000918called Tkinter. As of python 1.1, it is part of the standard Python
919distribution -- all you need to do is enable it in Modules/Setup
920(provided you have already installed Tk and Tcl). This is probably
921the easiest to install and use, and the most complete widget set. It
922is also very likely that in the future the standard Python GUI API
923will be based on or at least look very much like the Tkinter
924interface. For more info about Tk, including pointers to the source,
925see John Ousterhout's home page
Guido van Rossum796b2591995-01-20 23:05:52 +0000926<URL:http://playground.sun.com/~ouster/>.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000927
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000928- The standard Python distribution comes with an interface to STDWIN,
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000929a platform-independent low-level windowing interface. You have to ftp
930the source for STDWIN separately,
931e.g. <URL:ftp://ftp.cwi.nl/pub/stdwin/> or gatekeeper.dec.com in
932pub/misc/stdwin <URL:ftp://gatekeeper.dec.com/pub/misc/stdwin/>.
933STDWIN runs under X11 or the Mac; a Windows port has been attempted
934but I can't seem to get it working. Note that STDWIN is really not
935powerful enough to implement a modern GUI (no widgets, etc.) and that
936I don't have the time to maintain or extend it, so you may be better
937off using Tkinter or the Motif interface, unless you require
938portability to the Mac (which is also offered by SUIT, by the way --
939see below).
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000940
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000941- For SGI IRIX only, there's an interface to the complete GL (Graphics
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000942Library -- low level but very good 3D capabilities) as well as to
943FORMS (a buttons-and-sliders-etc package built on top of GL by Mark
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000944Overmars -- ftp'able from <URL:ftp://ftp.cs.ruu.nl/pub/SGI/FORMS/>).
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000945
946- There's an interface to X11, including the Athena and Motif widget
947sets (and a few individual widgets, like Mosaic's HTML widget and
948SGI's GL widget) in the Extensions set, which is separately ftp'able
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000949<URL:ftp://ftp.cwi.nl/pub/python/extensions.tar.gz>.
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000950
951- There's an interface to SUIT, the U of Virginia's Simple User
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000952Interface Toolkit; it can be ftp'ed from
953<URL:ftp://uvacs.cs.virginia.edu/pub/suit/python/SUIT_python.tar.Z>.
954A PC binary of Python 1.0.2 compiled with DJGPP and with SUIT support
955built-in has been made available by Antonio Costa
956<URL:ftp://asterix.inescn.pt/pub/PC/python/pyt102su.exe> (a
957self-extracting archive). Note that the UVa people themselves have
958expressed doubts about SUIT, and are planning to build a Python GUI
959API based upon Tk (though not necessarily on Tkinter); see
960<URL:http://server.cs.virginia.edu/~tnb2d/IT/IT.html>.
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000961
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000962- There's an interface to WAFE, a Tcl interface to the X11 Motif and
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000963Athena widget sets. Last I heard about it it was included in the WAFE
9641.0 prerelease
965<URL:ftp://ftp.wu-wien.ac.at/pub/src/X11/wafe/wafe-1.0.tar.gz-prerelease>.
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000966
Guido van Rossum061f1821994-10-06 16:03:45 +00009674.14. Q. Are there any interfaces to database packages in Python?
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000968
969A. There's an interface to SYBASE by John Redford
970<jredford@lehman.com>.
971
Guido van Rossum796b2591995-01-20 23:05:52 +0000972There's an interface to metalbase by Lance Ellinghaus
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000973<lance@markv.com>; it is part of the separate Extensions distribution
974<URL:ftp://ftp.cwi.nl/pub/python/extensions.tar.gz>.
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000975
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000976Anthony Baxter <anthony.baxter@aaii.oz.au> has written an interface to
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000977mSQL (mini-SQL). <URL:ftp://ftp.cwi.nl/pub/python/PymSQL.tar.gz>.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000978
Guido van Rossum796b2591995-01-20 23:05:52 +0000979Tom Culliton <culliton@clark.net> has written an Oracle module. Write
980him to get a copy of a late BETA version.
981
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00009824.15. Q. Is it possible to write obfuscated one-liners in Python?
Guido van Rossumc24da7c1994-09-23 14:08:41 +0000983
984A. Yes. See the following three examples, due to Ulf Bartelt:
985
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000986 # Primes < 1000
987 print filter(None,map(lambda y:y*reduce(lambda x,y:x*y!=0,
988 map(lambda x,y=y:y%x,range(2,int(pow(y,0.5)+1))),1),range(2,1000)))
Guido van Rossumc24da7c1994-09-23 14:08:41 +0000989
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000990 # First 10 Fibonacci numbers
991 print map(lambda x,f=lambda x,f:(x<=1) or (f(x-1,f)+f(x-2,f)): f(x,f),
992 range(10))
Guido van Rossumc24da7c1994-09-23 14:08:41 +0000993
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000994 # Mandelbrot set
995 print (lambda Ru,Ro,Iu,Io,IM,Sx,Sy:reduce(lambda x,y:x+y,map(lambda y,
996 Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,Sy=Sy,L=lambda yc,Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,i=IM,
997 Sx=Sx,Sy=Sy:reduce(lambda x,y:x+y,map(lambda x,xc=Ru,yc=yc,Ru=Ru,Ro=Ro,
998 i=i,Sx=Sx,F=lambda xc,yc,x,y,k,f=lambda xc,yc,x,y,k,f:(k<=0)or (x*x+y*y
999 >=4.0) or 1+f(xc,yc,x*x-y*y+xc,2.0*x*y+yc,k-1,f):f(xc,yc,x,y,k,f):chr(
1000 64+F(Ru+x*(Ro-Ru)/Sx,yc,0,0,i)),range(Sx))):L(Iu+y*(Io-Iu)/Sy),range(Sy
1001 ))))(-2.1, 0.7, -1.2, 1.2, 30, 80, 24)
1002 # \___ ___/ \___ ___/ | | |__ lines on screen
1003 # V V | |______ columns on screen
1004 # | | |__________ maximum of "iterations"
1005 # | |_________________ range on y axis
1006 # |____________________________ range on x axis
Guido van Rossumc24da7c1994-09-23 14:08:41 +00001007
1008Don't try this at home, kids!
1009
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000010104.16. Q. Is there an equivalent of C's "?:" ternary operator?
Guido van Rossumc24da7c1994-09-23 14:08:41 +00001011
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001012A. Not directly. In many cases you can mimic a?b:c with "a and b or
1013c", but there's a flaw: if b is zero (or empty, or None -- anything
1014that tests false) then c will be selected instead. In many cases you
1015can prove by looking at the code that this can't happen (e.g. because
1016b is a constant or has a type that can never be false), but in general
1017this can be a problem.
1018
1019Steve Majewski (or was it Tim Peters?) suggested the following
1020solution: (a and [b] or [c])[0]. Because [b] is a singleton list it
1021is never false, so the wrong path is never taken; then applying [0] to
1022the whole thing gets the b or c that you really wanted. Ugly, but it
1023gets you there in the rare cases where it is really inconvenient to
1024rewrite your code using 'if'.
1025
10264.17. Q. My class defines __del__ but it is not called when I delete the
1027object.
1028
1029A. There are several possible reasons for this.
1030
1031- The del statement does not necessarily call __del__ -- it simply
1032decrements the object's reference count, and if this reaches zero
1033__del__ is called.
1034
1035- If your data structures contain circular links (e.g. a tree where
1036each child has a parent pointer and each parent has a list of
1037children) the reference counts will never go back to zero. You'll
Guido van Rossum061f1821994-10-06 16:03:45 +00001038have to define an explicit close() method which removes those
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001039pointers. Please don't ever call __del__ directly -- __del__ should
Guido van Rossum061f1821994-10-06 16:03:45 +00001040call close() and close() should make sure that it can be called more
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001041than once for the same object.
1042
1043- If the object has ever been a local variable (or argument, which is
1044really the same thing) to a function that caught an expression in an
1045except clause, chances are that a reference to the object still exists
1046in that function's stack frame as contained in the stack trace.
1047Normally, deleting (better: assigning None to) sys.exc_traceback will
1048take care of this. If you a stack was printed for an unhandled
1049exception in an interactive interpreter, delete sys.last_traceback
1050instead.
1051
1052- There is code that deletes all objects when the interpreter exits,
1053but if your Python has been configured to support threads, it is not
1054called (because other threads may still be active). You can define
Guido van Rossum061f1821994-10-06 16:03:45 +00001055your own cleanup function using sys.exitfunc (see question 4.4).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001056
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001057- Finally, there are some obscure bugs if your __del__ method does
1058complicated things such as updating dictionaries or lists or
1059references globals. I hope to have fixed these in release 1.2.
1060
10614.18. Q. How do I change the shell environment for programs called
1062using os.popen() or os.system()? Changing os.environ doesn't work.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001063
1064A. Modifying the environment passed to subshells was left out of the
1065interpreter because there seemed to be no well-established portable
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001066way to do it (in particular, some systems, have putenv(), others have
1067setenv(), and some have none at all).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001068
1069However if all you want is to pass environment variables to the
1070commands run by os.system() or os.popen(), there's a simple solution:
1071prefix the command string with a couple of variable assignments and
Guido van Rossum796b2591995-01-20 23:05:52 +00001072export statements. The following would be universal for popen:
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001073
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001074 import os
1075 from commands import mkarg # nifty routine to add shell quoting
1076 def epopen(cmd, mode, env = {}):
1077 # env is a dictionary of environment variables
1078 prefix = ''
Guido van Rossum796b2591995-01-20 23:05:52 +00001079 for key, value in env.items():
1080 prefix = prefix + '%s=%s\n' % (key, mkarg(value)[1:])
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001081 prefix = prefix + 'export %s\n' % key
1082 return os.popen(prefix + cmd, mode)
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001083
10844.19. Q. What is a class?
1085
1086A. A class is the particular object type that is created by executing
Guido van Rossum796b2591995-01-20 23:05:52 +00001087a class statement. Class objects are used as templates, to create
1088class instance objects, which embody both the data structure and
1089program routines specific to a datatype.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001090
10914.20. Q. What is a method?
1092
1093A. A method is a function that you normally call as
Guido van Rossum796b2591995-01-20 23:05:52 +00001094x.name(arguments...) for some object x. The term is used for methods
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001095of classes and class instances as well as for methods of built-in
Guido van Rossum796b2591995-01-20 23:05:52 +00001096objects. (The latter have a completely different implementation and
1097only share the way their calls look in Python code.) Methods of
1098classes (and class instances) are defined as functions inside the
1099class definition.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001100
11014.21. Q. What is self?
1102
1103A. Self is merely a conventional name for the first argument of a
1104method -- i.e. a function defined inside a class definition. A method
1105defined as meth(self, a, b, c) should be called as x.meth(a, b, c) for
1106some instance x of the class in which the definition occurs;
1107the called method will think it is called as meth(x, a, b, c).
1108
11094.22. Q. What is a unbound method?
1110
1111A. An unbound method is a method defined in a class that is not yet
1112bound to an instance. You get an unbound method if you ask for a
1113class attribute that happens to be a function. You get a bound method
1114if you ask for an instance attribute. A bound method knows which
Guido van Rossum061f1821994-10-06 16:03:45 +00001115instance it belongs to and calling it supplies the instance automatically;
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001116an unbound method only knows which class it wants for its first
1117argument (a derived class is also OK). Calling an unbound method
1118doesn't "magically" derive the first argument from the context -- you
1119have to provide it explicitly.
1120
Guido van Rossuma6c707c1995-01-02 17:32:28 +000011214.23. Q. How do I call a method defined in a base class from a derived
1122class that overrides it?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001123
1124A. If your class definition starts with "class Derived(Base): ..."
1125then you can call method meth defined in Base (or one of Base's base
1126classes) as Base.meth(self, arguments...). Here, Base.meth is an
1127unbound method (see previous question).
1128
Guido van Rossuma6c707c1995-01-02 17:32:28 +000011294.24. Q. How do I call a method from a base class without using the
1130name of the base class?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001131
1132A. DON'T DO THIS. REALLY. I MEAN IT. It appears that you could call
1133self.__class__.__bases__[0].meth(self, arguments...) but this fails when
Guido van Rossum061f1821994-10-06 16:03:45 +00001134a doubly-derived method is derived from your class: for its instances,
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001135self.__class__.__bases__[0] is your class, not its base class -- so
1136(assuming you are doing this from within Derived.meth) you would start
1137a recursive call.
1138
11394.25. Q. How can I organize my code to make it easier to change the base
1140class?
1141
1142A. You could define an alias for the base class, assign the real base
1143class to it before your class definition, and use the alias throughout
1144your class. Then all you have to change is the value assigned to the
1145alias. Incidentally, this trick is also handy if you want to decide
1146dynamically (e.g. depending on availability of resources) which base
1147class to use. Example:
1148
1149BaseAlias = <real base class>
1150class Derived(BaseAlias):
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001151 def meth(self):
1152 BaseAlias.meth(self)
1153 ...
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001154
11554.26. Q. How can I find the methods or attributes of an object?
1156
1157A. This depends on the object type.
1158
1159For an instance x of a user-defined class, instance attributes are
1160found in the dictionary x.__dict__, and methods and attributes defined
1161by its class are found in x.__class__.__bases__[i].__dict__ (for i in
1162range(len(x.__class__.__bases__))). You'll have to walk the tree of
1163base classes to find *all* class methods and attributes.
1164
1165Many, but not all built-in types define a list of their method names
1166in x.__methods__, and if they have data attributes, their names may be
1167found in x.__members__. However this is only a convention.
1168
1169For more information, read the source of the standard (but
1170undocumented) module newdir.
1171
11724.27. Q. I can't seem to use os.read() on a pipe created with os.popen().
1173
1174A. os.read() is a low-level function which takes a file descriptor (a
1175small integer). os.popen() creates a high-level file object -- the
1176same type used for sys.std{in,out,err} and returned by the builtin
1177open() function. Thus, to read n bytes from a pipe p created with
1178os.popen(), you need to use p.read(n).
Guido van Rossuma7925f11994-01-26 10:20:16 +00001179
Guido van Rossum061f1821994-10-06 16:03:45 +000011804.28. Q. How can I create a stand-alone binary from a Python script?
1181
1182The demo script "Demo/scripts/freeze.py" does what you want. (It's
1183actually not a demo but a support tool -- there is some extra code in
1184the interpreter to accommodate it.) It requires that you have the
1185Python build tree handy, complete with all the lib*.a files.
1186
1187This works by scanning your source recursively for import statements
1188(both forms) and looking for the modules on the standard Python path
1189as well as in the source directory (for built-in modules). It then
1190"compiles" the modules written in Python to C code (array initializers
1191that can be turned into code objects using the marshal module) and
1192creates a custom-made config file that only contains those built-in
1193modules which are actually used in the program. It then compiles the
1194generated C code and links it with the rest of the Python interpreter
1195to form a self-contained binary which acts exactly like your script.
1196
Guido van Rossum061f1821994-10-06 16:03:45 +000011974.29. Q. Is there a special lib for writing CGI scripts in Python?
1198
1199A. There's documentation and code for a cgi.py module by Michael McLay
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001200<mclay@eeel.nist.gov> available from
1201<URL:http://www.eeel.nist.gov/python/>
Guido van Rossum061f1821994-10-06 16:03:45 +00001202
1203(For the curious: CGI or Common Gateway Interface is the protocol
1204between HTTP servers (WWW servers) and programs/scripts they run to
1205perform queries and other tasks that require returning a dynamically
1206generated document.)
1207
12084.30. Q. What other WWW tools are there for Python?
1209
1210A. The standard library has a module urllib, which can retrieve most
1211commonly used URL types (file, ftp, http, gopher).
1212
1213The Demo2/www directory (Demo2 has to be retrieved separately from the
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001214Python ftp sites <URL:ftp://ftp.cwi.nl/pub/python/demo2.tar.gz>)
1215contains some (really old) code to parse HTML and to display it.
Guido van Rossum061f1821994-10-06 16:03:45 +00001216
1217Steve Miale <smiale@cs.indiana.edu> has written a modular WWW browser
1218called Dancer. An alpha version can be FTP'ed from
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001219<URL:ftp://ftp.cs.indiana.edu/pub/smiale/dancer.tar.gz>. (There are a
1220few articles about Dancer in the (hyper)mail archive
1221<URL:http://www.cwi.nl/~guido/hypermail/python-1994q3/index.html>.)
Guido van Rossum061f1821994-10-06 16:03:45 +00001222
Guido van Rossum796b2591995-01-20 23:05:52 +000012234.31. Q. How do I run a subprocess with pipes connected to both input
1224and output?
1225
1226A. This is really a UNIX question. Also, in general, it is unwise to
1227do so, because you can easily cause a deadlock where the parent
1228process is blocked waiting for output from the child, while the child
1229is blocked waiting for input from the child. This can be caused
1230because the parent expects the child to output more text than it does,
1231or it can be caused by data being stuck in stdio buffers due to lack
1232of flushing. The Python parent can of course explicitly flush the data
1233it sends to the child before it reads any output, but if the child is
1234a naive C program it can easily have been written to never explicitly
1235flush its output, even if it is interactive, since flushing is
1236normally automatic.
1237
1238In many cases, all you really need is to run some data through a
1239command and get the result back. Unless the data is infinite in size,
1240the easiest (and often the most efficient!) way to do this is to write
1241it to a temporary file and run the command with that temporary file as
1242input. The standard module tempfile exports a function mktemp() which
1243generates unique temporary file names.
1244
1245If after reading all of the above you still want to connect two pipes
1246to a subprocess's standard input and output, here's a simple solution,
1247due to Jack Jansen:
1248
1249 import os
1250 import sys
1251 import string
1252
1253 MAXFD = 100 # Max number of file descriptors in this system
1254
1255 def popen2(cmd):
1256 cmd = string.split(cmd)
1257 p2cread, p2cwrite = os.pipe()
1258 c2pread, c2pwrite = os.pipe()
1259 pid = os.fork()
1260 if pid == 0:
1261 # Child
1262 os.close(0)
1263 os.close(1)
1264 if os.dup(p2cread) <> 0:
1265 sys.stderr.write('popen2: bad read dup\n')
1266 if os.dup(c2pwrite) <> 1:
1267 sys.stderr.write('popen2: bad write dup\n')
1268 for i in range(3, MAXFD):
1269 try:
1270 os.close(i)
1271 except:
1272 pass
1273 try:
1274 os.execv(cmd[0], cmd)
1275 finally:
1276 os._exit(1)
1277 os.close(p2cread)
1278 tochild = os.fdopen(p2cwrite, 'w')
1279 os.close(c2pwrite)
1280 fromchild = os.fdopen(c2pread, 'r')
1281 return fromchild, tochild
1282
1283Note that many interactive programs (e.g. vi) don't work well with
1284pipes substituted for standard input and output. You will have to use
1285pseudo ttys ("ptys") instead of pipes. There is some undocumented
1286code to use these in the library module pty.py -- I'm afraid you're on
1287your own here. What's *really* needed is a Python interface to Don
1288Libes' expect library -- any takers?
1289
Guido van Rossum061f1821994-10-06 16:03:45 +00001290
Guido van Rossuma7925f11994-01-26 10:20:16 +000012915. Extending Python
1292===================
1293
12945.1. Q. Can I create my own functions in C?
1295
1296A. Yes, you can create built-in modules containing functions,
Guido van Rossum24349991994-02-02 14:12:45 +00001297variables, exceptions and even new types in C. This is explained in
1298the document "Extending and Embedding the Python Interpreter" (the
1299LaTeX file Doc/ext.tex). Also read the chapter on dynamic loading.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001300
13015.2. Q. Can I create my own functions in C++?
1302
1303A. Yes, using the C-compatibility features found in C++. Basically
1304you place extern "C" { ... } around the Python include files and put
1305extern "C" before each function that is going to be called by the
1306Python interpreter. Global or static C++ objects with constructors
1307are probably not a good idea.
1308
Guido van Rossum7ce61c11994-06-13 15:13:56 +000013095.3. Q. How can I execute arbitrary Python statements from C?
1310
1311A. The highest-level function to do this is run_command() which takes
1312a single string argument which is executed in the context of module
1313__main__ and returns 0 for success and -1 when an exception occurred
1314(including SyntaxError). If you want more control, use run_string();
1315see the source for run_command() in Python/pythonrun.c.
1316
13175.4. Q. How can I evaluate an arbitrary Python expression from C?
1318
1319A. Call the function run_string() from the previous question with the
1320start symbol eval_input; it then parses an expression, evaluates it
1321and returns its value. See exec_eval() in Python/bltinmodule.c.
1322
13235.5. Q. How do I extract C values from a Python object?
1324
1325A. That depends on the object's type. If it's a tuple,
1326gettuplesize(o) returns its length and gettupleitem(o, i) returns its
1327i'th item; similar for lists with getlistsize(o) and getlistitem(o,
1328i). For strings, getstringsize(o) returns its length and
1329getstringvalue(o) a pointer to its value (note that Python strings may
1330contain null bytes so strlen() is not safe). To test which type an
1331object is, first make sure it isn't NULL, and then use
1332is_stringobject(o), is_tupleobject(o), is_listobject(o) etc.
1333
13345.6. Q. How do I use mkvalue() to create a tuple of arbitrary length?
1335
1336A. You can't. Use t = newtupleobject(n) instead, and fill it with
1337objects using settupleitem(t, i, o) -- note that this "eats" a
1338reference count of o. Similar for lists with newlistobject(n) and
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001339setlistitem(l, i, o). Note that you *must* set all the tuple items to
1340some value before you pass the tuple to Python code --
1341newtupleobject(n) initializes them to NULL, which isn't a valid Python
1342value.
1343
Guido van Rossum796b2591995-01-20 23:05:52 +000013445.7. Q. What happened to mktuple(), featured in an example in the
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001345Extensions manual?
1346
1347A. It's a typo, I meant newtupleobject() (see previous question).
1348
13495.8. Q. How do I call an object's method from C?
1350
1351A. Here's a function (untested) that might become part of the next
1352release in some form. It uses <stdarg.h> to allow passing the
1353argument list on to vmkvalue():
1354
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001355 object *call_method(object *inst, char *methodname, char *format, ...)
1356 {
1357 object *method;
1358 object *args;
1359 object *result;
1360 va_list va;
1361 method = getattr(inst, methodname);
1362 if (method == NULL) return NULL;
1363 va_start(va, format);
1364 args = vmkvalue(format, va);
1365 va_end(va);
1366 if (args == NULL) {
1367 DECREF(method);
1368 return NULL;
1369 }
1370 result = call_object(method, args);
1371 DECREF(method);
1372 DECREF(args);
1373 return result;
1374 }
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001375
1376This works for any instance that has methods -- whether built-in or
1377user-defined. You are responsible for eventually DECREF'ing the
1378return value.
1379
1380To call, e.g., a file object's "seek" method with arguments 10, 0
1381(assuming the file object pointer is "f"):
1382
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001383 res = call_method(f, "seek", "(OO)", 10, 0);
1384 if (res == NULL) {
1385 ... an exception occurred ...
1386 }
1387 else {
1388 DECREF(res);
1389 }
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001390
1391Note that since call_object() *always* wants a tuple for the argument
1392list, to call a function without arguments, pass "()" for the format,
1393and to call a function with one argument, surround the argument in
1394parentheses, e.g. "(i)".
1395
13965.9. Q. How do I catch the output from print_error()?
1397
1398A. (Due to Mark Hammond):
1399
1400* in Python code, define an object that supports the "write()" method.
1401FWIW, there seems to be a small problem that requires the 'softspace'
Guido van Rossum061f1821994-10-06 16:03:45 +00001402attribute to be defined too (I can't remember exact details of the
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001403problem).
1404
1405* redirect sys.stdout and sys.stderr to this object.
1406
Guido van Rossum061f1821994-10-06 16:03:45 +00001407* call print_error, or just allow the standard traceback mechanism to
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001408work.
1409
Guido van Rossum061f1821994-10-06 16:03:45 +00001410Then, the output will go wherever your write() method sends it.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001411
14125.10. Q. How do I access a module written in Python from C?
1413
1414A. You can get a pointer to the module object as follows:
1415
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001416 module = import_module("<modulename>");
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001417
1418If the module hasn't been imported yet (i.e. it is not yet present in
1419sys.modules), this initializes the module; otherwise it simply returns
1420the value of sys.modules["<modulename>"]. Note that it doesn't enter
1421the module into any namespace -- it only ensures it has been
1422initialized and is stored in sys.modules.
1423
1424You can then access the module's attributes (i.e. any name defined in
1425the module) as follows:
1426
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001427 attr = getattr(module, "<attrname>");
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001428
1429Calling setattr(), to assign to variables in the module, also works.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001430
Guido van Rossuma7925f11994-01-26 10:20:16 +00001431
14326. Python's design
1433==================
1434
14356.1. Q. Why isn't there a generic copying operation for objects in
1436Python?
1437
1438A. Hmm. Maybe there should be one, but it's difficult to assign a
1439useful meaning to copying of open files, sockets and windows, or
1440recursive data structures. As long as you design all your classes
1441yourself you are of course free to define a standard base class that
1442defines an overridable copying operation for all the objects you care
1443about. (One practical point: it would have to be a built-in function,
1444not a standard method name, since not all built-in object types have
1445methods; e.g. strings, integers and tuples don't.)
1446
14476.2. Q. Why isn't there a generic way to implement persistent objects
1448in Python? (Persistent == automatically saved to and restored from
1449disk.)
1450
1451A. Hmm, hmm. Basically for the same reasons as why there is no
1452generic copying operation.
1453
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001454A partial solution will appear in release 1.2. This will also provide
1455a partial solution to the problem of a generic copying operation.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001456
Guido van Rossuma7925f11994-01-26 10:20:16 +000014576.3. Q. Why isn't there a switch or case statement in Python?
1458
1459A. You can do this easily enough with a sequence of
1460if... elif... elif... else. There have been some proposals for switch
Guido van Rossum061f1821994-10-06 16:03:45 +00001461statement syntax, but there is no consensus (yet) on whether and how
Guido van Rossuma7925f11994-01-26 10:20:16 +00001462to do range tests.
1463
Guido van Rossumc50158e1994-05-31 09:18:50 +000014646.4. Q. Why does Python use indentation for grouping of statements?
1465
1466A. Basically I believe that using indentation for grouping is
1467extremely elegant and contributes a lot to the clarity of the average
1468Python program. Most people learn to love this feature after a while.
1469Some arguments for it:
1470
1471- Since there are no begin/end brackets there cannot be a disagreement
1472between grouping perceived by the parser and the human reader. I
1473remember long ago seeing a C fragment like this:
1474
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001475 if (x <= y)
1476 x++;
1477 y--;
1478 z++;
Guido van Rossumc50158e1994-05-31 09:18:50 +00001479
1480and staring a long time at it wondering why y was being decremented
1481even for x > y... (And I wasn't a C newbie then either.)
1482
1483- Since there are no begin/end brackets there can be no conflicting
1484coding styles. In C there are loads of different ways to place the
1485braces (including the choice whether to place braces around single
1486statements in certain cases, for consistency). If you're used to
1487reading (and writing) code that uses one style, you will feel at least
1488slightly uneasy when reading (or being required to write) another
1489style.
1490
1491- Many coding styles place begin/end brackets on a line by themself.
1492This makes programs considerably longer and wastes valuable screen
1493space, making it harder to get a good overview over a program.
1494Ideally, a function should fit on one basic tty screen (say, 20
1495lines). 20 lines of Python are worth a LOT more than 20 lines of C.
1496This is not solely due to the lack of begin/end brackets (the lack of
1497declarations also helps, and the powerful operations of course), but
1498it certainly helps!
1499
Guido van Rossum3de27361994-07-25 14:19:33 +000015006.5. Q. Why are Python strings immutable?
1501
1502A. There are two advantages. One is performance: knowing that a
1503string is immutable makes it easy to lay it out at construction time
1504-- fixed and unchanging storage requirements. (This is also one of
Guido van Rossum061f1821994-10-06 16:03:45 +00001505the reasons for the distinction between tuples and lists.) The
Guido van Rossum3de27361994-07-25 14:19:33 +00001506other is that strings in Python are considered as "elemental" as
1507numbers. No amount of activity will change the value 8 to anything
1508else, and in Python, no amount of activity will change the string
1509"eight" to anything else. (Adapted from Jim Roskind)
1510
15116.6. Q. Why don't strings have methods like index() or sort(), like
1512lists?
1513
1514A. Good question. Strings currently don't have methods at all
1515(likewise tuples and numbers). Long ago, it seemed unnecessary to
1516implement any of these functions in C, so a standard library module
1517"string" written in Python was created that performs string related
1518operations. Since then, the cry for performance has moved most of
1519them into the built-in module strop (this is imported by module
Guido van Rossumf8c76d01994-08-17 12:19:53 +00001520string, which is still the preferred interface, without loss of
Guido van Rossum3de27361994-07-25 14:19:33 +00001521performance except during initialization). Some of these functions
1522(e.g. index()) could easily be implemented as string methods instead,
1523but others (e.g. sort()) can't, since their interface prescribes that
1524they modify the object, while strings are immutable (see the previous
1525question).
1526
15276.7. Q. Why does Python use methods for some functionality
1528(e.g. list.index()) but functions for other (e.g. len(list))?
1529
1530A. Functions are used for those operations that are generic for a
1531group of types and which should work even for objects that don't have
1532methods at all (e.g. numbers, strings, tuples). Also, implementing
1533len(), max(), min() as a built-in function is actually less code than
1534implementing them as methods for each type. One can quibble about
1535individual cases but it's really too late to change such things
1536fundamentally now.
1537
15386.8. Q. Why can't I derive a class from built-in types (e.g. lists or
1539files)?
1540
1541A. This is caused by the relatively late addition of (user-defined)
1542classes to the language -- the implementation framework doesn't easily
1543allow it. See the answer to question 4.2 for a work-around. This
1544*may* be fixed in the (distant) future.
1545
15466.9. Q. Why must 'self' be declared and used explicitly in method
1547definitions and calls?
1548
1549A. By asking this question you reveal your C++ background. :-)
1550When I added classes, this was (again) the simplest way of
1551implementing methods without too many changes to the interpreter. I
1552borrowed the idea from Modula-3. It turns out to be very useful, for
1553a variety of reasons.
1554
1555First, it makes it more obvious that you are using a method or
1556instance attribute instead of a local variable. Reading "self.x" or
1557"self.meth()" makes it absolutely clear that an instance variable or
1558method is used even if you don't know the class definition by heart.
1559In C++, you can sort of tell by the lack of a local variable
Guido van Rossum061f1821994-10-06 16:03:45 +00001560declaration (assuming globals are rare or easily recognizable) -- but
Guido van Rossum3de27361994-07-25 14:19:33 +00001561in Python, there are no local variable declarations, so you'd have to
1562look up the class definition to be sure.
1563
1564Second, it means that no special syntax is necessary if you want to
1565explicitly reference or call the method from a particular class. In
1566C++, if you want to use a method from base class that is overridden in
1567a derived class, you have to use the :: operator -- in Python you can
1568write baseclass.methodname(self, <argument list>). This is
1569particularly useful for __init__() methods, and in general in cases
1570where a derived class method wants to extend the base class method of
1571the same name and thus has to call the base class method somehow.
1572
1573Lastly, for instance variables, it solves a syntactic problem with
1574assignment: since local variables in Python are (by definition!) those
1575variables to which a value assigned in a function body (and that
1576aren't explicitly declared global), there has to be some way to tell
1577the interpreter that an assignment was meant to assign to an instance
1578variable instead of to a local variable, and it should preferably be
1579syntactic (for efficiency reasons). C++ does this through
1580declarations, but Python doesn't have declarations and it would be a
1581pity having to introduce them just for this purpose. Using the
1582explicit "self.var" solves this nicely. Similarly, for using instance
1583variables, having to write "self.var" means that references to
1584unqualified names inside a method don't have to search the instance's
1585directories.
1586
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000015876.10. Q. Can't you emulate threads in the interpreter instead of
1588relying on an OS-specific thread implementation?
1589
1590A. Unfortunately, the interpreter pushes at least one C stack frame
1591for each Python stack frame. Also, extensions can call back into
1592Python at almost random moments. Therefore a complete threads
1593implementation requires thread support for C.
1594
Guido van Rossum061f1821994-10-06 16:03:45 +000015956.11. Q. Why can't lambda forms contain statements?
1596
1597A. Python lambda forms cannot contain statements because Python's
Guido van Rossum796b2591995-01-20 23:05:52 +00001598syntactic framework can't handle statements nested inside expressions.
Guido van Rossum061f1821994-10-06 16:03:45 +00001599
1600However, in Python, this is not a serious problem. Unlike lambda
1601forms in other languages, where they add functionality, Python lambdas
1602are only a shorthand notation if you're too lazy to define a function.
1603
1604Functions are already first class objects in Python, and can be
1605declared in a local scope. Therefore the only advantage of using a
1606lambda form instead of a locally-defined function is that you'll have
1607to invent a name for the function -- but that's just a local variable
1608to which the function object (which is exactly the same type of object
1609that a lambda form yields) is assigned!
1610
16116.12. Q. Why is there no more efficient way of iterating over a dictionary
1612than first constructing the list of keys()?
1613
1614A. Have you tried it? I bet it's fast enough for your purposes! In
1615most cases such a list takes only a few percent of the space occupied
1616by the dictionary -- it needs only 4 bytes (the size of a pointer) per
1617key -- a dictionary costs 8 bytes per key plus between 30 and 70
1618percent hash table overhead, plus the space for the keys and values --
1619by necessity all keys are unique objects and a string object (the most
1620common key type) costs at least 18 bytes plus the length of the
1621string. Add to that the values contained in the dictionary, and you
1622see that 4 bytes more per item really isn't that much more memory...
1623
1624A call to dict.keys() makes one fast scan over the dictionary
1625(internally, the iteration function does exist) copying the pointers
1626to the key objects into a pre-allocated list object of the right size.
1627The iteration time isn't lost (since you'll have to iterate anyway --
1628unless in the majority of cases your loop terminates very prematurely
1629(which I doubt since you're getting the keys in random order).
1630
1631I don't expose the dictionary iteration operation to Python
1632programmers because the dictionary shouldn't be modified during the
1633entire iteration -- if it is, there's a very small chance that the
1634dictionary is reorganized because the hash table becomes too full, and
1635then the iteration may miss some items and see others twice. Exactly
1636because this only occurs rarely, it would lead to hidden bugs in
1637programs: it's easy never to have it happen during test runs if you
1638only insert or delete a few items per iteration -- but your users will
1639surely hit upon it sooner or later.
1640
Guido van Rossuma7925f11994-01-26 10:20:16 +00001641
16427. Using Python on non-UNIX platforms
1643=====================================
1644
Guido van Rossum91f60831994-02-15 15:52:27 +000016457.1. Q. Is there a Mac version of Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +00001646
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001647A. Yes. It is on most ftp sites carrying Python as python.sea.hqx,
1648e.g. <URL:ftp://ftp.cwi.nl/pub/python/python_1.1.sea.hqx> -- this is a
1649self-extracting archive containing the application binary as well as
1650the Lib modules.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001651
Guido van Rossum91f60831994-02-15 15:52:27 +000016527.2. Q. Is there a DOS version of Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +00001653
Guido van Rossum91f60831994-02-15 15:52:27 +00001654A. Yes. More than one, actually: 16python.exe runs in standard DOS
1655mode on 186 CPUs or higher; 32python.exe uses a DOS extender and only
1656runs on a 386 or higher CPUs. Although 16python.exe does not pass the
1657test set because test_grammar is too big for the parser, it actually
1658has about 270 kbyte of allocatable heap space, which is sufficient for
1659fairly large programs. 32python.exe is distributed as a tar file
1660containing the required DOS extended and 387 emulator. Both are on
1661most ftp sites carrying Python.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001662
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001663The file dosbuild.tar.gz on the standard ftp sites contains
1664rudimentary Makefiles and instructions
1665<URL:ftp://ftp.cwi.nl/pub/python/dosbuild.tar.gz>.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001666
Guido van Rossum91f60831994-02-15 15:52:27 +000016677.3. Q. Is there a Windows version of Python?
1668
1669A. Yes. Use qwpython.exe. The only problem with it: ^C
1670unconditionally kills the entire program -- it does not raise
1671KeyboardInterrupt. You can also run 16python.exe or 32python.exe in a
1672"DOS box", but qwpython.exe appears to be slightly faster.
1673
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001674There aren't any Makefiles at this moment. Sorry.
1675
1676Using Win32s (a free NT compatibility package by Microsoft) you can
1677also use the NT version by Mark Hammond -- the Win32s package is also
1678in that directory (you'll need several MB of disk space to install
1679it). See the next question.
1680
Guido van Rossum91f60831994-02-15 15:52:27 +000016817.4. Q. Is there a Windows NT version of Python?
1682
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001683A. Yes. Mark Hammond <MHammond@cmutual.com.au> has built a full NT
1684port. This supports using DLLs for dynamic loading of Python modules,
1685and includes an interface to the Microsoft Foundation Classes and a
1686Python programming environment using it that's written mostly in
1687Python. See <URL:ftp://ftp.cwi.nl/pub/python/nt/> -- most mirrors
1688will also have this.
Guido van Rossum91f60831994-02-15 15:52:27 +00001689
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001690Sam Rushing <rushing@squirl.oau.org> once announced he knows how to
1691build Python for the Windows NT on the DEC Alpha AXP.
Guido van Rossum061f1821994-10-06 16:03:45 +00001692
1693Note that currently there is no unified compilation environment for
1694all NT platforms -- hopefully Microsoft will fix this with the release
1695of Visual C++ 2.0.
1696
Guido van Rossum7ce61c11994-06-13 15:13:56 +000016977.5. Q. Is there an OS/2 version of Python?
1698
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001699A. Yes. You can ftp it from the usual places as pyth_os2.zip, e.g.
1700<URL:ftp://ftp.cwi.nl/pub/python/pyth_os2.zip>. This contains both an
1701executable and Makefiles for those fortunate enough to have a C
1702compiler.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001703
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000017047.6. Q. Is there a VMS version of Python?
1705
1706A. I think not (yet). This question has been asked on the list
1707several times and I've never seen an answer. Maybe someone with a VMS
1708C compiler could attempt a port? Probably coming up with proper
1709Makefiles, config.h and config.c should be sufficient. If you succeed
1710(or even if you get stuck halfway), please let me know! (Info as of
171123 September 1994.)
1712
17137.7. Q. What about IBM mainframes, or other esoteric non-UNIX
1714platforms?
1715
1716A. Basically, the same story as for VMS... (Info as of 23 September
17171994.)
1718
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000017197.8. Q. Where are the source or Makefiles for the non-UNIX versions?
1720
1721A. The standard sources can (almost) be used. See the previous
1722questions for availability of Makefiles/projects or patches. If you
1723find things in the standard sources that don't work on your particular
1724platform, please let me know and I'll integrate a solution into the
1725next release of the standard sources. If you submit a fix, please use
1726some kind of #ifdef so as to keep the source working for other
1727platforms. In particular, if the patch works around the availability
1728of a particular function of header file, you should mimic the
1729HAVE_... style used by the configure script -- you can then submit a
1730config.h file for a particular platform so there are no absolutely
1731platform-specific #ifdefs in the rest of the sources.
1732
17337.9. Q. What is the status and support for the non-UNIX versions?
1734
1735A. I don't have access to most of these platforms, so in general I am
1736dependent on material submitted by volunteers(*). However I strive to
1737integrate all changes needed to get it to compile on a particular
1738platform back into the standard sources, so porting of the next
1739version to the various non-UNIX platforms should be easy.
1740
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001741(*) For the Macintosh, that volunteer is me, with help from Jack
1742Jansen <jack@cwi.nl>.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001743
17447.10. Q. I have the PC version but it appears to be only a binary.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001745Where's the library?
Guido van Rossuma7925f11994-01-26 10:20:16 +00001746
1747A. You still need to copy the files from the distribution directory
Guido van Rossum91f60831994-02-15 15:52:27 +00001748"python/Lib" to your system. If you don't have the full distribution,
Guido van Rossum3de27361994-07-25 14:19:33 +00001749you can get the file pythonlib<version>.tar.gz from most ftp sites
1750carrying Python; this is a subset of the distribution containing just
Guido van Rossum796b2591995-01-20 23:05:52 +00001751those files, e.g.
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001752<URL:ftp://ftp.cwi.nl/pub/python/pythonlib1.1.tar.gz>.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001753
Guido van Rossum5333c5d1994-04-11 11:06:22 +00001754Once you have installed the library, you need to point sys.path to it.
1755Assuming the library is in C:\misc\python\lib, the following commands
1756will point your Python interpreter to it (note the doubled backslashes
1757-- you can also use single forward slashes instead):
1758
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001759 >>> import sys
1760 >>> sys.path.insert(0, 'C:\\misc\\python\\lib')
1761 >>>
Guido van Rossum5333c5d1994-04-11 11:06:22 +00001762
1763For a more permanent effect, set the environment variable PYTHONPATH,
1764as follows (talking to a DOS prompt):
1765
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001766 C> SET PYTHONPATH=C:\misc\python\lib
Guido van Rossum5333c5d1994-04-11 11:06:22 +00001767
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000017687.11. Q. Where's the documentation for the Mac or PC version?
Guido van Rossuma7925f11994-01-26 10:20:16 +00001769
1770A. There isn't any. The documentation for the Unix version also
Guido van Rossum91f60831994-02-15 15:52:27 +00001771applies to the Mac and PC versions. Where applicable, differences
Guido van Rossuma7925f11994-01-26 10:20:16 +00001772are indicated in the text.
1773
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000017747.12. Q. The Mac (PC) version doesn't seem to have any facilities for
Guido van Rossum91f60831994-02-15 15:52:27 +00001775creating or editing programs apart from entering it interactively, and
1776there seems to be no way to save code that was entered interactively.
1777How do I create a Python program on the Mac (PC)?
Guido van Rossuma7925f11994-01-26 10:20:16 +00001778
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001779A. Use an external editor. On the Mac, BBEdit seems to be a popular
1780no-frills text editor. I work like this: start the interpreter; edit
1781a module file using BBedit; import and test it in the interpreter;
1782edit again in BBedit; then use the built-in function reload() to
1783re-read the imported module; etc.
Guido van Rossum5333c5d1994-04-11 11:06:22 +00001784
1785Regarding the same question for the PC, Kurt Wm. Hemr writes: "While
1786anyone with a pulse could certainly figure out how to do the same on
1787MS-Windows, I would recommend the NotGNU Emacs clone for MS-Windows.
1788Not only can you easily resave and "reload()" from Python after making
1789changes, but since WinNot auto-copies to the clipboard any text you
1790select, you can simply select the entire procedure (function) which
1791you changed in WinNot, switch to QWPython, and shift-ins to reenter
1792the changed program unit."