blob: f0f479d0ee5b9adc36a2b793ba5fa8c81e024df5 [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)
Guido van Rossum7be34a81995-05-31 15:17:12 +00005Reply-to: guido@cnri.reston.va.us (Guido van Rossum)
Guido van Rossum3fc9d731995-07-25 15:10:56 +00006Expires: Fri, 1 Sep 1995 00:00:00 GMT
7Supersedes: <D9G7ow.IDL@cwi.nl>
Guido van Rossuma7925f11994-01-26 10:20:16 +00008Approved: news-answers-request@MIT.Edu
9
10Archive-name: python-faq/part1
Guido van Rossum07779351995-02-07 16:59:56 +000011Submitted-by: Guido van Rossum <guido@cwi.nl>
Guido van Rossum3fc9d731995-07-25 15:10:56 +000012Version: 1.25
13Last-modified: 25 July 1995
Guido van Rossuma7925f11994-01-26 10:20:16 +000014
15This article contains answers to Frequently Asked Questions about
16Python (an object-oriented interpreted programming language -- see
17the answer to question 1.1 for a short overview).
18
Guido van Rossuma6c707c1995-01-02 17:32:28 +000019Copyright 1993-1995 Guido van Rossum. Unchanged electronic
Guido van Rossuma7925f11994-01-26 10:20:16 +000020redistribution of this FAQ is allowed. Printed redistribution only
21with permission of the author. No warranties.
22
23Author's address:
Guido van Rossuma6c707c1995-01-02 17:32:28 +000024 Guido van Rossum
25 CWI, dept. CST
26 Kruislaan 413
27 P.O. Box 94079
28 1090 GB Amsterdam
29 The Netherlands
Guido van Rossum796b2591995-01-20 23:05:52 +000030Email: <guido@cwi.nl>
Guido van Rossuma7925f11994-01-26 10:20:16 +000031
32The latest version of this FAQ is available by anonymous ftp from
Guido van Rossum7be34a81995-05-31 15:17:12 +000033<URL:ftp://ftp.python.org/pub/python/FAQ>. It will also be posted
Guido van Rossuma6c707c1995-01-02 17:32:28 +000034regularly to the newsgroups comp.answers <URL:news:comp.answers> and
35comp.lang.python <URL:news:comp.lang.python>.
Guido van Rossuma7925f11994-01-26 10:20:16 +000036
Guido van Rossuma6c707c1995-01-02 17:32:28 +000037Many FAQs, including this one, are available by anonymous ftp
38<URL:ftp://rtfm.mit.edu/pub/usenet/news.answers/>. The name under
39which a FAQ is archived appears in the Archive-name line at the top of
40the article. This FAQ is archived as python-faq/part1
41<URL:ftp://rtfm.mit.edu/pub/usenet/news.answers/python-faq/part1>.
Guido van Rossuma7925f11994-01-26 10:20:16 +000042
43There's a mail server on that machine which will send you files from
44the archive by e-mail if you have no ftp access. You send a e-mail
Guido van Rossuma6c707c1995-01-02 17:32:28 +000045message to <mail-server@rtfm.mit.edu> containing the single word help
46in the message body to receive instructions.
Guido van Rossuma7925f11994-01-26 10:20:16 +000047
48This FAQ is divided in the following chapters:
49
50 1. General information and availability
51 2. Python in the real world
52 3. Building Python
53 4. Programming in Python
54 5. Extending Python
55 6. Python's design
56 7. Using Python on non-UNIX platforms
57
58To find the start of a particular chapter, search for the chapter number
59followed by a dot and a space at the beginning of a line (e.g. to
60find chapter 4 in vi, type /^4\. /).
61
62Here's an overview of the questions per chapter:
63
64 1. General information and availability
65 1.1. Q. What is Python?
66 1.2. Q. Why is it called Python?
67 1.3. Q. How do I obtain a copy of the Python source?
68 1.4. Q. How do I get documentation on Python?
Guido van Rossumc50158e1994-05-31 09:18:50 +000069 1.5. Q. Are there other ftp sites that mirror the Python distribution?
70 1.6. Q. Is there a newsgroup or mailing list devoted to Python?
Guido van Rossuma6c707c1995-01-02 17:32:28 +000071 1.7. Q. Is there a WWW page devoted to Python?
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +000072 1.8. Q. Is the Python documentation available on the WWW?
73 1.9. Q. Is there a book on Python, or will there be one out soon?
74 1.10. Q. Are there any published articles about Python that I can quote?
75 1.11. Q. Are there short introductory papers or talks on Python?
76 1.12. Q. How does the Python version numbering scheme work?
77 1.13. Q. How do I get a beta test version of Python?
78 1.14. Q. Are there other ftp sites that carry Python related material?
79 1.15. Q. Are there copyright restrictions on the use of Python?
Guido van Rossum7be34a81995-05-31 15:17:12 +000080 1.16. Q. Why was Python created in the first place?
81 1.17. Q. What happened to Tim Peters (the author of python-mode.el, and many
82 entertaining and enlightening pieces of Python email)?
Guido van Rossuma7925f11994-01-26 10:20:16 +000083
84 2. Python in the real world
85 2.1. Q. How many people are using Python?
86 2.2. Q. Have any significant projects been done in Python?
87 2.3. Q. Are there any commercial projects going on using Python?
Guido van Rossum95f61a71994-01-26 17:23:37 +000088 2.4. Q. How stable is Python?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000089 2.5. Q. When will the next version be released?
90 2.6. Q. What new developments are expected for Python in the future?
91 2.7. Q. Is it reasonable to propose incompatible changes to Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +000092
93 3. Building Python
Guido van Rossum91f60831994-02-15 15:52:27 +000094 3.1. Q. Is there a test set?
95 3.2. Q. When running the test set, I get complaints about floating point
Guido van Rossuma7925f11994-01-26 10:20:16 +000096 operations, but when playing with floating point operations I cannot
97 find anything wrong with them.
Guido van Rossum7be34a81995-05-31 15:17:12 +000098 3.3. Q. Link errors after rerunning the configure script.
99 3.4. Q. The python interpreter complains about options passed to a
Guido van Rossum24349991994-02-02 14:12:45 +0000100 script (after the script name).
Guido van Rossum7be34a81995-05-31 15:17:12 +0000101 3.5. Q. When building on the SGI, make tries to run python to create
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000102 glmodule.c, but python hasn't been built or installed yet.
Guido van Rossum7be34a81995-05-31 15:17:12 +0000103 3.6. Q. Python built with gcc for the DEC Alpha doesn't work.
104 3.7. Q. I use VPATH but some targets are built in the source directory.
105 3.8. Q. Trouble building or linking with the GNU readline library.
106 3.9. Q. Trouble building Python on Linux.
107 3.10. Q. Trouble with prototypes on Ultrix.
108 3.11. Q. Trouble with posix.listdir on NeXTSTEP 3.2.
109 3.12. Q. Other trouble building Python on platform X.
110 3.13. Q. How to configure dynamic loading on Lixux.
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000111 3.14. Q. Under Solaris 2.x, using GCC, how do I use shared libraries?
112 3.15. Q. Errors when linking with a shared library containing C++ code.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000113
114 4. Programming in Python
Guido van Rossum24349991994-02-02 14:12:45 +0000115 4.1. Q. Is there a source code level debugger with breakpoints, step,
116 etc.?
117 4.2. Q. Can I create an object class with some methods implemented in
Guido van Rossuma7925f11994-01-26 10:20:16 +0000118 C and others in Python (e.g. through inheritance)? (Also phrased as:
119 Can I use a built-in type as base class?)
Guido van Rossum24349991994-02-02 14:12:45 +0000120 4.3. Q. Is there a curses/termcap package for Python?
121 4.4. Q. Is there an equivalent to C's onexit() in Python?
122 4.5. Q. When I define a function nested inside another function, the
Guido van Rossuma7925f11994-01-26 10:20:16 +0000123 nested function seemingly can't access the local variables of the
124 outer function. What is going on? How do I pass local data to a
125 nested function?
Guido van Rossum24349991994-02-02 14:12:45 +0000126 4.6. Q. How do I iterate over a sequence in reverse order?
127 4.7. Q. My program is too slow. How do I speed it up?
128 4.8. Q. When I have imported a module, then edit it, and import it
Guido van Rossuma7925f11994-01-26 10:20:16 +0000129 again (into the same Python process), the changes don't seem to take
130 place. What is going on?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000131 4.9. Q. How do I find the current module name?
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000132 4.10. Q. I have a module in which I want to execute some extra code
133 when it is run as a script. How do I find out whether I am running as
134 a script?
135 4.11. Q. I try to run a program from the Demo directory but it fails
136 with ImportError: No module named ...; what gives?
Guido van Rossum061f1821994-10-06 16:03:45 +0000137 4.12. Q. I have successfully built Python with STDWIN but it can't
138 find some modules (e.g. stdwinevents).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000139 4.13. Q. What GUI toolkits exist for Python?
Guido van Rossum061f1821994-10-06 16:03:45 +0000140 4.14. Q. Are there any interfaces to database packages in Python?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000141 4.15. Q. Is it possible to write obfuscated one-liners in Python?
142 4.16. Q. Is there an equivalent of C's "?:" ternary operator?
143 4.17. Q. My class defines __del__ but it is not called when I delete the
144 object.
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000145 4.18. Q. How do I change the shell environment for programs called
146 using os.popen() or os.system()? Changing os.environ doesn't work.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000147 4.19. Q. What is a class?
148 4.20. Q. What is a method?
149 4.21. Q. What is self?
150 4.22. Q. What is a unbound method?
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000151 4.23. Q. How do I call a method defined in a base class from a derived
152 class that overrides it?
153 4.24. Q. How do I call a method from a base class without using the
154 name of the base class?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000155 4.25. Q. How can I organize my code to make it easier to change the base
156 class?
157 4.26. Q. How can I find the methods or attributes of an object?
Guido van Rossum061f1821994-10-06 16:03:45 +0000158 4.27. Q. I can't seem to use os.read() on a pipe created with os.popen().
159 4.28. Q. How can I create a stand-alone binary from a Python script?
Guido van Rossum7be34a81995-05-31 15:17:12 +0000160 4.29. Q. What WWW tools are there for Python?
161 4.30. Q. How do I run a subprocess with pipes connected to both input
Guido van Rossum796b2591995-01-20 23:05:52 +0000162 and output?
Guido van Rossum7be34a81995-05-31 15:17:12 +0000163 4.31. Q. How do I call a function if I have the arguments in a tuple?
164 4.32. Q. How do I enable font-lock-mode for Python in Emacs?
165 4.33. Q. Is there an inverse to the format operator (a la C's scanf())?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000166
167 5. Extending Python
168 5.1. Q. Can I create my own functions in C?
169 5.2. Q. Can I create my own functions in C++?
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000170 5.3. Q. How can I execute arbitrary Python statements from C?
171 5.4. Q. How can I evaluate an arbitrary Python expression from C?
172 5.5. Q. How do I extract C values from a Python object?
173 5.6. Q. How do I use mkvalue() to create a tuple of arbitrary length?
Guido van Rossum796b2591995-01-20 23:05:52 +0000174 5.7. Q. What happened to mktuple(), featured in an example in the
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000175 Extensions manual?
176 5.8. Q. How do I call an object's method from C?
177 5.9. Q. How do I catch the output from print_error()?
178 5.10. Q. How do I access a module written in Python from C?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000179
180 6. Python's design
181 6.1. Q. Why isn't there a generic copying operation for objects in
182 Python?
183 6.2. Q. Why isn't there a generic way to implement persistent objects
184 in Python? (Persistent == automatically saved to and restored from
185 disk.)
186 6.3. Q. Why isn't there a switch or case statement in Python?
Guido van Rossumc50158e1994-05-31 09:18:50 +0000187 6.4. Q. Why does Python use indentation for grouping of statements?
Guido van Rossum3de27361994-07-25 14:19:33 +0000188 6.5. Q. Why are Python strings immutable?
189 6.6. Q. Why don't strings have methods like index() or sort(), like
190 lists?
191 6.7. Q. Why does Python use methods for some functionality
192 (e.g. list.index()) but functions for other (e.g. len(list))?
193 6.8. Q. Why can't I derive a class from built-in types (e.g. lists or
194 files)?
195 6.9. Q. Why must 'self' be declared and used explicitly in method
196 definitions and calls?
Guido van Rossum061f1821994-10-06 16:03:45 +0000197 6.10. Q. Can't you emulate threads in the interpreter instead of
198 relying on an OS-specific thread implementation?
199 6.11. Q. Why can't lambda forms contain statements?
200 6.12. Q. Why is there no more efficient way of iterating over a dictionary
201 than first constructing the list of keys()?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000202
203 7. Using Python on non-UNIX platforms
Guido van Rossum91f60831994-02-15 15:52:27 +0000204 7.1. Q. Is there a Mac version of Python?
205 7.2. Q. Is there a DOS version of Python?
Guido van Rossume530c581995-04-10 12:32:16 +0000206 7.3. Q. Is there a Windows 3.1(1) version of Python?
Guido van Rossum91f60831994-02-15 15:52:27 +0000207 7.4. Q. Is there a Windows NT version of Python?
Guido van Rossume530c581995-04-10 12:32:16 +0000208 7.5. Q. Is there a Windows 95 version of Python?
209 7.6. Q. Is there an OS/2 version of Python?
210 7.7. Q. Is there a VMS version of Python?
211 7.8. Q. What about IBM mainframes, or other esoteric non-UNIX
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000212 platforms?
Guido van Rossume530c581995-04-10 12:32:16 +0000213 7.9. Q. Where are the source or Makefiles for the non-UNIX versions?
214 7.10. Q. What is the status and support for the non-UNIX versions?
215 7.11. Q. I have a PC version but it appears to be only a binary.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000216 Where's the library?
Guido van Rossume530c581995-04-10 12:32:16 +0000217 7.12. Q. Where's the documentation for the Mac or PC version?
218 7.13. Q. The Mac (PC) version doesn't seem to have any facilities for
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000219 creating or editing programs apart from entering it interactively, and
220 there seems to be no way to save code that was entered interactively.
221 How do I create a Python program on the Mac (PC)?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000222
223To find a particular question, search for the question number followed
224by a dot, a space, and a Q at the beginning of a line (e.g. to find
225question 4.2 in vi, type /^4\.2\. Q/).
226
227
2281. General information and availability
229=======================================
230
2311.1. Q. What is Python?
232
233A. Python is an interpreted, interactive, object-oriented programming
234language. It incorporates modules, exceptions, dynamic typing, very
235high level dynamic data types, and classes. Python combines
236remarkable power with very clear syntax. It has interfaces to many
237system calls and libraries, as well as to various window systems, and
238is extensible in C or C++. It is also usable as an extension language
239for applications that need a programmable interface. Finally, Python
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000240is portable: it runs on many brands of UNIX, on the Mac, and on PCs
241under MS-DOS, Windows, Windows NT, and OS/2.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000242
243To find out more, the best thing to do is to start reading the
244tutorial from the documentation set (see a few questions further
245down).
246
2471.2. Q. Why is it called Python?
248
Guido van Rossum796b2591995-01-20 23:05:52 +0000249A. Apart from being a computer scientist, I'm also a fan of "Monty
Guido van Rossuma7925f11994-01-26 10:20:16 +0000250Python's Flying Circus" (a BBC comedy series from the seventies, in
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000251the -- unlikely -- case you didn't know). It occurred to me one day
252that I needed a name that was short, unique, and slightly mysterious.
253And I happened to be reading some scripts from the series at the
254time... So then I decided to call my language Python. But Python is
255not a joke. And don't you associate it with dangerous reptiles
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000256either! (If you need an icon, use an image of the 16-ton weight from
257the TV series or of a can of SPAM :-)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000258
2591.3. Q. How do I obtain a copy of the Python source?
260
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000261A. The latest complete Python source distribution is always available
262by anonymous ftp, e.g.
Guido van Rossum7be34a81995-05-31 15:17:12 +0000263<URL:ftp://ftp.python.org/pub/python/src/python1.2.tar.gz>. It is a
Guido van Rossume530c581995-04-10 12:32:16 +0000264gzipped tar file containing the complete C source, LaTeX
265documentation, Python library modules, example programs, and several
266useful pieces of freely distributable software. This will compile and
267run out of the box on most UNIX platforms. (See section 7 for
268non-UNIX information.)
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000269
270Occasionally a set of patches is issued which has to be applied using
271the patch program. These patches are placed in the same directory,
Guido van Rossum7be34a81995-05-31 15:17:12 +0000272e.g. <URL:ftp://ftp.python.org/pub/python/src/patch1.1.1>. (At the time
Guido van Rossume530c581995-04-10 12:32:16 +0000273of writing, no patches exist for 1.2.)
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000274
275An index of said ftp directory can be found in the file INDEX. An
276HTML version of the index can be found in the file index.html,
Guido van Rossum7be34a81995-05-31 15:17:12 +0000277<URL:ftp://ftp.python.org/pub/python/index.html>.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000278
2791.4. Q. How do I get documentation on Python?
280
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000281A. The LaTeX source for the documentation is part of the source
282distribution. If you don't have LaTeX, the latest Python
283documentation set is always available by anonymous ftp, e.g.
Guido van Rossum7be34a81995-05-31 15:17:12 +0000284<URL:ftp://ftp.python.org/pub/python/doc/postscript.tar.gz>. It is a
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000285gzipped tar file containing PostScript files of the reference manual,
Guido van Rossum3de27361994-07-25 14:19:33 +0000286the library manual, and the tutorial. Note that the library manual is
287the most important one of the set, as much of Python's power stems
288from the standard or built-in types, functions and modules, all of
289which are described here. PostScript for a high-level description of
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000290Python is in the file nluug-paper.ps (a separate file on the ftp
291site).
Guido van Rossuma7925f11994-01-26 10:20:16 +0000292
Guido van Rossumc50158e1994-05-31 09:18:50 +00002931.5. Q. Are there other ftp sites that mirror the Python distribution?
294
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000295A. The following anonymous ftp sites keep mirrors of the Python
296distribution:
Guido van Rossuma7925f11994-01-26 10:20:16 +0000297
Guido van Rossum7be34a81995-05-31 15:17:12 +0000298 <URL:ftp://ftp.cwi.nl/pub/python/>
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000299 <URL:ftp://gatekeeper.dec.com/pub/plan/python/>
300 <URL:ftp://ftp.uu.net/languages/python/>
301 <URL:ftp://ftp.wustl.edu/graphics/graphics/sgi-stuff/python/>
302 <URL:ftp://ftp.funet.fi/pub/languages/python/>
303 <URL:ftp://ftp.sunet.se/pub/lang/python/>
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000304 <URL:ftp://unix.hensa.ac.uk/mirrors/uunet/languages/python/>
305 <URL:ftp://ftp.sterling.com/programming/languages/python/>
306 <URL:ftp://ftp.ibp.fr/pub/python/>
307 <URL:ftp://ftp.switch.ch/software/sources/python/>
308 <URL:ftp://ftp.informatik.tu-muenchen.de/pub/comp/programming/languages/python/>
Guido van Rossuma7925f11994-01-26 10:20:16 +0000309
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000310Or try archie on the string "python".
Guido van Rossuma7925f11994-01-26 10:20:16 +0000311
Guido van Rossumc50158e1994-05-31 09:18:50 +00003121.6. Q. Is there a newsgroup or mailing list devoted to Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000313
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000314A. There is a newsgroup, comp.lang.python <URL:news:comp.lang.python>,
315and a mailing list. The newsgroup and mailing list are gatewayed into
316each other -- if you can read news it's unnecessary to subscribe to
317the mailing list. Send e-mail to <python-list-request@cwi.nl> to
Guido van Rossume530c581995-04-10 12:32:16 +0000318(un)subscribe to the mailing list. Hypermail archives of (nearly)
319everything posted to the mailing list (and thus the newsgroup) are
320available on our WWW server,
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000321<URL:http://www.cwi.nl/~guido/hypermail/index.html>. The raw archives
322are also available by ftp, e.g.
Guido van Rossum7be34a81995-05-31 15:17:12 +0000323<URL:ftp://ftp.python.org/pub/python/mail/mailinglist.gz>. The
Guido van Rossume530c581995-04-10 12:32:16 +0000324uncompressed versions of these files can be read with the standard
325UNIX Mail program ("Mail -f file") or with nn ("nn file"). To read
326them using MH, you could use "inc -file file".
Guido van Rossuma7925f11994-01-26 10:20:16 +0000327
Guido van Rossuma6c707c1995-01-02 17:32:28 +00003281.7. Q. Is there a WWW page devoted to Python?
329
Guido van Rossum7be34a81995-05-31 15:17:12 +0000330A. Yes, <URL:http://www.python.org/> is the official Python home page.
331At the time of writing, this page is not yet completely operational;
332you may have a look at the old Python home page:
333<URL:http://www.cwi.nl/~guido/Python.html> or at the U.S. copy:
334<URL:http://www.python.org/~guido/Python.html>.
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000335
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00003361.8. Q. Is the Python documentation available on the WWW?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000337
Guido van Rossum7be34a81995-05-31 15:17:12 +0000338A. Yes, see <URL:http://www.python.org/> (Python's home page). It
339contains pointers to hypertext versions of the whole documentation set
340(as hypertext, not just PostScript).
Guido van Rossuma7925f11994-01-26 10:20:16 +0000341
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000342If you wish to browse this collection of HTML files on your own
343machine, it is available bundled up by anonymous ftp,
Guido van Rossum7be34a81995-05-31 15:17:12 +0000344e.g. <URL:ftp://ftp.python.org/pub/python/doc/html.tar.gz>.
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000345
346An Emacs-INFO set containing the library manual is also available by
Guido van Rossum7be34a81995-05-31 15:17:12 +0000347ftp, e.g. <URL:ftp://ftp.python.org/pub/python/doc/lib-info.tar.gz>.
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000348
3491.9. Q. Is there a book on Python, or will there be one out soon?
350
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000351A. Mark Lutz is writing a Python book for O'Reilly and Associates, to
352be published early 1996. See the outline (in PostScript):
353<URL:http://www.python.org/workshops/1995-05/outlinep.eps>.
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000354
3551.10. Q. Are there any published articles about Python that I can quote?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000356
357A. So far the only refereed and published article that describes
358Python in some detail is:
359
360 Guido van Rossum and Jelke de Boer, "Interactively Testing Remote
361 Servers Using the Python Programming Language", CWI Quarterly, Volume
362 4, Issue 4 (December 1991), Amsterdam, pp 283-303.
363
364LaTeX source for this paper is available as part of the Python source
365distribution.
366
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00003671.11. Q. Are there short introductory papers or talks on Python?
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000368
369A. A recent high-level description of Python is:
Guido van Rossuma7925f11994-01-26 10:20:16 +0000370
371 Guido van Rossum, "An Introduction to Python for UNIX/C
372 Programmers", in the proceedings of the NLUUG najaarsconferentie
Guido van Rossum061f1821994-10-06 16:03:45 +0000373 1993 (dutch UNIX users group meeting November 1993).
Guido van Rossuma7925f11994-01-26 10:20:16 +0000374
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000375PostScript for this paper and for the slides used for the accompanying
376presentation is available by ftp as
Guido van Rossum7be34a81995-05-31 15:17:12 +0000377<URL:ftp://ftp.python.org/pub/python/doc/nluug-paper.ps> and
378<URL:ftp://ftp.python.org/pub/python/doc/nluug-slides.ps>, respectively.
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000379
380Slides for a talk on Python that I gave at the Usenix Symposium on
381Very High Level Languages in Santa Fe, NM, USA in October 1995 are
Guido van Rossum7be34a81995-05-31 15:17:12 +0000382available as <URL:ftp://ftp.python.org/pub/python/doc/vhll-slides.ps>.
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000383
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00003841.12. Q. How does the Python version numbering scheme work?
Guido van Rossum95f61a71994-01-26 17:23:37 +0000385
386A. Python versions are numbered A.B.C. A is the major version number
387-- it is only incremented for major changes in functionality or source
388structure. B is the minor version number, incremented for less
389earth-shattering changes to a release. C is the patchlevel -- it is
390incremented for each new release. Note that in the past, patches have
391added significant changes; in fact the changeover from 0.9.9 to 1.0.0
392was the first time that either A or B changed!
393
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000394Beta versions have an additional suffix of "-beta-N" for some small
395number N. Note that (for instance) all versions labeled 1.2-beta-N
396*precede* the actual release of 1.2.
397
3981.13. Q. How do I get a beta test version of Python?
399
400A. Write me. I might like you :-) Seriously, anybody who has
401previously used and installed Python can become a beta tester, but I
402expect feedback, so you have to write me first before I divulge the
403secret location of the latest beta release.
404
4051.14. Q. Are there other ftp sites that carry Python related material?
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000406
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000407A. An interesting ftp site for Python users is ftp.markv.com; the
408directory pub/python contains a growing collection of interesting
409Python scripts <URL:ftp://ftp.markv.com/pub/python/>. To submit a
410script for inclusion, place it together with a readme file (with
411extension .readme) in the publicly writable directory
412/incoming/python. This service is maintained by Lance Ellinghaus
413<lance@markv.com>. (I've heard complaints about this service not
414being very responsive -- try at your own risk.)
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000415
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00004161.15. Q. Are there copyright restrictions on the use of Python?
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000417
418A. Hardly. You can do anything you want with the source, as long as
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000419you leave the copyrights in, and display those copyrights in any
420documentation about Python that you produce. Also, don't use the
421author's institute's name in publicity without prior written
422permission, and don't hold them responsible for anything (read the
423actual copyright for a precise legal wording).
424
425In particular, if you honor the copyright rules, it's OK to use Python
426for commercial use, to sell copies of Python in source or binary form,
427or to sell products that enhance Python or incorporate Python (or part
428of it) in some form. I would still like to know about all commercial
429use of Python!
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000430
Guido van Rossum7be34a81995-05-31 15:17:12 +00004311.16. Q. Why was Python created in the first place?
432
433A. Here's a *very* brief summary of what got me started:
434
435- I had extensive experience with implementing an interpreted language
436in the ABC group at CWI, and from working with this group I had
437learned a lot about language design. This is the origin of many
438Python features, including the use of indentation for statement
439grouping and the inclusion of very-high-level data types (although the
440details are all different in Python).
441
442- I had a number of gripes about the ABC language, but also liked many
443of its features. It was impossible to extend the ABC language (or its
444implementation) to remedy my complaints -- in fact its lack of
445extensibility was one of its biggest problems.
446
447- I had some experience with using Modula-2+ and talked with the
448designers of Modula-3 (and read the M3 report). M3 is the origin of
449the syntax and semantics used for exceptions, and some other Python
450features.
451
452- I was working in the Amoeba distributed operating system group at
453CWI. We needed a better way to do system administration than by
454writing either C programs or Bourne shell scripts, since Amoeba had
455its own system call interface which wasn't easily accessible from the
456Bourne shell. My experience with error handling
457
458- It occurred to me that a scripting language with a syntax like ABC
459but with access to the Amoeba system calls would fill the need. I
460realized that it would be foolish to write an Amoeba-specific
461language, so I decided that I needed a language that was generally
462extensible.
463
464- During the 1989 Christmas holidays, I had a lot of time on my hand,
465so I decided to give it a try. During the next year, while still
466mostly working on it in my own time, Python was used in the Amoeba
467project with increasing success, and the feedback from colleagues made
468me add many early improvements.
469
470- In February 1991, after just over a year of development, I decided
471to post to USENET. The rest is in the Misc/HISTORY file.
472
4731.17. Q. What happened to Tim Peters (the author of python-mode.el, and many
474entertaining and enlightening pieces of Python email)?
475
476A. He worked at KSR (Kendall Square Research, a start-up building a
477new kind of massively parallel processor). When KSR folded down a
478couple of years ago, Tim lost his email access. He hasn't surfaced
479on the net since then.
480
481 Missing-him-too-ly yours...
482
Guido van Rossuma7925f11994-01-26 10:20:16 +0000483
4842. Python in the real world
485===========================
486
4872.1. Q. How many people are using Python?
488
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000489A. I don't know, but the maximum number of simultaneous subscriptions
490to the Python mailing list before it was gatewayed into the newsgroup
491was about 180 (several of which were local redistribution lists). I
492believe that many active Python users don't bother to subscribe to the
493list, and now that there's a newsgroup the mailing list subscription
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000494is even less meaningful. I see new names on the newsgroup all the
495time and my best guess is that there are currently at least several
496thousands of users.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000497
4982.2. Q. Have any significant projects been done in Python?
499
500A. Here at CWI (the home of Python), we have written a 20,000 line
501authoring environment for transportable hypermedia presentations, a
Guido van Rossum5333c5d1994-04-11 11:06:22 +00005025,000 line multimedia teleconferencing tool, as well as many many
503smaller programs.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000504
505The University of Virginia uses Python to control a virtual reality
506engine. Contact: Matt Conway <conway@virginia.edu>.
507
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000508The ILU project at Xerox PARC can generate Python glue for ILU
509interfaces. See <URL:ftp://ftp.parc.xerox.com/pub/ilu/ilu.html>.
510
Guido van Rossumac3f2121995-04-10 11:53:42 +0000511The University of California, Irvine uses a student administration
512system called TELE-Vision written entirely in Python. Contact: Ray
513Price <rlprice@uci.edu>.
514
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000515If you have done a significant project in Python that you'd like to be
516included in the list above, send me email!
Guido van Rossuma7925f11994-01-26 10:20:16 +0000517
5182.3. Q. Are there any commercial projects going on using Python?
519
520A. Several companies have revealed to me that they are planning or
Guido van Rossum796b2591995-01-20 23:05:52 +0000521considering use of Python in a future product.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000522
Guido van Rossumac3f2121995-04-10 11:53:42 +0000523Sunrise Software, have a product out using Python -- they use Python
524for a GUI management application and an SNMP network management
525application. Contact: <info@sunrise.com>.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000526
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000527Infoseek uses Python to implement their commercial WWW information
528retrieval service <URL:http://www.infoseek.com/>. Contact:
529<info@infoseek.com>.
Guido van Rossumf8c76d01994-08-17 12:19:53 +0000530
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000531Michael Powers of daVinci Time & Space is "writing tons-o-python for
532interactive television entertainment." Contact: <powers@dvts.com>.
533
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000534Paul Everitt of Connecting Minds is planning a Lotus Notes gateway.
535Contact: <Paul.Everitt@cminds.com>. Or see their WWW server
536<URL:http://www.cminds.com/>.
537
Guido van Rossumac3f2121995-04-10 11:53:42 +0000538KaPRE in Boulder, CO is using Python for on-site customization of C++
539applications, rapid-prototyping/development,
540language-based-components, and possibly more. This is pretty solid:
541Python's being shipped with their tool-set now, to beta sites.
542Contact: <lutz@KaPRE.COM> (Mark Lutz).
543
544Individuals at many other companies are using Python for internal
545development or for as yet unannounced products (witness their
546contributions to the Python mailing list or newsgroup).
Guido van Rossuma7925f11994-01-26 10:20:16 +0000547
548Python has also been elected as an extension language by MADE, a
549consortium supported by the European Committee's ESPRIT program and
550consisting of Bull, CWI and some other European companies. Contact:
551Ivan Herman <ivan@cwi.nl>.
552
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000553If you'd like to be included in the list above, send me email!
554
Guido van Rossum95f61a71994-01-26 17:23:37 +00005552.4. Q. How stable is Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000556
Guido van Rossum3de27361994-07-25 14:19:33 +0000557A. Very stable. While the current version number would suggest it is
558in the early stages of development, in fact new, stable releases
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000559(numbered 0.9.x through 1.1.x) have been coming out roughly every 3 to
5606 months for the past four years.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000561
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00005622.5. Q. When will the next version be released?
563
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000564A. I am planning to release 1.2 in February or early March 1995. It
565will contain hooks into the implementation of the import command, a
566(still limited) form of persistent objects, and the usual complement
567of bug fixes (including many fixed memory leaks and thread problems).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000568
5692.6. Q. What new developments are expected for Python in the future?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000570
Guido van Rossum7be34a81995-05-31 15:17:12 +0000571A. See my Work-In-Progress web page, currently at
572<URL:http://www.python.org:~guido/WIP.html>, and the pages for the
573Second Python Workshop (best reached via the Python home page,
574<URL:http://www.python.org/>).
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000575
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00005762.7. Q. Is it reasonable to propose incompatible changes to Python?
Guido van Rossum3de27361994-07-25 14:19:33 +0000577
578A. In general, no. There are already millions of lines of Python code
579around the world, so any changes in the language that invalidates more
580than a very small fraction of existing programs has to be frowned
581upon. Even if you can provide a conversion program, there still is
582the problem of updating all documentation. Providing a gradual
583upgrade path is the only way if a feature has to be changed.
584
Guido van Rossuma7925f11994-01-26 10:20:16 +0000585
5863. Building Python
587==================
588
Guido van Rossum91f60831994-02-15 15:52:27 +00005893.1. Q. Is there a test set?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000590
591A. Yes, simply do "import testall" (or "import autotest" if you aren't
592interested in the output). The standard modules whose name begins
593with "test" together comprise the test. The test set doesn't test
594*all* features of Python but it goes a long way to confirm that a new
595port is actually working. The Makefile contains an entry "make test"
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000596which runs the autotest module. NOTE: if "make test" fails, run the
597tests manually ("import testall") to see what goes wrong before
598reporting the error.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000599
Guido van Rossum91f60831994-02-15 15:52:27 +00006003.2. Q. When running the test set, I get complaints about floating point
Guido van Rossuma7925f11994-01-26 10:20:16 +0000601operations, but when playing with floating point operations I cannot
602find anything wrong with them.
603
604A. The test set makes occasional unwarranted assumptions about the
605semantics of C floating point operations. Until someone donates a
606better floating point test set, you will have to comment out the
607offending floating point tests and execute similar tests manually.
608
Guido van Rossum7be34a81995-05-31 15:17:12 +00006093.3. Q. Link errors after rerunning the configure script.
Guido van Rossum24349991994-02-02 14:12:45 +0000610
611A. It is generally necessary to run "make clean" after a configuration
612change.
613
Guido van Rossum7be34a81995-05-31 15:17:12 +00006143.4. Q. The python interpreter complains about options passed to a
Guido van Rossum24349991994-02-02 14:12:45 +0000615script (after the script name).
616
617A. You are probably linking with GNU getopt, e.g. through -liberty.
Guido van Rossumf8c76d01994-08-17 12:19:53 +0000618Don't. The reason for the complaint is that GNU getopt, unlike System
619V getopt and other getopt implementations, doesn't consider a
620non-option to be the end of the option list. A quick (and compatible)
621fix for scripts is to add "--" to the interpreter, like this:
622
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000623 #! /usr/local/bin/python --
Guido van Rossumf8c76d01994-08-17 12:19:53 +0000624
625You can also use this interactively:
626
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000627 python -- script.py [options]
628
629Note that a working getopt implementation is provided in the Python
630distribution (in Python/getopt.c) but not automatically used.
Guido van Rossum24349991994-02-02 14:12:45 +0000631
Guido van Rossum7be34a81995-05-31 15:17:12 +00006323.5. Q. When building on the SGI, make tries to run python to create
Guido van Rossum24349991994-02-02 14:12:45 +0000633glmodule.c, but python hasn't been built or installed yet.
634
635A. Comment out the line mentioning glmodule.c in Setup and build a
636python without gl first; install it or make sure it is in your $PATH,
637then edit the Setup file again to turn on the gl module, and make
638again. You don't need to do "make clean"; you do need to run "make
639Makefile" in the Modules subdirectory (or just run "make" at the
640toplevel).
641
Guido van Rossum7be34a81995-05-31 15:17:12 +00006423.6. Q. Python built with gcc for the DEC Alpha doesn't work.
Guido van Rossum3de27361994-07-25 14:19:33 +0000643
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000644People have reported problems with gcc 2.5.8 up to 2.6.3. The DEC
645OSF/1 cc compiler does not have these problems so it's likely a gcc
Guido van Rossume530c581995-04-10 12:32:16 +0000646bug. The latest news is that this has been fixed in Python 1.2 by a
647source change (I gave up waiting for a fixed gcc).
Guido van Rossum3de27361994-07-25 14:19:33 +0000648
Guido van Rossum7be34a81995-05-31 15:17:12 +00006493.7. Q. I use VPATH but some targets are built in the source directory.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000650
651A. On some systems (e.g. Sun), if the target already exists in the
652source directory, it is created there instead of in the build
653directory. This is usually because you have previously built without
654VPATH. Try running "make clobber" in the source directory.
655
Guido van Rossum7be34a81995-05-31 15:17:12 +00006563.8. Q. Trouble building or linking with the GNU readline library.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000657
Guido van Rossumac3f2121995-04-10 11:53:42 +0000658A. Consider using readline 2.0. Some hints:
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000659
660- You can use the GNU readline library to improve the interactive user
661interface: this gives you line editing and command history when
Guido van Rossum796b2591995-01-20 23:05:52 +0000662calling python interactively. You need to configure and build the GNU
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000663readline library before running the configure script. Its sources are
664no longer distributed with Python; you can ftp them from any GNU
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000665mirror site, or from its home site
666<URL:ftp://slc2.ins.cwru.edu/pub/dist/readline-2.0.tar.gz> (or a
667higher version number -- using version 1.x is not recommended). Pass
668the Python configure script the option --with-readline=DIRECTORY where
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000669DIRECTORY is the absolute pathname of the directory where you've built
670the readline library. Some hints on building and using the readline
671library:
672
673- On SGI IRIX 5, you may have to add the following
674to rldefs.h:
675
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000676 #ifndef sigmask
677 #define sigmask(sig) (1L << ((sig)-1))
678 #endif
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000679
680- On most systems, you will have to add #include "rldefs.h" to the
681top of several source files, and if you use the VPATH feature, you
682will have to add dependencies of the form foo.o: foo.c to the
683Makefile for several values of foo.
684
685- The readline library requires use of the termcap library. A
686known problem with this is that it contains entry points which
Guido van Rossum061f1821994-10-06 16:03:45 +0000687cause conflicts with the STDWIN and SGI GL libraries. The STDWIN
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000688conflict can be solved (and will be, in the next release of
Guido van Rossum061f1821994-10-06 16:03:45 +0000689STDWIN) by adding a line saying '#define werase w_erase' to the
690stdwin.h file (in the STDWIN distribution, subdirectory H). The
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000691GL conflict has been solved in the Python configure script by a
692hack that forces use of the static version of the termcap library.
693
Guido van Rossumac3f2121995-04-10 11:53:42 +0000694- Check the newsgroup gnu.bash.bug <URL:news:gnu.bash.bug> for
695specific problems with the readline library (I don't read this group
696but I've been told that it is the place for readline bugs).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000697
Guido van Rossum7be34a81995-05-31 15:17:12 +00006983.9. Q. Trouble building Python on Linux.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000699
Guido van Rossumac3f2121995-04-10 11:53:42 +0000700A. If you're building Python 1.2, Slackware 2.2 has a buggy bash
701(version 1.14.3) which breaks a sed script that is used to build
702Modules/Makefile. Replace /bin/sh with /bin/ash in both makesetup and
703Makefile.pre.in.
704
705In 1.1 and 1.1.1, there's a bug in the reference counting logic of
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000706ternary pow() which is only tripped by very picky mallocs, like the
707GNU malloc on Linux. This has been fixed in 1.2. To continue the
Guido van Rossumac3f2121995-04-10 11:53:42 +0000708tests in 1.1(.1), just disable the tests of pow() with three arguments
709from Lib/test/test_b2.py.
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000710
Guido van Rossumac3f2121995-04-10 11:53:42 +0000711Apart from this, Python builds and runs fine on most Linux versions
712(if you run into trouble on an old Linux version, consider upgrading).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000713
Guido van Rossum7be34a81995-05-31 15:17:12 +00007143.10. Q. Trouble with prototypes on Ultrix.
Guido van Rossum72eb83c1994-10-07 11:33:28 +0000715
716A. Ultrix cc seems broken -- use gcc, or edit config.h to #undef
717HAVE_PROTOTYPES.
718
Guido van Rossum7be34a81995-05-31 15:17:12 +00007193.11. Q. Trouble with posix.listdir on NeXTSTEP 3.2.
Guido van Rossum9351fdb1994-11-10 23:03:51 +0000720
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000721A. (This often manifests itself as a weird error from the
722compileall.py script run by "make libinstall".)
Guido van Rossum9351fdb1994-11-10 23:03:51 +0000723
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000724Don't use gcc, use the Next C compiler (cc). Even though it is
725derived from (an old version of) gcc, its interpretation of the
726"-posix" switch is different; in this particular case, cc is right and
727gcc is wrong.
Guido van Rossum9351fdb1994-11-10 23:03:51 +0000728
Guido van Rossum7be34a81995-05-31 15:17:12 +00007293.12. Q. Other trouble building Python on platform X.
Guido van Rossum95f61a71994-01-26 17:23:37 +0000730
Guido van Rossum796b2591995-01-20 23:05:52 +0000731A. Please email the details to <guido@cwi.nl> and I'll look into it.
732Please provide as many details as possible. In particular, if you
733don't tell me what type of computer and what operating system (and
734version) you are using it will be difficult for me to figure out what
735is the matter. If you get a specific error message, please email it
736to me too.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000737
Guido van Rossum7be34a81995-05-31 15:17:12 +00007383.13. Q. How to configure dynamic loading on Lixux.
Guido van Rossum07779351995-02-07 16:59:56 +0000739
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000740A. There are two answers, depending on whether you are using the new
741ELF object format or not.
Guido van Rossum07779351995-02-07 16:59:56 +0000742
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000743For ELF, this seems to be the procedure (thanks to Martin von Loewis):
Guido van Rossum07779351995-02-07 16:59:56 +0000744
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000745Compile Python to an ELF binary.
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000746
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000747In addition, you have to use the following flags:
Guido van Rossum07779351995-02-07 16:59:56 +0000748
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000749 - when linking python: -rdynamic -ldl
750 - when compiling an object that goes into a shared module: -fPIC
751 - when linking a shared module: -shared -ldl
Guido van Rossum07779351995-02-07 16:59:56 +0000752
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000753Furthermore, it appears that some Python releases did not understand
754that Linux has dynamic linking. Python 1.2 did it right, but you
755should check wether the generated config.h indicates the use of -ldl
756(i.e. dlopen, dlsym). Finally, you can load a shared module by saying
757'use foo'. Make sure the module is in your PYTHONPATH.
Guido van Rossum07779351995-02-07 16:59:56 +0000758
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000759For pre-ELF systems (thanks to Andrew Kuchling):
Guido van Rossum07779351995-02-07 16:59:56 +0000760
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000761Pre-ELF Linux requires that you use the GNU DLD library. The stages
762of using dynamic libraries on Linux are:
763
764 1) Get dld 3.2.5 from a Linux site. Be careful here; the most
765 recent GNU version is 3.2.3, and doesn't support Linux; be sure to
766 get it from a Linux mirror, not a GNU mirror (3.2.4 should also
767 work). Compile it and install the library libdld.a somewhere; I
768 used /usr/local/lib.
769
770 Suitable URLs for the dld distribution are currently:
771 <URL:ftp://sunsite.unc.edu/pub/Linux/libs/dld-3.2.5.src.tar.gz> and
772 <URL:ftp://tsx-11.mit.edu/pub/linux/sources/libs/dld-3.2.5.src.tar.gz>.
773 There's also a binary distribution of it:
774 <URL:ftp://sunsite.unc.edu/pub/Linux/libs/dld-3.2.5.bin.tar.gz>.
775
776 2) Get Jack Jansen's DL library; its location is given in the
777 _Extending Python_ manual as <URL:ftp://ftp.cwi.nl/pub/dynload/>.
778 Compile it and install libdl.a wherever you put libdld.a.
779
780 3) Run Python's configure script, giving it the --with-dl-dld option,
781 which requires a parameter giving the directory where you put the
782 libraries.
783
784 4) Recompile Python.
785
7863.14. Q. Under Solaris 2.x, using GCC, how do I use shared libraries?
787
788A. Use the linker in /usr/ucb/ld, not the GNU linker. The latter
789cannot create shared libraries.
790
7913.15. Q. Errors when linking with a shared library containing C++ code.
792
793A. Link the main Python binary with C++. Change the definition of
794LINKCC in Modules/Makefile to be your C++ compiler. You may have to
795edit config.c slightly to make it compilable with C++.
Guido van Rossum07779351995-02-07 16:59:56 +0000796
Guido van Rossuma7925f11994-01-26 10:20:16 +0000797
7984. Programming in Python
799========================
800
Guido van Rossum24349991994-02-02 14:12:45 +00008014.1. Q. Is there a source code level debugger with breakpoints, step,
802etc.?
803
804A. Yes. Check out module pdb; pdb.help() prints the documentation (or
805you can read it as Lib/pdb.doc). If you use the STDWIN option,
806there's also a windowing interface, wdb. You can write your own
807debugger by using the code for pdb or wdb as an example.
808
8094.2. Q. Can I create an object class with some methods implemented in
Guido van Rossuma7925f11994-01-26 10:20:16 +0000810C and others in Python (e.g. through inheritance)? (Also phrased as:
811Can I use a built-in type as base class?)
812
813A. No, but you can easily create a Python class which serves as a
814wrapper around a built-in object, e.g. (for dictionaries):
815
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000816 # A user-defined class behaving almost identical
817 # to a built-in dictionary.
818 class UserDict:
819 def __init__(self): self.data = {}
820 def __repr__(self): return repr(self.data)
821 def __cmp__(self, dict):
822 if type(dict) == type(self.data):
823 return cmp(self.data, dict)
824 else:
825 return cmp(self.data, dict.data)
826 def __len__(self): return len(self.data)
827 def __getitem__(self, key): return self.data[key]
828 def __setitem__(self, key, item): self.data[key] = item
829 def __delitem__(self, key): del self.data[key]
830 def keys(self): return self.data.keys()
831 def items(self): return self.data.items()
832 def values(self): return self.data.values()
833 def has_key(self, key): return self.data.has_key(key)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000834
Guido van Rossum24349991994-02-02 14:12:45 +00008354.3. Q. Is there a curses/termcap package for Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000836
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000837A. Yes -- Lance Ellinghaus has written a module that interfaces to
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000838System V's "ncurses". If you know a little curses and some Python,
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000839it's straightforward to use. It is part of the standard Python
840distribution, but not configured by default -- you must enable it by
841editing Modules/Setup. It requires a System V curses implementation.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000842
843You could also consider using the "alfa" (== character cell) version
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000844of STDWIN. (Standard Window System Interface, a portable windowing
845system interface by myself <URL:ftp://ftp.cwi.nl/pub/stdwin/>.) This
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000846will also prepare your program for porting to windowing environments
847such as X11 or the Macintosh.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000848
Guido van Rossum24349991994-02-02 14:12:45 +00008494.4. Q. Is there an equivalent to C's onexit() in Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000850
851A. Yes, if you import sys and assign a function to sys.exitfunc, it
852will be called when your program exits, is killed by an unhandled
853exception, or (on UNIX) receives a SIGHUP or SIGTERM signal.
854
Guido van Rossum24349991994-02-02 14:12:45 +00008554.5. Q. When I define a function nested inside another function, the
Guido van Rossuma7925f11994-01-26 10:20:16 +0000856nested function seemingly can't access the local variables of the
857outer function. What is going on? How do I pass local data to a
858nested function?
859
860A. Python does not have arbitrarily nested scopes. When you need to
861create a function that needs to access some data which you have
862available locally, create a new class to hold the data and return a
863method of an instance of that class, e.g.:
864
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000865 class MultiplierClass:
866 def __init__(self, factor):
867 self.factor = factor
868 def multiplier(self, argument):
869 return argument * self.factor
Guido van Rossuma7925f11994-01-26 10:20:16 +0000870
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000871 def generate_multiplier(factor):
872 return MultiplierClass(factor).multiplier
Guido van Rossuma7925f11994-01-26 10:20:16 +0000873
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000874 twice = generate_multiplier(2)
875 print twice(10)
876 # Output: 20
877
878An alternative solution uses default arguments, e.g.:
879
880 def generate_multiplier(factor):
881 def multiplier(arg, fact = factor):
882 return arg*fact
883 return multiplier
884
885 twice = generate_multiplier(2)
886 print twice(10)
887 # Output: 20
Guido van Rossuma7925f11994-01-26 10:20:16 +0000888
Guido van Rossum24349991994-02-02 14:12:45 +00008894.6. Q. How do I iterate over a sequence in reverse order?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000890
891A. If it is a list, the fastest solution is
892
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000893 list.reverse()
894 try:
895 for x in list:
896 "do something with x"
897 finally:
898 list.reverse()
Guido van Rossuma7925f11994-01-26 10:20:16 +0000899
900This has the disadvantage that while you are in the loop, the list
901is temporarily reversed. If you don't like this, you can make a copy.
902This appears expensive but is actually faster than other solutions:
903
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000904 rev = list[:]
905 rev.reverse()
906 for x in rev:
907 <do something with x>
Guido van Rossuma7925f11994-01-26 10:20:16 +0000908
909If it isn't a list, a more general but slower solution is:
910
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000911 i = len(list)
912 while i > 0:
913 i = i-1
914 x = list[i]
915 <do something with x>
Guido van Rossuma7925f11994-01-26 10:20:16 +0000916
917A more elegant solution, is to define a class which acts as a sequence
918and yields the elements in reverse order (solution due to Steve
919Majewski):
920
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000921 class Rev:
922 def __init__(self, seq):
923 self.forw = seq
924 def __len__(self):
925 return len(self.forw)
926 def __getitem__(self, i):
927 return self.forw[-(i + 1)]
Guido van Rossuma7925f11994-01-26 10:20:16 +0000928
929You can now simply write:
930
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000931 for x in Rev(list):
932 <do something with x>
Guido van Rossuma7925f11994-01-26 10:20:16 +0000933
Guido van Rossum061f1821994-10-06 16:03:45 +0000934Unfortunately, this solution is slowest of all, due to the method
Guido van Rossuma7925f11994-01-26 10:20:16 +0000935call overhead...
936
Guido van Rossum24349991994-02-02 14:12:45 +00009374.7. Q. My program is too slow. How do I speed it up?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000938
939A. That's a tough one, in general. There are many tricks to speed up
940Python code; I would consider rewriting parts in C only as a last
941resort. One thing to notice is that function and (especially) method
942calls are rather expensive; if you have designed a purely OO interface
943with lots of tiny functions that don't do much more than get or set an
944instance variable or call another method, you may consider using a
945more direct way, e.g. directly accessing instance variables. Also see
946the standard module "profile" (described in the file
947"python/lib/profile.doc") which makes it possible to find out where
948your program is spending most of its time (if you have some patience
949-- the profiling itself can slow your program down by an order of
950magnitude).
951
Guido van Rossum24349991994-02-02 14:12:45 +00009524.8. Q. When I have imported a module, then edit it, and import it
Guido van Rossuma7925f11994-01-26 10:20:16 +0000953again (into the same Python process), the changes don't seem to take
954place. What is going on?
955
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000956A. For reasons of efficiency as well as consistency, Python only reads
Guido van Rossum796b2591995-01-20 23:05:52 +0000957the module file on the first time a module is imported. (Otherwise a
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000958program consisting of many modules, each of which imports the same
Guido van Rossum796b2591995-01-20 23:05:52 +0000959basic module, would read the basic module over and over again.) To
960force rereading of a changed module, do this:
Guido van Rossuma7925f11994-01-26 10:20:16 +0000961
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000962 import modname
963 reload(modname)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000964
965Warning: this technique is not 100% fool-proof. In particular,
966modules containing statements like
967
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000968 from modname import some_objects
Guido van Rossuma7925f11994-01-26 10:20:16 +0000969
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000970will continue to work with the old version of the imported objects.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000971
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00009724.9. Q. How do I find the current module name?
Guido van Rossum24349991994-02-02 14:12:45 +0000973
Guido van Rossum061f1821994-10-06 16:03:45 +0000974A. A module can find out its own module name by looking at the
Guido van Rossum24349991994-02-02 14:12:45 +0000975(predefined) global variable __name__. If this has the value
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000976'__main__' you are running as a script.
977
Guido van Rossuma6c707c1995-01-02 17:32:28 +00009784.10. Q. I have a module in which I want to execute some extra code
979when it is run as a script. How do I find out whether I am running as
980a script?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000981
982A. See the previous question. E.g. if you put the following on the
983last line of your module, main() is called only when your module is
984running as a script:
Guido van Rossum24349991994-02-02 14:12:45 +0000985
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000986 if __name__ == '__main__': main()
Guido van Rossum24349991994-02-02 14:12:45 +0000987
Guido van Rossuma6c707c1995-01-02 17:32:28 +00009884.11. Q. I try to run a program from the Demo directory but it fails
989with ImportError: No module named ...; what gives?
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000990
Guido van Rossum061f1821994-10-06 16:03:45 +0000991A. This is probably an optional module (written in C!) which hasn't
992been configured on your system. This especially happens with modules
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000993like "Tkinter", "stdwin", "gl", "Xt" or "Xm". For Tkinter, STDWIN and
994many other modules, see Modules/Setup.in for info on how to add these
995modules to your Python, if it is possible at all. Sometimes you will
996have to ftp and build another package first (e.g. STDWIN). Sometimes
997the module only works on specific platforms (e.g. gl only works on SGI
998machines).
999
1000NOTE: if the complaint is about "Tkinter" (upper case T) and you have
1001already configured module "tkinter" (lower case t), the solution is
1002*not* to rename tkinter to Tkinter or vice versa. There is probably
Guido van Rossum796b2591995-01-20 23:05:52 +00001003something wrong with your module search path. Check out the value of
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001004sys.path.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001005
Guido van Rossum061f1821994-10-06 16:03:45 +00001006For X-related modules (Xt and Xm) you will have to do more work: they
1007are currently not part of the standard Python distribution. You will
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001008have to ftp the Extensions tar file, e.g.
Guido van Rossum7be34a81995-05-31 15:17:12 +00001009<URL:ftp://ftp.python.org/pub/python/src/extensions.tar.gz> and follow
1010the instructions there.
Guido van Rossum061f1821994-10-06 16:03:45 +00001011
1012See also the next question.
1013
10144.12. Q. I have successfully built Python with STDWIN but it can't
1015find some modules (e.g. stdwinevents).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001016
1017A. There's a subdirectory of the library directory named 'stdwin'
1018which should be in the default module search path. There's a line in
1019Modules/Setup(.in) that you have to enable for this purpose --
Guido van Rossum061f1821994-10-06 16:03:45 +00001020unfortunately in the latest release it's not near the other
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001021STDWIN-related lines so it's easy to miss it.
1022
10234.13. Q. What GUI toolkits exist for Python?
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001024
1025A. Depending on what platform(s) you are aiming at, there are several.
1026
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001027- There's a neat object-oriented interface to the Tcl/Tk widget set,
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001028called Tkinter. As of python 1.1, it is part of the standard Python
1029distribution -- all you need to do is enable it in Modules/Setup
1030(provided you have already installed Tk and Tcl). This is probably
1031the easiest to install and use, and the most complete widget set. It
1032is also very likely that in the future the standard Python GUI API
1033will be based on or at least look very much like the Tkinter
1034interface. For more info about Tk, including pointers to the source,
1035see John Ousterhout's home page
Guido van Rossum796b2591995-01-20 23:05:52 +00001036<URL:http://playground.sun.com/~ouster/>.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001037
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001038- The standard Python distribution comes with an interface to STDWIN,
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001039a platform-independent low-level windowing interface. You have to ftp
1040the source for STDWIN separately,
1041e.g. <URL:ftp://ftp.cwi.nl/pub/stdwin/> or gatekeeper.dec.com in
1042pub/misc/stdwin <URL:ftp://gatekeeper.dec.com/pub/misc/stdwin/>.
1043STDWIN runs under X11 or the Mac; a Windows port has been attempted
1044but I can't seem to get it working. Note that STDWIN is really not
1045powerful enough to implement a modern GUI (no widgets, etc.) and that
1046I don't have the time to maintain or extend it, so you may be better
1047off using Tkinter or the Motif interface, unless you require
1048portability to the Mac (which is also offered by SUIT, by the way --
1049see below).
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001050
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001051- For SGI IRIX only, there's an interface to the complete GL (Graphics
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001052Library -- low level but very good 3D capabilities) as well as to
1053FORMS (a buttons-and-sliders-etc package built on top of GL by Mark
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001054Overmars -- ftp'able from <URL:ftp://ftp.cs.ruu.nl/pub/SGI/FORMS/>).
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001055
1056- There's an interface to X11, including the Athena and Motif widget
1057sets (and a few individual widgets, like Mosaic's HTML widget and
1058SGI's GL widget) in the Extensions set, which is separately ftp'able
Guido van Rossum7be34a81995-05-31 15:17:12 +00001059<URL:ftp://ftp.python.org/pub/python/src/extensions.tar.gz>.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001060
1061- There's an interface to SUIT, the U of Virginia's Simple User
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001062Interface Toolkit; it can be ftp'ed from
1063<URL:ftp://uvacs.cs.virginia.edu/pub/suit/python/SUIT_python.tar.Z>.
1064A PC binary of Python 1.0.2 compiled with DJGPP and with SUIT support
1065built-in has been made available by Antonio Costa
1066<URL:ftp://asterix.inescn.pt/pub/PC/python/pyt102su.exe> (a
1067self-extracting archive). Note that the UVa people themselves have
1068expressed doubts about SUIT, and are planning to build a Python GUI
1069API based upon Tk (though not necessarily on Tkinter); see
1070<URL:http://server.cs.virginia.edu/~tnb2d/IT/IT.html>.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001071
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001072- There's an interface to WAFE, a Tcl interface to the X11 Motif and
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001073Athena widget sets. Last I heard about it it was included in the WAFE
10741.0 prerelease
1075<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 +00001076
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00001077- There's an interface to wxWindows. wxWindows is a portable GUI
1078class library written in C++. It supports XView, Motif, MS-Windows as
1079targets. There is some support for Macs and CURSES as well. wxWindows
1080preserves the look and feel of the underlying graphics toolkit. See
1081the wxPython WWW page at
1082<URL:http://www.aiai.ed.ac.uk/~jacs/wx/wxpython/wxpython.html>.
1083
Guido van Rossum3fc9d731995-07-25 15:10:56 +00001084- There's an object-oriented GUI based on the Microsoft Foundation
1085Classes model called WPY. Programs written in WPY run unchanged and
1086with native look and feel on NT, Windows 3.1 (using win32s) and on
1087Unix (using Tk). Source and binaries for NT and Linux are available
1088in <URL:ftp://ftp.python.org/pub/python/wpy>.
1089
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00001090- Python has been mentioned on the "Futurism" subpage of the Fresco
1091home page <URL:http://www.faslab.com/fresco/HomePage.html>. "Pesto"
1092is a Python interface to the CORBA dynamic invocation interface, and
1093thus Fresco. A Pesto prototype is running and is currently being
Guido van Rossum7be34a81995-05-31 15:17:12 +00001094packaged up for inclusion in the Fresco snapshot. See also the Pesto
1095web pages: <URL:http://www.faslab.com/fresco/pesto/Index.html>.
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00001096
Guido van Rossum061f1821994-10-06 16:03:45 +000010974.14. Q. Are there any interfaces to database packages in Python?
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001098
1099A. There's an interface to SYBASE by John Redford
1100<jredford@lehman.com>.
1101
Guido van Rossum796b2591995-01-20 23:05:52 +00001102There's an interface to metalbase by Lance Ellinghaus
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001103<lance@markv.com>; it is part of the separate Extensions distribution
Guido van Rossum7be34a81995-05-31 15:17:12 +00001104<URL:ftp://ftp.python.org/pub/python/src/extensions.tar.gz>.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001105
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001106Anthony Baxter <anthony.baxter@aaii.oz.au> has written an interface to
Guido van Rossume530c581995-04-10 12:32:16 +00001107mSQL (mini-SQL).
Guido van Rossum7be34a81995-05-31 15:17:12 +00001108<URL:ftp://ftp.python.org/pub/python/contrib/PymSQL.tar.gz>.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001109
Guido van Rossum7be34a81995-05-31 15:17:12 +00001110Tom Culliton <culliton@clark.net> has written an Oracle module.
1111See <URL:ftp://ftp.clark.net/pub/culliton/oramod.tgz>.
Guido van Rossum796b2591995-01-20 23:05:52 +00001112
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000011134.15. Q. Is it possible to write obfuscated one-liners in Python?
Guido van Rossumc24da7c1994-09-23 14:08:41 +00001114
1115A. Yes. See the following three examples, due to Ulf Bartelt:
1116
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001117 # Primes < 1000
1118 print filter(None,map(lambda y:y*reduce(lambda x,y:x*y!=0,
1119 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 +00001120
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001121 # First 10 Fibonacci numbers
1122 print map(lambda x,f=lambda x,f:(x<=1) or (f(x-1,f)+f(x-2,f)): f(x,f),
1123 range(10))
Guido van Rossumc24da7c1994-09-23 14:08:41 +00001124
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001125 # Mandelbrot set
1126 print (lambda Ru,Ro,Iu,Io,IM,Sx,Sy:reduce(lambda x,y:x+y,map(lambda y,
1127 Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,Sy=Sy,L=lambda yc,Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,i=IM,
1128 Sx=Sx,Sy=Sy:reduce(lambda x,y:x+y,map(lambda x,xc=Ru,yc=yc,Ru=Ru,Ro=Ro,
1129 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
1130 >=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(
1131 64+F(Ru+x*(Ro-Ru)/Sx,yc,0,0,i)),range(Sx))):L(Iu+y*(Io-Iu)/Sy),range(Sy
1132 ))))(-2.1, 0.7, -1.2, 1.2, 30, 80, 24)
1133 # \___ ___/ \___ ___/ | | |__ lines on screen
1134 # V V | |______ columns on screen
1135 # | | |__________ maximum of "iterations"
1136 # | |_________________ range on y axis
1137 # |____________________________ range on x axis
Guido van Rossumc24da7c1994-09-23 14:08:41 +00001138
1139Don't try this at home, kids!
1140
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000011414.16. Q. Is there an equivalent of C's "?:" ternary operator?
Guido van Rossumc24da7c1994-09-23 14:08:41 +00001142
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001143A. Not directly. In many cases you can mimic a?b:c with "a and b or
1144c", but there's a flaw: if b is zero (or empty, or None -- anything
1145that tests false) then c will be selected instead. In many cases you
1146can prove by looking at the code that this can't happen (e.g. because
1147b is a constant or has a type that can never be false), but in general
1148this can be a problem.
1149
1150Steve Majewski (or was it Tim Peters?) suggested the following
1151solution: (a and [b] or [c])[0]. Because [b] is a singleton list it
1152is never false, so the wrong path is never taken; then applying [0] to
1153the whole thing gets the b or c that you really wanted. Ugly, but it
1154gets you there in the rare cases where it is really inconvenient to
1155rewrite your code using 'if'.
1156
11574.17. Q. My class defines __del__ but it is not called when I delete the
1158object.
1159
1160A. There are several possible reasons for this.
1161
1162- The del statement does not necessarily call __del__ -- it simply
1163decrements the object's reference count, and if this reaches zero
1164__del__ is called.
1165
1166- If your data structures contain circular links (e.g. a tree where
1167each child has a parent pointer and each parent has a list of
1168children) the reference counts will never go back to zero. You'll
Guido van Rossum061f1821994-10-06 16:03:45 +00001169have to define an explicit close() method which removes those
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001170pointers. Please don't ever call __del__ directly -- __del__ should
Guido van Rossum061f1821994-10-06 16:03:45 +00001171call close() and close() should make sure that it can be called more
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001172than once for the same object.
1173
1174- If the object has ever been a local variable (or argument, which is
1175really the same thing) to a function that caught an expression in an
1176except clause, chances are that a reference to the object still exists
1177in that function's stack frame as contained in the stack trace.
1178Normally, deleting (better: assigning None to) sys.exc_traceback will
1179take care of this. If you a stack was printed for an unhandled
1180exception in an interactive interpreter, delete sys.last_traceback
1181instead.
1182
1183- There is code that deletes all objects when the interpreter exits,
1184but if your Python has been configured to support threads, it is not
1185called (because other threads may still be active). You can define
Guido van Rossum061f1821994-10-06 16:03:45 +00001186your own cleanup function using sys.exitfunc (see question 4.4).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001187
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001188- Finally, there are some obscure bugs if your __del__ method does
1189complicated things such as updating dictionaries or lists or
1190references globals. I hope to have fixed these in release 1.2.
1191
11924.18. Q. How do I change the shell environment for programs called
1193using os.popen() or os.system()? Changing os.environ doesn't work.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001194
1195A. Modifying the environment passed to subshells was left out of the
1196interpreter because there seemed to be no well-established portable
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001197way to do it (in particular, some systems, have putenv(), others have
1198setenv(), and some have none at all).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001199
1200However if all you want is to pass environment variables to the
1201commands run by os.system() or os.popen(), there's a simple solution:
1202prefix the command string with a couple of variable assignments and
Guido van Rossum796b2591995-01-20 23:05:52 +00001203export statements. The following would be universal for popen:
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001204
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001205 import os
1206 from commands import mkarg # nifty routine to add shell quoting
1207 def epopen(cmd, mode, env = {}):
1208 # env is a dictionary of environment variables
1209 prefix = ''
Guido van Rossum796b2591995-01-20 23:05:52 +00001210 for key, value in env.items():
1211 prefix = prefix + '%s=%s\n' % (key, mkarg(value)[1:])
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001212 prefix = prefix + 'export %s\n' % key
1213 return os.popen(prefix + cmd, mode)
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001214
12154.19. Q. What is a class?
1216
1217A. A class is the particular object type that is created by executing
Guido van Rossum796b2591995-01-20 23:05:52 +00001218a class statement. Class objects are used as templates, to create
1219class instance objects, which embody both the data structure and
1220program routines specific to a datatype.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001221
12224.20. Q. What is a method?
1223
1224A. A method is a function that you normally call as
Guido van Rossum796b2591995-01-20 23:05:52 +00001225x.name(arguments...) for some object x. The term is used for methods
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001226of classes and class instances as well as for methods of built-in
Guido van Rossum796b2591995-01-20 23:05:52 +00001227objects. (The latter have a completely different implementation and
1228only share the way their calls look in Python code.) Methods of
1229classes (and class instances) are defined as functions inside the
1230class definition.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001231
12324.21. Q. What is self?
1233
1234A. Self is merely a conventional name for the first argument of a
1235method -- i.e. a function defined inside a class definition. A method
1236defined as meth(self, a, b, c) should be called as x.meth(a, b, c) for
1237some instance x of the class in which the definition occurs;
1238the called method will think it is called as meth(x, a, b, c).
1239
12404.22. Q. What is a unbound method?
1241
1242A. An unbound method is a method defined in a class that is not yet
1243bound to an instance. You get an unbound method if you ask for a
1244class attribute that happens to be a function. You get a bound method
1245if you ask for an instance attribute. A bound method knows which
Guido van Rossum061f1821994-10-06 16:03:45 +00001246instance it belongs to and calling it supplies the instance automatically;
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001247an unbound method only knows which class it wants for its first
1248argument (a derived class is also OK). Calling an unbound method
1249doesn't "magically" derive the first argument from the context -- you
1250have to provide it explicitly.
1251
Guido van Rossuma6c707c1995-01-02 17:32:28 +000012524.23. Q. How do I call a method defined in a base class from a derived
1253class that overrides it?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001254
1255A. If your class definition starts with "class Derived(Base): ..."
1256then you can call method meth defined in Base (or one of Base's base
1257classes) as Base.meth(self, arguments...). Here, Base.meth is an
1258unbound method (see previous question).
1259
Guido van Rossuma6c707c1995-01-02 17:32:28 +000012604.24. Q. How do I call a method from a base class without using the
1261name of the base class?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001262
1263A. DON'T DO THIS. REALLY. I MEAN IT. It appears that you could call
1264self.__class__.__bases__[0].meth(self, arguments...) but this fails when
Guido van Rossum061f1821994-10-06 16:03:45 +00001265a doubly-derived method is derived from your class: for its instances,
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001266self.__class__.__bases__[0] is your class, not its base class -- so
1267(assuming you are doing this from within Derived.meth) you would start
1268a recursive call.
1269
12704.25. Q. How can I organize my code to make it easier to change the base
1271class?
1272
1273A. You could define an alias for the base class, assign the real base
1274class to it before your class definition, and use the alias throughout
1275your class. Then all you have to change is the value assigned to the
1276alias. Incidentally, this trick is also handy if you want to decide
1277dynamically (e.g. depending on availability of resources) which base
1278class to use. Example:
1279
1280BaseAlias = <real base class>
1281class Derived(BaseAlias):
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001282 def meth(self):
1283 BaseAlias.meth(self)
1284 ...
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001285
12864.26. Q. How can I find the methods or attributes of an object?
1287
1288A. This depends on the object type.
1289
1290For an instance x of a user-defined class, instance attributes are
1291found in the dictionary x.__dict__, and methods and attributes defined
1292by its class are found in x.__class__.__bases__[i].__dict__ (for i in
1293range(len(x.__class__.__bases__))). You'll have to walk the tree of
1294base classes to find *all* class methods and attributes.
1295
1296Many, but not all built-in types define a list of their method names
1297in x.__methods__, and if they have data attributes, their names may be
1298found in x.__members__. However this is only a convention.
1299
1300For more information, read the source of the standard (but
1301undocumented) module newdir.
1302
13034.27. Q. I can't seem to use os.read() on a pipe created with os.popen().
1304
1305A. os.read() is a low-level function which takes a file descriptor (a
1306small integer). os.popen() creates a high-level file object -- the
1307same type used for sys.std{in,out,err} and returned by the builtin
1308open() function. Thus, to read n bytes from a pipe p created with
1309os.popen(), you need to use p.read(n).
Guido van Rossuma7925f11994-01-26 10:20:16 +00001310
Guido van Rossum061f1821994-10-06 16:03:45 +000013114.28. Q. How can I create a stand-alone binary from a Python script?
1312
1313The demo script "Demo/scripts/freeze.py" does what you want. (It's
1314actually not a demo but a support tool -- there is some extra code in
1315the interpreter to accommodate it.) It requires that you have the
1316Python build tree handy, complete with all the lib*.a files.
1317
1318This works by scanning your source recursively for import statements
1319(both forms) and looking for the modules on the standard Python path
1320as well as in the source directory (for built-in modules). It then
1321"compiles" the modules written in Python to C code (array initializers
1322that can be turned into code objects using the marshal module) and
1323creates a custom-made config file that only contains those built-in
1324modules which are actually used in the program. It then compiles the
1325generated C code and links it with the rest of the Python interpreter
1326to form a self-contained binary which acts exactly like your script.
1327
Guido van Rossum7be34a81995-05-31 15:17:12 +00001328Hint: the freeze program only works if your script's filename ends in
1329".py".
Guido van Rossum061f1821994-10-06 16:03:45 +00001330
Guido van Rossum7be34a81995-05-31 15:17:12 +000013314.29. Q. What WWW tools are there for Python?
Guido van Rossum061f1821994-10-06 16:03:45 +00001332
Guido van Rossum7be34a81995-05-31 15:17:12 +00001333A. There's a whole lot in the 1.2 release; see the section on
1334"Internet and WWW" in the Library Reference Manual. Highlights: a CGI
1335parser, a url retriever, an html parser.
Guido van Rossum061f1821994-10-06 16:03:45 +00001336
1337Steve Miale <smiale@cs.indiana.edu> has written a modular WWW browser
1338called Dancer. An alpha version can be FTP'ed from
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001339<URL:ftp://ftp.cs.indiana.edu/pub/smiale/dancer.tar.gz>. (There are a
1340few articles about Dancer in the (hyper)mail archive
1341<URL:http://www.cwi.nl/~guido/hypermail/python-1994q3/index.html>.)
Guido van Rossum061f1821994-10-06 16:03:45 +00001342
Guido van Rossum7be34a81995-05-31 15:17:12 +000013434.30. Q. How do I run a subprocess with pipes connected to both input
Guido van Rossum796b2591995-01-20 23:05:52 +00001344and output?
1345
1346A. This is really a UNIX question. Also, in general, it is unwise to
1347do so, because you can easily cause a deadlock where the parent
1348process is blocked waiting for output from the child, while the child
1349is blocked waiting for input from the child. This can be caused
1350because the parent expects the child to output more text than it does,
1351or it can be caused by data being stuck in stdio buffers due to lack
1352of flushing. The Python parent can of course explicitly flush the data
1353it sends to the child before it reads any output, but if the child is
1354a naive C program it can easily have been written to never explicitly
1355flush its output, even if it is interactive, since flushing is
1356normally automatic.
1357
1358In many cases, all you really need is to run some data through a
1359command and get the result back. Unless the data is infinite in size,
1360the easiest (and often the most efficient!) way to do this is to write
1361it to a temporary file and run the command with that temporary file as
1362input. The standard module tempfile exports a function mktemp() which
1363generates unique temporary file names.
1364
1365If after reading all of the above you still want to connect two pipes
1366to a subprocess's standard input and output, here's a simple solution,
1367due to Jack Jansen:
1368
1369 import os
1370 import sys
1371 import string
1372
1373 MAXFD = 100 # Max number of file descriptors in this system
1374
1375 def popen2(cmd):
1376 cmd = string.split(cmd)
1377 p2cread, p2cwrite = os.pipe()
1378 c2pread, c2pwrite = os.pipe()
1379 pid = os.fork()
1380 if pid == 0:
1381 # Child
1382 os.close(0)
1383 os.close(1)
1384 if os.dup(p2cread) <> 0:
1385 sys.stderr.write('popen2: bad read dup\n')
1386 if os.dup(c2pwrite) <> 1:
1387 sys.stderr.write('popen2: bad write dup\n')
1388 for i in range(3, MAXFD):
1389 try:
1390 os.close(i)
1391 except:
1392 pass
1393 try:
1394 os.execv(cmd[0], cmd)
1395 finally:
1396 os._exit(1)
1397 os.close(p2cread)
1398 tochild = os.fdopen(p2cwrite, 'w')
1399 os.close(c2pwrite)
1400 fromchild = os.fdopen(c2pread, 'r')
1401 return fromchild, tochild
1402
1403Note that many interactive programs (e.g. vi) don't work well with
1404pipes substituted for standard input and output. You will have to use
1405pseudo ttys ("ptys") instead of pipes. There is some undocumented
1406code to use these in the library module pty.py -- I'm afraid you're on
Guido van Rossum3fc9d731995-07-25 15:10:56 +00001407your own here.
1408
1409A different answer is a Python interface to Don Libes' "expect"
1410library. A prerelease of this is available on the Python ftp mirror
1411sites in the contrib subdirectory as expy-0.3.tar.gz, e.g.
1412<URL:ftp://ftp.python.org/pub/python/contrib/expy-0.3.tar.gz>.
Guido van Rossum796b2591995-01-20 23:05:52 +00001413
Guido van Rossum7be34a81995-05-31 15:17:12 +000014144.31. Q. How do I call a function if I have the arguments in a tuple?
Guido van Rossumac3f2121995-04-10 11:53:42 +00001415
1416A. Use the built-in function apply(). For instance,
1417
1418 func(1, 2, 3)
1419
1420is equivalent to
1421
1422 args = (1, 2, 3)
1423 apply(func, args)
1424
1425Note that func(args) is not the same -- it calls func() with exactly
1426one argument, the tuple args, instead of three arguments, the integers
14271, 2 and 3.
1428
Guido van Rossum7be34a81995-05-31 15:17:12 +000014294.32. Q. How do I enable font-lock-mode for Python in Emacs?
1430
1431A. Assuming you're already using python-mode and font-lock-mode
1432separately, all you need to do is put this in your .emacs file:
1433
1434 (defun my-python-mode-hook ()
1435 (setq font-lock-keywords python-font-lock-keywords)
1436 (font-lock-mode 1))
1437 (add-hook 'python-mode-hook 'my-python-mode-hook)
1438
14394.33. Q. Is there an inverse to the format operator (a la C's scanf())?
1440
1441A. Not as such.
1442
1443For simple input parsing, the easiest approach is usually to split
1444the line into whitespace-delimited words using string.split(), and to
1445convert decimal strings to numeric values using string.atoi(),
1446string.atol() or string.atof(). (Python's atoi() is 32-bit and its
1447atol() is arbitrary precision.) If you want to use another delimiter
1448than whitespace, use string.splitfield() (possibly combining it with
1449string.strip() which removes surrounding whitespace from a string).
1450
1451For more complicated input parsing, regular expressions (see module
1452regex) are better suited and more powerful than C's scanf().
1453
Guido van Rossum061f1821994-10-06 16:03:45 +00001454
Guido van Rossuma7925f11994-01-26 10:20:16 +000014555. Extending Python
1456===================
1457
14585.1. Q. Can I create my own functions in C?
1459
1460A. Yes, you can create built-in modules containing functions,
Guido van Rossum24349991994-02-02 14:12:45 +00001461variables, exceptions and even new types in C. This is explained in
1462the document "Extending and Embedding the Python Interpreter" (the
1463LaTeX file Doc/ext.tex). Also read the chapter on dynamic loading.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001464
14655.2. Q. Can I create my own functions in C++?
1466
1467A. Yes, using the C-compatibility features found in C++. Basically
1468you place extern "C" { ... } around the Python include files and put
1469extern "C" before each function that is going to be called by the
1470Python interpreter. Global or static C++ objects with constructors
1471are probably not a good idea.
1472
Guido van Rossum7ce61c11994-06-13 15:13:56 +000014735.3. Q. How can I execute arbitrary Python statements from C?
1474
1475A. The highest-level function to do this is run_command() which takes
1476a single string argument which is executed in the context of module
1477__main__ and returns 0 for success and -1 when an exception occurred
1478(including SyntaxError). If you want more control, use run_string();
1479see the source for run_command() in Python/pythonrun.c.
1480
14815.4. Q. How can I evaluate an arbitrary Python expression from C?
1482
1483A. Call the function run_string() from the previous question with the
1484start symbol eval_input; it then parses an expression, evaluates it
1485and returns its value. See exec_eval() in Python/bltinmodule.c.
1486
14875.5. Q. How do I extract C values from a Python object?
1488
1489A. That depends on the object's type. If it's a tuple,
1490gettuplesize(o) returns its length and gettupleitem(o, i) returns its
1491i'th item; similar for lists with getlistsize(o) and getlistitem(o,
1492i). For strings, getstringsize(o) returns its length and
1493getstringvalue(o) a pointer to its value (note that Python strings may
1494contain null bytes so strlen() is not safe). To test which type an
1495object is, first make sure it isn't NULL, and then use
1496is_stringobject(o), is_tupleobject(o), is_listobject(o) etc.
1497
14985.6. Q. How do I use mkvalue() to create a tuple of arbitrary length?
1499
1500A. You can't. Use t = newtupleobject(n) instead, and fill it with
1501objects using settupleitem(t, i, o) -- note that this "eats" a
1502reference count of o. Similar for lists with newlistobject(n) and
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001503setlistitem(l, i, o). Note that you *must* set all the tuple items to
1504some value before you pass the tuple to Python code --
1505newtupleobject(n) initializes them to NULL, which isn't a valid Python
1506value.
1507
Guido van Rossum796b2591995-01-20 23:05:52 +000015085.7. Q. What happened to mktuple(), featured in an example in the
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001509Extensions manual?
1510
1511A. It's a typo, I meant newtupleobject() (see previous question).
1512
15135.8. Q. How do I call an object's method from C?
1514
1515A. Here's a function (untested) that might become part of the next
1516release in some form. It uses <stdarg.h> to allow passing the
1517argument list on to vmkvalue():
1518
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001519 object *call_method(object *inst, char *methodname, char *format, ...)
1520 {
1521 object *method;
1522 object *args;
1523 object *result;
1524 va_list va;
1525 method = getattr(inst, methodname);
1526 if (method == NULL) return NULL;
1527 va_start(va, format);
1528 args = vmkvalue(format, va);
1529 va_end(va);
1530 if (args == NULL) {
1531 DECREF(method);
1532 return NULL;
1533 }
1534 result = call_object(method, args);
1535 DECREF(method);
1536 DECREF(args);
1537 return result;
1538 }
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001539
1540This works for any instance that has methods -- whether built-in or
1541user-defined. You are responsible for eventually DECREF'ing the
1542return value.
1543
1544To call, e.g., a file object's "seek" method with arguments 10, 0
1545(assuming the file object pointer is "f"):
1546
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001547 res = call_method(f, "seek", "(OO)", 10, 0);
1548 if (res == NULL) {
1549 ... an exception occurred ...
1550 }
1551 else {
1552 DECREF(res);
1553 }
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001554
1555Note that since call_object() *always* wants a tuple for the argument
1556list, to call a function without arguments, pass "()" for the format,
1557and to call a function with one argument, surround the argument in
1558parentheses, e.g. "(i)".
1559
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00001560(This function and a companion will be available in 1.2 under the
1561names PyEval_CallFunction and PyEval_CallMethod.)
1562
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000015635.9. Q. How do I catch the output from print_error()?
1564
1565A. (Due to Mark Hammond):
1566
1567* in Python code, define an object that supports the "write()" method.
1568FWIW, there seems to be a small problem that requires the 'softspace'
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00001569attribute to be defined too (fixed in 1.2).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001570
1571* redirect sys.stdout and sys.stderr to this object.
1572
Guido van Rossum061f1821994-10-06 16:03:45 +00001573* call print_error, or just allow the standard traceback mechanism to
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001574work.
1575
Guido van Rossum061f1821994-10-06 16:03:45 +00001576Then, the output will go wherever your write() method sends it.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001577
15785.10. Q. How do I access a module written in Python from C?
1579
1580A. You can get a pointer to the module object as follows:
1581
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001582 module = import_module("<modulename>");
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001583
1584If the module hasn't been imported yet (i.e. it is not yet present in
1585sys.modules), this initializes the module; otherwise it simply returns
1586the value of sys.modules["<modulename>"]. Note that it doesn't enter
1587the module into any namespace -- it only ensures it has been
1588initialized and is stored in sys.modules.
1589
1590You can then access the module's attributes (i.e. any name defined in
1591the module) as follows:
1592
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001593 attr = getattr(module, "<attrname>");
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001594
1595Calling setattr(), to assign to variables in the module, also works.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001596
Guido van Rossuma7925f11994-01-26 10:20:16 +00001597
15986. Python's design
1599==================
1600
16016.1. Q. Why isn't there a generic copying operation for objects in
1602Python?
1603
1604A. Hmm. Maybe there should be one, but it's difficult to assign a
1605useful meaning to copying of open files, sockets and windows, or
1606recursive data structures. As long as you design all your classes
1607yourself you are of course free to define a standard base class that
1608defines an overridable copying operation for all the objects you care
1609about. (One practical point: it would have to be a built-in function,
1610not a standard method name, since not all built-in object types have
1611methods; e.g. strings, integers and tuples don't.)
1612
16136.2. Q. Why isn't there a generic way to implement persistent objects
1614in Python? (Persistent == automatically saved to and restored from
1615disk.)
1616
1617A. Hmm, hmm. Basically for the same reasons as why there is no
1618generic copying operation.
1619
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001620A partial solution will appear in release 1.2. This will also provide
1621a partial solution to the problem of a generic copying operation.
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00001622(Partial because it still won't cope with open files etc.; however it
1623does handle user-defined classes!)
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001624
Guido van Rossuma7925f11994-01-26 10:20:16 +000016256.3. Q. Why isn't there a switch or case statement in Python?
1626
1627A. You can do this easily enough with a sequence of
1628if... elif... elif... else. There have been some proposals for switch
Guido van Rossum061f1821994-10-06 16:03:45 +00001629statement syntax, but there is no consensus (yet) on whether and how
Guido van Rossuma7925f11994-01-26 10:20:16 +00001630to do range tests.
1631
Guido van Rossumc50158e1994-05-31 09:18:50 +000016326.4. Q. Why does Python use indentation for grouping of statements?
1633
1634A. Basically I believe that using indentation for grouping is
1635extremely elegant and contributes a lot to the clarity of the average
1636Python program. Most people learn to love this feature after a while.
1637Some arguments for it:
1638
1639- Since there are no begin/end brackets there cannot be a disagreement
1640between grouping perceived by the parser and the human reader. I
1641remember long ago seeing a C fragment like this:
1642
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001643 if (x <= y)
1644 x++;
1645 y--;
1646 z++;
Guido van Rossumc50158e1994-05-31 09:18:50 +00001647
1648and staring a long time at it wondering why y was being decremented
1649even for x > y... (And I wasn't a C newbie then either.)
1650
Guido van Rossum7be34a81995-05-31 15:17:12 +00001651- Since there are no begin/end brackets, Python is much less prone to
1652coding-style conflicts. In C there are loads of different ways to
1653place the braces (including the choice whether to place braces around
1654single statements in certain cases, for consistency). If you're used
1655to reading (and writing) code that uses one style, you will feel at
1656least slightly uneasy when reading (or being required to write)
1657another style.
Guido van Rossumc50158e1994-05-31 09:18:50 +00001658
1659- Many coding styles place begin/end brackets on a line by themself.
1660This makes programs considerably longer and wastes valuable screen
1661space, making it harder to get a good overview over a program.
1662Ideally, a function should fit on one basic tty screen (say, 20
1663lines). 20 lines of Python are worth a LOT more than 20 lines of C.
1664This is not solely due to the lack of begin/end brackets (the lack of
1665declarations also helps, and the powerful operations of course), but
1666it certainly helps!
1667
Guido van Rossum3de27361994-07-25 14:19:33 +000016686.5. Q. Why are Python strings immutable?
1669
1670A. There are two advantages. One is performance: knowing that a
1671string is immutable makes it easy to lay it out at construction time
1672-- fixed and unchanging storage requirements. (This is also one of
Guido van Rossum061f1821994-10-06 16:03:45 +00001673the reasons for the distinction between tuples and lists.) The
Guido van Rossum3de27361994-07-25 14:19:33 +00001674other is that strings in Python are considered as "elemental" as
1675numbers. No amount of activity will change the value 8 to anything
1676else, and in Python, no amount of activity will change the string
1677"eight" to anything else. (Adapted from Jim Roskind)
1678
16796.6. Q. Why don't strings have methods like index() or sort(), like
1680lists?
1681
1682A. Good question. Strings currently don't have methods at all
1683(likewise tuples and numbers). Long ago, it seemed unnecessary to
1684implement any of these functions in C, so a standard library module
1685"string" written in Python was created that performs string related
1686operations. Since then, the cry for performance has moved most of
1687them into the built-in module strop (this is imported by module
Guido van Rossumf8c76d01994-08-17 12:19:53 +00001688string, which is still the preferred interface, without loss of
Guido van Rossum3de27361994-07-25 14:19:33 +00001689performance except during initialization). Some of these functions
1690(e.g. index()) could easily be implemented as string methods instead,
1691but others (e.g. sort()) can't, since their interface prescribes that
1692they modify the object, while strings are immutable (see the previous
1693question).
1694
16956.7. Q. Why does Python use methods for some functionality
1696(e.g. list.index()) but functions for other (e.g. len(list))?
1697
1698A. Functions are used for those operations that are generic for a
1699group of types and which should work even for objects that don't have
1700methods at all (e.g. numbers, strings, tuples). Also, implementing
1701len(), max(), min() as a built-in function is actually less code than
1702implementing them as methods for each type. One can quibble about
1703individual cases but it's really too late to change such things
1704fundamentally now.
1705
17066.8. Q. Why can't I derive a class from built-in types (e.g. lists or
1707files)?
1708
1709A. This is caused by the relatively late addition of (user-defined)
1710classes to the language -- the implementation framework doesn't easily
1711allow it. See the answer to question 4.2 for a work-around. This
1712*may* be fixed in the (distant) future.
1713
17146.9. Q. Why must 'self' be declared and used explicitly in method
1715definitions and calls?
1716
1717A. By asking this question you reveal your C++ background. :-)
1718When I added classes, this was (again) the simplest way of
1719implementing methods without too many changes to the interpreter. I
1720borrowed the idea from Modula-3. It turns out to be very useful, for
1721a variety of reasons.
1722
1723First, it makes it more obvious that you are using a method or
1724instance attribute instead of a local variable. Reading "self.x" or
1725"self.meth()" makes it absolutely clear that an instance variable or
1726method is used even if you don't know the class definition by heart.
1727In C++, you can sort of tell by the lack of a local variable
Guido van Rossum061f1821994-10-06 16:03:45 +00001728declaration (assuming globals are rare or easily recognizable) -- but
Guido van Rossum3de27361994-07-25 14:19:33 +00001729in Python, there are no local variable declarations, so you'd have to
1730look up the class definition to be sure.
1731
1732Second, it means that no special syntax is necessary if you want to
1733explicitly reference or call the method from a particular class. In
1734C++, if you want to use a method from base class that is overridden in
1735a derived class, you have to use the :: operator -- in Python you can
1736write baseclass.methodname(self, <argument list>). This is
1737particularly useful for __init__() methods, and in general in cases
1738where a derived class method wants to extend the base class method of
1739the same name and thus has to call the base class method somehow.
1740
1741Lastly, for instance variables, it solves a syntactic problem with
1742assignment: since local variables in Python are (by definition!) those
1743variables to which a value assigned in a function body (and that
1744aren't explicitly declared global), there has to be some way to tell
1745the interpreter that an assignment was meant to assign to an instance
1746variable instead of to a local variable, and it should preferably be
1747syntactic (for efficiency reasons). C++ does this through
1748declarations, but Python doesn't have declarations and it would be a
1749pity having to introduce them just for this purpose. Using the
1750explicit "self.var" solves this nicely. Similarly, for using instance
1751variables, having to write "self.var" means that references to
1752unqualified names inside a method don't have to search the instance's
1753directories.
1754
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000017556.10. Q. Can't you emulate threads in the interpreter instead of
1756relying on an OS-specific thread implementation?
1757
1758A. Unfortunately, the interpreter pushes at least one C stack frame
1759for each Python stack frame. Also, extensions can call back into
1760Python at almost random moments. Therefore a complete threads
1761implementation requires thread support for C.
1762
Guido van Rossum061f1821994-10-06 16:03:45 +000017636.11. Q. Why can't lambda forms contain statements?
1764
1765A. Python lambda forms cannot contain statements because Python's
Guido van Rossum796b2591995-01-20 23:05:52 +00001766syntactic framework can't handle statements nested inside expressions.
Guido van Rossum061f1821994-10-06 16:03:45 +00001767
1768However, in Python, this is not a serious problem. Unlike lambda
1769forms in other languages, where they add functionality, Python lambdas
1770are only a shorthand notation if you're too lazy to define a function.
1771
1772Functions are already first class objects in Python, and can be
1773declared in a local scope. Therefore the only advantage of using a
1774lambda form instead of a locally-defined function is that you'll have
1775to invent a name for the function -- but that's just a local variable
1776to which the function object (which is exactly the same type of object
1777that a lambda form yields) is assigned!
1778
17796.12. Q. Why is there no more efficient way of iterating over a dictionary
1780than first constructing the list of keys()?
1781
1782A. Have you tried it? I bet it's fast enough for your purposes! In
1783most cases such a list takes only a few percent of the space occupied
1784by the dictionary -- it needs only 4 bytes (the size of a pointer) per
1785key -- a dictionary costs 8 bytes per key plus between 30 and 70
1786percent hash table overhead, plus the space for the keys and values --
1787by necessity all keys are unique objects and a string object (the most
1788common key type) costs at least 18 bytes plus the length of the
1789string. Add to that the values contained in the dictionary, and you
1790see that 4 bytes more per item really isn't that much more memory...
1791
1792A call to dict.keys() makes one fast scan over the dictionary
1793(internally, the iteration function does exist) copying the pointers
1794to the key objects into a pre-allocated list object of the right size.
1795The iteration time isn't lost (since you'll have to iterate anyway --
1796unless in the majority of cases your loop terminates very prematurely
1797(which I doubt since you're getting the keys in random order).
1798
1799I don't expose the dictionary iteration operation to Python
1800programmers because the dictionary shouldn't be modified during the
1801entire iteration -- if it is, there's a very small chance that the
1802dictionary is reorganized because the hash table becomes too full, and
1803then the iteration may miss some items and see others twice. Exactly
1804because this only occurs rarely, it would lead to hidden bugs in
1805programs: it's easy never to have it happen during test runs if you
1806only insert or delete a few items per iteration -- but your users will
1807surely hit upon it sooner or later.
1808
Guido van Rossuma7925f11994-01-26 10:20:16 +00001809
18107. Using Python on non-UNIX platforms
1811=====================================
1812
Guido van Rossum91f60831994-02-15 15:52:27 +000018137.1. Q. Is there a Mac version of Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +00001814
Guido van Rossume530c581995-04-10 12:32:16 +00001815A. Yes, see the "mac" subdirectory of the distribution sites,
Guido van Rossum7be34a81995-05-31 15:17:12 +00001816e.g. <URL:ftp://ftp.python.org/pub/python/mac/>.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001817
Guido van Rossum91f60831994-02-15 15:52:27 +000018187.2. Q. Is there a DOS version of Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +00001819
Guido van Rossume530c581995-04-10 12:32:16 +00001820A. Yes, see the "pc" subdirectory of the distribution sites,
Guido van Rossum7be34a81995-05-31 15:17:12 +00001821e.g. <URL:ftp://ftp.python.org/pub/python/pc/>.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001822
Guido van Rossume530c581995-04-10 12:32:16 +000018237.3. Q. Is there a Windows 3.1(1) version of Python?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001824
Guido van Rossume530c581995-04-10 12:32:16 +00001825A. Yes, also see the "pc" subdirectory of the distribution sites,
Guido van Rossum3fc9d731995-07-25 15:10:56 +00001826e.g. <URL:ftp://ftp.python.org/pub/python/pc/>. You may also be able
1827to run either of the Windows NT versions (see next question) if you
1828have Microsoft's "win32s".
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001829
Guido van Rossum91f60831994-02-15 15:52:27 +000018307.4. Q. Is there a Windows NT version of Python?
1831
Guido van Rossum3fc9d731995-07-25 15:10:56 +00001832A. There are two, both sporting DLL support for dynamic loading of
1833Python modules, and extensions to access the Win32 GUI API.
1834
1835Mark Hammond <MHammond@cmutual.com.au> maintains an NT port which
1836includes an interface to the Microsoft Foundation Classes and a Python
1837programming environment using it that's written mostly in Python. See
1838<URL:ftp://ftp.python.org/pub/python/nt/>.
1839
1840Jim Ahlstrom's WPY portable GUI runs on Windows NT and is modeled
1841after the Microsoft Foundation Classes. Source and binaries are
1842available in <URL:ftp://ftp.python.org/pub/python/wpy>.
Guido van Rossum91f60831994-02-15 15:52:27 +00001843
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001844Sam Rushing <rushing@squirl.oau.org> once announced he knows how to
1845build Python for the Windows NT on the DEC Alpha AXP.
Guido van Rossum061f1821994-10-06 16:03:45 +00001846
1847Note that currently there is no unified compilation environment for
1848all NT platforms -- hopefully Microsoft will fix this with the release
1849of Visual C++ 2.0.
1850
Guido van Rossume530c581995-04-10 12:32:16 +000018517.5. Q. Is there a Windows 95 version of Python?
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001852
Guido van Rossum3fc9d731995-07-25 15:10:56 +00001853A. The Windows NT versions might work, otherwise the Windows 3.1(1)
Guido van Rossume530c581995-04-10 12:32:16 +00001854version should work (isn't Windows 95 supposed to be backwards
1855compatible?).
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001856
Guido van Rossume530c581995-04-10 12:32:16 +000018577.6. Q. Is there an OS/2 version of Python?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001858
Guido van Rossume530c581995-04-10 12:32:16 +00001859A. Yes, also see the "pc" subdirectory of the distribution sites,
Guido van Rossum7be34a81995-05-31 15:17:12 +00001860e.g. <URL:ftp://ftp.python.org/pub/python/pc/>.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001861
Guido van Rossume530c581995-04-10 12:32:16 +000018627.7. Q. Is there a VMS version of Python?
1863
Guido van Rossum3fc9d731995-07-25 15:10:56 +00001864A. Donn Cave <donn@cac.washington.edu> did a partial port. The
1865results of his efforts are on public display in
1866<<URL:ftp://ftp.python.org/pub/python/contrib/vms.tar.gz/>.
Guido van Rossume530c581995-04-10 12:32:16 +00001867
Guido van Rossum3fc9d731995-07-25 15:10:56 +000018687.8. Q. What about IBM mainframes, or other non-UNIX platforms?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001869
Guido van Rossum3fc9d731995-07-25 15:10:56 +00001870A. I haven't heard about these, except I remember once hearing about
1871an attempt at an OS/9 port. If you're interested in any of this, go
1872directly to the newsgroup and ask there, who knows what you may find.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001873
Guido van Rossume530c581995-04-10 12:32:16 +000018747.9. Q. Where are the source or Makefiles for the non-UNIX versions?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001875
Guido van Rossume530c581995-04-10 12:32:16 +00001876A. The standard sources can (almost) be used. Additional sources can
1877be found in the platform-specific subdirectories of the distribution.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001878
Guido van Rossume530c581995-04-10 12:32:16 +000018797.10. Q. What is the status and support for the non-UNIX versions?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001880
1881A. I don't have access to most of these platforms, so in general I am
1882dependent on material submitted by volunteers(*). However I strive to
1883integrate all changes needed to get it to compile on a particular
1884platform back into the standard sources, so porting of the next
1885version to the various non-UNIX platforms should be easy.
1886
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001887(*) For the Macintosh, that volunteer is me, with help from Jack
1888Jansen <jack@cwi.nl>.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001889
Guido van Rossume530c581995-04-10 12:32:16 +000018907.11. Q. I have a PC version but it appears to be only a binary.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001891Where's the library?
Guido van Rossuma7925f11994-01-26 10:20:16 +00001892
1893A. You still need to copy the files from the distribution directory
Guido van Rossum91f60831994-02-15 15:52:27 +00001894"python/Lib" to your system. If you don't have the full distribution,
Guido van Rossume530c581995-04-10 12:32:16 +00001895you can get the file lib<version>.tar.gz from most ftp sites carrying
1896Python; this is a subset of the distribution containing just those
Guido van Rossum7be34a81995-05-31 15:17:12 +00001897files, e.g. <URL:ftp://ftp.python.org/pub/python/src/lib1.1.tar.gz>.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001898
Guido van Rossum5333c5d1994-04-11 11:06:22 +00001899Once you have installed the library, you need to point sys.path to it.
1900Assuming the library is in C:\misc\python\lib, the following commands
1901will point your Python interpreter to it (note the doubled backslashes
1902-- you can also use single forward slashes instead):
1903
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001904 >>> import sys
1905 >>> sys.path.insert(0, 'C:\\misc\\python\\lib')
1906 >>>
Guido van Rossum5333c5d1994-04-11 11:06:22 +00001907
1908For a more permanent effect, set the environment variable PYTHONPATH,
1909as follows (talking to a DOS prompt):
1910
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001911 C> SET PYTHONPATH=C:\misc\python\lib
Guido van Rossum5333c5d1994-04-11 11:06:22 +00001912
Guido van Rossume530c581995-04-10 12:32:16 +000019137.12. Q. Where's the documentation for the Mac or PC version?
Guido van Rossuma7925f11994-01-26 10:20:16 +00001914
Guido van Rossume530c581995-04-10 12:32:16 +00001915A. The documentation for the Unix version also applies to the Mac and
1916PC versions. Where applicable, differences are indicated in the text.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001917
Guido van Rossume530c581995-04-10 12:32:16 +000019187.13. Q. The Mac (PC) version doesn't seem to have any facilities for
Guido van Rossum91f60831994-02-15 15:52:27 +00001919creating or editing programs apart from entering it interactively, and
1920there seems to be no way to save code that was entered interactively.
1921How do I create a Python program on the Mac (PC)?
Guido van Rossuma7925f11994-01-26 10:20:16 +00001922
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001923A. Use an external editor. On the Mac, BBEdit seems to be a popular
1924no-frills text editor. I work like this: start the interpreter; edit
1925a module file using BBedit; import and test it in the interpreter;
1926edit again in BBedit; then use the built-in function reload() to
1927re-read the imported module; etc.
Guido van Rossum5333c5d1994-04-11 11:06:22 +00001928
1929Regarding the same question for the PC, Kurt Wm. Hemr writes: "While
1930anyone with a pulse could certainly figure out how to do the same on
1931MS-Windows, I would recommend the NotGNU Emacs clone for MS-Windows.
1932Not only can you easily resave and "reload()" from Python after making
1933changes, but since WinNot auto-copies to the clipboard any text you
1934select, you can simply select the entire procedure (function) which
1935you changed in WinNot, switch to QWPython, and shift-ins to reenter
1936the changed program unit."