blob: 0da9dd0df2b9bf42675b59ca9798a86f5420a3fc [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 Rossum07779351995-02-07 16:59:56 +00009Submitted-by: Guido van Rossum <guido@cwi.nl>
10Version: 1.20
11Last-modified: 7 Februari 1995
Guido van Rossuma7925f11994-01-26 10:20:16 +000012
13This article contains answers to Frequently Asked Questions about
14Python (an object-oriented interpreted programming language -- see
15the answer to question 1.1 for a short overview).
16
Guido van Rossuma6c707c1995-01-02 17:32:28 +000017Copyright 1993-1995 Guido van Rossum. Unchanged electronic
Guido van Rossuma7925f11994-01-26 10:20:16 +000018redistribution of this FAQ is allowed. Printed redistribution only
19with permission of the author. No warranties.
20
21Author's address:
Guido van Rossuma6c707c1995-01-02 17:32:28 +000022 Guido van Rossum
23 CWI, dept. CST
24 Kruislaan 413
25 P.O. Box 94079
26 1090 GB Amsterdam
27 The Netherlands
Guido van Rossum796b2591995-01-20 23:05:52 +000028Email: <guido@cwi.nl>
Guido van Rossuma7925f11994-01-26 10:20:16 +000029
30The latest version of this FAQ is available by anonymous ftp from
Guido van Rossuma6c707c1995-01-02 17:32:28 +000031<URL:ftp://ftp.cwi.nl/pub/python/python-FAQ>. It will also be posted
32regularly to the newsgroups comp.answers <URL:news:comp.answers> and
33comp.lang.python <URL:news:comp.lang.python>.
Guido van Rossuma7925f11994-01-26 10:20:16 +000034
Guido van Rossuma6c707c1995-01-02 17:32:28 +000035Many FAQs, including this one, are available by anonymous ftp
36<URL:ftp://rtfm.mit.edu/pub/usenet/news.answers/>. The name under
37which a FAQ is archived appears in the Archive-name line at the top of
38the article. This FAQ is archived as python-faq/part1
39<URL:ftp://rtfm.mit.edu/pub/usenet/news.answers/python-faq/part1>.
Guido van Rossuma7925f11994-01-26 10:20:16 +000040
41There's a mail server on that machine which will send you files from
42the archive by e-mail if you have no ftp access. You send a e-mail
Guido van Rossuma6c707c1995-01-02 17:32:28 +000043message to <mail-server@rtfm.mit.edu> containing the single word help
44in the message body to receive instructions.
Guido van Rossuma7925f11994-01-26 10:20:16 +000045
46This FAQ is divided in the following chapters:
47
48 1. General information and availability
49 2. Python in the real world
50 3. Building Python
51 4. Programming in Python
52 5. Extending Python
53 6. Python's design
54 7. Using Python on non-UNIX platforms
55
56To find the start of a particular chapter, search for the chapter number
57followed by a dot and a space at the beginning of a line (e.g. to
58find chapter 4 in vi, type /^4\. /).
59
60Here's an overview of the questions per chapter:
61
62 1. General information and availability
63 1.1. Q. What is Python?
64 1.2. Q. Why is it called Python?
65 1.3. Q. How do I obtain a copy of the Python source?
66 1.4. Q. How do I get documentation on Python?
Guido van Rossumc50158e1994-05-31 09:18:50 +000067 1.5. Q. Are there other ftp sites that mirror the Python distribution?
68 1.6. Q. Is there a newsgroup or mailing list devoted to Python?
Guido van Rossuma6c707c1995-01-02 17:32:28 +000069 1.7. Q. Is there a WWW page devoted to Python?
70 1.8. Q. Is there a book on Python, or will there be one out soon?
71 1.9. Q. Are there any published articles about Python that I can quote?
72 1.10. Q. Are there short introductory papers or talks on Python?
73 1.11. Q. How does the Python version numbering scheme work?
74 1.12. Q. Are there other ftp sites that carry Python related material?
75 1.13. Q. Are there copyright restrictions on the use of Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +000076
77 2. Python in the real world
78 2.1. Q. How many people are using Python?
79 2.2. Q. Have any significant projects been done in Python?
80 2.3. Q. Are there any commercial projects going on using Python?
Guido van Rossum95f61a71994-01-26 17:23:37 +000081 2.4. Q. How stable is Python?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000082 2.5. Q. When will the next version be released?
83 2.6. Q. What new developments are expected for Python in the future?
84 2.7. Q. Is it reasonable to propose incompatible changes to Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +000085
86 3. Building Python
Guido van Rossum91f60831994-02-15 15:52:27 +000087 3.1. Q. Is there a test set?
88 3.2. Q. When running the test set, I get complaints about floating point
Guido van Rossuma7925f11994-01-26 10:20:16 +000089 operations, but when playing with floating point operations I cannot
90 find anything wrong with them.
Guido van Rossum9351fdb1994-11-10 23:03:51 +000091 3.3. Q. Link errors building Python with STDWIN 0.9.8. on SGI IRIX.
92 3.4. Q. Link errors building Python with STDWIN 0.9.9.
93 3.5. Q. Link errors after rerunning the configure script.
94 3.6. Q. The python interpreter complains about options passed to a
Guido van Rossum24349991994-02-02 14:12:45 +000095 script (after the script name).
Guido van Rossum9351fdb1994-11-10 23:03:51 +000096 3.7. Q. When building on the SGI, make tries to run python to create
Guido van Rossum5333c5d1994-04-11 11:06:22 +000097 glmodule.c, but python hasn't been built or installed yet.
Guido van Rossum9351fdb1994-11-10 23:03:51 +000098 3.8. Q. Python built with gcc for the DEC Alpha doesn't work.
99 3.9. Q. I use VPATH but some targets are built in the source directory.
100 3.10. Q. Trouble building or linking with the GNU readline library.
101 3.11. Q. Trouble building Python on Linux.
102 3.12. Q. Trouble with prototypes on Ultrix.
103 3.13. Q. Trouble with posix.listdir on NeXTSTEP 3.2.
104 3.14. Q. Other trouble building Python on platform X.
Guido van Rossum07779351995-02-07 16:59:56 +0000105 3.15. Q. How to configure dynamic loading on Lixux.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000106
107 4. Programming in Python
Guido van Rossum24349991994-02-02 14:12:45 +0000108 4.1. Q. Is there a source code level debugger with breakpoints, step,
109 etc.?
110 4.2. Q. Can I create an object class with some methods implemented in
Guido van Rossuma7925f11994-01-26 10:20:16 +0000111 C and others in Python (e.g. through inheritance)? (Also phrased as:
112 Can I use a built-in type as base class?)
Guido van Rossum24349991994-02-02 14:12:45 +0000113 4.3. Q. Is there a curses/termcap package for Python?
114 4.4. Q. Is there an equivalent to C's onexit() in Python?
115 4.5. Q. When I define a function nested inside another function, the
Guido van Rossuma7925f11994-01-26 10:20:16 +0000116 nested function seemingly can't access the local variables of the
117 outer function. What is going on? How do I pass local data to a
118 nested function?
Guido van Rossum24349991994-02-02 14:12:45 +0000119 4.6. Q. How do I iterate over a sequence in reverse order?
120 4.7. Q. My program is too slow. How do I speed it up?
121 4.8. Q. When I have imported a module, then edit it, and import it
Guido van Rossuma7925f11994-01-26 10:20:16 +0000122 again (into the same Python process), the changes don't seem to take
123 place. What is going on?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000124 4.9. Q. How do I find the current module name?
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000125 4.10. Q. I have a module in which I want to execute some extra code
126 when it is run as a script. How do I find out whether I am running as
127 a script?
128 4.11. Q. I try to run a program from the Demo directory but it fails
129 with ImportError: No module named ...; what gives?
Guido van Rossum061f1821994-10-06 16:03:45 +0000130 4.12. Q. I have successfully built Python with STDWIN but it can't
131 find some modules (e.g. stdwinevents).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000132 4.13. Q. What GUI toolkits exist for Python?
Guido van Rossum061f1821994-10-06 16:03:45 +0000133 4.14. Q. Are there any interfaces to database packages in Python?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000134 4.15. Q. Is it possible to write obfuscated one-liners in Python?
135 4.16. Q. Is there an equivalent of C's "?:" ternary operator?
136 4.17. Q. My class defines __del__ but it is not called when I delete the
137 object.
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000138 4.18. Q. How do I change the shell environment for programs called
139 using os.popen() or os.system()? Changing os.environ doesn't work.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000140 4.19. Q. What is a class?
141 4.20. Q. What is a method?
142 4.21. Q. What is self?
143 4.22. Q. What is a unbound method?
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000144 4.23. Q. How do I call a method defined in a base class from a derived
145 class that overrides it?
146 4.24. Q. How do I call a method from a base class without using the
147 name of the base class?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000148 4.25. Q. How can I organize my code to make it easier to change the base
149 class?
150 4.26. Q. How can I find the methods or attributes of an object?
Guido van Rossum061f1821994-10-06 16:03:45 +0000151 4.27. Q. I can't seem to use os.read() on a pipe created with os.popen().
152 4.28. Q. How can I create a stand-alone binary from a Python script?
153 4.29. Q. Is there a special lib for writing CGI scripts in Python?
154 4.30. Q. What other WWW tools are there for Python?
Guido van Rossum796b2591995-01-20 23:05:52 +0000155 4.31. Q. How do I run a subprocess with pipes connected to both input
156 and output?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000157
158 5. Extending Python
159 5.1. Q. Can I create my own functions in C?
160 5.2. Q. Can I create my own functions in C++?
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000161 5.3. Q. How can I execute arbitrary Python statements from C?
162 5.4. Q. How can I evaluate an arbitrary Python expression from C?
163 5.5. Q. How do I extract C values from a Python object?
164 5.6. Q. How do I use mkvalue() to create a tuple of arbitrary length?
Guido van Rossum796b2591995-01-20 23:05:52 +0000165 5.7. Q. What happened to mktuple(), featured in an example in the
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000166 Extensions manual?
167 5.8. Q. How do I call an object's method from C?
168 5.9. Q. How do I catch the output from print_error()?
169 5.10. Q. How do I access a module written in Python from C?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000170
171 6. Python's design
172 6.1. Q. Why isn't there a generic copying operation for objects in
173 Python?
174 6.2. Q. Why isn't there a generic way to implement persistent objects
175 in Python? (Persistent == automatically saved to and restored from
176 disk.)
177 6.3. Q. Why isn't there a switch or case statement in Python?
Guido van Rossumc50158e1994-05-31 09:18:50 +0000178 6.4. Q. Why does Python use indentation for grouping of statements?
Guido van Rossum3de27361994-07-25 14:19:33 +0000179 6.5. Q. Why are Python strings immutable?
180 6.6. Q. Why don't strings have methods like index() or sort(), like
181 lists?
182 6.7. Q. Why does Python use methods for some functionality
183 (e.g. list.index()) but functions for other (e.g. len(list))?
184 6.8. Q. Why can't I derive a class from built-in types (e.g. lists or
185 files)?
186 6.9. Q. Why must 'self' be declared and used explicitly in method
187 definitions and calls?
Guido van Rossum061f1821994-10-06 16:03:45 +0000188 6.10. Q. Can't you emulate threads in the interpreter instead of
189 relying on an OS-specific thread implementation?
190 6.11. Q. Why can't lambda forms contain statements?
191 6.12. Q. Why is there no more efficient way of iterating over a dictionary
192 than first constructing the list of keys()?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000193
194 7. Using Python on non-UNIX platforms
Guido van Rossum91f60831994-02-15 15:52:27 +0000195 7.1. Q. Is there a Mac version of Python?
196 7.2. Q. Is there a DOS version of Python?
197 7.3. Q. Is there a Windows version of Python?
198 7.4. Q. Is there a Windows NT version of Python?
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000199 7.5. Q. Is there an OS/2 version of Python?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000200 7.6. Q. Is there a VMS version of Python?
201 7.7. Q. What about IBM mainframes, or other esoteric non-UNIX
202 platforms?
203 7.8. Q. Where are the source or Makefiles for the non-UNIX versions?
204 7.9. Q. What is the status and support for the non-UNIX versions?
205 7.10. Q. I have the PC version but it appears to be only a binary.
206 Where's the library?
207 7.11. Q. Where's the documentation for the Mac or PC version?
208 7.12. Q. The Mac (PC) version doesn't seem to have any facilities for
209 creating or editing programs apart from entering it interactively, and
210 there seems to be no way to save code that was entered interactively.
211 How do I create a Python program on the Mac (PC)?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000212
213To find a particular question, search for the question number followed
214by a dot, a space, and a Q at the beginning of a line (e.g. to find
215question 4.2 in vi, type /^4\.2\. Q/).
216
217
2181. General information and availability
219=======================================
220
2211.1. Q. What is Python?
222
223A. Python is an interpreted, interactive, object-oriented programming
224language. It incorporates modules, exceptions, dynamic typing, very
225high level dynamic data types, and classes. Python combines
226remarkable power with very clear syntax. It has interfaces to many
227system calls and libraries, as well as to various window systems, and
228is extensible in C or C++. It is also usable as an extension language
229for applications that need a programmable interface. Finally, Python
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000230is portable: it runs on many brands of UNIX, on the Mac, and on PCs
231under MS-DOS, Windows, Windows NT, and OS/2.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000232
233To find out more, the best thing to do is to start reading the
234tutorial from the documentation set (see a few questions further
235down).
236
2371.2. Q. Why is it called Python?
238
Guido van Rossum796b2591995-01-20 23:05:52 +0000239A. Apart from being a computer scientist, I'm also a fan of "Monty
Guido van Rossuma7925f11994-01-26 10:20:16 +0000240Python's Flying Circus" (a BBC comedy series from the seventies, in
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000241the -- unlikely -- case you didn't know). It occurred to me one day
242that I needed a name that was short, unique, and slightly mysterious.
243And I happened to be reading some scripts from the series at the
244time... So then I decided to call my language Python. But Python is
245not a joke. And don't you associate it with dangerous reptiles
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000246either! (If you need an icon, use an image of the 16-ton weight from
247the TV series or of a can of SPAM :-)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000248
2491.3. Q. How do I obtain a copy of the Python source?
250
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000251A. The latest complete Python source distribution is always available
252by anonymous ftp, e.g.
253<URL:ftp://ftp.cwi.nl/pub/python/python1.1.tar.gz>. It is a gzipped
254tar file containing the complete C source, LaTeX documentation, Python
255library modules, example programs, and several useful pieces of freely
256distributable software. This will compile and run out of the box on
257most UNIX platforms. (See section 7 for non-UNIX information.)
258
259Occasionally a set of patches is issued which has to be applied using
260the patch program. These patches are placed in the same directory,
261e.g. <URL:ftp://ftp.cwi.nl/pub/python/patch1.1.1>.
262
263An index of said ftp directory can be found in the file INDEX. An
264HTML version of the index can be found in the file index.html,
265<URL:ftp://ftp.cwi.nl/pub/python/index.html>.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000266
2671.4. Q. How do I get documentation on Python?
268
269A. The latest Python documentation set is always available by
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000270anonymous ftp, e.g.
271<URL:ftp://ftp.cwi.nl/pub/python/pythondoc-ps1.1.tar.gz>. It is a
272gzipped tar file containing PostScript files of the reference manual,
Guido van Rossum3de27361994-07-25 14:19:33 +0000273the library manual, and the tutorial. Note that the library manual is
274the most important one of the set, as much of Python's power stems
275from the standard or built-in types, functions and modules, all of
276which are described here. PostScript for a high-level description of
277Python is in the file nluug-paper.ps.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000278
Guido van Rossumc50158e1994-05-31 09:18:50 +00002791.5. Q. Are there other ftp sites that mirror the Python distribution?
280
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000281A. The following anonymous ftp sites keep mirrors of the Python
282distribution:
Guido van Rossuma7925f11994-01-26 10:20:16 +0000283
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000284 <URL:ftp://gatekeeper.dec.com/pub/plan/python/>
285 <URL:ftp://ftp.uu.net/languages/python/>
286 <URL:ftp://ftp.wustl.edu/graphics/graphics/sgi-stuff/python/>
287 <URL:ftp://ftp.funet.fi/pub/languages/python/>
288 <URL:ftp://ftp.sunet.se/pub/lang/python/>
289 <URL:ftp://unix.hensa.ac.uk/uunet/languages/python/>
290 <URL:ftp://ftp.sterlng.com/programming/languages/python/>
291 <URL:ftp://ftp.ibp.fr/pub/python/>
Guido van Rossuma7925f11994-01-26 10:20:16 +0000292
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000293Or try archie on the string "python".
Guido van Rossuma7925f11994-01-26 10:20:16 +0000294
Guido van Rossumc50158e1994-05-31 09:18:50 +00002951.6. Q. Is there a newsgroup or mailing list devoted to Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000296
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000297A. There is a newsgroup, comp.lang.python <URL:news:comp.lang.python>,
298and a mailing list. The newsgroup and mailing list are gatewayed into
299each other -- if you can read news it's unnecessary to subscribe to
300the mailing list. Send e-mail to <python-list-request@cwi.nl> to
Guido van Rossum796b2591995-01-20 23:05:52 +0000301(un)subscribe to the mailing list. Hypermail archives of
Guido van Rossum061f1821994-10-06 16:03:45 +0000302(nearly) everything posted to the mailing list (and thus the
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000303newsgroup) are available on our WWW server,
304<URL:http://www.cwi.nl/~guido/hypermail/index.html>. The raw archives
305are also available by ftp, e.g.
306<URL:ftp://ftp.cwi.nl/pub/python/mailinglist.gz>. The uncompressed
307versions of these files can be read with the standard UNIX Mail
308program ("Mail -f file") or with nn ("nn file"). To read them using
309MH, you could use "inc -file file".
Guido van Rossuma7925f11994-01-26 10:20:16 +0000310
Guido van Rossuma6c707c1995-01-02 17:32:28 +00003111.7. Q. Is there a WWW page devoted to Python?
312
313A. The official Python home page is
314<URL:http://www.cwi.nl/~guido/Python.html>. Michael McLay at NIST
315maintains a Python page at <URL:http://www.eeel.nist.gov/python/>.
316
3171.8. Q. Is there a book on Python, or will there be one out soon?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000318
Guido van Rossum061f1821994-10-06 16:03:45 +0000319A. I am writing one. Addison-Wesley is interested. Optimistically,
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000320it will be ready for the press by mid-1995. Mark Lutz is working on
321one too -- more details as they emerge.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000322
Guido van Rossuma6c707c1995-01-02 17:32:28 +00003231.9. Q. Are there any published articles about Python that I can quote?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000324
325A. So far the only refereed and published article that describes
326Python in some detail is:
327
328 Guido van Rossum and Jelke de Boer, "Interactively Testing Remote
329 Servers Using the Python Programming Language", CWI Quarterly, Volume
330 4, Issue 4 (December 1991), Amsterdam, pp 283-303.
331
332LaTeX source for this paper is available as part of the Python source
333distribution.
334
Guido van Rossuma6c707c1995-01-02 17:32:28 +00003351.10. Q. Are there short introductory papers or talks on Python?
336
337A. A recent high-level description of Python is:
Guido van Rossuma7925f11994-01-26 10:20:16 +0000338
339 Guido van Rossum, "An Introduction to Python for UNIX/C
340 Programmers", in the proceedings of the NLUUG najaarsconferentie
Guido van Rossum061f1821994-10-06 16:03:45 +0000341 1993 (dutch UNIX users group meeting November 1993).
Guido van Rossuma7925f11994-01-26 10:20:16 +0000342
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000343PostScript for this paper and for the slides used for the accompanying
344presentation is available by ftp as
345<URL:ftp://ftp.cwi.nl/pub/python/nluug-paper.ps> and
346<URL:ftp://ftp.cwi.nl/pub/python/nluug-slides.ps>, respectively.
347
348Slides for a talk on Python that I gave at the Usenix Symposium on
349Very High Level Languages in Santa Fe, NM, USA in October 1995 are
350available as <URL:ftp://ftp.cwi.nl/pub/python/vhll-slides.ps>.
351
3521.11. Q. How does the Python version numbering scheme work?
Guido van Rossum95f61a71994-01-26 17:23:37 +0000353
354A. Python versions are numbered A.B.C. A is the major version number
355-- it is only incremented for major changes in functionality or source
356structure. B is the minor version number, incremented for less
357earth-shattering changes to a release. C is the patchlevel -- it is
358incremented for each new release. Note that in the past, patches have
359added significant changes; in fact the changeover from 0.9.9 to 1.0.0
360was the first time that either A or B changed!
361
Guido van Rossuma6c707c1995-01-02 17:32:28 +00003621.12. Q. Are there other ftp sites that carry Python related material?
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000363
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000364A. An interesting ftp site for Python users is ftp.markv.com; the
365directory pub/python contains a growing collection of interesting
366Python scripts <URL:ftp://ftp.markv.com/pub/python/>. To submit a
367script for inclusion, place it together with a readme file (with
368extension .readme) in the publicly writable directory
369/incoming/python. This service is maintained by Lance Ellinghaus
370<lance@markv.com>. (I've heard complaints about this service not
371being very responsive -- try at your own risk.)
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000372
Guido van Rossuma6c707c1995-01-02 17:32:28 +00003731.13. Q. Are there copyright restrictions on the use of Python?
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000374
375A. Hardly. You can do anything you want with the source, as long as
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000376you leave the copyrights in, and display those copyrights in any
377documentation about Python that you produce. Also, don't use the
378author's institute's name in publicity without prior written
379permission, and don't hold them responsible for anything (read the
380actual copyright for a precise legal wording).
381
382In particular, if you honor the copyright rules, it's OK to use Python
383for commercial use, to sell copies of Python in source or binary form,
384or to sell products that enhance Python or incorporate Python (or part
385of it) in some form. I would still like to know about all commercial
386use of Python!
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000387
Guido van Rossuma7925f11994-01-26 10:20:16 +0000388
3892. Python in the real world
390===========================
391
3922.1. Q. How many people are using Python?
393
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000394A. I don't know, but the maximum number of simultaneous subscriptions
395to the Python mailing list before it was gatewayed into the newsgroup
396was about 180 (several of which were local redistribution lists). I
397believe that many active Python users don't bother to subscribe to the
398list, and now that there's a newsgroup the mailing list subscription
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000399is even less meaningful. I see new names on the newsgroup all the
400time and my best guess is that there are currently at least several
401thousands of users.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000402
4032.2. Q. Have any significant projects been done in Python?
404
405A. Here at CWI (the home of Python), we have written a 20,000 line
406authoring environment for transportable hypermedia presentations, a
Guido van Rossum5333c5d1994-04-11 11:06:22 +00004075,000 line multimedia teleconferencing tool, as well as many many
408smaller programs.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000409
410The University of Virginia uses Python to control a virtual reality
411engine. Contact: Matt Conway <conway@virginia.edu>.
412
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000413The ILU project at Xerox PARC can generate Python glue for ILU
414interfaces. See <URL:ftp://ftp.parc.xerox.com/pub/ilu/ilu.html>.
415
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000416If you have done a significant project in Python that you'd like to be
417included in the list above, send me email!
Guido van Rossuma7925f11994-01-26 10:20:16 +0000418
4192.3. Q. Are there any commercial projects going on using Python?
420
421A. Several companies have revealed to me that they are planning or
Guido van Rossum796b2591995-01-20 23:05:52 +0000422considering use of Python in a future product.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000423
424The furthest is Sunrise Software, who already have a product out using
425Python -- they use Python for a GUI management application and an SNMP
Guido van Rossum061f1821994-10-06 16:03:45 +0000426network management application. Contact: <info@sunrise.com>.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000427
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000428Infoseek uses Python to implement their commercial WWW information
429retrieval service <URL:http://www.infoseek.com/>. Contact:
430<info@infoseek.com>.
Guido van Rossumf8c76d01994-08-17 12:19:53 +0000431
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000432Michael Powers of daVinci Time & Space is "writing tons-o-python for
433interactive television entertainment." Contact: <powers@dvts.com>.
434
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000435Paul Everitt of Connecting Minds is planning a Lotus Notes gateway.
436Contact: <Paul.Everitt@cminds.com>. Or see their WWW server
437<URL:http://www.cminds.com/>.
438
Guido van Rossuma7925f11994-01-26 10:20:16 +0000439Individuals at many other companies are using Python for
440internal development (witness their contributions to the Python
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000441mailing list or newsgroup).
Guido van Rossuma7925f11994-01-26 10:20:16 +0000442
443Python has also been elected as an extension language by MADE, a
444consortium supported by the European Committee's ESPRIT program and
445consisting of Bull, CWI and some other European companies. Contact:
446Ivan Herman <ivan@cwi.nl>.
447
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000448If you'd like to be included in the list above, send me email!
449
Guido van Rossum95f61a71994-01-26 17:23:37 +00004502.4. Q. How stable is Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000451
Guido van Rossum3de27361994-07-25 14:19:33 +0000452A. Very stable. While the current version number would suggest it is
453in the early stages of development, in fact new, stable releases
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000454(numbered 0.9.x through 1.1.x) have been coming out roughly every 3 to
4556 months for the past four years.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000456
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00004572.5. Q. When will the next version be released?
458
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000459A. I am planning to release 1.2 in January 1995. It will contain
460hooks into the implementation of the import command, a (still limited)
461form of persistent objects, and the usual complement of bug fixes
462(including many fixed memory leaks and thread problems).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000463
4642.6. Q. What new developments are expected for Python in the future?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000465
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000466A. There will be better ports to the Mac, DOS, Windows, Windows NT, and
Guido van Rossumf8c76d01994-08-17 12:19:53 +0000467OS/2. The Windows ports will support dynamically loaded modules using
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000468DLLs.
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000469
470Also planned is improved support for embedding Python in other
471applications, e.g. by renaming most global symbols to have a "Py"
472prefix and providing more documentation and threading support.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000473
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000474Some proposals were discussed at the recent Python workshop:
475
476 - persistent objects
477
478 - safe execution of untrusted Python code
479
480 - extensions to the import statement for managing packages
481 (groups of related modules)
482
483 - automatic generation of C/C++ interface glue
484
485 - interfaces to OMG IDL (== Interface Definition Language by
486 the Object Management Group)
487
488 - a portable GUI API (Graphical User Interface Application
489 Programmers Interface)
490
491 - module customization tools
492
493 - standardized documentation strings on module, class and
494 function objects
495
496 - the formation of a Python Steering Committee
497
498 - another Python Workshop
499
500For more info, have a look at the WWW page for the last Python
501Workshop <URL:http://www.eeel.nist.gov/python/workshop11-94/>.
502
503
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00005042.7. Q. Is it reasonable to propose incompatible changes to Python?
Guido van Rossum3de27361994-07-25 14:19:33 +0000505
506A. In general, no. There are already millions of lines of Python code
507around the world, so any changes in the language that invalidates more
508than a very small fraction of existing programs has to be frowned
509upon. Even if you can provide a conversion program, there still is
510the problem of updating all documentation. Providing a gradual
511upgrade path is the only way if a feature has to be changed.
512
Guido van Rossuma7925f11994-01-26 10:20:16 +0000513
5143. Building Python
515==================
516
Guido van Rossum91f60831994-02-15 15:52:27 +00005173.1. Q. Is there a test set?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000518
519A. Yes, simply do "import testall" (or "import autotest" if you aren't
520interested in the output). The standard modules whose name begins
521with "test" together comprise the test. The test set doesn't test
522*all* features of Python but it goes a long way to confirm that a new
523port is actually working. The Makefile contains an entry "make test"
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000524which runs the autotest module. NOTE: if "make test" fails, run the
525tests manually ("import testall") to see what goes wrong before
526reporting the error.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000527
Guido van Rossum91f60831994-02-15 15:52:27 +00005283.2. Q. When running the test set, I get complaints about floating point
Guido van Rossuma7925f11994-01-26 10:20:16 +0000529operations, but when playing with floating point operations I cannot
530find anything wrong with them.
531
532A. The test set makes occasional unwarranted assumptions about the
533semantics of C floating point operations. Until someone donates a
534better floating point test set, you will have to comment out the
535offending floating point tests and execute similar tests manually.
536
Guido van Rossum061f1821994-10-06 16:03:45 +00005373.3. Q. Link errors building Python with STDWIN 0.9.8. on SGI IRIX.
Guido van Rossum24349991994-02-02 14:12:45 +0000538
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000539A. 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 +0000540
Guido van Rossum9351fdb1994-11-10 23:03:51 +00005413.4. Q. Link errors building Python with STDWIN 0.9.9.
Guido van Rossum061f1821994-10-06 16:03:45 +0000542
Guido van Rossum796b2591995-01-20 23:05:52 +0000543A. The linker probably complains that it can't find routines like
544'tecreate', 'tenew' etc. The STDWIN 0.9.9 distribution requires that
545you add TWO libraries from stdwin to the line for stdwin in the
546Setupfile. Use something like this (all on one line!):
Guido van Rossum061f1821994-10-06 16:03:45 +0000547
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000548 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 +0000549
Guido van Rossum9351fdb1994-11-10 23:03:51 +00005503.5. Q. Link errors after rerunning the configure script.
Guido van Rossum24349991994-02-02 14:12:45 +0000551
552A. It is generally necessary to run "make clean" after a configuration
553change.
554
Guido van Rossum9351fdb1994-11-10 23:03:51 +00005553.6. Q. The python interpreter complains about options passed to a
Guido van Rossum24349991994-02-02 14:12:45 +0000556script (after the script name).
557
558A. You are probably linking with GNU getopt, e.g. through -liberty.
Guido van Rossumf8c76d01994-08-17 12:19:53 +0000559Don't. The reason for the complaint is that GNU getopt, unlike System
560V getopt and other getopt implementations, doesn't consider a
561non-option to be the end of the option list. A quick (and compatible)
562fix for scripts is to add "--" to the interpreter, like this:
563
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000564 #! /usr/local/bin/python --
Guido van Rossumf8c76d01994-08-17 12:19:53 +0000565
566You can also use this interactively:
567
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000568 python -- script.py [options]
569
570Note that a working getopt implementation is provided in the Python
571distribution (in Python/getopt.c) but not automatically used.
Guido van Rossum24349991994-02-02 14:12:45 +0000572
Guido van Rossum9351fdb1994-11-10 23:03:51 +00005733.7. Q. When building on the SGI, make tries to run python to create
Guido van Rossum24349991994-02-02 14:12:45 +0000574glmodule.c, but python hasn't been built or installed yet.
575
576A. Comment out the line mentioning glmodule.c in Setup and build a
577python without gl first; install it or make sure it is in your $PATH,
578then edit the Setup file again to turn on the gl module, and make
579again. You don't need to do "make clean"; you do need to run "make
580Makefile" in the Modules subdirectory (or just run "make" at the
581toplevel).
582
Guido van Rossum9351fdb1994-11-10 23:03:51 +00005833.8. Q. Python built with gcc for the DEC Alpha doesn't work.
Guido van Rossum3de27361994-07-25 14:19:33 +0000584
Guido van Rossum07779351995-02-07 16:59:56 +0000585People have reported problems with gcc 2.5.8 up to 2.6.2. The
586DEC OSF/1 cc compiler does not have these problems so it's probably
587gcc's fault. The latest news is that the problem still exists but
588only when you use -O and only in the Modules subdirectory. So one
589solution is to compile everything except Modules with gcc -O and then
590do the Modules directory with gcc without -O. If you find a version
591of gcc which doesn't have this bug or if you can reduce it further to
592a particular file, please let me know.
Guido van Rossum3de27361994-07-25 14:19:33 +0000593
Guido van Rossum9351fdb1994-11-10 23:03:51 +00005943.9. Q. I use VPATH but some targets are built in the source directory.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000595
596A. On some systems (e.g. Sun), if the target already exists in the
597source directory, it is created there instead of in the build
598directory. This is usually because you have previously built without
599VPATH. Try running "make clobber" in the source directory.
600
Guido van Rossum9351fdb1994-11-10 23:03:51 +00006013.10. Q. Trouble building or linking with the GNU readline library.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000602
603A. Consider using readline 2.0. From the Python 1.1 README:
604
605- You can use the GNU readline library to improve the interactive user
606interface: this gives you line editing and command history when
Guido van Rossum796b2591995-01-20 23:05:52 +0000607calling python interactively. You need to configure and build the GNU
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000608readline library before running the configure script. Its sources are
609no longer distributed with Python; you can ftp them from any GNU
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000610mirror site, or from its home site
611<URL:ftp://slc2.ins.cwru.edu/pub/dist/readline-2.0.tar.gz> (or a
612higher version number -- using version 1.x is not recommended). Pass
613the Python configure script the option --with-readline=DIRECTORY where
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000614DIRECTORY is the absolute pathname of the directory where you've built
615the readline library. Some hints on building and using the readline
616library:
617
618- On SGI IRIX 5, you may have to add the following
619to rldefs.h:
620
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000621 #ifndef sigmask
622 #define sigmask(sig) (1L << ((sig)-1))
623 #endif
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000624
625- On most systems, you will have to add #include "rldefs.h" to the
626top of several source files, and if you use the VPATH feature, you
627will have to add dependencies of the form foo.o: foo.c to the
628Makefile for several values of foo.
629
630- The readline library requires use of the termcap library. A
631known problem with this is that it contains entry points which
Guido van Rossum061f1821994-10-06 16:03:45 +0000632cause conflicts with the STDWIN and SGI GL libraries. The STDWIN
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000633conflict can be solved (and will be, in the next release of
Guido van Rossum061f1821994-10-06 16:03:45 +0000634STDWIN) by adding a line saying '#define werase w_erase' to the
635stdwin.h file (in the STDWIN distribution, subdirectory H). The
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000636GL conflict has been solved in the Python configure script by a
637hack that forces use of the static version of the termcap library.
638
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000639- Check the newsgroup gnu.bash.bugs <URL:news:gnu.bash.bugs> for
640specific problems with the readline library (I don't get this group
641here but I've been told that it is the place for readline bugs.)
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000642
Guido van Rossum9351fdb1994-11-10 23:03:51 +00006433.11. Q. Trouble building Python on Linux.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000644
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000645A. (Adapted from Bennet Todd:)
646
647It seems to work for some people but not for others.
648
649This might depend on which Linux release you're using. Older Linux
650releases had (understandably) fewer gracious hack to improve Unix
651compatibility; really old Linux was Posix 1003.1 compatible, without
652nearly as much Unix compatibility as one might have wished. Current
653releases of Linux build most current free software, either BSD or
654System V, with little or no trouble.
655
656Besides that, there are quite a few different releases currently,
657including MCC, Slackware, SLS, and Debian, and probably more. The
658differences between their compilation environments are far smaller
659than they used to be, but they're still updated with different
660frequencies. It's likely that the current Slackware works fine, as it
661currently seems to be the most popular. But that's just a guess.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000662
Guido van Rossum9351fdb1994-11-10 23:03:51 +00006633.12. Q. Trouble with prototypes on Ultrix.
Guido van Rossum72eb83c1994-10-07 11:33:28 +0000664
665A. Ultrix cc seems broken -- use gcc, or edit config.h to #undef
666HAVE_PROTOTYPES.
667
Guido van Rossum9351fdb1994-11-10 23:03:51 +00006683.13. Q. Trouble with posix.listdir on NeXTSTEP 3.2.
669
Guido van Rossum07779351995-02-07 16:59:56 +0000670A. You need to pass "-posix" to the C compiler, especially in the link
671phase -- it uses the correct version of the C library. The easiest
672way is perhaps to do "make OPT=-posix". You also need to remove this
Guido van Rossum796b2591995-01-20 23:05:52 +0000673NeXT-specific section from import.c:
Guido van Rossum9351fdb1994-11-10 23:03:51 +0000674
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000675 #if defined(NeXT) || defined(WITH_RLD)
676 #define DYNAMIC_LINK
677 #define USE_RLD
678 #endif
Guido van Rossum9351fdb1994-11-10 23:03:51 +0000679
6803.14. Q. Other trouble building Python on platform X.
Guido van Rossum95f61a71994-01-26 17:23:37 +0000681
Guido van Rossum796b2591995-01-20 23:05:52 +0000682A. Please email the details to <guido@cwi.nl> and I'll look into it.
683Please provide as many details as possible. In particular, if you
684don't tell me what type of computer and what operating system (and
685version) you are using it will be difficult for me to figure out what
686is the matter. If you get a specific error message, please email it
687to me too.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000688
Guido van Rossum07779351995-02-07 16:59:56 +00006893.15. Q. How to configure dynamic loading on Lixux.
690
691A. (Thanks to Andrew Kuchling):
692
693Linux requires that you use the GNU DLD library. The stages of using
694dynamic libraries on Linux are:
695
6961) Get dld from [some -- see below] Linux site. I believe the version
697number is 3.2.4. Be careful here; the most recent GNU version is
6983.2.3, and doesn't support Linux; be sure to get it from a Linux
699mirror, not a GNU mirror. Compile it and install the library libdld.a
700somewhere; I used /usr/local/lib.
701
7022) Get Jack Jansen's DL library; its location is given in the
703_Extending Python_ manual as ftp://ftp.cwi.nl/pub/dynload/. Compile
704it and install libdl.a wherever you put libdld.a.
705
7063) Run Python's configure script, giving it the --with-dl-dld option,
707which requires a parameter giving the directory where you put the
708libraries.
709
7104) Recompile Python.
711
712If you run into trouble with this, send me some mail and we can work
713on the problem.
714
715I don't use ELF binaries, because I'd rather wait until everything is
716reliable; my current impression (and please correct me if I'm wrong)
717is that gcc-elf is still a bit wonky, and you have to compile the
718entire GNU library yourself. I'd much rather wait until ELF is a more
719standard part of Linux distributions. I suspect that once ELF is
720common, Python's configure script will pick up the dlfcn.h header file
721and dynamic linking will work automatically. Time will tell,
722however...
723
724It turns out that Linux DLD is not on sunsite.unc.edu; instead it's
725located at:
726<URL:ftp://tsx-11.mit.edu/pub/linux/sources/libs/dld-3.2.4.src.tar.gz>
727
728tsx-11 has mirrors; off the top of my head, one is ftp.uu.net, where
729the URL would be:
730<URL:ftp://ftp.uu.net/systems/unix/linux/sources/libs/dld-3.2.4.src.tar.gz>
731
732
733
Guido van Rossuma7925f11994-01-26 10:20:16 +0000734
7354. Programming in Python
736========================
737
Guido van Rossum24349991994-02-02 14:12:45 +00007384.1. Q. Is there a source code level debugger with breakpoints, step,
739etc.?
740
741A. Yes. Check out module pdb; pdb.help() prints the documentation (or
742you can read it as Lib/pdb.doc). If you use the STDWIN option,
743there's also a windowing interface, wdb. You can write your own
744debugger by using the code for pdb or wdb as an example.
745
7464.2. Q. Can I create an object class with some methods implemented in
Guido van Rossuma7925f11994-01-26 10:20:16 +0000747C and others in Python (e.g. through inheritance)? (Also phrased as:
748Can I use a built-in type as base class?)
749
750A. No, but you can easily create a Python class which serves as a
751wrapper around a built-in object, e.g. (for dictionaries):
752
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000753 # A user-defined class behaving almost identical
754 # to a built-in dictionary.
755 class UserDict:
756 def __init__(self): self.data = {}
757 def __repr__(self): return repr(self.data)
758 def __cmp__(self, dict):
759 if type(dict) == type(self.data):
760 return cmp(self.data, dict)
761 else:
762 return cmp(self.data, dict.data)
763 def __len__(self): return len(self.data)
764 def __getitem__(self, key): return self.data[key]
765 def __setitem__(self, key, item): self.data[key] = item
766 def __delitem__(self, key): del self.data[key]
767 def keys(self): return self.data.keys()
768 def items(self): return self.data.items()
769 def values(self): return self.data.values()
770 def has_key(self, key): return self.data.has_key(key)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000771
Guido van Rossum24349991994-02-02 14:12:45 +00007724.3. Q. Is there a curses/termcap package for Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000773
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000774A. Yes -- Lance Ellinghaus has written a module that interfaces to
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000775System V's "ncurses". If you know a little curses and some Python,
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000776it's straightforward to use. It is part of the standard Python
777distribution, but not configured by default -- you must enable it by
778editing Modules/Setup. It requires a System V curses implementation.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000779
780You could also consider using the "alfa" (== character cell) version
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000781of STDWIN. (Standard Window System Interface, a portable windowing
782system interface by myself <URL:ftp://ftp.cwi.nl/pub/stdwin/>.) This
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000783will also prepare your program for porting to windowing environments
784such as X11 or the Macintosh.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000785
Guido van Rossum24349991994-02-02 14:12:45 +00007864.4. Q. Is there an equivalent to C's onexit() in Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000787
788A. Yes, if you import sys and assign a function to sys.exitfunc, it
789will be called when your program exits, is killed by an unhandled
790exception, or (on UNIX) receives a SIGHUP or SIGTERM signal.
791
Guido van Rossum24349991994-02-02 14:12:45 +00007924.5. Q. When I define a function nested inside another function, the
Guido van Rossuma7925f11994-01-26 10:20:16 +0000793nested function seemingly can't access the local variables of the
794outer function. What is going on? How do I pass local data to a
795nested function?
796
797A. Python does not have arbitrarily nested scopes. When you need to
798create a function that needs to access some data which you have
799available locally, create a new class to hold the data and return a
800method of an instance of that class, e.g.:
801
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000802 class MultiplierClass:
803 def __init__(self, factor):
804 self.factor = factor
805 def multiplier(self, argument):
806 return argument * self.factor
Guido van Rossuma7925f11994-01-26 10:20:16 +0000807
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000808 def generate_multiplier(factor):
809 return MultiplierClass(factor).multiplier
Guido van Rossuma7925f11994-01-26 10:20:16 +0000810
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000811 twice = generate_multiplier(2)
812 print twice(10)
813 # Output: 20
814
815An alternative solution uses default arguments, e.g.:
816
817 def generate_multiplier(factor):
818 def multiplier(arg, fact = factor):
819 return arg*fact
820 return multiplier
821
822 twice = generate_multiplier(2)
823 print twice(10)
824 # Output: 20
Guido van Rossuma7925f11994-01-26 10:20:16 +0000825
Guido van Rossum24349991994-02-02 14:12:45 +00008264.6. Q. How do I iterate over a sequence in reverse order?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000827
828A. If it is a list, the fastest solution is
829
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000830 list.reverse()
831 try:
832 for x in list:
833 "do something with x"
834 finally:
835 list.reverse()
Guido van Rossuma7925f11994-01-26 10:20:16 +0000836
837This has the disadvantage that while you are in the loop, the list
838is temporarily reversed. If you don't like this, you can make a copy.
839This appears expensive but is actually faster than other solutions:
840
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000841 rev = list[:]
842 rev.reverse()
843 for x in rev:
844 <do something with x>
Guido van Rossuma7925f11994-01-26 10:20:16 +0000845
846If it isn't a list, a more general but slower solution is:
847
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000848 i = len(list)
849 while i > 0:
850 i = i-1
851 x = list[i]
852 <do something with x>
Guido van Rossuma7925f11994-01-26 10:20:16 +0000853
854A more elegant solution, is to define a class which acts as a sequence
855and yields the elements in reverse order (solution due to Steve
856Majewski):
857
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000858 class Rev:
859 def __init__(self, seq):
860 self.forw = seq
861 def __len__(self):
862 return len(self.forw)
863 def __getitem__(self, i):
864 return self.forw[-(i + 1)]
Guido van Rossuma7925f11994-01-26 10:20:16 +0000865
866You can now simply write:
867
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000868 for x in Rev(list):
869 <do something with x>
Guido van Rossuma7925f11994-01-26 10:20:16 +0000870
Guido van Rossum061f1821994-10-06 16:03:45 +0000871Unfortunately, this solution is slowest of all, due to the method
Guido van Rossuma7925f11994-01-26 10:20:16 +0000872call overhead...
873
Guido van Rossum24349991994-02-02 14:12:45 +00008744.7. Q. My program is too slow. How do I speed it up?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000875
876A. That's a tough one, in general. There are many tricks to speed up
877Python code; I would consider rewriting parts in C only as a last
878resort. One thing to notice is that function and (especially) method
879calls are rather expensive; if you have designed a purely OO interface
880with lots of tiny functions that don't do much more than get or set an
881instance variable or call another method, you may consider using a
882more direct way, e.g. directly accessing instance variables. Also see
883the standard module "profile" (described in the file
884"python/lib/profile.doc") which makes it possible to find out where
885your program is spending most of its time (if you have some patience
886-- the profiling itself can slow your program down by an order of
887magnitude).
888
Guido van Rossum24349991994-02-02 14:12:45 +00008894.8. Q. When I have imported a module, then edit it, and import it
Guido van Rossuma7925f11994-01-26 10:20:16 +0000890again (into the same Python process), the changes don't seem to take
891place. What is going on?
892
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000893A. For reasons of efficiency as well as consistency, Python only reads
Guido van Rossum796b2591995-01-20 23:05:52 +0000894the module file on the first time a module is imported. (Otherwise a
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000895program consisting of many modules, each of which imports the same
Guido van Rossum796b2591995-01-20 23:05:52 +0000896basic module, would read the basic module over and over again.) To
897force rereading of a changed module, do this:
Guido van Rossuma7925f11994-01-26 10:20:16 +0000898
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000899 import modname
900 reload(modname)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000901
902Warning: this technique is not 100% fool-proof. In particular,
903modules containing statements like
904
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000905 from modname import some_objects
Guido van Rossuma7925f11994-01-26 10:20:16 +0000906
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000907will continue to work with the old version of the imported objects.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000908
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00009094.9. Q. How do I find the current module name?
Guido van Rossum24349991994-02-02 14:12:45 +0000910
Guido van Rossum061f1821994-10-06 16:03:45 +0000911A. A module can find out its own module name by looking at the
Guido van Rossum24349991994-02-02 14:12:45 +0000912(predefined) global variable __name__. If this has the value
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000913'__main__' you are running as a script.
914
Guido van Rossuma6c707c1995-01-02 17:32:28 +00009154.10. Q. I have a module in which I want to execute some extra code
916when it is run as a script. How do I find out whether I am running as
917a script?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000918
919A. See the previous question. E.g. if you put the following on the
920last line of your module, main() is called only when your module is
921running as a script:
Guido van Rossum24349991994-02-02 14:12:45 +0000922
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000923 if __name__ == '__main__': main()
Guido van Rossum24349991994-02-02 14:12:45 +0000924
Guido van Rossuma6c707c1995-01-02 17:32:28 +00009254.11. Q. I try to run a program from the Demo directory but it fails
926with ImportError: No module named ...; what gives?
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000927
Guido van Rossum061f1821994-10-06 16:03:45 +0000928A. This is probably an optional module (written in C!) which hasn't
929been configured on your system. This especially happens with modules
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000930like "Tkinter", "stdwin", "gl", "Xt" or "Xm". For Tkinter, STDWIN and
931many other modules, see Modules/Setup.in for info on how to add these
932modules to your Python, if it is possible at all. Sometimes you will
933have to ftp and build another package first (e.g. STDWIN). Sometimes
934the module only works on specific platforms (e.g. gl only works on SGI
935machines).
936
937NOTE: if the complaint is about "Tkinter" (upper case T) and you have
938already configured module "tkinter" (lower case t), the solution is
939*not* to rename tkinter to Tkinter or vice versa. There is probably
Guido van Rossum796b2591995-01-20 23:05:52 +0000940something wrong with your module search path. Check out the value of
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000941sys.path.
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000942
Guido van Rossum061f1821994-10-06 16:03:45 +0000943For X-related modules (Xt and Xm) you will have to do more work: they
944are currently not part of the standard Python distribution. You will
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000945have to ftp the Extensions tar file, e.g.
946<URL:ftp://ftp.cwi.nl/pub/python/extensions.tar.gz> and follow the
947instructions there. Note: the X related modules are still somewhat
948flakey, so don't try this unless you alread know a bit or two about
949building X applications on your platform.
Guido van Rossum061f1821994-10-06 16:03:45 +0000950
951See also the next question.
952
9534.12. Q. I have successfully built Python with STDWIN but it can't
954find some modules (e.g. stdwinevents).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000955
956A. There's a subdirectory of the library directory named 'stdwin'
957which should be in the default module search path. There's a line in
958Modules/Setup(.in) that you have to enable for this purpose --
Guido van Rossum061f1821994-10-06 16:03:45 +0000959unfortunately in the latest release it's not near the other
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000960STDWIN-related lines so it's easy to miss it.
961
9624.13. Q. What GUI toolkits exist for Python?
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000963
964A. Depending on what platform(s) you are aiming at, there are several.
965
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000966- There's a neat object-oriented interface to the Tcl/Tk widget set,
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000967called Tkinter. As of python 1.1, it is part of the standard Python
968distribution -- all you need to do is enable it in Modules/Setup
969(provided you have already installed Tk and Tcl). This is probably
970the easiest to install and use, and the most complete widget set. It
971is also very likely that in the future the standard Python GUI API
972will be based on or at least look very much like the Tkinter
973interface. For more info about Tk, including pointers to the source,
974see John Ousterhout's home page
Guido van Rossum796b2591995-01-20 23:05:52 +0000975<URL:http://playground.sun.com/~ouster/>.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000976
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000977- The standard Python distribution comes with an interface to STDWIN,
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000978a platform-independent low-level windowing interface. You have to ftp
979the source for STDWIN separately,
980e.g. <URL:ftp://ftp.cwi.nl/pub/stdwin/> or gatekeeper.dec.com in
981pub/misc/stdwin <URL:ftp://gatekeeper.dec.com/pub/misc/stdwin/>.
982STDWIN runs under X11 or the Mac; a Windows port has been attempted
983but I can't seem to get it working. Note that STDWIN is really not
984powerful enough to implement a modern GUI (no widgets, etc.) and that
985I don't have the time to maintain or extend it, so you may be better
986off using Tkinter or the Motif interface, unless you require
987portability to the Mac (which is also offered by SUIT, by the way --
988see below).
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000989
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000990- For SGI IRIX only, there's an interface to the complete GL (Graphics
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000991Library -- low level but very good 3D capabilities) as well as to
992FORMS (a buttons-and-sliders-etc package built on top of GL by Mark
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000993Overmars -- ftp'able from <URL:ftp://ftp.cs.ruu.nl/pub/SGI/FORMS/>).
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000994
995- There's an interface to X11, including the Athena and Motif widget
996sets (and a few individual widgets, like Mosaic's HTML widget and
997SGI's GL widget) in the Extensions set, which is separately ftp'able
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000998<URL:ftp://ftp.cwi.nl/pub/python/extensions.tar.gz>.
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000999
1000- There's an interface to SUIT, the U of Virginia's Simple User
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001001Interface Toolkit; it can be ftp'ed from
1002<URL:ftp://uvacs.cs.virginia.edu/pub/suit/python/SUIT_python.tar.Z>.
1003A PC binary of Python 1.0.2 compiled with DJGPP and with SUIT support
1004built-in has been made available by Antonio Costa
1005<URL:ftp://asterix.inescn.pt/pub/PC/python/pyt102su.exe> (a
1006self-extracting archive). Note that the UVa people themselves have
1007expressed doubts about SUIT, and are planning to build a Python GUI
1008API based upon Tk (though not necessarily on Tkinter); see
1009<URL:http://server.cs.virginia.edu/~tnb2d/IT/IT.html>.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001010
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001011- There's an interface to WAFE, a Tcl interface to the X11 Motif and
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001012Athena widget sets. Last I heard about it it was included in the WAFE
10131.0 prerelease
1014<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 +00001015
Guido van Rossum061f1821994-10-06 16:03:45 +000010164.14. Q. Are there any interfaces to database packages in Python?
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001017
1018A. There's an interface to SYBASE by John Redford
1019<jredford@lehman.com>.
1020
Guido van Rossum796b2591995-01-20 23:05:52 +00001021There's an interface to metalbase by Lance Ellinghaus
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001022<lance@markv.com>; it is part of the separate Extensions distribution
1023<URL:ftp://ftp.cwi.nl/pub/python/extensions.tar.gz>.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001024
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001025Anthony Baxter <anthony.baxter@aaii.oz.au> has written an interface to
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001026mSQL (mini-SQL). <URL:ftp://ftp.cwi.nl/pub/python/PymSQL.tar.gz>.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001027
Guido van Rossum796b2591995-01-20 23:05:52 +00001028Tom Culliton <culliton@clark.net> has written an Oracle module. Write
Guido van Rossum07779351995-02-07 16:59:56 +00001029him to get a copy of the latest version.
Guido van Rossum796b2591995-01-20 23:05:52 +00001030
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000010314.15. Q. Is it possible to write obfuscated one-liners in Python?
Guido van Rossumc24da7c1994-09-23 14:08:41 +00001032
1033A. Yes. See the following three examples, due to Ulf Bartelt:
1034
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001035 # Primes < 1000
1036 print filter(None,map(lambda y:y*reduce(lambda x,y:x*y!=0,
1037 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 +00001038
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001039 # First 10 Fibonacci numbers
1040 print map(lambda x,f=lambda x,f:(x<=1) or (f(x-1,f)+f(x-2,f)): f(x,f),
1041 range(10))
Guido van Rossumc24da7c1994-09-23 14:08:41 +00001042
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001043 # Mandelbrot set
1044 print (lambda Ru,Ro,Iu,Io,IM,Sx,Sy:reduce(lambda x,y:x+y,map(lambda y,
1045 Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,Sy=Sy,L=lambda yc,Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,i=IM,
1046 Sx=Sx,Sy=Sy:reduce(lambda x,y:x+y,map(lambda x,xc=Ru,yc=yc,Ru=Ru,Ro=Ro,
1047 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
1048 >=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(
1049 64+F(Ru+x*(Ro-Ru)/Sx,yc,0,0,i)),range(Sx))):L(Iu+y*(Io-Iu)/Sy),range(Sy
1050 ))))(-2.1, 0.7, -1.2, 1.2, 30, 80, 24)
1051 # \___ ___/ \___ ___/ | | |__ lines on screen
1052 # V V | |______ columns on screen
1053 # | | |__________ maximum of "iterations"
1054 # | |_________________ range on y axis
1055 # |____________________________ range on x axis
Guido van Rossumc24da7c1994-09-23 14:08:41 +00001056
1057Don't try this at home, kids!
1058
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000010594.16. Q. Is there an equivalent of C's "?:" ternary operator?
Guido van Rossumc24da7c1994-09-23 14:08:41 +00001060
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001061A. Not directly. In many cases you can mimic a?b:c with "a and b or
1062c", but there's a flaw: if b is zero (or empty, or None -- anything
1063that tests false) then c will be selected instead. In many cases you
1064can prove by looking at the code that this can't happen (e.g. because
1065b is a constant or has a type that can never be false), but in general
1066this can be a problem.
1067
1068Steve Majewski (or was it Tim Peters?) suggested the following
1069solution: (a and [b] or [c])[0]. Because [b] is a singleton list it
1070is never false, so the wrong path is never taken; then applying [0] to
1071the whole thing gets the b or c that you really wanted. Ugly, but it
1072gets you there in the rare cases where it is really inconvenient to
1073rewrite your code using 'if'.
1074
10754.17. Q. My class defines __del__ but it is not called when I delete the
1076object.
1077
1078A. There are several possible reasons for this.
1079
1080- The del statement does not necessarily call __del__ -- it simply
1081decrements the object's reference count, and if this reaches zero
1082__del__ is called.
1083
1084- If your data structures contain circular links (e.g. a tree where
1085each child has a parent pointer and each parent has a list of
1086children) the reference counts will never go back to zero. You'll
Guido van Rossum061f1821994-10-06 16:03:45 +00001087have to define an explicit close() method which removes those
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001088pointers. Please don't ever call __del__ directly -- __del__ should
Guido van Rossum061f1821994-10-06 16:03:45 +00001089call close() and close() should make sure that it can be called more
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001090than once for the same object.
1091
1092- If the object has ever been a local variable (or argument, which is
1093really the same thing) to a function that caught an expression in an
1094except clause, chances are that a reference to the object still exists
1095in that function's stack frame as contained in the stack trace.
1096Normally, deleting (better: assigning None to) sys.exc_traceback will
1097take care of this. If you a stack was printed for an unhandled
1098exception in an interactive interpreter, delete sys.last_traceback
1099instead.
1100
1101- There is code that deletes all objects when the interpreter exits,
1102but if your Python has been configured to support threads, it is not
1103called (because other threads may still be active). You can define
Guido van Rossum061f1821994-10-06 16:03:45 +00001104your own cleanup function using sys.exitfunc (see question 4.4).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001105
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001106- Finally, there are some obscure bugs if your __del__ method does
1107complicated things such as updating dictionaries or lists or
1108references globals. I hope to have fixed these in release 1.2.
1109
11104.18. Q. How do I change the shell environment for programs called
1111using os.popen() or os.system()? Changing os.environ doesn't work.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001112
1113A. Modifying the environment passed to subshells was left out of the
1114interpreter because there seemed to be no well-established portable
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001115way to do it (in particular, some systems, have putenv(), others have
1116setenv(), and some have none at all).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001117
1118However if all you want is to pass environment variables to the
1119commands run by os.system() or os.popen(), there's a simple solution:
1120prefix the command string with a couple of variable assignments and
Guido van Rossum796b2591995-01-20 23:05:52 +00001121export statements. The following would be universal for popen:
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001122
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001123 import os
1124 from commands import mkarg # nifty routine to add shell quoting
1125 def epopen(cmd, mode, env = {}):
1126 # env is a dictionary of environment variables
1127 prefix = ''
Guido van Rossum796b2591995-01-20 23:05:52 +00001128 for key, value in env.items():
1129 prefix = prefix + '%s=%s\n' % (key, mkarg(value)[1:])
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001130 prefix = prefix + 'export %s\n' % key
1131 return os.popen(prefix + cmd, mode)
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001132
11334.19. Q. What is a class?
1134
1135A. A class is the particular object type that is created by executing
Guido van Rossum796b2591995-01-20 23:05:52 +00001136a class statement. Class objects are used as templates, to create
1137class instance objects, which embody both the data structure and
1138program routines specific to a datatype.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001139
11404.20. Q. What is a method?
1141
1142A. A method is a function that you normally call as
Guido van Rossum796b2591995-01-20 23:05:52 +00001143x.name(arguments...) for some object x. The term is used for methods
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001144of classes and class instances as well as for methods of built-in
Guido van Rossum796b2591995-01-20 23:05:52 +00001145objects. (The latter have a completely different implementation and
1146only share the way their calls look in Python code.) Methods of
1147classes (and class instances) are defined as functions inside the
1148class definition.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001149
11504.21. Q. What is self?
1151
1152A. Self is merely a conventional name for the first argument of a
1153method -- i.e. a function defined inside a class definition. A method
1154defined as meth(self, a, b, c) should be called as x.meth(a, b, c) for
1155some instance x of the class in which the definition occurs;
1156the called method will think it is called as meth(x, a, b, c).
1157
11584.22. Q. What is a unbound method?
1159
1160A. An unbound method is a method defined in a class that is not yet
1161bound to an instance. You get an unbound method if you ask for a
1162class attribute that happens to be a function. You get a bound method
1163if you ask for an instance attribute. A bound method knows which
Guido van Rossum061f1821994-10-06 16:03:45 +00001164instance it belongs to and calling it supplies the instance automatically;
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001165an unbound method only knows which class it wants for its first
1166argument (a derived class is also OK). Calling an unbound method
1167doesn't "magically" derive the first argument from the context -- you
1168have to provide it explicitly.
1169
Guido van Rossuma6c707c1995-01-02 17:32:28 +000011704.23. Q. How do I call a method defined in a base class from a derived
1171class that overrides it?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001172
1173A. If your class definition starts with "class Derived(Base): ..."
1174then you can call method meth defined in Base (or one of Base's base
1175classes) as Base.meth(self, arguments...). Here, Base.meth is an
1176unbound method (see previous question).
1177
Guido van Rossuma6c707c1995-01-02 17:32:28 +000011784.24. Q. How do I call a method from a base class without using the
1179name of the base class?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001180
1181A. DON'T DO THIS. REALLY. I MEAN IT. It appears that you could call
1182self.__class__.__bases__[0].meth(self, arguments...) but this fails when
Guido van Rossum061f1821994-10-06 16:03:45 +00001183a doubly-derived method is derived from your class: for its instances,
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001184self.__class__.__bases__[0] is your class, not its base class -- so
1185(assuming you are doing this from within Derived.meth) you would start
1186a recursive call.
1187
11884.25. Q. How can I organize my code to make it easier to change the base
1189class?
1190
1191A. You could define an alias for the base class, assign the real base
1192class to it before your class definition, and use the alias throughout
1193your class. Then all you have to change is the value assigned to the
1194alias. Incidentally, this trick is also handy if you want to decide
1195dynamically (e.g. depending on availability of resources) which base
1196class to use. Example:
1197
1198BaseAlias = <real base class>
1199class Derived(BaseAlias):
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001200 def meth(self):
1201 BaseAlias.meth(self)
1202 ...
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001203
12044.26. Q. How can I find the methods or attributes of an object?
1205
1206A. This depends on the object type.
1207
1208For an instance x of a user-defined class, instance attributes are
1209found in the dictionary x.__dict__, and methods and attributes defined
1210by its class are found in x.__class__.__bases__[i].__dict__ (for i in
1211range(len(x.__class__.__bases__))). You'll have to walk the tree of
1212base classes to find *all* class methods and attributes.
1213
1214Many, but not all built-in types define a list of their method names
1215in x.__methods__, and if they have data attributes, their names may be
1216found in x.__members__. However this is only a convention.
1217
1218For more information, read the source of the standard (but
1219undocumented) module newdir.
1220
12214.27. Q. I can't seem to use os.read() on a pipe created with os.popen().
1222
1223A. os.read() is a low-level function which takes a file descriptor (a
1224small integer). os.popen() creates a high-level file object -- the
1225same type used for sys.std{in,out,err} and returned by the builtin
1226open() function. Thus, to read n bytes from a pipe p created with
1227os.popen(), you need to use p.read(n).
Guido van Rossuma7925f11994-01-26 10:20:16 +00001228
Guido van Rossum061f1821994-10-06 16:03:45 +000012294.28. Q. How can I create a stand-alone binary from a Python script?
1230
1231The demo script "Demo/scripts/freeze.py" does what you want. (It's
1232actually not a demo but a support tool -- there is some extra code in
1233the interpreter to accommodate it.) It requires that you have the
1234Python build tree handy, complete with all the lib*.a files.
1235
1236This works by scanning your source recursively for import statements
1237(both forms) and looking for the modules on the standard Python path
1238as well as in the source directory (for built-in modules). It then
1239"compiles" the modules written in Python to C code (array initializers
1240that can be turned into code objects using the marshal module) and
1241creates a custom-made config file that only contains those built-in
1242modules which are actually used in the program. It then compiles the
1243generated C code and links it with the rest of the Python interpreter
1244to form a self-contained binary which acts exactly like your script.
1245
Guido van Rossum061f1821994-10-06 16:03:45 +000012464.29. Q. Is there a special lib for writing CGI scripts in Python?
1247
1248A. There's documentation and code for a cgi.py module by Michael McLay
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001249<mclay@eeel.nist.gov> available from
1250<URL:http://www.eeel.nist.gov/python/>
Guido van Rossum061f1821994-10-06 16:03:45 +00001251
1252(For the curious: CGI or Common Gateway Interface is the protocol
1253between HTTP servers (WWW servers) and programs/scripts they run to
1254perform queries and other tasks that require returning a dynamically
1255generated document.)
1256
12574.30. Q. What other WWW tools are there for Python?
1258
1259A. The standard library has a module urllib, which can retrieve most
1260commonly used URL types (file, ftp, http, gopher).
1261
1262The Demo2/www directory (Demo2 has to be retrieved separately from the
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001263Python ftp sites <URL:ftp://ftp.cwi.nl/pub/python/demo2.tar.gz>)
1264contains some (really old) code to parse HTML and to display it.
Guido van Rossum061f1821994-10-06 16:03:45 +00001265
1266Steve Miale <smiale@cs.indiana.edu> has written a modular WWW browser
1267called Dancer. An alpha version can be FTP'ed from
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001268<URL:ftp://ftp.cs.indiana.edu/pub/smiale/dancer.tar.gz>. (There are a
1269few articles about Dancer in the (hyper)mail archive
1270<URL:http://www.cwi.nl/~guido/hypermail/python-1994q3/index.html>.)
Guido van Rossum061f1821994-10-06 16:03:45 +00001271
Guido van Rossum796b2591995-01-20 23:05:52 +000012724.31. Q. How do I run a subprocess with pipes connected to both input
1273and output?
1274
1275A. This is really a UNIX question. Also, in general, it is unwise to
1276do so, because you can easily cause a deadlock where the parent
1277process is blocked waiting for output from the child, while the child
1278is blocked waiting for input from the child. This can be caused
1279because the parent expects the child to output more text than it does,
1280or it can be caused by data being stuck in stdio buffers due to lack
1281of flushing. The Python parent can of course explicitly flush the data
1282it sends to the child before it reads any output, but if the child is
1283a naive C program it can easily have been written to never explicitly
1284flush its output, even if it is interactive, since flushing is
1285normally automatic.
1286
1287In many cases, all you really need is to run some data through a
1288command and get the result back. Unless the data is infinite in size,
1289the easiest (and often the most efficient!) way to do this is to write
1290it to a temporary file and run the command with that temporary file as
1291input. The standard module tempfile exports a function mktemp() which
1292generates unique temporary file names.
1293
1294If after reading all of the above you still want to connect two pipes
1295to a subprocess's standard input and output, here's a simple solution,
1296due to Jack Jansen:
1297
1298 import os
1299 import sys
1300 import string
1301
1302 MAXFD = 100 # Max number of file descriptors in this system
1303
1304 def popen2(cmd):
1305 cmd = string.split(cmd)
1306 p2cread, p2cwrite = os.pipe()
1307 c2pread, c2pwrite = os.pipe()
1308 pid = os.fork()
1309 if pid == 0:
1310 # Child
1311 os.close(0)
1312 os.close(1)
1313 if os.dup(p2cread) <> 0:
1314 sys.stderr.write('popen2: bad read dup\n')
1315 if os.dup(c2pwrite) <> 1:
1316 sys.stderr.write('popen2: bad write dup\n')
1317 for i in range(3, MAXFD):
1318 try:
1319 os.close(i)
1320 except:
1321 pass
1322 try:
1323 os.execv(cmd[0], cmd)
1324 finally:
1325 os._exit(1)
1326 os.close(p2cread)
1327 tochild = os.fdopen(p2cwrite, 'w')
1328 os.close(c2pwrite)
1329 fromchild = os.fdopen(c2pread, 'r')
1330 return fromchild, tochild
1331
1332Note that many interactive programs (e.g. vi) don't work well with
1333pipes substituted for standard input and output. You will have to use
1334pseudo ttys ("ptys") instead of pipes. There is some undocumented
1335code to use these in the library module pty.py -- I'm afraid you're on
1336your own here. What's *really* needed is a Python interface to Don
1337Libes' expect library -- any takers?
1338
Guido van Rossum061f1821994-10-06 16:03:45 +00001339
Guido van Rossuma7925f11994-01-26 10:20:16 +000013405. Extending Python
1341===================
1342
13435.1. Q. Can I create my own functions in C?
1344
1345A. Yes, you can create built-in modules containing functions,
Guido van Rossum24349991994-02-02 14:12:45 +00001346variables, exceptions and even new types in C. This is explained in
1347the document "Extending and Embedding the Python Interpreter" (the
1348LaTeX file Doc/ext.tex). Also read the chapter on dynamic loading.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001349
13505.2. Q. Can I create my own functions in C++?
1351
1352A. Yes, using the C-compatibility features found in C++. Basically
1353you place extern "C" { ... } around the Python include files and put
1354extern "C" before each function that is going to be called by the
1355Python interpreter. Global or static C++ objects with constructors
1356are probably not a good idea.
1357
Guido van Rossum7ce61c11994-06-13 15:13:56 +000013585.3. Q. How can I execute arbitrary Python statements from C?
1359
1360A. The highest-level function to do this is run_command() which takes
1361a single string argument which is executed in the context of module
1362__main__ and returns 0 for success and -1 when an exception occurred
1363(including SyntaxError). If you want more control, use run_string();
1364see the source for run_command() in Python/pythonrun.c.
1365
13665.4. Q. How can I evaluate an arbitrary Python expression from C?
1367
1368A. Call the function run_string() from the previous question with the
1369start symbol eval_input; it then parses an expression, evaluates it
1370and returns its value. See exec_eval() in Python/bltinmodule.c.
1371
13725.5. Q. How do I extract C values from a Python object?
1373
1374A. That depends on the object's type. If it's a tuple,
1375gettuplesize(o) returns its length and gettupleitem(o, i) returns its
1376i'th item; similar for lists with getlistsize(o) and getlistitem(o,
1377i). For strings, getstringsize(o) returns its length and
1378getstringvalue(o) a pointer to its value (note that Python strings may
1379contain null bytes so strlen() is not safe). To test which type an
1380object is, first make sure it isn't NULL, and then use
1381is_stringobject(o), is_tupleobject(o), is_listobject(o) etc.
1382
13835.6. Q. How do I use mkvalue() to create a tuple of arbitrary length?
1384
1385A. You can't. Use t = newtupleobject(n) instead, and fill it with
1386objects using settupleitem(t, i, o) -- note that this "eats" a
1387reference count of o. Similar for lists with newlistobject(n) and
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001388setlistitem(l, i, o). Note that you *must* set all the tuple items to
1389some value before you pass the tuple to Python code --
1390newtupleobject(n) initializes them to NULL, which isn't a valid Python
1391value.
1392
Guido van Rossum796b2591995-01-20 23:05:52 +000013935.7. Q. What happened to mktuple(), featured in an example in the
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001394Extensions manual?
1395
1396A. It's a typo, I meant newtupleobject() (see previous question).
1397
13985.8. Q. How do I call an object's method from C?
1399
1400A. Here's a function (untested) that might become part of the next
1401release in some form. It uses <stdarg.h> to allow passing the
1402argument list on to vmkvalue():
1403
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001404 object *call_method(object *inst, char *methodname, char *format, ...)
1405 {
1406 object *method;
1407 object *args;
1408 object *result;
1409 va_list va;
1410 method = getattr(inst, methodname);
1411 if (method == NULL) return NULL;
1412 va_start(va, format);
1413 args = vmkvalue(format, va);
1414 va_end(va);
1415 if (args == NULL) {
1416 DECREF(method);
1417 return NULL;
1418 }
1419 result = call_object(method, args);
1420 DECREF(method);
1421 DECREF(args);
1422 return result;
1423 }
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001424
1425This works for any instance that has methods -- whether built-in or
1426user-defined. You are responsible for eventually DECREF'ing the
1427return value.
1428
1429To call, e.g., a file object's "seek" method with arguments 10, 0
1430(assuming the file object pointer is "f"):
1431
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001432 res = call_method(f, "seek", "(OO)", 10, 0);
1433 if (res == NULL) {
1434 ... an exception occurred ...
1435 }
1436 else {
1437 DECREF(res);
1438 }
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001439
1440Note that since call_object() *always* wants a tuple for the argument
1441list, to call a function without arguments, pass "()" for the format,
1442and to call a function with one argument, surround the argument in
1443parentheses, e.g. "(i)".
1444
14455.9. Q. How do I catch the output from print_error()?
1446
1447A. (Due to Mark Hammond):
1448
1449* in Python code, define an object that supports the "write()" method.
1450FWIW, there seems to be a small problem that requires the 'softspace'
Guido van Rossum061f1821994-10-06 16:03:45 +00001451attribute to be defined too (I can't remember exact details of the
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001452problem).
1453
1454* redirect sys.stdout and sys.stderr to this object.
1455
Guido van Rossum061f1821994-10-06 16:03:45 +00001456* call print_error, or just allow the standard traceback mechanism to
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001457work.
1458
Guido van Rossum061f1821994-10-06 16:03:45 +00001459Then, the output will go wherever your write() method sends it.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001460
14615.10. Q. How do I access a module written in Python from C?
1462
1463A. You can get a pointer to the module object as follows:
1464
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001465 module = import_module("<modulename>");
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001466
1467If the module hasn't been imported yet (i.e. it is not yet present in
1468sys.modules), this initializes the module; otherwise it simply returns
1469the value of sys.modules["<modulename>"]. Note that it doesn't enter
1470the module into any namespace -- it only ensures it has been
1471initialized and is stored in sys.modules.
1472
1473You can then access the module's attributes (i.e. any name defined in
1474the module) as follows:
1475
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001476 attr = getattr(module, "<attrname>");
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001477
1478Calling setattr(), to assign to variables in the module, also works.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001479
Guido van Rossuma7925f11994-01-26 10:20:16 +00001480
14816. Python's design
1482==================
1483
14846.1. Q. Why isn't there a generic copying operation for objects in
1485Python?
1486
1487A. Hmm. Maybe there should be one, but it's difficult to assign a
1488useful meaning to copying of open files, sockets and windows, or
1489recursive data structures. As long as you design all your classes
1490yourself you are of course free to define a standard base class that
1491defines an overridable copying operation for all the objects you care
1492about. (One practical point: it would have to be a built-in function,
1493not a standard method name, since not all built-in object types have
1494methods; e.g. strings, integers and tuples don't.)
1495
14966.2. Q. Why isn't there a generic way to implement persistent objects
1497in Python? (Persistent == automatically saved to and restored from
1498disk.)
1499
1500A. Hmm, hmm. Basically for the same reasons as why there is no
1501generic copying operation.
1502
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001503A partial solution will appear in release 1.2. This will also provide
1504a partial solution to the problem of a generic copying operation.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001505
Guido van Rossuma7925f11994-01-26 10:20:16 +000015066.3. Q. Why isn't there a switch or case statement in Python?
1507
1508A. You can do this easily enough with a sequence of
1509if... elif... elif... else. There have been some proposals for switch
Guido van Rossum061f1821994-10-06 16:03:45 +00001510statement syntax, but there is no consensus (yet) on whether and how
Guido van Rossuma7925f11994-01-26 10:20:16 +00001511to do range tests.
1512
Guido van Rossumc50158e1994-05-31 09:18:50 +000015136.4. Q. Why does Python use indentation for grouping of statements?
1514
1515A. Basically I believe that using indentation for grouping is
1516extremely elegant and contributes a lot to the clarity of the average
1517Python program. Most people learn to love this feature after a while.
1518Some arguments for it:
1519
1520- Since there are no begin/end brackets there cannot be a disagreement
1521between grouping perceived by the parser and the human reader. I
1522remember long ago seeing a C fragment like this:
1523
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001524 if (x <= y)
1525 x++;
1526 y--;
1527 z++;
Guido van Rossumc50158e1994-05-31 09:18:50 +00001528
1529and staring a long time at it wondering why y was being decremented
1530even for x > y... (And I wasn't a C newbie then either.)
1531
1532- Since there are no begin/end brackets there can be no conflicting
1533coding styles. In C there are loads of different ways to place the
1534braces (including the choice whether to place braces around single
1535statements in certain cases, for consistency). If you're used to
1536reading (and writing) code that uses one style, you will feel at least
1537slightly uneasy when reading (or being required to write) another
1538style.
1539
1540- Many coding styles place begin/end brackets on a line by themself.
1541This makes programs considerably longer and wastes valuable screen
1542space, making it harder to get a good overview over a program.
1543Ideally, a function should fit on one basic tty screen (say, 20
1544lines). 20 lines of Python are worth a LOT more than 20 lines of C.
1545This is not solely due to the lack of begin/end brackets (the lack of
1546declarations also helps, and the powerful operations of course), but
1547it certainly helps!
1548
Guido van Rossum3de27361994-07-25 14:19:33 +000015496.5. Q. Why are Python strings immutable?
1550
1551A. There are two advantages. One is performance: knowing that a
1552string is immutable makes it easy to lay it out at construction time
1553-- fixed and unchanging storage requirements. (This is also one of
Guido van Rossum061f1821994-10-06 16:03:45 +00001554the reasons for the distinction between tuples and lists.) The
Guido van Rossum3de27361994-07-25 14:19:33 +00001555other is that strings in Python are considered as "elemental" as
1556numbers. No amount of activity will change the value 8 to anything
1557else, and in Python, no amount of activity will change the string
1558"eight" to anything else. (Adapted from Jim Roskind)
1559
15606.6. Q. Why don't strings have methods like index() or sort(), like
1561lists?
1562
1563A. Good question. Strings currently don't have methods at all
1564(likewise tuples and numbers). Long ago, it seemed unnecessary to
1565implement any of these functions in C, so a standard library module
1566"string" written in Python was created that performs string related
1567operations. Since then, the cry for performance has moved most of
1568them into the built-in module strop (this is imported by module
Guido van Rossumf8c76d01994-08-17 12:19:53 +00001569string, which is still the preferred interface, without loss of
Guido van Rossum3de27361994-07-25 14:19:33 +00001570performance except during initialization). Some of these functions
1571(e.g. index()) could easily be implemented as string methods instead,
1572but others (e.g. sort()) can't, since their interface prescribes that
1573they modify the object, while strings are immutable (see the previous
1574question).
1575
15766.7. Q. Why does Python use methods for some functionality
1577(e.g. list.index()) but functions for other (e.g. len(list))?
1578
1579A. Functions are used for those operations that are generic for a
1580group of types and which should work even for objects that don't have
1581methods at all (e.g. numbers, strings, tuples). Also, implementing
1582len(), max(), min() as a built-in function is actually less code than
1583implementing them as methods for each type. One can quibble about
1584individual cases but it's really too late to change such things
1585fundamentally now.
1586
15876.8. Q. Why can't I derive a class from built-in types (e.g. lists or
1588files)?
1589
1590A. This is caused by the relatively late addition of (user-defined)
1591classes to the language -- the implementation framework doesn't easily
1592allow it. See the answer to question 4.2 for a work-around. This
1593*may* be fixed in the (distant) future.
1594
15956.9. Q. Why must 'self' be declared and used explicitly in method
1596definitions and calls?
1597
1598A. By asking this question you reveal your C++ background. :-)
1599When I added classes, this was (again) the simplest way of
1600implementing methods without too many changes to the interpreter. I
1601borrowed the idea from Modula-3. It turns out to be very useful, for
1602a variety of reasons.
1603
1604First, it makes it more obvious that you are using a method or
1605instance attribute instead of a local variable. Reading "self.x" or
1606"self.meth()" makes it absolutely clear that an instance variable or
1607method is used even if you don't know the class definition by heart.
1608In C++, you can sort of tell by the lack of a local variable
Guido van Rossum061f1821994-10-06 16:03:45 +00001609declaration (assuming globals are rare or easily recognizable) -- but
Guido van Rossum3de27361994-07-25 14:19:33 +00001610in Python, there are no local variable declarations, so you'd have to
1611look up the class definition to be sure.
1612
1613Second, it means that no special syntax is necessary if you want to
1614explicitly reference or call the method from a particular class. In
1615C++, if you want to use a method from base class that is overridden in
1616a derived class, you have to use the :: operator -- in Python you can
1617write baseclass.methodname(self, <argument list>). This is
1618particularly useful for __init__() methods, and in general in cases
1619where a derived class method wants to extend the base class method of
1620the same name and thus has to call the base class method somehow.
1621
1622Lastly, for instance variables, it solves a syntactic problem with
1623assignment: since local variables in Python are (by definition!) those
1624variables to which a value assigned in a function body (and that
1625aren't explicitly declared global), there has to be some way to tell
1626the interpreter that an assignment was meant to assign to an instance
1627variable instead of to a local variable, and it should preferably be
1628syntactic (for efficiency reasons). C++ does this through
1629declarations, but Python doesn't have declarations and it would be a
1630pity having to introduce them just for this purpose. Using the
1631explicit "self.var" solves this nicely. Similarly, for using instance
1632variables, having to write "self.var" means that references to
1633unqualified names inside a method don't have to search the instance's
1634directories.
1635
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000016366.10. Q. Can't you emulate threads in the interpreter instead of
1637relying on an OS-specific thread implementation?
1638
1639A. Unfortunately, the interpreter pushes at least one C stack frame
1640for each Python stack frame. Also, extensions can call back into
1641Python at almost random moments. Therefore a complete threads
1642implementation requires thread support for C.
1643
Guido van Rossum061f1821994-10-06 16:03:45 +000016446.11. Q. Why can't lambda forms contain statements?
1645
1646A. Python lambda forms cannot contain statements because Python's
Guido van Rossum796b2591995-01-20 23:05:52 +00001647syntactic framework can't handle statements nested inside expressions.
Guido van Rossum061f1821994-10-06 16:03:45 +00001648
1649However, in Python, this is not a serious problem. Unlike lambda
1650forms in other languages, where they add functionality, Python lambdas
1651are only a shorthand notation if you're too lazy to define a function.
1652
1653Functions are already first class objects in Python, and can be
1654declared in a local scope. Therefore the only advantage of using a
1655lambda form instead of a locally-defined function is that you'll have
1656to invent a name for the function -- but that's just a local variable
1657to which the function object (which is exactly the same type of object
1658that a lambda form yields) is assigned!
1659
16606.12. Q. Why is there no more efficient way of iterating over a dictionary
1661than first constructing the list of keys()?
1662
1663A. Have you tried it? I bet it's fast enough for your purposes! In
1664most cases such a list takes only a few percent of the space occupied
1665by the dictionary -- it needs only 4 bytes (the size of a pointer) per
1666key -- a dictionary costs 8 bytes per key plus between 30 and 70
1667percent hash table overhead, plus the space for the keys and values --
1668by necessity all keys are unique objects and a string object (the most
1669common key type) costs at least 18 bytes plus the length of the
1670string. Add to that the values contained in the dictionary, and you
1671see that 4 bytes more per item really isn't that much more memory...
1672
1673A call to dict.keys() makes one fast scan over the dictionary
1674(internally, the iteration function does exist) copying the pointers
1675to the key objects into a pre-allocated list object of the right size.
1676The iteration time isn't lost (since you'll have to iterate anyway --
1677unless in the majority of cases your loop terminates very prematurely
1678(which I doubt since you're getting the keys in random order).
1679
1680I don't expose the dictionary iteration operation to Python
1681programmers because the dictionary shouldn't be modified during the
1682entire iteration -- if it is, there's a very small chance that the
1683dictionary is reorganized because the hash table becomes too full, and
1684then the iteration may miss some items and see others twice. Exactly
1685because this only occurs rarely, it would lead to hidden bugs in
1686programs: it's easy never to have it happen during test runs if you
1687only insert or delete a few items per iteration -- but your users will
1688surely hit upon it sooner or later.
1689
Guido van Rossuma7925f11994-01-26 10:20:16 +00001690
16917. Using Python on non-UNIX platforms
1692=====================================
1693
Guido van Rossum91f60831994-02-15 15:52:27 +000016947.1. Q. Is there a Mac version of Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +00001695
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001696A. Yes. It is on most ftp sites carrying Python as python.sea.hqx,
1697e.g. <URL:ftp://ftp.cwi.nl/pub/python/python_1.1.sea.hqx> -- this is a
1698self-extracting archive containing the application binary as well as
1699the Lib modules.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001700
Guido van Rossum91f60831994-02-15 15:52:27 +000017017.2. Q. Is there a DOS version of Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +00001702
Guido van Rossum91f60831994-02-15 15:52:27 +00001703A. Yes. More than one, actually: 16python.exe runs in standard DOS
1704mode on 186 CPUs or higher; 32python.exe uses a DOS extender and only
1705runs on a 386 or higher CPUs. Although 16python.exe does not pass the
1706test set because test_grammar is too big for the parser, it actually
1707has about 270 kbyte of allocatable heap space, which is sufficient for
1708fairly large programs. 32python.exe is distributed as a tar file
1709containing the required DOS extended and 387 emulator. Both are on
1710most ftp sites carrying Python.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001711
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001712The file dosbuild.tar.gz on the standard ftp sites contains
1713rudimentary Makefiles and instructions
1714<URL:ftp://ftp.cwi.nl/pub/python/dosbuild.tar.gz>.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001715
Guido van Rossum91f60831994-02-15 15:52:27 +000017167.3. Q. Is there a Windows version of Python?
1717
1718A. Yes. Use qwpython.exe. The only problem with it: ^C
1719unconditionally kills the entire program -- it does not raise
1720KeyboardInterrupt. You can also run 16python.exe or 32python.exe in a
1721"DOS box", but qwpython.exe appears to be slightly faster.
1722
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001723There aren't any Makefiles at this moment. Sorry.
1724
1725Using Win32s (a free NT compatibility package by Microsoft) you can
1726also use the NT version by Mark Hammond -- the Win32s package is also
1727in that directory (you'll need several MB of disk space to install
1728it). See the next question.
1729
Guido van Rossum91f60831994-02-15 15:52:27 +000017307.4. Q. Is there a Windows NT version of Python?
1731
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001732A. Yes. Mark Hammond <MHammond@cmutual.com.au> has built a full NT
1733port. This supports using DLLs for dynamic loading of Python modules,
1734and includes an interface to the Microsoft Foundation Classes and a
1735Python programming environment using it that's written mostly in
1736Python. See <URL:ftp://ftp.cwi.nl/pub/python/nt/> -- most mirrors
1737will also have this.
Guido van Rossum91f60831994-02-15 15:52:27 +00001738
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001739Sam Rushing <rushing@squirl.oau.org> once announced he knows how to
1740build Python for the Windows NT on the DEC Alpha AXP.
Guido van Rossum061f1821994-10-06 16:03:45 +00001741
1742Note that currently there is no unified compilation environment for
1743all NT platforms -- hopefully Microsoft will fix this with the release
1744of Visual C++ 2.0.
1745
Guido van Rossum7ce61c11994-06-13 15:13:56 +000017467.5. Q. Is there an OS/2 version of Python?
1747
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001748A. Yes. You can ftp it from the usual places as pyth_os2.zip, e.g.
1749<URL:ftp://ftp.cwi.nl/pub/python/pyth_os2.zip>. This contains both an
1750executable and Makefiles for those fortunate enough to have a C
1751compiler.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001752
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000017537.6. Q. Is there a VMS version of Python?
1754
1755A. I think not (yet). This question has been asked on the list
1756several times and I've never seen an answer. Maybe someone with a VMS
1757C compiler could attempt a port? Probably coming up with proper
1758Makefiles, config.h and config.c should be sufficient. If you succeed
1759(or even if you get stuck halfway), please let me know! (Info as of
176023 September 1994.)
1761
17627.7. Q. What about IBM mainframes, or other esoteric non-UNIX
1763platforms?
1764
1765A. Basically, the same story as for VMS... (Info as of 23 September
17661994.)
1767
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000017687.8. Q. Where are the source or Makefiles for the non-UNIX versions?
1769
1770A. The standard sources can (almost) be used. See the previous
1771questions for availability of Makefiles/projects or patches. If you
1772find things in the standard sources that don't work on your particular
1773platform, please let me know and I'll integrate a solution into the
1774next release of the standard sources. If you submit a fix, please use
1775some kind of #ifdef so as to keep the source working for other
1776platforms. In particular, if the patch works around the availability
1777of a particular function of header file, you should mimic the
1778HAVE_... style used by the configure script -- you can then submit a
1779config.h file for a particular platform so there are no absolutely
1780platform-specific #ifdefs in the rest of the sources.
1781
17827.9. Q. What is the status and support for the non-UNIX versions?
1783
1784A. I don't have access to most of these platforms, so in general I am
1785dependent on material submitted by volunteers(*). However I strive to
1786integrate all changes needed to get it to compile on a particular
1787platform back into the standard sources, so porting of the next
1788version to the various non-UNIX platforms should be easy.
1789
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001790(*) For the Macintosh, that volunteer is me, with help from Jack
1791Jansen <jack@cwi.nl>.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001792
17937.10. Q. I have the PC version but it appears to be only a binary.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001794Where's the library?
Guido van Rossuma7925f11994-01-26 10:20:16 +00001795
1796A. You still need to copy the files from the distribution directory
Guido van Rossum91f60831994-02-15 15:52:27 +00001797"python/Lib" to your system. If you don't have the full distribution,
Guido van Rossum3de27361994-07-25 14:19:33 +00001798you can get the file pythonlib<version>.tar.gz from most ftp sites
1799carrying Python; this is a subset of the distribution containing just
Guido van Rossum796b2591995-01-20 23:05:52 +00001800those files, e.g.
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001801<URL:ftp://ftp.cwi.nl/pub/python/pythonlib1.1.tar.gz>.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001802
Guido van Rossum5333c5d1994-04-11 11:06:22 +00001803Once you have installed the library, you need to point sys.path to it.
1804Assuming the library is in C:\misc\python\lib, the following commands
1805will point your Python interpreter to it (note the doubled backslashes
1806-- you can also use single forward slashes instead):
1807
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001808 >>> import sys
1809 >>> sys.path.insert(0, 'C:\\misc\\python\\lib')
1810 >>>
Guido van Rossum5333c5d1994-04-11 11:06:22 +00001811
1812For a more permanent effect, set the environment variable PYTHONPATH,
1813as follows (talking to a DOS prompt):
1814
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001815 C> SET PYTHONPATH=C:\misc\python\lib
Guido van Rossum5333c5d1994-04-11 11:06:22 +00001816
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000018177.11. Q. Where's the documentation for the Mac or PC version?
Guido van Rossuma7925f11994-01-26 10:20:16 +00001818
1819A. There isn't any. The documentation for the Unix version also
Guido van Rossum91f60831994-02-15 15:52:27 +00001820applies to the Mac and PC versions. Where applicable, differences
Guido van Rossuma7925f11994-01-26 10:20:16 +00001821are indicated in the text.
1822
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000018237.12. Q. The Mac (PC) version doesn't seem to have any facilities for
Guido van Rossum91f60831994-02-15 15:52:27 +00001824creating or editing programs apart from entering it interactively, and
1825there seems to be no way to save code that was entered interactively.
1826How do I create a Python program on the Mac (PC)?
Guido van Rossuma7925f11994-01-26 10:20:16 +00001827
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001828A. Use an external editor. On the Mac, BBEdit seems to be a popular
1829no-frills text editor. I work like this: start the interpreter; edit
1830a module file using BBedit; import and test it in the interpreter;
1831edit again in BBedit; then use the built-in function reload() to
1832re-read the imported module; etc.
Guido van Rossum5333c5d1994-04-11 11:06:22 +00001833
1834Regarding the same question for the PC, Kurt Wm. Hemr writes: "While
1835anyone with a pulse could certainly figure out how to do the same on
1836MS-Windows, I would recommend the NotGNU Emacs clone for MS-Windows.
1837Not only can you easily resave and "reload()" from Python after making
1838changes, but since WinNot auto-copies to the clipboard any text you
1839select, you can simply select the entire procedure (function) which
1840you changed in WinNot, switch to QWPython, and shift-ins to reenter
1841the changed program unit."