blob: 6c9d57c50a3c91acf95f0007f1b79053ea94a4d9 [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)
6Expires: Sat, 1 Jul 1995 00:00:00 GMT
7Supersedes: <D6uD1L.BLv@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 Rossum7be34a81995-05-31 15:17:12 +000012Version: 1.24
13Last-modified: 31 May 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 Rossuma7925f11994-01-26 10:20:16 +0000111
112 4. Programming in Python
Guido van Rossum24349991994-02-02 14:12:45 +0000113 4.1. Q. Is there a source code level debugger with breakpoints, step,
114 etc.?
115 4.2. Q. Can I create an object class with some methods implemented in
Guido van Rossuma7925f11994-01-26 10:20:16 +0000116 C and others in Python (e.g. through inheritance)? (Also phrased as:
117 Can I use a built-in type as base class?)
Guido van Rossum24349991994-02-02 14:12:45 +0000118 4.3. Q. Is there a curses/termcap package for Python?
119 4.4. Q. Is there an equivalent to C's onexit() in Python?
120 4.5. Q. When I define a function nested inside another function, the
Guido van Rossuma7925f11994-01-26 10:20:16 +0000121 nested function seemingly can't access the local variables of the
122 outer function. What is going on? How do I pass local data to a
123 nested function?
Guido van Rossum24349991994-02-02 14:12:45 +0000124 4.6. Q. How do I iterate over a sequence in reverse order?
125 4.7. Q. My program is too slow. How do I speed it up?
126 4.8. Q. When I have imported a module, then edit it, and import it
Guido van Rossuma7925f11994-01-26 10:20:16 +0000127 again (into the same Python process), the changes don't seem to take
128 place. What is going on?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000129 4.9. Q. How do I find the current module name?
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000130 4.10. Q. I have a module in which I want to execute some extra code
131 when it is run as a script. How do I find out whether I am running as
132 a script?
133 4.11. Q. I try to run a program from the Demo directory but it fails
134 with ImportError: No module named ...; what gives?
Guido van Rossum061f1821994-10-06 16:03:45 +0000135 4.12. Q. I have successfully built Python with STDWIN but it can't
136 find some modules (e.g. stdwinevents).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000137 4.13. Q. What GUI toolkits exist for Python?
Guido van Rossum061f1821994-10-06 16:03:45 +0000138 4.14. Q. Are there any interfaces to database packages in Python?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000139 4.15. Q. Is it possible to write obfuscated one-liners in Python?
140 4.16. Q. Is there an equivalent of C's "?:" ternary operator?
141 4.17. Q. My class defines __del__ but it is not called when I delete the
142 object.
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000143 4.18. Q. How do I change the shell environment for programs called
144 using os.popen() or os.system()? Changing os.environ doesn't work.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000145 4.19. Q. What is a class?
146 4.20. Q. What is a method?
147 4.21. Q. What is self?
148 4.22. Q. What is a unbound method?
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000149 4.23. Q. How do I call a method defined in a base class from a derived
150 class that overrides it?
151 4.24. Q. How do I call a method from a base class without using the
152 name of the base class?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000153 4.25. Q. How can I organize my code to make it easier to change the base
154 class?
155 4.26. Q. How can I find the methods or attributes of an object?
Guido van Rossum061f1821994-10-06 16:03:45 +0000156 4.27. Q. I can't seem to use os.read() on a pipe created with os.popen().
157 4.28. Q. How can I create a stand-alone binary from a Python script?
Guido van Rossum7be34a81995-05-31 15:17:12 +0000158 4.29. Q. What WWW tools are there for Python?
159 4.30. Q. How do I run a subprocess with pipes connected to both input
Guido van Rossum796b2591995-01-20 23:05:52 +0000160 and output?
Guido van Rossum7be34a81995-05-31 15:17:12 +0000161 4.31. Q. How do I call a function if I have the arguments in a tuple?
162 4.32. Q. How do I enable font-lock-mode for Python in Emacs?
163 4.33. Q. Is there an inverse to the format operator (a la C's scanf())?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000164
165 5. Extending Python
166 5.1. Q. Can I create my own functions in C?
167 5.2. Q. Can I create my own functions in C++?
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000168 5.3. Q. How can I execute arbitrary Python statements from C?
169 5.4. Q. How can I evaluate an arbitrary Python expression from C?
170 5.5. Q. How do I extract C values from a Python object?
171 5.6. Q. How do I use mkvalue() to create a tuple of arbitrary length?
Guido van Rossum796b2591995-01-20 23:05:52 +0000172 5.7. Q. What happened to mktuple(), featured in an example in the
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000173 Extensions manual?
174 5.8. Q. How do I call an object's method from C?
175 5.9. Q. How do I catch the output from print_error()?
176 5.10. Q. How do I access a module written in Python from C?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000177
178 6. Python's design
179 6.1. Q. Why isn't there a generic copying operation for objects in
180 Python?
181 6.2. Q. Why isn't there a generic way to implement persistent objects
182 in Python? (Persistent == automatically saved to and restored from
183 disk.)
184 6.3. Q. Why isn't there a switch or case statement in Python?
Guido van Rossumc50158e1994-05-31 09:18:50 +0000185 6.4. Q. Why does Python use indentation for grouping of statements?
Guido van Rossum3de27361994-07-25 14:19:33 +0000186 6.5. Q. Why are Python strings immutable?
187 6.6. Q. Why don't strings have methods like index() or sort(), like
188 lists?
189 6.7. Q. Why does Python use methods for some functionality
190 (e.g. list.index()) but functions for other (e.g. len(list))?
191 6.8. Q. Why can't I derive a class from built-in types (e.g. lists or
192 files)?
193 6.9. Q. Why must 'self' be declared and used explicitly in method
194 definitions and calls?
Guido van Rossum061f1821994-10-06 16:03:45 +0000195 6.10. Q. Can't you emulate threads in the interpreter instead of
196 relying on an OS-specific thread implementation?
197 6.11. Q. Why can't lambda forms contain statements?
198 6.12. Q. Why is there no more efficient way of iterating over a dictionary
199 than first constructing the list of keys()?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000200
201 7. Using Python on non-UNIX platforms
Guido van Rossum91f60831994-02-15 15:52:27 +0000202 7.1. Q. Is there a Mac version of Python?
203 7.2. Q. Is there a DOS version of Python?
Guido van Rossume530c581995-04-10 12:32:16 +0000204 7.3. Q. Is there a Windows 3.1(1) version of Python?
Guido van Rossum91f60831994-02-15 15:52:27 +0000205 7.4. Q. Is there a Windows NT version of Python?
Guido van Rossume530c581995-04-10 12:32:16 +0000206 7.5. Q. Is there a Windows 95 version of Python?
207 7.6. Q. Is there an OS/2 version of Python?
208 7.7. Q. Is there a VMS version of Python?
209 7.8. Q. What about IBM mainframes, or other esoteric non-UNIX
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000210 platforms?
Guido van Rossume530c581995-04-10 12:32:16 +0000211 7.9. Q. Where are the source or Makefiles for the non-UNIX versions?
212 7.10. Q. What is the status and support for the non-UNIX versions?
213 7.11. Q. I have a PC version but it appears to be only a binary.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000214 Where's the library?
Guido van Rossume530c581995-04-10 12:32:16 +0000215 7.12. Q. Where's the documentation for the Mac or PC version?
216 7.13. Q. The Mac (PC) version doesn't seem to have any facilities for
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000217 creating or editing programs apart from entering it interactively, and
218 there seems to be no way to save code that was entered interactively.
219 How do I create a Python program on the Mac (PC)?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000220
221To find a particular question, search for the question number followed
222by a dot, a space, and a Q at the beginning of a line (e.g. to find
223question 4.2 in vi, type /^4\.2\. Q/).
224
225
2261. General information and availability
227=======================================
228
2291.1. Q. What is Python?
230
231A. Python is an interpreted, interactive, object-oriented programming
232language. It incorporates modules, exceptions, dynamic typing, very
233high level dynamic data types, and classes. Python combines
234remarkable power with very clear syntax. It has interfaces to many
235system calls and libraries, as well as to various window systems, and
236is extensible in C or C++. It is also usable as an extension language
237for applications that need a programmable interface. Finally, Python
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000238is portable: it runs on many brands of UNIX, on the Mac, and on PCs
239under MS-DOS, Windows, Windows NT, and OS/2.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000240
241To find out more, the best thing to do is to start reading the
242tutorial from the documentation set (see a few questions further
243down).
244
2451.2. Q. Why is it called Python?
246
Guido van Rossum796b2591995-01-20 23:05:52 +0000247A. Apart from being a computer scientist, I'm also a fan of "Monty
Guido van Rossuma7925f11994-01-26 10:20:16 +0000248Python's Flying Circus" (a BBC comedy series from the seventies, in
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000249the -- unlikely -- case you didn't know). It occurred to me one day
250that I needed a name that was short, unique, and slightly mysterious.
251And I happened to be reading some scripts from the series at the
252time... So then I decided to call my language Python. But Python is
253not a joke. And don't you associate it with dangerous reptiles
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000254either! (If you need an icon, use an image of the 16-ton weight from
255the TV series or of a can of SPAM :-)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000256
2571.3. Q. How do I obtain a copy of the Python source?
258
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000259A. The latest complete Python source distribution is always available
260by anonymous ftp, e.g.
Guido van Rossum7be34a81995-05-31 15:17:12 +0000261<URL:ftp://ftp.python.org/pub/python/src/python1.2.tar.gz>. It is a
Guido van Rossume530c581995-04-10 12:32:16 +0000262gzipped tar file containing the complete C source, LaTeX
263documentation, Python library modules, example programs, and several
264useful pieces of freely distributable software. This will compile and
265run out of the box on most UNIX platforms. (See section 7 for
266non-UNIX information.)
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000267
268Occasionally a set of patches is issued which has to be applied using
269the patch program. These patches are placed in the same directory,
Guido van Rossum7be34a81995-05-31 15:17:12 +0000270e.g. <URL:ftp://ftp.python.org/pub/python/src/patch1.1.1>. (At the time
Guido van Rossume530c581995-04-10 12:32:16 +0000271of writing, no patches exist for 1.2.)
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000272
273An index of said ftp directory can be found in the file INDEX. An
274HTML version of the index can be found in the file index.html,
Guido van Rossum7be34a81995-05-31 15:17:12 +0000275<URL:ftp://ftp.python.org/pub/python/index.html>.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000276
2771.4. Q. How do I get documentation on Python?
278
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000279A. The LaTeX source for the documentation is part of the source
280distribution. If you don't have LaTeX, the latest Python
281documentation set is always available by anonymous ftp, e.g.
Guido van Rossum7be34a81995-05-31 15:17:12 +0000282<URL:ftp://ftp.python.org/pub/python/doc/postscript.tar.gz>. It is a
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000283gzipped tar file containing PostScript files of the reference manual,
Guido van Rossum3de27361994-07-25 14:19:33 +0000284the library manual, and the tutorial. Note that the library manual is
285the most important one of the set, as much of Python's power stems
286from the standard or built-in types, functions and modules, all of
287which are described here. PostScript for a high-level description of
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000288Python is in the file nluug-paper.ps (a separate file on the ftp
289site).
Guido van Rossuma7925f11994-01-26 10:20:16 +0000290
Guido van Rossumc50158e1994-05-31 09:18:50 +00002911.5. Q. Are there other ftp sites that mirror the Python distribution?
292
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000293A. The following anonymous ftp sites keep mirrors of the Python
294distribution:
Guido van Rossuma7925f11994-01-26 10:20:16 +0000295
Guido van Rossum7be34a81995-05-31 15:17:12 +0000296 <URL:ftp://ftp.cwi.nl/pub/python/>
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000297 <URL:ftp://gatekeeper.dec.com/pub/plan/python/>
298 <URL:ftp://ftp.uu.net/languages/python/>
299 <URL:ftp://ftp.wustl.edu/graphics/graphics/sgi-stuff/python/>
300 <URL:ftp://ftp.funet.fi/pub/languages/python/>
301 <URL:ftp://ftp.sunet.se/pub/lang/python/>
302 <URL:ftp://unix.hensa.ac.uk/uunet/languages/python/>
303 <URL:ftp://ftp.sterlng.com/programming/languages/python/>
304 <URL:ftp://ftp.ibp.fr/pub/python/>
Guido van Rossuma7925f11994-01-26 10:20:16 +0000305
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000306Or try archie on the string "python".
Guido van Rossuma7925f11994-01-26 10:20:16 +0000307
Guido van Rossumc50158e1994-05-31 09:18:50 +00003081.6. Q. Is there a newsgroup or mailing list devoted to Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000309
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000310A. There is a newsgroup, comp.lang.python <URL:news:comp.lang.python>,
311and a mailing list. The newsgroup and mailing list are gatewayed into
312each other -- if you can read news it's unnecessary to subscribe to
313the mailing list. Send e-mail to <python-list-request@cwi.nl> to
Guido van Rossume530c581995-04-10 12:32:16 +0000314(un)subscribe to the mailing list. Hypermail archives of (nearly)
315everything posted to the mailing list (and thus the newsgroup) are
316available on our WWW server,
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000317<URL:http://www.cwi.nl/~guido/hypermail/index.html>. The raw archives
318are also available by ftp, e.g.
Guido van Rossum7be34a81995-05-31 15:17:12 +0000319<URL:ftp://ftp.python.org/pub/python/mail/mailinglist.gz>. The
Guido van Rossume530c581995-04-10 12:32:16 +0000320uncompressed versions of these files can be read with the standard
321UNIX Mail program ("Mail -f file") or with nn ("nn file"). To read
322them using MH, you could use "inc -file file".
Guido van Rossuma7925f11994-01-26 10:20:16 +0000323
Guido van Rossuma6c707c1995-01-02 17:32:28 +00003241.7. Q. Is there a WWW page devoted to Python?
325
Guido van Rossum7be34a81995-05-31 15:17:12 +0000326A. Yes, <URL:http://www.python.org/> is the official Python home page.
327At the time of writing, this page is not yet completely operational;
328you may have a look at the old Python home page:
329<URL:http://www.cwi.nl/~guido/Python.html> or at the U.S. copy:
330<URL:http://www.python.org/~guido/Python.html>.
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000331
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00003321.8. Q. Is the Python documentation available on the WWW?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000333
Guido van Rossum7be34a81995-05-31 15:17:12 +0000334A. Yes, see <URL:http://www.python.org/> (Python's home page). It
335contains pointers to hypertext versions of the whole documentation set
336(as hypertext, not just PostScript).
Guido van Rossuma7925f11994-01-26 10:20:16 +0000337
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000338If you wish to browse this collection of HTML files on your own
339machine, it is available bundled up by anonymous ftp,
Guido van Rossum7be34a81995-05-31 15:17:12 +0000340e.g. <URL:ftp://ftp.python.org/pub/python/doc/html.tar.gz>.
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000341
342An Emacs-INFO set containing the library manual is also available by
Guido van Rossum7be34a81995-05-31 15:17:12 +0000343ftp, e.g. <URL:ftp://ftp.python.org/pub/python/doc/lib-info.tar.gz>.
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000344
3451.9. Q. Is there a book on Python, or will there be one out soon?
346
347A. I am writing one. Publishers are interested. Optimistically, it
348will be ready for the press by mid-1995. Mark Lutz is working on one
349too -- more details as they emerge.
350
3511.10. Q. Are there any published articles about Python that I can quote?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000352
353A. So far the only refereed and published article that describes
354Python in some detail is:
355
356 Guido van Rossum and Jelke de Boer, "Interactively Testing Remote
357 Servers Using the Python Programming Language", CWI Quarterly, Volume
358 4, Issue 4 (December 1991), Amsterdam, pp 283-303.
359
360LaTeX source for this paper is available as part of the Python source
361distribution.
362
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00003631.11. Q. Are there short introductory papers or talks on Python?
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000364
365A. A recent high-level description of Python is:
Guido van Rossuma7925f11994-01-26 10:20:16 +0000366
367 Guido van Rossum, "An Introduction to Python for UNIX/C
368 Programmers", in the proceedings of the NLUUG najaarsconferentie
Guido van Rossum061f1821994-10-06 16:03:45 +0000369 1993 (dutch UNIX users group meeting November 1993).
Guido van Rossuma7925f11994-01-26 10:20:16 +0000370
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000371PostScript for this paper and for the slides used for the accompanying
372presentation is available by ftp as
Guido van Rossum7be34a81995-05-31 15:17:12 +0000373<URL:ftp://ftp.python.org/pub/python/doc/nluug-paper.ps> and
374<URL:ftp://ftp.python.org/pub/python/doc/nluug-slides.ps>, respectively.
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000375
376Slides for a talk on Python that I gave at the Usenix Symposium on
377Very High Level Languages in Santa Fe, NM, USA in October 1995 are
Guido van Rossum7be34a81995-05-31 15:17:12 +0000378available as <URL:ftp://ftp.python.org/pub/python/doc/vhll-slides.ps>.
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000379
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00003801.12. Q. How does the Python version numbering scheme work?
Guido van Rossum95f61a71994-01-26 17:23:37 +0000381
382A. Python versions are numbered A.B.C. A is the major version number
383-- it is only incremented for major changes in functionality or source
384structure. B is the minor version number, incremented for less
385earth-shattering changes to a release. C is the patchlevel -- it is
386incremented for each new release. Note that in the past, patches have
387added significant changes; in fact the changeover from 0.9.9 to 1.0.0
388was the first time that either A or B changed!
389
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000390Beta versions have an additional suffix of "-beta-N" for some small
391number N. Note that (for instance) all versions labeled 1.2-beta-N
392*precede* the actual release of 1.2.
393
3941.13. Q. How do I get a beta test version of Python?
395
396A. Write me. I might like you :-) Seriously, anybody who has
397previously used and installed Python can become a beta tester, but I
398expect feedback, so you have to write me first before I divulge the
399secret location of the latest beta release.
400
4011.14. Q. Are there other ftp sites that carry Python related material?
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000402
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000403A. An interesting ftp site for Python users is ftp.markv.com; the
404directory pub/python contains a growing collection of interesting
405Python scripts <URL:ftp://ftp.markv.com/pub/python/>. To submit a
406script for inclusion, place it together with a readme file (with
407extension .readme) in the publicly writable directory
408/incoming/python. This service is maintained by Lance Ellinghaus
409<lance@markv.com>. (I've heard complaints about this service not
410being very responsive -- try at your own risk.)
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000411
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00004121.15. Q. Are there copyright restrictions on the use of Python?
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000413
414A. Hardly. You can do anything you want with the source, as long as
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000415you leave the copyrights in, and display those copyrights in any
416documentation about Python that you produce. Also, don't use the
417author's institute's name in publicity without prior written
418permission, and don't hold them responsible for anything (read the
419actual copyright for a precise legal wording).
420
421In particular, if you honor the copyright rules, it's OK to use Python
422for commercial use, to sell copies of Python in source or binary form,
423or to sell products that enhance Python or incorporate Python (or part
424of it) in some form. I would still like to know about all commercial
425use of Python!
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000426
Guido van Rossum7be34a81995-05-31 15:17:12 +00004271.16. Q. Why was Python created in the first place?
428
429A. Here's a *very* brief summary of what got me started:
430
431- I had extensive experience with implementing an interpreted language
432in the ABC group at CWI, and from working with this group I had
433learned a lot about language design. This is the origin of many
434Python features, including the use of indentation for statement
435grouping and the inclusion of very-high-level data types (although the
436details are all different in Python).
437
438- I had a number of gripes about the ABC language, but also liked many
439of its features. It was impossible to extend the ABC language (or its
440implementation) to remedy my complaints -- in fact its lack of
441extensibility was one of its biggest problems.
442
443- I had some experience with using Modula-2+ and talked with the
444designers of Modula-3 (and read the M3 report). M3 is the origin of
445the syntax and semantics used for exceptions, and some other Python
446features.
447
448- I was working in the Amoeba distributed operating system group at
449CWI. We needed a better way to do system administration than by
450writing either C programs or Bourne shell scripts, since Amoeba had
451its own system call interface which wasn't easily accessible from the
452Bourne shell. My experience with error handling
453
454- It occurred to me that a scripting language with a syntax like ABC
455but with access to the Amoeba system calls would fill the need. I
456realized that it would be foolish to write an Amoeba-specific
457language, so I decided that I needed a language that was generally
458extensible.
459
460- During the 1989 Christmas holidays, I had a lot of time on my hand,
461so I decided to give it a try. During the next year, while still
462mostly working on it in my own time, Python was used in the Amoeba
463project with increasing success, and the feedback from colleagues made
464me add many early improvements.
465
466- In February 1991, after just over a year of development, I decided
467to post to USENET. The rest is in the Misc/HISTORY file.
468
4691.17. Q. What happened to Tim Peters (the author of python-mode.el, and many
470entertaining and enlightening pieces of Python email)?
471
472A. He worked at KSR (Kendall Square Research, a start-up building a
473new kind of massively parallel processor). When KSR folded down a
474couple of years ago, Tim lost his email access. He hasn't surfaced
475on the net since then.
476
477 Missing-him-too-ly yours...
478
Guido van Rossuma7925f11994-01-26 10:20:16 +0000479
4802. Python in the real world
481===========================
482
4832.1. Q. How many people are using Python?
484
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000485A. I don't know, but the maximum number of simultaneous subscriptions
486to the Python mailing list before it was gatewayed into the newsgroup
487was about 180 (several of which were local redistribution lists). I
488believe that many active Python users don't bother to subscribe to the
489list, and now that there's a newsgroup the mailing list subscription
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000490is even less meaningful. I see new names on the newsgroup all the
491time and my best guess is that there are currently at least several
492thousands of users.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000493
4942.2. Q. Have any significant projects been done in Python?
495
496A. Here at CWI (the home of Python), we have written a 20,000 line
497authoring environment for transportable hypermedia presentations, a
Guido van Rossum5333c5d1994-04-11 11:06:22 +00004985,000 line multimedia teleconferencing tool, as well as many many
499smaller programs.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000500
501The University of Virginia uses Python to control a virtual reality
502engine. Contact: Matt Conway <conway@virginia.edu>.
503
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000504The ILU project at Xerox PARC can generate Python glue for ILU
505interfaces. See <URL:ftp://ftp.parc.xerox.com/pub/ilu/ilu.html>.
506
Guido van Rossumac3f2121995-04-10 11:53:42 +0000507The University of California, Irvine uses a student administration
508system called TELE-Vision written entirely in Python. Contact: Ray
509Price <rlprice@uci.edu>.
510
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000511If you have done a significant project in Python that you'd like to be
512included in the list above, send me email!
Guido van Rossuma7925f11994-01-26 10:20:16 +0000513
5142.3. Q. Are there any commercial projects going on using Python?
515
516A. Several companies have revealed to me that they are planning or
Guido van Rossum796b2591995-01-20 23:05:52 +0000517considering use of Python in a future product.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000518
Guido van Rossumac3f2121995-04-10 11:53:42 +0000519Sunrise Software, have a product out using Python -- they use Python
520for a GUI management application and an SNMP network management
521application. Contact: <info@sunrise.com>.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000522
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000523Infoseek uses Python to implement their commercial WWW information
524retrieval service <URL:http://www.infoseek.com/>. Contact:
525<info@infoseek.com>.
Guido van Rossumf8c76d01994-08-17 12:19:53 +0000526
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000527Michael Powers of daVinci Time & Space is "writing tons-o-python for
528interactive television entertainment." Contact: <powers@dvts.com>.
529
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000530Paul Everitt of Connecting Minds is planning a Lotus Notes gateway.
531Contact: <Paul.Everitt@cminds.com>. Or see their WWW server
532<URL:http://www.cminds.com/>.
533
Guido van Rossumac3f2121995-04-10 11:53:42 +0000534KaPRE in Boulder, CO is using Python for on-site customization of C++
535applications, rapid-prototyping/development,
536language-based-components, and possibly more. This is pretty solid:
537Python's being shipped with their tool-set now, to beta sites.
538Contact: <lutz@KaPRE.COM> (Mark Lutz).
539
540Individuals at many other companies are using Python for internal
541development or for as yet unannounced products (witness their
542contributions to the Python mailing list or newsgroup).
Guido van Rossuma7925f11994-01-26 10:20:16 +0000543
544Python has also been elected as an extension language by MADE, a
545consortium supported by the European Committee's ESPRIT program and
546consisting of Bull, CWI and some other European companies. Contact:
547Ivan Herman <ivan@cwi.nl>.
548
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000549If you'd like to be included in the list above, send me email!
550
Guido van Rossum95f61a71994-01-26 17:23:37 +00005512.4. Q. How stable is Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000552
Guido van Rossum3de27361994-07-25 14:19:33 +0000553A. Very stable. While the current version number would suggest it is
554in the early stages of development, in fact new, stable releases
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000555(numbered 0.9.x through 1.1.x) have been coming out roughly every 3 to
5566 months for the past four years.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000557
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00005582.5. Q. When will the next version be released?
559
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000560A. I am planning to release 1.2 in February or early March 1995. It
561will contain hooks into the implementation of the import command, a
562(still limited) form of persistent objects, and the usual complement
563of bug fixes (including many fixed memory leaks and thread problems).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000564
5652.6. Q. What new developments are expected for Python in the future?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000566
Guido van Rossum7be34a81995-05-31 15:17:12 +0000567A. See my Work-In-Progress web page, currently at
568<URL:http://www.python.org:~guido/WIP.html>, and the pages for the
569Second Python Workshop (best reached via the Python home page,
570<URL:http://www.python.org/>).
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000571
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00005722.7. Q. Is it reasonable to propose incompatible changes to Python?
Guido van Rossum3de27361994-07-25 14:19:33 +0000573
574A. In general, no. There are already millions of lines of Python code
575around the world, so any changes in the language that invalidates more
576than a very small fraction of existing programs has to be frowned
577upon. Even if you can provide a conversion program, there still is
578the problem of updating all documentation. Providing a gradual
579upgrade path is the only way if a feature has to be changed.
580
Guido van Rossuma7925f11994-01-26 10:20:16 +0000581
5823. Building Python
583==================
584
Guido van Rossum91f60831994-02-15 15:52:27 +00005853.1. Q. Is there a test set?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000586
587A. Yes, simply do "import testall" (or "import autotest" if you aren't
588interested in the output). The standard modules whose name begins
589with "test" together comprise the test. The test set doesn't test
590*all* features of Python but it goes a long way to confirm that a new
591port is actually working. The Makefile contains an entry "make test"
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000592which runs the autotest module. NOTE: if "make test" fails, run the
593tests manually ("import testall") to see what goes wrong before
594reporting the error.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000595
Guido van Rossum91f60831994-02-15 15:52:27 +00005963.2. Q. When running the test set, I get complaints about floating point
Guido van Rossuma7925f11994-01-26 10:20:16 +0000597operations, but when playing with floating point operations I cannot
598find anything wrong with them.
599
600A. The test set makes occasional unwarranted assumptions about the
601semantics of C floating point operations. Until someone donates a
602better floating point test set, you will have to comment out the
603offending floating point tests and execute similar tests manually.
604
Guido van Rossum7be34a81995-05-31 15:17:12 +00006053.3. Q. Link errors after rerunning the configure script.
Guido van Rossum24349991994-02-02 14:12:45 +0000606
607A. It is generally necessary to run "make clean" after a configuration
608change.
609
Guido van Rossum7be34a81995-05-31 15:17:12 +00006103.4. Q. The python interpreter complains about options passed to a
Guido van Rossum24349991994-02-02 14:12:45 +0000611script (after the script name).
612
613A. You are probably linking with GNU getopt, e.g. through -liberty.
Guido van Rossumf8c76d01994-08-17 12:19:53 +0000614Don't. The reason for the complaint is that GNU getopt, unlike System
615V getopt and other getopt implementations, doesn't consider a
616non-option to be the end of the option list. A quick (and compatible)
617fix for scripts is to add "--" to the interpreter, like this:
618
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000619 #! /usr/local/bin/python --
Guido van Rossumf8c76d01994-08-17 12:19:53 +0000620
621You can also use this interactively:
622
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000623 python -- script.py [options]
624
625Note that a working getopt implementation is provided in the Python
626distribution (in Python/getopt.c) but not automatically used.
Guido van Rossum24349991994-02-02 14:12:45 +0000627
Guido van Rossum7be34a81995-05-31 15:17:12 +00006283.5. Q. When building on the SGI, make tries to run python to create
Guido van Rossum24349991994-02-02 14:12:45 +0000629glmodule.c, but python hasn't been built or installed yet.
630
631A. Comment out the line mentioning glmodule.c in Setup and build a
632python without gl first; install it or make sure it is in your $PATH,
633then edit the Setup file again to turn on the gl module, and make
634again. You don't need to do "make clean"; you do need to run "make
635Makefile" in the Modules subdirectory (or just run "make" at the
636toplevel).
637
Guido van Rossum7be34a81995-05-31 15:17:12 +00006383.6. Q. Python built with gcc for the DEC Alpha doesn't work.
Guido van Rossum3de27361994-07-25 14:19:33 +0000639
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000640People have reported problems with gcc 2.5.8 up to 2.6.3. The DEC
641OSF/1 cc compiler does not have these problems so it's likely a gcc
Guido van Rossume530c581995-04-10 12:32:16 +0000642bug. The latest news is that this has been fixed in Python 1.2 by a
643source change (I gave up waiting for a fixed gcc).
Guido van Rossum3de27361994-07-25 14:19:33 +0000644
Guido van Rossum7be34a81995-05-31 15:17:12 +00006453.7. Q. I use VPATH but some targets are built in the source directory.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000646
647A. On some systems (e.g. Sun), if the target already exists in the
648source directory, it is created there instead of in the build
649directory. This is usually because you have previously built without
650VPATH. Try running "make clobber" in the source directory.
651
Guido van Rossum7be34a81995-05-31 15:17:12 +00006523.8. Q. Trouble building or linking with the GNU readline library.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000653
Guido van Rossumac3f2121995-04-10 11:53:42 +0000654A. Consider using readline 2.0. Some hints:
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000655
656- You can use the GNU readline library to improve the interactive user
657interface: this gives you line editing and command history when
Guido van Rossum796b2591995-01-20 23:05:52 +0000658calling python interactively. You need to configure and build the GNU
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000659readline library before running the configure script. Its sources are
660no longer distributed with Python; you can ftp them from any GNU
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000661mirror site, or from its home site
662<URL:ftp://slc2.ins.cwru.edu/pub/dist/readline-2.0.tar.gz> (or a
663higher version number -- using version 1.x is not recommended). Pass
664the Python configure script the option --with-readline=DIRECTORY where
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000665DIRECTORY is the absolute pathname of the directory where you've built
666the readline library. Some hints on building and using the readline
667library:
668
669- On SGI IRIX 5, you may have to add the following
670to rldefs.h:
671
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000672 #ifndef sigmask
673 #define sigmask(sig) (1L << ((sig)-1))
674 #endif
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000675
676- On most systems, you will have to add #include "rldefs.h" to the
677top of several source files, and if you use the VPATH feature, you
678will have to add dependencies of the form foo.o: foo.c to the
679Makefile for several values of foo.
680
681- The readline library requires use of the termcap library. A
682known problem with this is that it contains entry points which
Guido van Rossum061f1821994-10-06 16:03:45 +0000683cause conflicts with the STDWIN and SGI GL libraries. The STDWIN
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000684conflict can be solved (and will be, in the next release of
Guido van Rossum061f1821994-10-06 16:03:45 +0000685STDWIN) by adding a line saying '#define werase w_erase' to the
686stdwin.h file (in the STDWIN distribution, subdirectory H). The
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000687GL conflict has been solved in the Python configure script by a
688hack that forces use of the static version of the termcap library.
689
Guido van Rossumac3f2121995-04-10 11:53:42 +0000690- Check the newsgroup gnu.bash.bug <URL:news:gnu.bash.bug> for
691specific problems with the readline library (I don't read this group
692but I've been told that it is the place for readline bugs).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000693
Guido van Rossum7be34a81995-05-31 15:17:12 +00006943.9. Q. Trouble building Python on Linux.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000695
Guido van Rossumac3f2121995-04-10 11:53:42 +0000696A. If you're building Python 1.2, Slackware 2.2 has a buggy bash
697(version 1.14.3) which breaks a sed script that is used to build
698Modules/Makefile. Replace /bin/sh with /bin/ash in both makesetup and
699Makefile.pre.in.
700
701In 1.1 and 1.1.1, there's a bug in the reference counting logic of
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000702ternary pow() which is only tripped by very picky mallocs, like the
703GNU malloc on Linux. This has been fixed in 1.2. To continue the
Guido van Rossumac3f2121995-04-10 11:53:42 +0000704tests in 1.1(.1), just disable the tests of pow() with three arguments
705from Lib/test/test_b2.py.
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000706
Guido van Rossumac3f2121995-04-10 11:53:42 +0000707Apart from this, Python builds and runs fine on most Linux versions
708(if you run into trouble on an old Linux version, consider upgrading).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000709
Guido van Rossum7be34a81995-05-31 15:17:12 +00007103.10. Q. Trouble with prototypes on Ultrix.
Guido van Rossum72eb83c1994-10-07 11:33:28 +0000711
712A. Ultrix cc seems broken -- use gcc, or edit config.h to #undef
713HAVE_PROTOTYPES.
714
Guido van Rossum7be34a81995-05-31 15:17:12 +00007153.11. Q. Trouble with posix.listdir on NeXTSTEP 3.2.
Guido van Rossum9351fdb1994-11-10 23:03:51 +0000716
Guido van Rossumac3f2121995-04-10 11:53:42 +0000717A. (This is fixed in 1.2.) You need to pass "-posix" to the C
718compiler, especially in the link phase -- it uses the correct version
719of the C library. The easiest way is perhaps to do "make OPT=-posix".
720You also need to remove this NeXT-specific section from import.c:
Guido van Rossum9351fdb1994-11-10 23:03:51 +0000721
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000722 #if defined(NeXT) || defined(WITH_RLD)
723 #define DYNAMIC_LINK
724 #define USE_RLD
725 #endif
Guido van Rossum9351fdb1994-11-10 23:03:51 +0000726
Guido van Rossum7be34a81995-05-31 15:17:12 +00007273.12. Q. Other trouble building Python on platform X.
Guido van Rossum95f61a71994-01-26 17:23:37 +0000728
Guido van Rossum796b2591995-01-20 23:05:52 +0000729A. Please email the details to <guido@cwi.nl> and I'll look into it.
730Please provide as many details as possible. In particular, if you
731don't tell me what type of computer and what operating system (and
732version) you are using it will be difficult for me to figure out what
733is the matter. If you get a specific error message, please email it
734to me too.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000735
Guido van Rossum7be34a81995-05-31 15:17:12 +00007363.13. Q. How to configure dynamic loading on Lixux.
Guido van Rossum07779351995-02-07 16:59:56 +0000737
738A. (Thanks to Andrew Kuchling):
739
740Linux requires that you use the GNU DLD library. The stages of using
741dynamic libraries on Linux are:
742
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00007431) Get dld 3.2.5 from a Linux site. Be careful here; the most recent
744GNU version is 3.2.3, and doesn't support Linux; be sure to get it
745from a Linux mirror, not a GNU mirror (3.2.4 should also work).
746Compile it and install the library libdld.a somewhere; I used
747/usr/local/lib.
748
749Suitable URLs for the dld distribution are currently:
750<URL:ftp://sunsite.unc.edu/pub/Linux/libs/dld-3.2.5.src.tar.gz> and
751<URL:ftp://tsx-11.mit.edu/pub/linux/sources/libs/dld-3.2.5.src.tar.gz>.
752There's also a binary distribution of it:
753<URL:ftp://sunsite.unc.edu/pub/Linux/libs/dld-3.2.5.bin.tar.gz>.
Guido van Rossum07779351995-02-07 16:59:56 +0000754
7552) Get Jack Jansen's DL library; its location is given in the
Guido van Rossum7be34a81995-05-31 15:17:12 +0000756_Extending Python_ manual as <URL:ftp://ftp.cwi.nl/pub/dynload/>.
757Compile it and install libdl.a wherever you put libdld.a.
Guido van Rossum07779351995-02-07 16:59:56 +0000758
7593) Run Python's configure script, giving it the --with-dl-dld option,
760which requires a parameter giving the directory where you put the
761libraries.
762
7634) Recompile Python.
764
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000765Note that once ELF is common in the Linux world, it will be much
766easier (probably within a year) -- the standard SVR4-style dlopen()
767interface is then available. This has already proved to work
768perfectly using a beta version of the ELF-capable GCC for Linux.
Guido van Rossum07779351995-02-07 16:59:56 +0000769
Guido van Rossuma7925f11994-01-26 10:20:16 +0000770
7714. Programming in Python
772========================
773
Guido van Rossum24349991994-02-02 14:12:45 +00007744.1. Q. Is there a source code level debugger with breakpoints, step,
775etc.?
776
777A. Yes. Check out module pdb; pdb.help() prints the documentation (or
778you can read it as Lib/pdb.doc). If you use the STDWIN option,
779there's also a windowing interface, wdb. You can write your own
780debugger by using the code for pdb or wdb as an example.
781
7824.2. Q. Can I create an object class with some methods implemented in
Guido van Rossuma7925f11994-01-26 10:20:16 +0000783C and others in Python (e.g. through inheritance)? (Also phrased as:
784Can I use a built-in type as base class?)
785
786A. No, but you can easily create a Python class which serves as a
787wrapper around a built-in object, e.g. (for dictionaries):
788
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000789 # A user-defined class behaving almost identical
790 # to a built-in dictionary.
791 class UserDict:
792 def __init__(self): self.data = {}
793 def __repr__(self): return repr(self.data)
794 def __cmp__(self, dict):
795 if type(dict) == type(self.data):
796 return cmp(self.data, dict)
797 else:
798 return cmp(self.data, dict.data)
799 def __len__(self): return len(self.data)
800 def __getitem__(self, key): return self.data[key]
801 def __setitem__(self, key, item): self.data[key] = item
802 def __delitem__(self, key): del self.data[key]
803 def keys(self): return self.data.keys()
804 def items(self): return self.data.items()
805 def values(self): return self.data.values()
806 def has_key(self, key): return self.data.has_key(key)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000807
Guido van Rossum24349991994-02-02 14:12:45 +00008084.3. Q. Is there a curses/termcap package for Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000809
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000810A. Yes -- Lance Ellinghaus has written a module that interfaces to
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000811System V's "ncurses". If you know a little curses and some Python,
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000812it's straightforward to use. It is part of the standard Python
813distribution, but not configured by default -- you must enable it by
814editing Modules/Setup. It requires a System V curses implementation.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000815
816You could also consider using the "alfa" (== character cell) version
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000817of STDWIN. (Standard Window System Interface, a portable windowing
818system interface by myself <URL:ftp://ftp.cwi.nl/pub/stdwin/>.) This
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000819will also prepare your program for porting to windowing environments
820such as X11 or the Macintosh.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000821
Guido van Rossum24349991994-02-02 14:12:45 +00008224.4. Q. Is there an equivalent to C's onexit() in Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000823
824A. Yes, if you import sys and assign a function to sys.exitfunc, it
825will be called when your program exits, is killed by an unhandled
826exception, or (on UNIX) receives a SIGHUP or SIGTERM signal.
827
Guido van Rossum24349991994-02-02 14:12:45 +00008284.5. Q. When I define a function nested inside another function, the
Guido van Rossuma7925f11994-01-26 10:20:16 +0000829nested function seemingly can't access the local variables of the
830outer function. What is going on? How do I pass local data to a
831nested function?
832
833A. Python does not have arbitrarily nested scopes. When you need to
834create a function that needs to access some data which you have
835available locally, create a new class to hold the data and return a
836method of an instance of that class, e.g.:
837
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000838 class MultiplierClass:
839 def __init__(self, factor):
840 self.factor = factor
841 def multiplier(self, argument):
842 return argument * self.factor
Guido van Rossuma7925f11994-01-26 10:20:16 +0000843
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000844 def generate_multiplier(factor):
845 return MultiplierClass(factor).multiplier
Guido van Rossuma7925f11994-01-26 10:20:16 +0000846
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000847 twice = generate_multiplier(2)
848 print twice(10)
849 # Output: 20
850
851An alternative solution uses default arguments, e.g.:
852
853 def generate_multiplier(factor):
854 def multiplier(arg, fact = factor):
855 return arg*fact
856 return multiplier
857
858 twice = generate_multiplier(2)
859 print twice(10)
860 # Output: 20
Guido van Rossuma7925f11994-01-26 10:20:16 +0000861
Guido van Rossum24349991994-02-02 14:12:45 +00008624.6. Q. How do I iterate over a sequence in reverse order?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000863
864A. If it is a list, the fastest solution is
865
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000866 list.reverse()
867 try:
868 for x in list:
869 "do something with x"
870 finally:
871 list.reverse()
Guido van Rossuma7925f11994-01-26 10:20:16 +0000872
873This has the disadvantage that while you are in the loop, the list
874is temporarily reversed. If you don't like this, you can make a copy.
875This appears expensive but is actually faster than other solutions:
876
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000877 rev = list[:]
878 rev.reverse()
879 for x in rev:
880 <do something with x>
Guido van Rossuma7925f11994-01-26 10:20:16 +0000881
882If it isn't a list, a more general but slower solution is:
883
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000884 i = len(list)
885 while i > 0:
886 i = i-1
887 x = list[i]
888 <do something with x>
Guido van Rossuma7925f11994-01-26 10:20:16 +0000889
890A more elegant solution, is to define a class which acts as a sequence
891and yields the elements in reverse order (solution due to Steve
892Majewski):
893
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000894 class Rev:
895 def __init__(self, seq):
896 self.forw = seq
897 def __len__(self):
898 return len(self.forw)
899 def __getitem__(self, i):
900 return self.forw[-(i + 1)]
Guido van Rossuma7925f11994-01-26 10:20:16 +0000901
902You can now simply write:
903
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000904 for x in Rev(list):
905 <do something with x>
Guido van Rossuma7925f11994-01-26 10:20:16 +0000906
Guido van Rossum061f1821994-10-06 16:03:45 +0000907Unfortunately, this solution is slowest of all, due to the method
Guido van Rossuma7925f11994-01-26 10:20:16 +0000908call overhead...
909
Guido van Rossum24349991994-02-02 14:12:45 +00009104.7. Q. My program is too slow. How do I speed it up?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000911
912A. That's a tough one, in general. There are many tricks to speed up
913Python code; I would consider rewriting parts in C only as a last
914resort. One thing to notice is that function and (especially) method
915calls are rather expensive; if you have designed a purely OO interface
916with lots of tiny functions that don't do much more than get or set an
917instance variable or call another method, you may consider using a
918more direct way, e.g. directly accessing instance variables. Also see
919the standard module "profile" (described in the file
920"python/lib/profile.doc") which makes it possible to find out where
921your program is spending most of its time (if you have some patience
922-- the profiling itself can slow your program down by an order of
923magnitude).
924
Guido van Rossum24349991994-02-02 14:12:45 +00009254.8. Q. When I have imported a module, then edit it, and import it
Guido van Rossuma7925f11994-01-26 10:20:16 +0000926again (into the same Python process), the changes don't seem to take
927place. What is going on?
928
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000929A. For reasons of efficiency as well as consistency, Python only reads
Guido van Rossum796b2591995-01-20 23:05:52 +0000930the module file on the first time a module is imported. (Otherwise a
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000931program consisting of many modules, each of which imports the same
Guido van Rossum796b2591995-01-20 23:05:52 +0000932basic module, would read the basic module over and over again.) To
933force rereading of a changed module, do this:
Guido van Rossuma7925f11994-01-26 10:20:16 +0000934
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000935 import modname
936 reload(modname)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000937
938Warning: this technique is not 100% fool-proof. In particular,
939modules containing statements like
940
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000941 from modname import some_objects
Guido van Rossuma7925f11994-01-26 10:20:16 +0000942
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000943will continue to work with the old version of the imported objects.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000944
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00009454.9. Q. How do I find the current module name?
Guido van Rossum24349991994-02-02 14:12:45 +0000946
Guido van Rossum061f1821994-10-06 16:03:45 +0000947A. A module can find out its own module name by looking at the
Guido van Rossum24349991994-02-02 14:12:45 +0000948(predefined) global variable __name__. If this has the value
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000949'__main__' you are running as a script.
950
Guido van Rossuma6c707c1995-01-02 17:32:28 +00009514.10. Q. I have a module in which I want to execute some extra code
952when it is run as a script. How do I find out whether I am running as
953a script?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000954
955A. See the previous question. E.g. if you put the following on the
956last line of your module, main() is called only when your module is
957running as a script:
Guido van Rossum24349991994-02-02 14:12:45 +0000958
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000959 if __name__ == '__main__': main()
Guido van Rossum24349991994-02-02 14:12:45 +0000960
Guido van Rossuma6c707c1995-01-02 17:32:28 +00009614.11. Q. I try to run a program from the Demo directory but it fails
962with ImportError: No module named ...; what gives?
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000963
Guido van Rossum061f1821994-10-06 16:03:45 +0000964A. This is probably an optional module (written in C!) which hasn't
965been configured on your system. This especially happens with modules
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000966like "Tkinter", "stdwin", "gl", "Xt" or "Xm". For Tkinter, STDWIN and
967many other modules, see Modules/Setup.in for info on how to add these
968modules to your Python, if it is possible at all. Sometimes you will
969have to ftp and build another package first (e.g. STDWIN). Sometimes
970the module only works on specific platforms (e.g. gl only works on SGI
971machines).
972
973NOTE: if the complaint is about "Tkinter" (upper case T) and you have
974already configured module "tkinter" (lower case t), the solution is
975*not* to rename tkinter to Tkinter or vice versa. There is probably
Guido van Rossum796b2591995-01-20 23:05:52 +0000976something wrong with your module search path. Check out the value of
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000977sys.path.
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000978
Guido van Rossum061f1821994-10-06 16:03:45 +0000979For X-related modules (Xt and Xm) you will have to do more work: they
980are currently not part of the standard Python distribution. You will
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000981have to ftp the Extensions tar file, e.g.
Guido van Rossum7be34a81995-05-31 15:17:12 +0000982<URL:ftp://ftp.python.org/pub/python/src/extensions.tar.gz> and follow
983the instructions there.
Guido van Rossum061f1821994-10-06 16:03:45 +0000984
985See also the next question.
986
9874.12. Q. I have successfully built Python with STDWIN but it can't
988find some modules (e.g. stdwinevents).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000989
990A. There's a subdirectory of the library directory named 'stdwin'
991which should be in the default module search path. There's a line in
992Modules/Setup(.in) that you have to enable for this purpose --
Guido van Rossum061f1821994-10-06 16:03:45 +0000993unfortunately in the latest release it's not near the other
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000994STDWIN-related lines so it's easy to miss it.
995
9964.13. Q. What GUI toolkits exist for Python?
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000997
998A. Depending on what platform(s) you are aiming at, there are several.
999
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001000- There's a neat object-oriented interface to the Tcl/Tk widget set,
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001001called Tkinter. As of python 1.1, it is part of the standard Python
1002distribution -- all you need to do is enable it in Modules/Setup
1003(provided you have already installed Tk and Tcl). This is probably
1004the easiest to install and use, and the most complete widget set. It
1005is also very likely that in the future the standard Python GUI API
1006will be based on or at least look very much like the Tkinter
1007interface. For more info about Tk, including pointers to the source,
1008see John Ousterhout's home page
Guido van Rossum796b2591995-01-20 23:05:52 +00001009<URL:http://playground.sun.com/~ouster/>.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001010
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001011- The standard Python distribution comes with an interface to STDWIN,
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001012a platform-independent low-level windowing interface. You have to ftp
1013the source for STDWIN separately,
1014e.g. <URL:ftp://ftp.cwi.nl/pub/stdwin/> or gatekeeper.dec.com in
1015pub/misc/stdwin <URL:ftp://gatekeeper.dec.com/pub/misc/stdwin/>.
1016STDWIN runs under X11 or the Mac; a Windows port has been attempted
1017but I can't seem to get it working. Note that STDWIN is really not
1018powerful enough to implement a modern GUI (no widgets, etc.) and that
1019I don't have the time to maintain or extend it, so you may be better
1020off using Tkinter or the Motif interface, unless you require
1021portability to the Mac (which is also offered by SUIT, by the way --
1022see below).
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001023
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001024- For SGI IRIX only, there's an interface to the complete GL (Graphics
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001025Library -- low level but very good 3D capabilities) as well as to
1026FORMS (a buttons-and-sliders-etc package built on top of GL by Mark
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001027Overmars -- ftp'able from <URL:ftp://ftp.cs.ruu.nl/pub/SGI/FORMS/>).
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001028
1029- There's an interface to X11, including the Athena and Motif widget
1030sets (and a few individual widgets, like Mosaic's HTML widget and
1031SGI's GL widget) in the Extensions set, which is separately ftp'able
Guido van Rossum7be34a81995-05-31 15:17:12 +00001032<URL:ftp://ftp.python.org/pub/python/src/extensions.tar.gz>.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001033
1034- There's an interface to SUIT, the U of Virginia's Simple User
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001035Interface Toolkit; it can be ftp'ed from
1036<URL:ftp://uvacs.cs.virginia.edu/pub/suit/python/SUIT_python.tar.Z>.
1037A PC binary of Python 1.0.2 compiled with DJGPP and with SUIT support
1038built-in has been made available by Antonio Costa
1039<URL:ftp://asterix.inescn.pt/pub/PC/python/pyt102su.exe> (a
1040self-extracting archive). Note that the UVa people themselves have
1041expressed doubts about SUIT, and are planning to build a Python GUI
1042API based upon Tk (though not necessarily on Tkinter); see
1043<URL:http://server.cs.virginia.edu/~tnb2d/IT/IT.html>.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001044
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001045- There's an interface to WAFE, a Tcl interface to the X11 Motif and
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001046Athena widget sets. Last I heard about it it was included in the WAFE
10471.0 prerelease
1048<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 +00001049
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00001050- There's an interface to wxWindows. wxWindows is a portable GUI
1051class library written in C++. It supports XView, Motif, MS-Windows as
1052targets. There is some support for Macs and CURSES as well. wxWindows
1053preserves the look and feel of the underlying graphics toolkit. See
1054the wxPython WWW page at
1055<URL:http://www.aiai.ed.ac.uk/~jacs/wx/wxpython/wxpython.html>.
1056
1057- Python has been mentioned on the "Futurism" subpage of the Fresco
1058home page <URL:http://www.faslab.com/fresco/HomePage.html>. "Pesto"
1059is a Python interface to the CORBA dynamic invocation interface, and
1060thus Fresco. A Pesto prototype is running and is currently being
Guido van Rossum7be34a81995-05-31 15:17:12 +00001061packaged up for inclusion in the Fresco snapshot. See also the Pesto
1062web pages: <URL:http://www.faslab.com/fresco/pesto/Index.html>.
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00001063
Guido van Rossum061f1821994-10-06 16:03:45 +000010644.14. Q. Are there any interfaces to database packages in Python?
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001065
1066A. There's an interface to SYBASE by John Redford
1067<jredford@lehman.com>.
1068
Guido van Rossum796b2591995-01-20 23:05:52 +00001069There's an interface to metalbase by Lance Ellinghaus
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001070<lance@markv.com>; it is part of the separate Extensions distribution
Guido van Rossum7be34a81995-05-31 15:17:12 +00001071<URL:ftp://ftp.python.org/pub/python/src/extensions.tar.gz>.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001072
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001073Anthony Baxter <anthony.baxter@aaii.oz.au> has written an interface to
Guido van Rossume530c581995-04-10 12:32:16 +00001074mSQL (mini-SQL).
Guido van Rossum7be34a81995-05-31 15:17:12 +00001075<URL:ftp://ftp.python.org/pub/python/contrib/PymSQL.tar.gz>.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001076
Guido van Rossum7be34a81995-05-31 15:17:12 +00001077Tom Culliton <culliton@clark.net> has written an Oracle module.
1078See <URL:ftp://ftp.clark.net/pub/culliton/oramod.tgz>.
Guido van Rossum796b2591995-01-20 23:05:52 +00001079
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000010804.15. Q. Is it possible to write obfuscated one-liners in Python?
Guido van Rossumc24da7c1994-09-23 14:08:41 +00001081
1082A. Yes. See the following three examples, due to Ulf Bartelt:
1083
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001084 # Primes < 1000
1085 print filter(None,map(lambda y:y*reduce(lambda x,y:x*y!=0,
1086 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 +00001087
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001088 # First 10 Fibonacci numbers
1089 print map(lambda x,f=lambda x,f:(x<=1) or (f(x-1,f)+f(x-2,f)): f(x,f),
1090 range(10))
Guido van Rossumc24da7c1994-09-23 14:08:41 +00001091
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001092 # Mandelbrot set
1093 print (lambda Ru,Ro,Iu,Io,IM,Sx,Sy:reduce(lambda x,y:x+y,map(lambda y,
1094 Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,Sy=Sy,L=lambda yc,Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,i=IM,
1095 Sx=Sx,Sy=Sy:reduce(lambda x,y:x+y,map(lambda x,xc=Ru,yc=yc,Ru=Ru,Ro=Ro,
1096 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
1097 >=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(
1098 64+F(Ru+x*(Ro-Ru)/Sx,yc,0,0,i)),range(Sx))):L(Iu+y*(Io-Iu)/Sy),range(Sy
1099 ))))(-2.1, 0.7, -1.2, 1.2, 30, 80, 24)
1100 # \___ ___/ \___ ___/ | | |__ lines on screen
1101 # V V | |______ columns on screen
1102 # | | |__________ maximum of "iterations"
1103 # | |_________________ range on y axis
1104 # |____________________________ range on x axis
Guido van Rossumc24da7c1994-09-23 14:08:41 +00001105
1106Don't try this at home, kids!
1107
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000011084.16. Q. Is there an equivalent of C's "?:" ternary operator?
Guido van Rossumc24da7c1994-09-23 14:08:41 +00001109
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001110A. Not directly. In many cases you can mimic a?b:c with "a and b or
1111c", but there's a flaw: if b is zero (or empty, or None -- anything
1112that tests false) then c will be selected instead. In many cases you
1113can prove by looking at the code that this can't happen (e.g. because
1114b is a constant or has a type that can never be false), but in general
1115this can be a problem.
1116
1117Steve Majewski (or was it Tim Peters?) suggested the following
1118solution: (a and [b] or [c])[0]. Because [b] is a singleton list it
1119is never false, so the wrong path is never taken; then applying [0] to
1120the whole thing gets the b or c that you really wanted. Ugly, but it
1121gets you there in the rare cases where it is really inconvenient to
1122rewrite your code using 'if'.
1123
11244.17. Q. My class defines __del__ but it is not called when I delete the
1125object.
1126
1127A. There are several possible reasons for this.
1128
1129- The del statement does not necessarily call __del__ -- it simply
1130decrements the object's reference count, and if this reaches zero
1131__del__ is called.
1132
1133- If your data structures contain circular links (e.g. a tree where
1134each child has a parent pointer and each parent has a list of
1135children) the reference counts will never go back to zero. You'll
Guido van Rossum061f1821994-10-06 16:03:45 +00001136have to define an explicit close() method which removes those
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001137pointers. Please don't ever call __del__ directly -- __del__ should
Guido van Rossum061f1821994-10-06 16:03:45 +00001138call close() and close() should make sure that it can be called more
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001139than once for the same object.
1140
1141- If the object has ever been a local variable (or argument, which is
1142really the same thing) to a function that caught an expression in an
1143except clause, chances are that a reference to the object still exists
1144in that function's stack frame as contained in the stack trace.
1145Normally, deleting (better: assigning None to) sys.exc_traceback will
1146take care of this. If you a stack was printed for an unhandled
1147exception in an interactive interpreter, delete sys.last_traceback
1148instead.
1149
1150- There is code that deletes all objects when the interpreter exits,
1151but if your Python has been configured to support threads, it is not
1152called (because other threads may still be active). You can define
Guido van Rossum061f1821994-10-06 16:03:45 +00001153your own cleanup function using sys.exitfunc (see question 4.4).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001154
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001155- Finally, there are some obscure bugs if your __del__ method does
1156complicated things such as updating dictionaries or lists or
1157references globals. I hope to have fixed these in release 1.2.
1158
11594.18. Q. How do I change the shell environment for programs called
1160using os.popen() or os.system()? Changing os.environ doesn't work.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001161
1162A. Modifying the environment passed to subshells was left out of the
1163interpreter because there seemed to be no well-established portable
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001164way to do it (in particular, some systems, have putenv(), others have
1165setenv(), and some have none at all).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001166
1167However if all you want is to pass environment variables to the
1168commands run by os.system() or os.popen(), there's a simple solution:
1169prefix the command string with a couple of variable assignments and
Guido van Rossum796b2591995-01-20 23:05:52 +00001170export statements. The following would be universal for popen:
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001171
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001172 import os
1173 from commands import mkarg # nifty routine to add shell quoting
1174 def epopen(cmd, mode, env = {}):
1175 # env is a dictionary of environment variables
1176 prefix = ''
Guido van Rossum796b2591995-01-20 23:05:52 +00001177 for key, value in env.items():
1178 prefix = prefix + '%s=%s\n' % (key, mkarg(value)[1:])
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001179 prefix = prefix + 'export %s\n' % key
1180 return os.popen(prefix + cmd, mode)
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001181
11824.19. Q. What is a class?
1183
1184A. A class is the particular object type that is created by executing
Guido van Rossum796b2591995-01-20 23:05:52 +00001185a class statement. Class objects are used as templates, to create
1186class instance objects, which embody both the data structure and
1187program routines specific to a datatype.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001188
11894.20. Q. What is a method?
1190
1191A. A method is a function that you normally call as
Guido van Rossum796b2591995-01-20 23:05:52 +00001192x.name(arguments...) for some object x. The term is used for methods
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001193of classes and class instances as well as for methods of built-in
Guido van Rossum796b2591995-01-20 23:05:52 +00001194objects. (The latter have a completely different implementation and
1195only share the way their calls look in Python code.) Methods of
1196classes (and class instances) are defined as functions inside the
1197class definition.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001198
11994.21. Q. What is self?
1200
1201A. Self is merely a conventional name for the first argument of a
1202method -- i.e. a function defined inside a class definition. A method
1203defined as meth(self, a, b, c) should be called as x.meth(a, b, c) for
1204some instance x of the class in which the definition occurs;
1205the called method will think it is called as meth(x, a, b, c).
1206
12074.22. Q. What is a unbound method?
1208
1209A. An unbound method is a method defined in a class that is not yet
1210bound to an instance. You get an unbound method if you ask for a
1211class attribute that happens to be a function. You get a bound method
1212if you ask for an instance attribute. A bound method knows which
Guido van Rossum061f1821994-10-06 16:03:45 +00001213instance it belongs to and calling it supplies the instance automatically;
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001214an unbound method only knows which class it wants for its first
1215argument (a derived class is also OK). Calling an unbound method
1216doesn't "magically" derive the first argument from the context -- you
1217have to provide it explicitly.
1218
Guido van Rossuma6c707c1995-01-02 17:32:28 +000012194.23. Q. How do I call a method defined in a base class from a derived
1220class that overrides it?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001221
1222A. If your class definition starts with "class Derived(Base): ..."
1223then you can call method meth defined in Base (or one of Base's base
1224classes) as Base.meth(self, arguments...). Here, Base.meth is an
1225unbound method (see previous question).
1226
Guido van Rossuma6c707c1995-01-02 17:32:28 +000012274.24. Q. How do I call a method from a base class without using the
1228name of the base class?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001229
1230A. DON'T DO THIS. REALLY. I MEAN IT. It appears that you could call
1231self.__class__.__bases__[0].meth(self, arguments...) but this fails when
Guido van Rossum061f1821994-10-06 16:03:45 +00001232a doubly-derived method is derived from your class: for its instances,
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001233self.__class__.__bases__[0] is your class, not its base class -- so
1234(assuming you are doing this from within Derived.meth) you would start
1235a recursive call.
1236
12374.25. Q. How can I organize my code to make it easier to change the base
1238class?
1239
1240A. You could define an alias for the base class, assign the real base
1241class to it before your class definition, and use the alias throughout
1242your class. Then all you have to change is the value assigned to the
1243alias. Incidentally, this trick is also handy if you want to decide
1244dynamically (e.g. depending on availability of resources) which base
1245class to use. Example:
1246
1247BaseAlias = <real base class>
1248class Derived(BaseAlias):
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001249 def meth(self):
1250 BaseAlias.meth(self)
1251 ...
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001252
12534.26. Q. How can I find the methods or attributes of an object?
1254
1255A. This depends on the object type.
1256
1257For an instance x of a user-defined class, instance attributes are
1258found in the dictionary x.__dict__, and methods and attributes defined
1259by its class are found in x.__class__.__bases__[i].__dict__ (for i in
1260range(len(x.__class__.__bases__))). You'll have to walk the tree of
1261base classes to find *all* class methods and attributes.
1262
1263Many, but not all built-in types define a list of their method names
1264in x.__methods__, and if they have data attributes, their names may be
1265found in x.__members__. However this is only a convention.
1266
1267For more information, read the source of the standard (but
1268undocumented) module newdir.
1269
12704.27. Q. I can't seem to use os.read() on a pipe created with os.popen().
1271
1272A. os.read() is a low-level function which takes a file descriptor (a
1273small integer). os.popen() creates a high-level file object -- the
1274same type used for sys.std{in,out,err} and returned by the builtin
1275open() function. Thus, to read n bytes from a pipe p created with
1276os.popen(), you need to use p.read(n).
Guido van Rossuma7925f11994-01-26 10:20:16 +00001277
Guido van Rossum061f1821994-10-06 16:03:45 +000012784.28. Q. How can I create a stand-alone binary from a Python script?
1279
1280The demo script "Demo/scripts/freeze.py" does what you want. (It's
1281actually not a demo but a support tool -- there is some extra code in
1282the interpreter to accommodate it.) It requires that you have the
1283Python build tree handy, complete with all the lib*.a files.
1284
1285This works by scanning your source recursively for import statements
1286(both forms) and looking for the modules on the standard Python path
1287as well as in the source directory (for built-in modules). It then
1288"compiles" the modules written in Python to C code (array initializers
1289that can be turned into code objects using the marshal module) and
1290creates a custom-made config file that only contains those built-in
1291modules which are actually used in the program. It then compiles the
1292generated C code and links it with the rest of the Python interpreter
1293to form a self-contained binary which acts exactly like your script.
1294
Guido van Rossum7be34a81995-05-31 15:17:12 +00001295Hint: the freeze program only works if your script's filename ends in
1296".py".
Guido van Rossum061f1821994-10-06 16:03:45 +00001297
Guido van Rossum7be34a81995-05-31 15:17:12 +000012984.29. Q. What WWW tools are there for Python?
Guido van Rossum061f1821994-10-06 16:03:45 +00001299
Guido van Rossum7be34a81995-05-31 15:17:12 +00001300A. There's a whole lot in the 1.2 release; see the section on
1301"Internet and WWW" in the Library Reference Manual. Highlights: a CGI
1302parser, a url retriever, an html parser.
Guido van Rossum061f1821994-10-06 16:03:45 +00001303
1304Steve Miale <smiale@cs.indiana.edu> has written a modular WWW browser
1305called Dancer. An alpha version can be FTP'ed from
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001306<URL:ftp://ftp.cs.indiana.edu/pub/smiale/dancer.tar.gz>. (There are a
1307few articles about Dancer in the (hyper)mail archive
1308<URL:http://www.cwi.nl/~guido/hypermail/python-1994q3/index.html>.)
Guido van Rossum061f1821994-10-06 16:03:45 +00001309
Guido van Rossum7be34a81995-05-31 15:17:12 +000013104.30. Q. How do I run a subprocess with pipes connected to both input
Guido van Rossum796b2591995-01-20 23:05:52 +00001311and output?
1312
1313A. This is really a UNIX question. Also, in general, it is unwise to
1314do so, because you can easily cause a deadlock where the parent
1315process is blocked waiting for output from the child, while the child
1316is blocked waiting for input from the child. This can be caused
1317because the parent expects the child to output more text than it does,
1318or it can be caused by data being stuck in stdio buffers due to lack
1319of flushing. The Python parent can of course explicitly flush the data
1320it sends to the child before it reads any output, but if the child is
1321a naive C program it can easily have been written to never explicitly
1322flush its output, even if it is interactive, since flushing is
1323normally automatic.
1324
1325In many cases, all you really need is to run some data through a
1326command and get the result back. Unless the data is infinite in size,
1327the easiest (and often the most efficient!) way to do this is to write
1328it to a temporary file and run the command with that temporary file as
1329input. The standard module tempfile exports a function mktemp() which
1330generates unique temporary file names.
1331
1332If after reading all of the above you still want to connect two pipes
1333to a subprocess's standard input and output, here's a simple solution,
1334due to Jack Jansen:
1335
1336 import os
1337 import sys
1338 import string
1339
1340 MAXFD = 100 # Max number of file descriptors in this system
1341
1342 def popen2(cmd):
1343 cmd = string.split(cmd)
1344 p2cread, p2cwrite = os.pipe()
1345 c2pread, c2pwrite = os.pipe()
1346 pid = os.fork()
1347 if pid == 0:
1348 # Child
1349 os.close(0)
1350 os.close(1)
1351 if os.dup(p2cread) <> 0:
1352 sys.stderr.write('popen2: bad read dup\n')
1353 if os.dup(c2pwrite) <> 1:
1354 sys.stderr.write('popen2: bad write dup\n')
1355 for i in range(3, MAXFD):
1356 try:
1357 os.close(i)
1358 except:
1359 pass
1360 try:
1361 os.execv(cmd[0], cmd)
1362 finally:
1363 os._exit(1)
1364 os.close(p2cread)
1365 tochild = os.fdopen(p2cwrite, 'w')
1366 os.close(c2pwrite)
1367 fromchild = os.fdopen(c2pread, 'r')
1368 return fromchild, tochild
1369
1370Note that many interactive programs (e.g. vi) don't work well with
1371pipes substituted for standard input and output. You will have to use
1372pseudo ttys ("ptys") instead of pipes. There is some undocumented
1373code to use these in the library module pty.py -- I'm afraid you're on
1374your own here. What's *really* needed is a Python interface to Don
1375Libes' expect library -- any takers?
1376
Guido van Rossum7be34a81995-05-31 15:17:12 +000013774.31. Q. How do I call a function if I have the arguments in a tuple?
Guido van Rossumac3f2121995-04-10 11:53:42 +00001378
1379A. Use the built-in function apply(). For instance,
1380
1381 func(1, 2, 3)
1382
1383is equivalent to
1384
1385 args = (1, 2, 3)
1386 apply(func, args)
1387
1388Note that func(args) is not the same -- it calls func() with exactly
1389one argument, the tuple args, instead of three arguments, the integers
13901, 2 and 3.
1391
Guido van Rossum7be34a81995-05-31 15:17:12 +000013924.32. Q. How do I enable font-lock-mode for Python in Emacs?
1393
1394A. Assuming you're already using python-mode and font-lock-mode
1395separately, all you need to do is put this in your .emacs file:
1396
1397 (defun my-python-mode-hook ()
1398 (setq font-lock-keywords python-font-lock-keywords)
1399 (font-lock-mode 1))
1400 (add-hook 'python-mode-hook 'my-python-mode-hook)
1401
14024.33. Q. Is there an inverse to the format operator (a la C's scanf())?
1403
1404A. Not as such.
1405
1406For simple input parsing, the easiest approach is usually to split
1407the line into whitespace-delimited words using string.split(), and to
1408convert decimal strings to numeric values using string.atoi(),
1409string.atol() or string.atof(). (Python's atoi() is 32-bit and its
1410atol() is arbitrary precision.) If you want to use another delimiter
1411than whitespace, use string.splitfield() (possibly combining it with
1412string.strip() which removes surrounding whitespace from a string).
1413
1414For more complicated input parsing, regular expressions (see module
1415regex) are better suited and more powerful than C's scanf().
1416
Guido van Rossum061f1821994-10-06 16:03:45 +00001417
Guido van Rossuma7925f11994-01-26 10:20:16 +000014185. Extending Python
1419===================
1420
14215.1. Q. Can I create my own functions in C?
1422
1423A. Yes, you can create built-in modules containing functions,
Guido van Rossum24349991994-02-02 14:12:45 +00001424variables, exceptions and even new types in C. This is explained in
1425the document "Extending and Embedding the Python Interpreter" (the
1426LaTeX file Doc/ext.tex). Also read the chapter on dynamic loading.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001427
14285.2. Q. Can I create my own functions in C++?
1429
1430A. Yes, using the C-compatibility features found in C++. Basically
1431you place extern "C" { ... } around the Python include files and put
1432extern "C" before each function that is going to be called by the
1433Python interpreter. Global or static C++ objects with constructors
1434are probably not a good idea.
1435
Guido van Rossum7ce61c11994-06-13 15:13:56 +000014365.3. Q. How can I execute arbitrary Python statements from C?
1437
1438A. The highest-level function to do this is run_command() which takes
1439a single string argument which is executed in the context of module
1440__main__ and returns 0 for success and -1 when an exception occurred
1441(including SyntaxError). If you want more control, use run_string();
1442see the source for run_command() in Python/pythonrun.c.
1443
14445.4. Q. How can I evaluate an arbitrary Python expression from C?
1445
1446A. Call the function run_string() from the previous question with the
1447start symbol eval_input; it then parses an expression, evaluates it
1448and returns its value. See exec_eval() in Python/bltinmodule.c.
1449
14505.5. Q. How do I extract C values from a Python object?
1451
1452A. That depends on the object's type. If it's a tuple,
1453gettuplesize(o) returns its length and gettupleitem(o, i) returns its
1454i'th item; similar for lists with getlistsize(o) and getlistitem(o,
1455i). For strings, getstringsize(o) returns its length and
1456getstringvalue(o) a pointer to its value (note that Python strings may
1457contain null bytes so strlen() is not safe). To test which type an
1458object is, first make sure it isn't NULL, and then use
1459is_stringobject(o), is_tupleobject(o), is_listobject(o) etc.
1460
14615.6. Q. How do I use mkvalue() to create a tuple of arbitrary length?
1462
1463A. You can't. Use t = newtupleobject(n) instead, and fill it with
1464objects using settupleitem(t, i, o) -- note that this "eats" a
1465reference count of o. Similar for lists with newlistobject(n) and
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001466setlistitem(l, i, o). Note that you *must* set all the tuple items to
1467some value before you pass the tuple to Python code --
1468newtupleobject(n) initializes them to NULL, which isn't a valid Python
1469value.
1470
Guido van Rossum796b2591995-01-20 23:05:52 +000014715.7. Q. What happened to mktuple(), featured in an example in the
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001472Extensions manual?
1473
1474A. It's a typo, I meant newtupleobject() (see previous question).
1475
14765.8. Q. How do I call an object's method from C?
1477
1478A. Here's a function (untested) that might become part of the next
1479release in some form. It uses <stdarg.h> to allow passing the
1480argument list on to vmkvalue():
1481
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001482 object *call_method(object *inst, char *methodname, char *format, ...)
1483 {
1484 object *method;
1485 object *args;
1486 object *result;
1487 va_list va;
1488 method = getattr(inst, methodname);
1489 if (method == NULL) return NULL;
1490 va_start(va, format);
1491 args = vmkvalue(format, va);
1492 va_end(va);
1493 if (args == NULL) {
1494 DECREF(method);
1495 return NULL;
1496 }
1497 result = call_object(method, args);
1498 DECREF(method);
1499 DECREF(args);
1500 return result;
1501 }
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001502
1503This works for any instance that has methods -- whether built-in or
1504user-defined. You are responsible for eventually DECREF'ing the
1505return value.
1506
1507To call, e.g., a file object's "seek" method with arguments 10, 0
1508(assuming the file object pointer is "f"):
1509
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001510 res = call_method(f, "seek", "(OO)", 10, 0);
1511 if (res == NULL) {
1512 ... an exception occurred ...
1513 }
1514 else {
1515 DECREF(res);
1516 }
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001517
1518Note that since call_object() *always* wants a tuple for the argument
1519list, to call a function without arguments, pass "()" for the format,
1520and to call a function with one argument, surround the argument in
1521parentheses, e.g. "(i)".
1522
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00001523(This function and a companion will be available in 1.2 under the
1524names PyEval_CallFunction and PyEval_CallMethod.)
1525
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000015265.9. Q. How do I catch the output from print_error()?
1527
1528A. (Due to Mark Hammond):
1529
1530* in Python code, define an object that supports the "write()" method.
1531FWIW, there seems to be a small problem that requires the 'softspace'
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00001532attribute to be defined too (fixed in 1.2).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001533
1534* redirect sys.stdout and sys.stderr to this object.
1535
Guido van Rossum061f1821994-10-06 16:03:45 +00001536* call print_error, or just allow the standard traceback mechanism to
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001537work.
1538
Guido van Rossum061f1821994-10-06 16:03:45 +00001539Then, the output will go wherever your write() method sends it.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001540
15415.10. Q. How do I access a module written in Python from C?
1542
1543A. You can get a pointer to the module object as follows:
1544
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001545 module = import_module("<modulename>");
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001546
1547If the module hasn't been imported yet (i.e. it is not yet present in
1548sys.modules), this initializes the module; otherwise it simply returns
1549the value of sys.modules["<modulename>"]. Note that it doesn't enter
1550the module into any namespace -- it only ensures it has been
1551initialized and is stored in sys.modules.
1552
1553You can then access the module's attributes (i.e. any name defined in
1554the module) as follows:
1555
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001556 attr = getattr(module, "<attrname>");
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001557
1558Calling setattr(), to assign to variables in the module, also works.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001559
Guido van Rossuma7925f11994-01-26 10:20:16 +00001560
15616. Python's design
1562==================
1563
15646.1. Q. Why isn't there a generic copying operation for objects in
1565Python?
1566
1567A. Hmm. Maybe there should be one, but it's difficult to assign a
1568useful meaning to copying of open files, sockets and windows, or
1569recursive data structures. As long as you design all your classes
1570yourself you are of course free to define a standard base class that
1571defines an overridable copying operation for all the objects you care
1572about. (One practical point: it would have to be a built-in function,
1573not a standard method name, since not all built-in object types have
1574methods; e.g. strings, integers and tuples don't.)
1575
15766.2. Q. Why isn't there a generic way to implement persistent objects
1577in Python? (Persistent == automatically saved to and restored from
1578disk.)
1579
1580A. Hmm, hmm. Basically for the same reasons as why there is no
1581generic copying operation.
1582
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001583A partial solution will appear in release 1.2. This will also provide
1584a partial solution to the problem of a generic copying operation.
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00001585(Partial because it still won't cope with open files etc.; however it
1586does handle user-defined classes!)
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001587
Guido van Rossuma7925f11994-01-26 10:20:16 +000015886.3. Q. Why isn't there a switch or case statement in Python?
1589
1590A. You can do this easily enough with a sequence of
1591if... elif... elif... else. There have been some proposals for switch
Guido van Rossum061f1821994-10-06 16:03:45 +00001592statement syntax, but there is no consensus (yet) on whether and how
Guido van Rossuma7925f11994-01-26 10:20:16 +00001593to do range tests.
1594
Guido van Rossumc50158e1994-05-31 09:18:50 +000015956.4. Q. Why does Python use indentation for grouping of statements?
1596
1597A. Basically I believe that using indentation for grouping is
1598extremely elegant and contributes a lot to the clarity of the average
1599Python program. Most people learn to love this feature after a while.
1600Some arguments for it:
1601
1602- Since there are no begin/end brackets there cannot be a disagreement
1603between grouping perceived by the parser and the human reader. I
1604remember long ago seeing a C fragment like this:
1605
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001606 if (x <= y)
1607 x++;
1608 y--;
1609 z++;
Guido van Rossumc50158e1994-05-31 09:18:50 +00001610
1611and staring a long time at it wondering why y was being decremented
1612even for x > y... (And I wasn't a C newbie then either.)
1613
Guido van Rossum7be34a81995-05-31 15:17:12 +00001614- Since there are no begin/end brackets, Python is much less prone to
1615coding-style conflicts. In C there are loads of different ways to
1616place the braces (including the choice whether to place braces around
1617single statements in certain cases, for consistency). If you're used
1618to reading (and writing) code that uses one style, you will feel at
1619least slightly uneasy when reading (or being required to write)
1620another style.
Guido van Rossumc50158e1994-05-31 09:18:50 +00001621
1622- Many coding styles place begin/end brackets on a line by themself.
1623This makes programs considerably longer and wastes valuable screen
1624space, making it harder to get a good overview over a program.
1625Ideally, a function should fit on one basic tty screen (say, 20
1626lines). 20 lines of Python are worth a LOT more than 20 lines of C.
1627This is not solely due to the lack of begin/end brackets (the lack of
1628declarations also helps, and the powerful operations of course), but
1629it certainly helps!
1630
Guido van Rossum3de27361994-07-25 14:19:33 +000016316.5. Q. Why are Python strings immutable?
1632
1633A. There are two advantages. One is performance: knowing that a
1634string is immutable makes it easy to lay it out at construction time
1635-- fixed and unchanging storage requirements. (This is also one of
Guido van Rossum061f1821994-10-06 16:03:45 +00001636the reasons for the distinction between tuples and lists.) The
Guido van Rossum3de27361994-07-25 14:19:33 +00001637other is that strings in Python are considered as "elemental" as
1638numbers. No amount of activity will change the value 8 to anything
1639else, and in Python, no amount of activity will change the string
1640"eight" to anything else. (Adapted from Jim Roskind)
1641
16426.6. Q. Why don't strings have methods like index() or sort(), like
1643lists?
1644
1645A. Good question. Strings currently don't have methods at all
1646(likewise tuples and numbers). Long ago, it seemed unnecessary to
1647implement any of these functions in C, so a standard library module
1648"string" written in Python was created that performs string related
1649operations. Since then, the cry for performance has moved most of
1650them into the built-in module strop (this is imported by module
Guido van Rossumf8c76d01994-08-17 12:19:53 +00001651string, which is still the preferred interface, without loss of
Guido van Rossum3de27361994-07-25 14:19:33 +00001652performance except during initialization). Some of these functions
1653(e.g. index()) could easily be implemented as string methods instead,
1654but others (e.g. sort()) can't, since their interface prescribes that
1655they modify the object, while strings are immutable (see the previous
1656question).
1657
16586.7. Q. Why does Python use methods for some functionality
1659(e.g. list.index()) but functions for other (e.g. len(list))?
1660
1661A. Functions are used for those operations that are generic for a
1662group of types and which should work even for objects that don't have
1663methods at all (e.g. numbers, strings, tuples). Also, implementing
1664len(), max(), min() as a built-in function is actually less code than
1665implementing them as methods for each type. One can quibble about
1666individual cases but it's really too late to change such things
1667fundamentally now.
1668
16696.8. Q. Why can't I derive a class from built-in types (e.g. lists or
1670files)?
1671
1672A. This is caused by the relatively late addition of (user-defined)
1673classes to the language -- the implementation framework doesn't easily
1674allow it. See the answer to question 4.2 for a work-around. This
1675*may* be fixed in the (distant) future.
1676
16776.9. Q. Why must 'self' be declared and used explicitly in method
1678definitions and calls?
1679
1680A. By asking this question you reveal your C++ background. :-)
1681When I added classes, this was (again) the simplest way of
1682implementing methods without too many changes to the interpreter. I
1683borrowed the idea from Modula-3. It turns out to be very useful, for
1684a variety of reasons.
1685
1686First, it makes it more obvious that you are using a method or
1687instance attribute instead of a local variable. Reading "self.x" or
1688"self.meth()" makes it absolutely clear that an instance variable or
1689method is used even if you don't know the class definition by heart.
1690In C++, you can sort of tell by the lack of a local variable
Guido van Rossum061f1821994-10-06 16:03:45 +00001691declaration (assuming globals are rare or easily recognizable) -- but
Guido van Rossum3de27361994-07-25 14:19:33 +00001692in Python, there are no local variable declarations, so you'd have to
1693look up the class definition to be sure.
1694
1695Second, it means that no special syntax is necessary if you want to
1696explicitly reference or call the method from a particular class. In
1697C++, if you want to use a method from base class that is overridden in
1698a derived class, you have to use the :: operator -- in Python you can
1699write baseclass.methodname(self, <argument list>). This is
1700particularly useful for __init__() methods, and in general in cases
1701where a derived class method wants to extend the base class method of
1702the same name and thus has to call the base class method somehow.
1703
1704Lastly, for instance variables, it solves a syntactic problem with
1705assignment: since local variables in Python are (by definition!) those
1706variables to which a value assigned in a function body (and that
1707aren't explicitly declared global), there has to be some way to tell
1708the interpreter that an assignment was meant to assign to an instance
1709variable instead of to a local variable, and it should preferably be
1710syntactic (for efficiency reasons). C++ does this through
1711declarations, but Python doesn't have declarations and it would be a
1712pity having to introduce them just for this purpose. Using the
1713explicit "self.var" solves this nicely. Similarly, for using instance
1714variables, having to write "self.var" means that references to
1715unqualified names inside a method don't have to search the instance's
1716directories.
1717
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000017186.10. Q. Can't you emulate threads in the interpreter instead of
1719relying on an OS-specific thread implementation?
1720
1721A. Unfortunately, the interpreter pushes at least one C stack frame
1722for each Python stack frame. Also, extensions can call back into
1723Python at almost random moments. Therefore a complete threads
1724implementation requires thread support for C.
1725
Guido van Rossum061f1821994-10-06 16:03:45 +000017266.11. Q. Why can't lambda forms contain statements?
1727
1728A. Python lambda forms cannot contain statements because Python's
Guido van Rossum796b2591995-01-20 23:05:52 +00001729syntactic framework can't handle statements nested inside expressions.
Guido van Rossum061f1821994-10-06 16:03:45 +00001730
1731However, in Python, this is not a serious problem. Unlike lambda
1732forms in other languages, where they add functionality, Python lambdas
1733are only a shorthand notation if you're too lazy to define a function.
1734
1735Functions are already first class objects in Python, and can be
1736declared in a local scope. Therefore the only advantage of using a
1737lambda form instead of a locally-defined function is that you'll have
1738to invent a name for the function -- but that's just a local variable
1739to which the function object (which is exactly the same type of object
1740that a lambda form yields) is assigned!
1741
17426.12. Q. Why is there no more efficient way of iterating over a dictionary
1743than first constructing the list of keys()?
1744
1745A. Have you tried it? I bet it's fast enough for your purposes! In
1746most cases such a list takes only a few percent of the space occupied
1747by the dictionary -- it needs only 4 bytes (the size of a pointer) per
1748key -- a dictionary costs 8 bytes per key plus between 30 and 70
1749percent hash table overhead, plus the space for the keys and values --
1750by necessity all keys are unique objects and a string object (the most
1751common key type) costs at least 18 bytes plus the length of the
1752string. Add to that the values contained in the dictionary, and you
1753see that 4 bytes more per item really isn't that much more memory...
1754
1755A call to dict.keys() makes one fast scan over the dictionary
1756(internally, the iteration function does exist) copying the pointers
1757to the key objects into a pre-allocated list object of the right size.
1758The iteration time isn't lost (since you'll have to iterate anyway --
1759unless in the majority of cases your loop terminates very prematurely
1760(which I doubt since you're getting the keys in random order).
1761
1762I don't expose the dictionary iteration operation to Python
1763programmers because the dictionary shouldn't be modified during the
1764entire iteration -- if it is, there's a very small chance that the
1765dictionary is reorganized because the hash table becomes too full, and
1766then the iteration may miss some items and see others twice. Exactly
1767because this only occurs rarely, it would lead to hidden bugs in
1768programs: it's easy never to have it happen during test runs if you
1769only insert or delete a few items per iteration -- but your users will
1770surely hit upon it sooner or later.
1771
Guido van Rossuma7925f11994-01-26 10:20:16 +00001772
17737. Using Python on non-UNIX platforms
1774=====================================
1775
Guido van Rossum91f60831994-02-15 15:52:27 +000017767.1. Q. Is there a Mac version of Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +00001777
Guido van Rossume530c581995-04-10 12:32:16 +00001778A. Yes, see the "mac" subdirectory of the distribution sites,
Guido van Rossum7be34a81995-05-31 15:17:12 +00001779e.g. <URL:ftp://ftp.python.org/pub/python/mac/>.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001780
Guido van Rossum91f60831994-02-15 15:52:27 +000017817.2. Q. Is there a DOS version of Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +00001782
Guido van Rossume530c581995-04-10 12:32:16 +00001783A. Yes, see the "pc" subdirectory of the distribution sites,
Guido van Rossum7be34a81995-05-31 15:17:12 +00001784e.g. <URL:ftp://ftp.python.org/pub/python/pc/>.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001785
Guido van Rossume530c581995-04-10 12:32:16 +000017867.3. Q. Is there a Windows 3.1(1) version of Python?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001787
Guido van Rossume530c581995-04-10 12:32:16 +00001788A. Yes, also see the "pc" subdirectory of the distribution sites,
Guido van Rossum7be34a81995-05-31 15:17:12 +00001789e.g. <URL:ftp://ftp.python.org/pub/python/pc/>.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001790
Guido van Rossum91f60831994-02-15 15:52:27 +000017917.4. Q. Is there a Windows NT version of Python?
1792
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001793A. Yes. Mark Hammond <MHammond@cmutual.com.au> has built a full NT
1794port. This supports using DLLs for dynamic loading of Python modules,
1795and includes an interface to the Microsoft Foundation Classes and a
1796Python programming environment using it that's written mostly in
Guido van Rossum7be34a81995-05-31 15:17:12 +00001797Python. See <URL:ftp://ftp.python.org/pub/python/nt/> -- most mirrors
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001798will also have this.
Guido van Rossum91f60831994-02-15 15:52:27 +00001799
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001800Sam Rushing <rushing@squirl.oau.org> once announced he knows how to
1801build Python for the Windows NT on the DEC Alpha AXP.
Guido van Rossum061f1821994-10-06 16:03:45 +00001802
1803Note that currently there is no unified compilation environment for
1804all NT platforms -- hopefully Microsoft will fix this with the release
1805of Visual C++ 2.0.
1806
Guido van Rossume530c581995-04-10 12:32:16 +000018077.5. Q. Is there a Windows 95 version of Python?
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001808
Guido van Rossume530c581995-04-10 12:32:16 +00001809A. The Windows NT version might work, otherwise the Windows 3.1(1)
1810version should work (isn't Windows 95 supposed to be backwards
1811compatible?).
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001812
Guido van Rossume530c581995-04-10 12:32:16 +000018137.6. Q. Is there an OS/2 version of Python?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001814
Guido van Rossume530c581995-04-10 12:32:16 +00001815A. Yes, also see the "pc" subdirectory of the distribution sites,
Guido van Rossum7be34a81995-05-31 15:17:12 +00001816e.g. <URL:ftp://ftp.python.org/pub/python/pc/>.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001817
Guido van Rossume530c581995-04-10 12:32:16 +000018187.7. Q. Is there a VMS version of Python?
1819
1820A. I think not. This question has been asked on the list several
1821times and I've never seen an affirmative answer.
1822
18237.8. Q. What about IBM mainframes, or other esoteric non-UNIX
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001824platforms?
1825
Guido van Rossume530c581995-04-10 12:32:16 +00001826A. Basically, the same story as for VMS...
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001827
Guido van Rossume530c581995-04-10 12:32:16 +000018287.9. Q. Where are the source or Makefiles for the non-UNIX versions?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001829
Guido van Rossume530c581995-04-10 12:32:16 +00001830A. The standard sources can (almost) be used. Additional sources can
1831be found in the platform-specific subdirectories of the distribution.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001832
Guido van Rossume530c581995-04-10 12:32:16 +000018337.10. Q. What is the status and support for the non-UNIX versions?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001834
1835A. I don't have access to most of these platforms, so in general I am
1836dependent on material submitted by volunteers(*). However I strive to
1837integrate all changes needed to get it to compile on a particular
1838platform back into the standard sources, so porting of the next
1839version to the various non-UNIX platforms should be easy.
1840
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001841(*) For the Macintosh, that volunteer is me, with help from Jack
1842Jansen <jack@cwi.nl>.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001843
Guido van Rossume530c581995-04-10 12:32:16 +000018447.11. Q. I have a PC version but it appears to be only a binary.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001845Where's the library?
Guido van Rossuma7925f11994-01-26 10:20:16 +00001846
1847A. You still need to copy the files from the distribution directory
Guido van Rossum91f60831994-02-15 15:52:27 +00001848"python/Lib" to your system. If you don't have the full distribution,
Guido van Rossume530c581995-04-10 12:32:16 +00001849you can get the file lib<version>.tar.gz from most ftp sites carrying
1850Python; this is a subset of the distribution containing just those
Guido van Rossum7be34a81995-05-31 15:17:12 +00001851files, e.g. <URL:ftp://ftp.python.org/pub/python/src/lib1.1.tar.gz>.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001852
Guido van Rossum5333c5d1994-04-11 11:06:22 +00001853Once you have installed the library, you need to point sys.path to it.
1854Assuming the library is in C:\misc\python\lib, the following commands
1855will point your Python interpreter to it (note the doubled backslashes
1856-- you can also use single forward slashes instead):
1857
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001858 >>> import sys
1859 >>> sys.path.insert(0, 'C:\\misc\\python\\lib')
1860 >>>
Guido van Rossum5333c5d1994-04-11 11:06:22 +00001861
1862For a more permanent effect, set the environment variable PYTHONPATH,
1863as follows (talking to a DOS prompt):
1864
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001865 C> SET PYTHONPATH=C:\misc\python\lib
Guido van Rossum5333c5d1994-04-11 11:06:22 +00001866
Guido van Rossume530c581995-04-10 12:32:16 +000018677.12. Q. Where's the documentation for the Mac or PC version?
Guido van Rossuma7925f11994-01-26 10:20:16 +00001868
Guido van Rossume530c581995-04-10 12:32:16 +00001869A. The documentation for the Unix version also applies to the Mac and
1870PC versions. Where applicable, differences are indicated in the text.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001871
Guido van Rossume530c581995-04-10 12:32:16 +000018727.13. Q. The Mac (PC) version doesn't seem to have any facilities for
Guido van Rossum91f60831994-02-15 15:52:27 +00001873creating or editing programs apart from entering it interactively, and
1874there seems to be no way to save code that was entered interactively.
1875How do I create a Python program on the Mac (PC)?
Guido van Rossuma7925f11994-01-26 10:20:16 +00001876
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001877A. Use an external editor. On the Mac, BBEdit seems to be a popular
1878no-frills text editor. I work like this: start the interpreter; edit
1879a module file using BBedit; import and test it in the interpreter;
1880edit again in BBedit; then use the built-in function reload() to
1881re-read the imported module; etc.
Guido van Rossum5333c5d1994-04-11 11:06:22 +00001882
1883Regarding the same question for the PC, Kurt Wm. Hemr writes: "While
1884anyone with a pulse could certainly figure out how to do the same on
1885MS-Windows, I would recommend the NotGNU Emacs clone for MS-Windows.
1886Not only can you easily resave and "reload()" from Python after making
1887changes, but since WinNot auto-copies to the clipboard any text you
1888select, you can simply select the entire procedure (function) which
1889you changed in WinNot, switch to QWPython, and shift-ins to reenter
1890the changed program unit."