blob: 99cd51003526cc57a27153b7353299df7bb8e2b3 [file] [log] [blame]
Guido van Rossuma7925f11994-01-26 10:20:16 +00001Subject: FAQ: Python -- an object-oriented language
Guido van Rossum5333c5d1994-04-11 11:06:22 +00002Newsgroups: comp.lang.python,comp.answers,news.answers
3Followup-to: comp.lang.python
Guido van Rossuma7925f11994-01-26 10:20:16 +00004From: guido@cwi.nl (Guido van Rossum)
Guido van Rossum7be34a81995-05-31 15:17:12 +00005Reply-to: guido@cnri.reston.va.us (Guido van Rossum)
Guido van Rossum3fc9d731995-07-25 15:10:56 +00006Expires: Fri, 1 Sep 1995 00:00:00 GMT
Guido van Rossumbf8e7d51995-08-28 03:09:13 +00007Supersedes: <DCA29p.A9A@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 Rossumbf8e7d51995-08-28 03:09:13 +000012Version: 1.25++
Guido van Rossum3fc9d731995-07-25 15:10:56 +000013Last-modified: 25 July 1995
Guido van Rossuma7925f11994-01-26 10:20:16 +000014
15This article contains answers to Frequently Asked Questions about
16Python (an object-oriented interpreted programming language -- see
17the answer to question 1.1 for a short overview).
18
Guido van Rossuma6c707c1995-01-02 17:32:28 +000019Copyright 1993-1995 Guido van Rossum. Unchanged electronic
Guido van Rossuma7925f11994-01-26 10:20:16 +000020redistribution of this FAQ is allowed. Printed redistribution only
21with permission of the author. No warranties.
22
23Author's address:
Guido van Rossuma6c707c1995-01-02 17:32:28 +000024 Guido van Rossum
25 CWI, dept. CST
26 Kruislaan 413
27 P.O. Box 94079
28 1090 GB Amsterdam
29 The Netherlands
Guido van Rossum796b2591995-01-20 23:05:52 +000030Email: <guido@cwi.nl>
Guido van Rossuma7925f11994-01-26 10:20:16 +000031
32The latest version of this FAQ is available by anonymous ftp from
Guido van Rossum7be34a81995-05-31 15:17:12 +000033<URL:ftp://ftp.python.org/pub/python/FAQ>. It will also be posted
Guido van Rossuma6c707c1995-01-02 17:32:28 +000034regularly to the newsgroups comp.answers <URL:news:comp.answers> and
35comp.lang.python <URL:news:comp.lang.python>.
Guido van Rossuma7925f11994-01-26 10:20:16 +000036
Guido van Rossuma6c707c1995-01-02 17:32:28 +000037Many FAQs, including this one, are available by anonymous ftp
38<URL:ftp://rtfm.mit.edu/pub/usenet/news.answers/>. The name under
39which a FAQ is archived appears in the Archive-name line at the top of
40the article. This FAQ is archived as python-faq/part1
41<URL:ftp://rtfm.mit.edu/pub/usenet/news.answers/python-faq/part1>.
Guido van Rossuma7925f11994-01-26 10:20:16 +000042
43There's a mail server on that machine which will send you files from
44the archive by e-mail if you have no ftp access. You send a e-mail
Guido van Rossuma6c707c1995-01-02 17:32:28 +000045message to <mail-server@rtfm.mit.edu> containing the single word help
46in the message body to receive instructions.
Guido van Rossuma7925f11994-01-26 10:20:16 +000047
48This FAQ is divided in the following chapters:
49
50 1. General information and availability
51 2. Python in the real world
52 3. Building Python
53 4. Programming in Python
54 5. Extending Python
55 6. Python's design
56 7. Using Python on non-UNIX platforms
57
58To find the start of a particular chapter, search for the chapter number
59followed by a dot and a space at the beginning of a line (e.g. to
60find chapter 4 in vi, type /^4\. /).
61
62Here's an overview of the questions per chapter:
63
64 1. General information and availability
65 1.1. Q. What is Python?
66 1.2. Q. Why is it called Python?
67 1.3. Q. How do I obtain a copy of the Python source?
68 1.4. Q. How do I get documentation on Python?
Guido van Rossumc50158e1994-05-31 09:18:50 +000069 1.5. Q. Are there other ftp sites that mirror the Python distribution?
70 1.6. Q. Is there a newsgroup or mailing list devoted to Python?
Guido van Rossuma6c707c1995-01-02 17:32:28 +000071 1.7. Q. Is there a WWW page devoted to Python?
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +000072 1.8. Q. Is the Python documentation available on the WWW?
73 1.9. Q. Is there a book on Python, or will there be one out soon?
74 1.10. Q. Are there any published articles about Python that I can quote?
75 1.11. Q. Are there short introductory papers or talks on Python?
76 1.12. Q. How does the Python version numbering scheme work?
77 1.13. Q. How do I get a beta test version of Python?
78 1.14. Q. Are there other ftp sites that carry Python related material?
79 1.15. Q. Are there copyright restrictions on the use of Python?
Guido van Rossum7be34a81995-05-31 15:17:12 +000080 1.16. Q. Why was Python created in the first place?
81 1.17. Q. What happened to Tim Peters (the author of python-mode.el, and many
82 entertaining and enlightening pieces of Python email)?
Guido van Rossuma7925f11994-01-26 10:20:16 +000083
84 2. Python in the real world
85 2.1. Q. How many people are using Python?
86 2.2. Q. Have any significant projects been done in Python?
87 2.3. Q. Are there any commercial projects going on using Python?
Guido van Rossum95f61a71994-01-26 17:23:37 +000088 2.4. Q. How stable is Python?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000089 2.5. Q. When will the next version be released?
90 2.6. Q. What new developments are expected for Python in the future?
91 2.7. Q. Is it reasonable to propose incompatible changes to Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +000092
93 3. Building Python
Guido van Rossum91f60831994-02-15 15:52:27 +000094 3.1. Q. Is there a test set?
95 3.2. Q. When running the test set, I get complaints about floating point
Guido van Rossuma7925f11994-01-26 10:20:16 +000096 operations, but when playing with floating point operations I cannot
97 find anything wrong with them.
Guido van Rossum7be34a81995-05-31 15:17:12 +000098 3.3. Q. Link errors after rerunning the configure script.
99 3.4. Q. The python interpreter complains about options passed to a
Guido van Rossum24349991994-02-02 14:12:45 +0000100 script (after the script name).
Guido van Rossum7be34a81995-05-31 15:17:12 +0000101 3.5. Q. When building on the SGI, make tries to run python to create
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000102 glmodule.c, but python hasn't been built or installed yet.
Guido van Rossum7be34a81995-05-31 15:17:12 +0000103 3.6. Q. Python built with gcc for the DEC Alpha doesn't work.
104 3.7. Q. I use VPATH but some targets are built in the source directory.
105 3.8. Q. Trouble building or linking with the GNU readline library.
106 3.9. Q. Trouble building Python on Linux.
107 3.10. Q. Trouble with prototypes on Ultrix.
108 3.11. Q. Trouble with posix.listdir on NeXTSTEP 3.2.
109 3.12. Q. Other trouble building Python on platform X.
110 3.13. Q. How to configure dynamic loading on Lixux.
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000111 3.14. Q. Under Solaris 2.x, using GCC, how do I use shared libraries?
112 3.15. Q. Errors when linking with a shared library containing C++ code.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000113
114 4. Programming in Python
Guido van Rossum24349991994-02-02 14:12:45 +0000115 4.1. Q. Is there a source code level debugger with breakpoints, step,
116 etc.?
117 4.2. Q. Can I create an object class with some methods implemented in
Guido van Rossuma7925f11994-01-26 10:20:16 +0000118 C and others in Python (e.g. through inheritance)? (Also phrased as:
119 Can I use a built-in type as base class?)
Guido van Rossum24349991994-02-02 14:12:45 +0000120 4.3. Q. Is there a curses/termcap package for Python?
121 4.4. Q. Is there an equivalent to C's onexit() in Python?
122 4.5. Q. When I define a function nested inside another function, the
Guido van Rossuma7925f11994-01-26 10:20:16 +0000123 nested function seemingly can't access the local variables of the
124 outer function. What is going on? How do I pass local data to a
125 nested function?
Guido van Rossum24349991994-02-02 14:12:45 +0000126 4.6. Q. How do I iterate over a sequence in reverse order?
127 4.7. Q. My program is too slow. How do I speed it up?
128 4.8. Q. When I have imported a module, then edit it, and import it
Guido van Rossuma7925f11994-01-26 10:20:16 +0000129 again (into the same Python process), the changes don't seem to take
130 place. What is going on?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000131 4.9. Q. How do I find the current module name?
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000132 4.10. Q. I have a module in which I want to execute some extra code
133 when it is run as a script. How do I find out whether I am running as
134 a script?
135 4.11. Q. I try to run a program from the Demo directory but it fails
136 with ImportError: No module named ...; what gives?
Guido van Rossum061f1821994-10-06 16:03:45 +0000137 4.12. Q. I have successfully built Python with STDWIN but it can't
138 find some modules (e.g. stdwinevents).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000139 4.13. Q. What GUI toolkits exist for Python?
Guido van Rossum061f1821994-10-06 16:03:45 +0000140 4.14. Q. Are there any interfaces to database packages in Python?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000141 4.15. Q. Is it possible to write obfuscated one-liners in Python?
142 4.16. Q. Is there an equivalent of C's "?:" ternary operator?
143 4.17. Q. My class defines __del__ but it is not called when I delete the
144 object.
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000145 4.18. Q. How do I change the shell environment for programs called
146 using os.popen() or os.system()? Changing os.environ doesn't work.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000147 4.19. Q. What is a class?
148 4.20. Q. What is a method?
149 4.21. Q. What is self?
150 4.22. Q. What is a unbound method?
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000151 4.23. Q. How do I call a method defined in a base class from a derived
152 class that overrides it?
153 4.24. Q. How do I call a method from a base class without using the
154 name of the base class?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000155 4.25. Q. How can I organize my code to make it easier to change the base
156 class?
157 4.26. Q. How can I find the methods or attributes of an object?
Guido van Rossum061f1821994-10-06 16:03:45 +0000158 4.27. Q. I can't seem to use os.read() on a pipe created with os.popen().
159 4.28. Q. How can I create a stand-alone binary from a Python script?
Guido van Rossum7be34a81995-05-31 15:17:12 +0000160 4.29. Q. What WWW tools are there for Python?
161 4.30. Q. How do I run a subprocess with pipes connected to both input
Guido van Rossum796b2591995-01-20 23:05:52 +0000162 and output?
Guido van Rossum7be34a81995-05-31 15:17:12 +0000163 4.31. Q. How do I call a function if I have the arguments in a tuple?
164 4.32. Q. How do I enable font-lock-mode for Python in Emacs?
165 4.33. Q. Is there an inverse to the format operator (a la C's scanf())?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000166
167 5. Extending Python
168 5.1. Q. Can I create my own functions in C?
169 5.2. Q. Can I create my own functions in C++?
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000170 5.3. Q. How can I execute arbitrary Python statements from C?
171 5.4. Q. How can I evaluate an arbitrary Python expression from C?
172 5.5. Q. How do I extract C values from a Python object?
173 5.6. Q. How do I use mkvalue() to create a tuple of arbitrary length?
Guido van Rossum796b2591995-01-20 23:05:52 +0000174 5.7. Q. What happened to mktuple(), featured in an example in the
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000175 Extensions manual?
176 5.8. Q. How do I call an object's method from C?
177 5.9. Q. How do I catch the output from print_error()?
178 5.10. Q. How do I access a module written in Python from C?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000179
180 6. Python's design
181 6.1. Q. Why isn't there a generic copying operation for objects in
182 Python?
183 6.2. Q. Why isn't there a generic way to implement persistent objects
184 in Python? (Persistent == automatically saved to and restored from
185 disk.)
186 6.3. Q. Why isn't there a switch or case statement in Python?
Guido van Rossumc50158e1994-05-31 09:18:50 +0000187 6.4. Q. Why does Python use indentation for grouping of statements?
Guido van Rossum3de27361994-07-25 14:19:33 +0000188 6.5. Q. Why are Python strings immutable?
189 6.6. Q. Why don't strings have methods like index() or sort(), like
190 lists?
191 6.7. Q. Why does Python use methods for some functionality
192 (e.g. list.index()) but functions for other (e.g. len(list))?
193 6.8. Q. Why can't I derive a class from built-in types (e.g. lists or
194 files)?
195 6.9. Q. Why must 'self' be declared and used explicitly in method
196 definitions and calls?
Guido van Rossum061f1821994-10-06 16:03:45 +0000197 6.10. Q. Can't you emulate threads in the interpreter instead of
198 relying on an OS-specific thread implementation?
199 6.11. Q. Why can't lambda forms contain statements?
200 6.12. Q. Why is there no more efficient way of iterating over a dictionary
201 than first constructing the list of keys()?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000202
203 7. Using Python on non-UNIX platforms
Guido van Rossum91f60831994-02-15 15:52:27 +0000204 7.1. Q. Is there a Mac version of Python?
205 7.2. Q. Is there a DOS version of Python?
Guido van Rossume530c581995-04-10 12:32:16 +0000206 7.3. Q. Is there a Windows 3.1(1) version of Python?
Guido van Rossum91f60831994-02-15 15:52:27 +0000207 7.4. Q. Is there a Windows NT version of Python?
Guido van Rossume530c581995-04-10 12:32:16 +0000208 7.5. Q. Is there a Windows 95 version of Python?
209 7.6. Q. Is there an OS/2 version of Python?
210 7.7. Q. Is there a VMS version of Python?
Guido van Rossumbf8e7d51995-08-28 03:09:13 +0000211 7.8. Q. What about IBM mainframes, or other non-UNIX platforms?
Guido van Rossume530c581995-04-10 12:32:16 +0000212 7.9. Q. Where are the source or Makefiles for the non-UNIX versions?
213 7.10. Q. What is the status and support for the non-UNIX versions?
214 7.11. Q. I have a PC version but it appears to be only a binary.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000215 Where's the library?
Guido van Rossume530c581995-04-10 12:32:16 +0000216 7.12. Q. Where's the documentation for the Mac or PC version?
217 7.13. Q. The Mac (PC) version doesn't seem to have any facilities for
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000218 creating or editing programs apart from entering it interactively, and
219 there seems to be no way to save code that was entered interactively.
220 How do I create a Python program on the Mac (PC)?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000221
222To find a particular question, search for the question number followed
223by a dot, a space, and a Q at the beginning of a line (e.g. to find
224question 4.2 in vi, type /^4\.2\. Q/).
225
226
2271. General information and availability
228=======================================
229
2301.1. Q. What is Python?
231
232A. Python is an interpreted, interactive, object-oriented programming
233language. It incorporates modules, exceptions, dynamic typing, very
234high level dynamic data types, and classes. Python combines
235remarkable power with very clear syntax. It has interfaces to many
236system calls and libraries, as well as to various window systems, and
237is extensible in C or C++. It is also usable as an extension language
238for applications that need a programmable interface. Finally, Python
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000239is portable: it runs on many brands of UNIX, on the Mac, and on PCs
240under MS-DOS, Windows, Windows NT, and OS/2.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000241
242To find out more, the best thing to do is to start reading the
243tutorial from the documentation set (see a few questions further
244down).
245
2461.2. Q. Why is it called Python?
247
Guido van Rossum796b2591995-01-20 23:05:52 +0000248A. Apart from being a computer scientist, I'm also a fan of "Monty
Guido van Rossuma7925f11994-01-26 10:20:16 +0000249Python's Flying Circus" (a BBC comedy series from the seventies, in
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000250the -- unlikely -- case you didn't know). It occurred to me one day
251that I needed a name that was short, unique, and slightly mysterious.
252And I happened to be reading some scripts from the series at the
253time... So then I decided to call my language Python. But Python is
254not a joke. And don't you associate it with dangerous reptiles
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000255either! (If you need an icon, use an image of the 16-ton weight from
256the TV series or of a can of SPAM :-)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000257
2581.3. Q. How do I obtain a copy of the Python source?
259
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000260A. The latest complete Python source distribution is always available
261by anonymous ftp, e.g.
Guido van Rossum7be34a81995-05-31 15:17:12 +0000262<URL:ftp://ftp.python.org/pub/python/src/python1.2.tar.gz>. It is a
Guido van Rossume530c581995-04-10 12:32:16 +0000263gzipped tar file containing the complete C source, LaTeX
264documentation, Python library modules, example programs, and several
265useful pieces of freely distributable software. This will compile and
266run out of the box on most UNIX platforms. (See section 7 for
267non-UNIX information.)
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000268
269Occasionally a set of patches is issued which has to be applied using
270the patch program. These patches are placed in the same directory,
Guido van Rossum7be34a81995-05-31 15:17:12 +0000271e.g. <URL:ftp://ftp.python.org/pub/python/src/patch1.1.1>. (At the time
Guido van Rossume530c581995-04-10 12:32:16 +0000272of writing, no patches exist for 1.2.)
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000273
274An index of said ftp directory can be found in the file INDEX. An
275HTML version of the index can be found in the file index.html,
Guido van Rossum7be34a81995-05-31 15:17:12 +0000276<URL:ftp://ftp.python.org/pub/python/index.html>.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000277
2781.4. Q. How do I get documentation on Python?
279
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000280A. The LaTeX source for the documentation is part of the source
281distribution. If you don't have LaTeX, the latest Python
282documentation set is always available by anonymous ftp, e.g.
Guido van Rossum7be34a81995-05-31 15:17:12 +0000283<URL:ftp://ftp.python.org/pub/python/doc/postscript.tar.gz>. It is a
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000284gzipped tar file containing PostScript files of the reference manual,
Guido van Rossum3de27361994-07-25 14:19:33 +0000285the library manual, and the tutorial. Note that the library manual is
286the most important one of the set, as much of Python's power stems
287from the standard or built-in types, functions and modules, all of
288which are described here. PostScript for a high-level description of
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000289Python is in the file nluug-paper.ps (a separate file on the ftp
290site).
Guido van Rossuma7925f11994-01-26 10:20:16 +0000291
Guido van Rossumc50158e1994-05-31 09:18:50 +00002921.5. Q. Are there other ftp sites that mirror the Python distribution?
293
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000294A. The following anonymous ftp sites keep mirrors of the Python
295distribution:
Guido van Rossuma7925f11994-01-26 10:20:16 +0000296
Guido van Rossum7be34a81995-05-31 15:17:12 +0000297 <URL:ftp://ftp.cwi.nl/pub/python/>
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000298 <URL:ftp://gatekeeper.dec.com/pub/plan/python/>
299 <URL:ftp://ftp.uu.net/languages/python/>
300 <URL:ftp://ftp.wustl.edu/graphics/graphics/sgi-stuff/python/>
301 <URL:ftp://ftp.funet.fi/pub/languages/python/>
302 <URL:ftp://ftp.sunet.se/pub/lang/python/>
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000303 <URL:ftp://unix.hensa.ac.uk/mirrors/uunet/languages/python/>
304 <URL:ftp://ftp.sterling.com/programming/languages/python/>
305 <URL:ftp://ftp.ibp.fr/pub/python/>
306 <URL:ftp://ftp.switch.ch/software/sources/python/>
307 <URL:ftp://ftp.informatik.tu-muenchen.de/pub/comp/programming/languages/python/>
Guido van Rossuma7925f11994-01-26 10:20:16 +0000308
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000309Or try archie on the string "python".
Guido van Rossuma7925f11994-01-26 10:20:16 +0000310
Guido van Rossumc50158e1994-05-31 09:18:50 +00003111.6. Q. Is there a newsgroup or mailing list devoted to Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000312
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000313A. There is a newsgroup, comp.lang.python <URL:news:comp.lang.python>,
314and a mailing list. The newsgroup and mailing list are gatewayed into
315each other -- if you can read news it's unnecessary to subscribe to
316the mailing list. Send e-mail to <python-list-request@cwi.nl> to
Guido van Rossume530c581995-04-10 12:32:16 +0000317(un)subscribe to the mailing list. Hypermail archives of (nearly)
318everything posted to the mailing list (and thus the newsgroup) are
319available on our WWW server,
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000320<URL:http://www.cwi.nl/~guido/hypermail/index.html>. The raw archives
321are also available by ftp, e.g.
Guido van Rossum7be34a81995-05-31 15:17:12 +0000322<URL:ftp://ftp.python.org/pub/python/mail/mailinglist.gz>. The
Guido van Rossume530c581995-04-10 12:32:16 +0000323uncompressed versions of these files can be read with the standard
324UNIX Mail program ("Mail -f file") or with nn ("nn file"). To read
Guido van Rossumbf8e7d51995-08-28 03:09:13 +0000325them using MH, you could use "inc -file file". (The archival service
326has stopped archiving new articles around the end of April 1995. I
327hope to revive it on the PSA server www.python.org sometime in the
328future.)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000329
Guido van Rossuma6c707c1995-01-02 17:32:28 +00003301.7. Q. Is there a WWW page devoted to Python?
331
Guido van Rossum7be34a81995-05-31 15:17:12 +0000332A. Yes, <URL:http://www.python.org/> is the official Python home page.
333At the time of writing, this page is not yet completely operational;
334you may have a look at the old Python home page:
335<URL:http://www.cwi.nl/~guido/Python.html> or at the U.S. copy:
336<URL:http://www.python.org/~guido/Python.html>.
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000337
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00003381.8. Q. Is the Python documentation available on the WWW?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000339
Guido van Rossum7be34a81995-05-31 15:17:12 +0000340A. Yes, see <URL:http://www.python.org/> (Python's home page). It
341contains pointers to hypertext versions of the whole documentation set
342(as hypertext, not just PostScript).
Guido van Rossuma7925f11994-01-26 10:20:16 +0000343
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000344If you wish to browse this collection of HTML files on your own
345machine, it is available bundled up by anonymous ftp,
Guido van Rossum7be34a81995-05-31 15:17:12 +0000346e.g. <URL:ftp://ftp.python.org/pub/python/doc/html.tar.gz>.
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000347
348An Emacs-INFO set containing the library manual is also available by
Guido van Rossum7be34a81995-05-31 15:17:12 +0000349ftp, e.g. <URL:ftp://ftp.python.org/pub/python/doc/lib-info.tar.gz>.
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000350
3511.9. Q. Is there a book on Python, or will there be one out soon?
352
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000353A. Mark Lutz is writing a Python book for O'Reilly and Associates, to
354be published early 1996. See the outline (in PostScript):
355<URL:http://www.python.org/workshops/1995-05/outlinep.eps>.
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000356
3571.10. Q. Are there any published articles about Python that I can quote?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000358
359A. So far the only refereed and published article that describes
360Python in some detail is:
361
362 Guido van Rossum and Jelke de Boer, "Interactively Testing Remote
363 Servers Using the Python Programming Language", CWI Quarterly, Volume
364 4, Issue 4 (December 1991), Amsterdam, pp 283-303.
365
366LaTeX source for this paper is available as part of the Python source
367distribution.
368
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00003691.11. Q. Are there short introductory papers or talks on Python?
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000370
371A. A recent high-level description of Python is:
Guido van Rossuma7925f11994-01-26 10:20:16 +0000372
373 Guido van Rossum, "An Introduction to Python for UNIX/C
374 Programmers", in the proceedings of the NLUUG najaarsconferentie
Guido van Rossum061f1821994-10-06 16:03:45 +0000375 1993 (dutch UNIX users group meeting November 1993).
Guido van Rossuma7925f11994-01-26 10:20:16 +0000376
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000377PostScript for this paper and for the slides used for the accompanying
378presentation is available by ftp as
Guido van Rossum7be34a81995-05-31 15:17:12 +0000379<URL:ftp://ftp.python.org/pub/python/doc/nluug-paper.ps> and
380<URL:ftp://ftp.python.org/pub/python/doc/nluug-slides.ps>, respectively.
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000381
382Slides for a talk on Python that I gave at the Usenix Symposium on
383Very High Level Languages in Santa Fe, NM, USA in October 1995 are
Guido van Rossum7be34a81995-05-31 15:17:12 +0000384available as <URL:ftp://ftp.python.org/pub/python/doc/vhll-slides.ps>.
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000385
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00003861.12. Q. How does the Python version numbering scheme work?
Guido van Rossum95f61a71994-01-26 17:23:37 +0000387
388A. Python versions are numbered A.B.C. A is the major version number
389-- it is only incremented for major changes in functionality or source
390structure. B is the minor version number, incremented for less
391earth-shattering changes to a release. C is the patchlevel -- it is
392incremented for each new release. Note that in the past, patches have
393added significant changes; in fact the changeover from 0.9.9 to 1.0.0
394was the first time that either A or B changed!
395
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000396Beta versions have an additional suffix of "-beta-N" for some small
397number N. Note that (for instance) all versions labeled 1.2-beta-N
398*precede* the actual release of 1.2.
399
4001.13. Q. How do I get a beta test version of Python?
401
402A. Write me. I might like you :-) Seriously, anybody who has
403previously used and installed Python can become a beta tester, but I
404expect feedback, so you have to write me first before I divulge the
405secret location of the latest beta release.
406
4071.14. Q. Are there other ftp sites that carry Python related material?
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000408
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000409A. An interesting ftp site for Python users is ftp.markv.com; the
410directory pub/python contains a growing collection of interesting
411Python scripts <URL:ftp://ftp.markv.com/pub/python/>. To submit a
412script for inclusion, place it together with a readme file (with
413extension .readme) in the publicly writable directory
414/incoming/python. This service is maintained by Lance Ellinghaus
415<lance@markv.com>. (I've heard complaints about this service not
416being very responsive -- try at your own risk.)
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000417
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00004181.15. Q. Are there copyright restrictions on the use of Python?
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000419
420A. Hardly. You can do anything you want with the source, as long as
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000421you leave the copyrights in, and display those copyrights in any
422documentation about Python that you produce. Also, don't use the
423author's institute's name in publicity without prior written
424permission, and don't hold them responsible for anything (read the
425actual copyright for a precise legal wording).
426
427In particular, if you honor the copyright rules, it's OK to use Python
428for commercial use, to sell copies of Python in source or binary form,
429or to sell products that enhance Python or incorporate Python (or part
430of it) in some form. I would still like to know about all commercial
431use of Python!
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000432
Guido van Rossum7be34a81995-05-31 15:17:12 +00004331.16. Q. Why was Python created in the first place?
434
435A. Here's a *very* brief summary of what got me started:
436
437- I had extensive experience with implementing an interpreted language
438in the ABC group at CWI, and from working with this group I had
439learned a lot about language design. This is the origin of many
440Python features, including the use of indentation for statement
441grouping and the inclusion of very-high-level data types (although the
442details are all different in Python).
443
444- I had a number of gripes about the ABC language, but also liked many
445of its features. It was impossible to extend the ABC language (or its
446implementation) to remedy my complaints -- in fact its lack of
447extensibility was one of its biggest problems.
448
449- I had some experience with using Modula-2+ and talked with the
450designers of Modula-3 (and read the M3 report). M3 is the origin of
451the syntax and semantics used for exceptions, and some other Python
452features.
453
454- I was working in the Amoeba distributed operating system group at
455CWI. We needed a better way to do system administration than by
456writing either C programs or Bourne shell scripts, since Amoeba had
457its own system call interface which wasn't easily accessible from the
Guido van Rossumbf8e7d51995-08-28 03:09:13 +0000458Bourne shell. My experience with error handling in Amoeba made me
459acutely aware of the importance of exceptions as a programming
460language feature.
Guido van Rossum7be34a81995-05-31 15:17:12 +0000461
462- It occurred to me that a scripting language with a syntax like ABC
463but with access to the Amoeba system calls would fill the need. I
464realized that it would be foolish to write an Amoeba-specific
465language, so I decided that I needed a language that was generally
466extensible.
467
468- During the 1989 Christmas holidays, I had a lot of time on my hand,
469so I decided to give it a try. During the next year, while still
470mostly working on it in my own time, Python was used in the Amoeba
471project with increasing success, and the feedback from colleagues made
472me add many early improvements.
473
474- In February 1991, after just over a year of development, I decided
475to post to USENET. The rest is in the Misc/HISTORY file.
476
4771.17. Q. What happened to Tim Peters (the author of python-mode.el, and many
478entertaining and enlightening pieces of Python email)?
479
480A. He worked at KSR (Kendall Square Research, a start-up building a
481new kind of massively parallel processor). When KSR folded down a
482couple of years ago, Tim lost his email access. He hasn't surfaced
483on the net since then.
484
485 Missing-him-too-ly yours...
486
Guido van Rossuma7925f11994-01-26 10:20:16 +0000487
4882. Python in the real world
489===========================
490
4912.1. Q. How many people are using Python?
492
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000493A. I don't know, but the maximum number of simultaneous subscriptions
494to the Python mailing list before it was gatewayed into the newsgroup
495was about 180 (several of which were local redistribution lists). I
496believe that many active Python users don't bother to subscribe to the
497list, and now that there's a newsgroup the mailing list subscription
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000498is even less meaningful. I see new names on the newsgroup all the
499time and my best guess is that there are currently at least several
500thousands of users.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000501
5022.2. Q. Have any significant projects been done in Python?
503
504A. Here at CWI (the home of Python), we have written a 20,000 line
505authoring environment for transportable hypermedia presentations, a
Guido van Rossum5333c5d1994-04-11 11:06:22 +00005065,000 line multimedia teleconferencing tool, as well as many many
507smaller programs.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000508
509The University of Virginia uses Python to control a virtual reality
510engine. Contact: Matt Conway <conway@virginia.edu>.
511
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000512The ILU project at Xerox PARC can generate Python glue for ILU
513interfaces. See <URL:ftp://ftp.parc.xerox.com/pub/ilu/ilu.html>.
514
Guido van Rossumac3f2121995-04-10 11:53:42 +0000515The University of California, Irvine uses a student administration
516system called TELE-Vision written entirely in Python. Contact: Ray
517Price <rlprice@uci.edu>.
518
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000519If you have done a significant project in Python that you'd like to be
520included in the list above, send me email!
Guido van Rossuma7925f11994-01-26 10:20:16 +0000521
5222.3. Q. Are there any commercial projects going on using Python?
523
524A. Several companies have revealed to me that they are planning or
Guido van Rossum796b2591995-01-20 23:05:52 +0000525considering use of Python in a future product.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000526
Guido van Rossumbf8e7d51995-08-28 03:09:13 +0000527Sunrise Software has a product out using Python -- they use Python
Guido van Rossumac3f2121995-04-10 11:53:42 +0000528for a GUI management application and an SNMP network management
529application. Contact: <info@sunrise.com>.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000530
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000531Infoseek uses Python to implement their commercial WWW information
532retrieval service <URL:http://www.infoseek.com/>. Contact:
533<info@infoseek.com>.
Guido van Rossumf8c76d01994-08-17 12:19:53 +0000534
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000535Michael Powers of daVinci Time & Space is "writing tons-o-python for
536interactive television entertainment." Contact: <powers@dvts.com>.
537
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000538Paul Everitt of Connecting Minds is planning a Lotus Notes gateway.
539Contact: <Paul.Everitt@cminds.com>. Or see their WWW server
540<URL:http://www.cminds.com/>.
541
Guido van Rossumac3f2121995-04-10 11:53:42 +0000542KaPRE in Boulder, CO is using Python for on-site customization of C++
543applications, rapid-prototyping/development,
544language-based-components, and possibly more. This is pretty solid:
545Python's being shipped with their tool-set now, to beta sites.
546Contact: <lutz@KaPRE.COM> (Mark Lutz).
547
548Individuals at many other companies are using Python for internal
549development or for as yet unannounced products (witness their
550contributions to the Python mailing list or newsgroup).
Guido van Rossuma7925f11994-01-26 10:20:16 +0000551
552Python has also been elected as an extension language by MADE, a
553consortium supported by the European Committee's ESPRIT program and
554consisting of Bull, CWI and some other European companies. Contact:
555Ivan Herman <ivan@cwi.nl>.
556
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000557If you'd like to be included in the list above, send me email!
558
Guido van Rossum95f61a71994-01-26 17:23:37 +00005592.4. Q. How stable is Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000560
Guido van Rossum3de27361994-07-25 14:19:33 +0000561A. Very stable. While the current version number would suggest it is
562in the early stages of development, in fact new, stable releases
Guido van Rossumbf8e7d51995-08-28 03:09:13 +0000563(numbered 0.9.x through 1.2) have been coming out roughly every 3 to
Guido van Rossuma6c707c1995-01-02 17:32:28 +00005646 months for the past four years.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000565
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00005662.5. Q. When will the next version be released?
567
Guido van Rossumbf8e7d51995-08-28 03:09:13 +0000568A. I am planning to release 1.3 in September or early October 1995.
569It will contain keyword parameters as the most important new language
570feature. Beta versions may be available as early as August.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000571
5722.6. Q. What new developments are expected for Python in the future?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000573
Guido van Rossum7be34a81995-05-31 15:17:12 +0000574A. See my Work-In-Progress web page, currently at
575<URL:http://www.python.org:~guido/WIP.html>, and the pages for the
576Second Python Workshop (best reached via the Python home page,
577<URL:http://www.python.org/>).
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000578
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00005792.7. Q. Is it reasonable to propose incompatible changes to Python?
Guido van Rossum3de27361994-07-25 14:19:33 +0000580
581A. In general, no. There are already millions of lines of Python code
582around the world, so any changes in the language that invalidates more
583than a very small fraction of existing programs has to be frowned
584upon. Even if you can provide a conversion program, there still is
585the problem of updating all documentation. Providing a gradual
586upgrade path is the only way if a feature has to be changed.
587
Guido van Rossuma7925f11994-01-26 10:20:16 +0000588
5893. Building Python
590==================
591
Guido van Rossum91f60831994-02-15 15:52:27 +00005923.1. Q. Is there a test set?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000593
594A. Yes, simply do "import testall" (or "import autotest" if you aren't
595interested in the output). The standard modules whose name begins
596with "test" together comprise the test. The test set doesn't test
597*all* features of Python but it goes a long way to confirm that a new
598port is actually working. The Makefile contains an entry "make test"
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000599which runs the autotest module. NOTE: if "make test" fails, run the
600tests manually ("import testall") to see what goes wrong before
601reporting the error.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000602
Guido van Rossum91f60831994-02-15 15:52:27 +00006033.2. Q. When running the test set, I get complaints about floating point
Guido van Rossuma7925f11994-01-26 10:20:16 +0000604operations, but when playing with floating point operations I cannot
605find anything wrong with them.
606
607A. The test set makes occasional unwarranted assumptions about the
608semantics of C floating point operations. Until someone donates a
609better floating point test set, you will have to comment out the
610offending floating point tests and execute similar tests manually.
611
Guido van Rossum7be34a81995-05-31 15:17:12 +00006123.3. Q. Link errors after rerunning the configure script.
Guido van Rossum24349991994-02-02 14:12:45 +0000613
614A. It is generally necessary to run "make clean" after a configuration
615change.
616
Guido van Rossum7be34a81995-05-31 15:17:12 +00006173.4. Q. The python interpreter complains about options passed to a
Guido van Rossum24349991994-02-02 14:12:45 +0000618script (after the script name).
619
620A. You are probably linking with GNU getopt, e.g. through -liberty.
Guido van Rossumf8c76d01994-08-17 12:19:53 +0000621Don't. The reason for the complaint is that GNU getopt, unlike System
622V getopt and other getopt implementations, doesn't consider a
623non-option to be the end of the option list. A quick (and compatible)
624fix for scripts is to add "--" to the interpreter, like this:
625
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000626 #! /usr/local/bin/python --
Guido van Rossumf8c76d01994-08-17 12:19:53 +0000627
628You can also use this interactively:
629
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000630 python -- script.py [options]
631
632Note that a working getopt implementation is provided in the Python
633distribution (in Python/getopt.c) but not automatically used.
Guido van Rossum24349991994-02-02 14:12:45 +0000634
Guido van Rossum7be34a81995-05-31 15:17:12 +00006353.5. Q. When building on the SGI, make tries to run python to create
Guido van Rossum24349991994-02-02 14:12:45 +0000636glmodule.c, but python hasn't been built or installed yet.
637
638A. Comment out the line mentioning glmodule.c in Setup and build a
639python without gl first; install it or make sure it is in your $PATH,
640then edit the Setup file again to turn on the gl module, and make
641again. You don't need to do "make clean"; you do need to run "make
642Makefile" in the Modules subdirectory (or just run "make" at the
643toplevel).
644
Guido van Rossum7be34a81995-05-31 15:17:12 +00006453.6. Q. Python built with gcc for the DEC Alpha doesn't work.
Guido van Rossum3de27361994-07-25 14:19:33 +0000646
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000647People have reported problems with gcc 2.5.8 up to 2.6.3. The DEC
648OSF/1 cc compiler does not have these problems so it's likely a gcc
Guido van Rossume530c581995-04-10 12:32:16 +0000649bug. The latest news is that this has been fixed in Python 1.2 by a
650source change (I gave up waiting for a fixed gcc).
Guido van Rossum3de27361994-07-25 14:19:33 +0000651
Guido van Rossum7be34a81995-05-31 15:17:12 +00006523.7. Q. I use VPATH but some targets are built in the source directory.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000653
654A. On some systems (e.g. Sun), if the target already exists in the
655source directory, it is created there instead of in the build
656directory. This is usually because you have previously built without
657VPATH. Try running "make clobber" in the source directory.
658
Guido van Rossum7be34a81995-05-31 15:17:12 +00006593.8. Q. Trouble building or linking with the GNU readline library.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000660
Guido van Rossumac3f2121995-04-10 11:53:42 +0000661A. Consider using readline 2.0. Some hints:
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000662
663- You can use the GNU readline library to improve the interactive user
664interface: this gives you line editing and command history when
Guido van Rossum796b2591995-01-20 23:05:52 +0000665calling python interactively. You need to configure and build the GNU
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000666readline library before running the configure script. Its sources are
667no longer distributed with Python; you can ftp them from any GNU
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000668mirror site, or from its home site
669<URL:ftp://slc2.ins.cwru.edu/pub/dist/readline-2.0.tar.gz> (or a
670higher version number -- using version 1.x is not recommended). Pass
671the Python configure script the option --with-readline=DIRECTORY where
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000672DIRECTORY is the absolute pathname of the directory where you've built
673the readline library. Some hints on building and using the readline
674library:
675
676- On SGI IRIX 5, you may have to add the following
677to rldefs.h:
678
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000679 #ifndef sigmask
680 #define sigmask(sig) (1L << ((sig)-1))
681 #endif
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000682
683- On most systems, you will have to add #include "rldefs.h" to the
684top of several source files, and if you use the VPATH feature, you
685will have to add dependencies of the form foo.o: foo.c to the
686Makefile for several values of foo.
687
688- The readline library requires use of the termcap library. A
689known problem with this is that it contains entry points which
Guido van Rossum061f1821994-10-06 16:03:45 +0000690cause conflicts with the STDWIN and SGI GL libraries. The STDWIN
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000691conflict can be solved (and will be, in the next release of
Guido van Rossum061f1821994-10-06 16:03:45 +0000692STDWIN) by adding a line saying '#define werase w_erase' to the
693stdwin.h file (in the STDWIN distribution, subdirectory H). The
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000694GL conflict has been solved in the Python configure script by a
695hack that forces use of the static version of the termcap library.
696
Guido van Rossumac3f2121995-04-10 11:53:42 +0000697- Check the newsgroup gnu.bash.bug <URL:news:gnu.bash.bug> for
698specific problems with the readline library (I don't read this group
699but I've been told that it is the place for readline bugs).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000700
Guido van Rossum7be34a81995-05-31 15:17:12 +00007013.9. Q. Trouble building Python on Linux.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000702
Guido van Rossumac3f2121995-04-10 11:53:42 +0000703A. If you're building Python 1.2, Slackware 2.2 has a buggy bash
704(version 1.14.3) which breaks a sed script that is used to build
705Modules/Makefile. Replace /bin/sh with /bin/ash in both makesetup and
706Makefile.pre.in.
707
708In 1.1 and 1.1.1, there's a bug in the reference counting logic of
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000709ternary pow() which is only tripped by very picky mallocs, like the
710GNU malloc on Linux. This has been fixed in 1.2. To continue the
Guido van Rossumac3f2121995-04-10 11:53:42 +0000711tests in 1.1(.1), just disable the tests of pow() with three arguments
712from Lib/test/test_b2.py.
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000713
Guido van Rossumac3f2121995-04-10 11:53:42 +0000714Apart from this, Python builds and runs fine on most Linux versions
715(if you run into trouble on an old Linux version, consider upgrading).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000716
Guido van Rossum7be34a81995-05-31 15:17:12 +00007173.10. Q. Trouble with prototypes on Ultrix.
Guido van Rossum72eb83c1994-10-07 11:33:28 +0000718
719A. Ultrix cc seems broken -- use gcc, or edit config.h to #undef
720HAVE_PROTOTYPES.
721
Guido van Rossum7be34a81995-05-31 15:17:12 +00007223.11. Q. Trouble with posix.listdir on NeXTSTEP 3.2.
Guido van Rossum9351fdb1994-11-10 23:03:51 +0000723
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000724A. (This often manifests itself as a weird error from the
725compileall.py script run by "make libinstall".)
Guido van Rossum9351fdb1994-11-10 23:03:51 +0000726
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000727Don't use gcc, use the Next C compiler (cc). Even though it is
728derived from (an old version of) gcc, its interpretation of the
729"-posix" switch is different; in this particular case, cc is right and
730gcc is wrong.
Guido van Rossum9351fdb1994-11-10 23:03:51 +0000731
Guido van Rossum7be34a81995-05-31 15:17:12 +00007323.12. Q. Other trouble building Python on platform X.
Guido van Rossum95f61a71994-01-26 17:23:37 +0000733
Guido van Rossum796b2591995-01-20 23:05:52 +0000734A. Please email the details to <guido@cwi.nl> and I'll look into it.
735Please provide as many details as possible. In particular, if you
736don't tell me what type of computer and what operating system (and
737version) you are using it will be difficult for me to figure out what
738is the matter. If you get a specific error message, please email it
739to me too.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000740
Guido van Rossum7be34a81995-05-31 15:17:12 +00007413.13. Q. How to configure dynamic loading on Lixux.
Guido van Rossum07779351995-02-07 16:59:56 +0000742
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000743A. There are two answers, depending on whether you are using the new
744ELF object format or not.
Guido van Rossum07779351995-02-07 16:59:56 +0000745
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000746For ELF, this seems to be the procedure (thanks to Martin von Loewis):
Guido van Rossum07779351995-02-07 16:59:56 +0000747
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000748Compile Python to an ELF binary.
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000749
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000750In addition, you have to use the following flags:
Guido van Rossum07779351995-02-07 16:59:56 +0000751
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000752 - when linking python: -rdynamic -ldl
753 - when compiling an object that goes into a shared module: -fPIC
754 - when linking a shared module: -shared -ldl
Guido van Rossum07779351995-02-07 16:59:56 +0000755
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000756Furthermore, it appears that some Python releases did not understand
757that Linux has dynamic linking. Python 1.2 did it right, but you
758should check wether the generated config.h indicates the use of -ldl
759(i.e. dlopen, dlsym). Finally, you can load a shared module by saying
760'use foo'. Make sure the module is in your PYTHONPATH.
Guido van Rossum07779351995-02-07 16:59:56 +0000761
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000762For pre-ELF systems (thanks to Andrew Kuchling):
Guido van Rossum07779351995-02-07 16:59:56 +0000763
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000764Pre-ELF Linux requires that you use the GNU DLD library. The stages
765of using dynamic libraries on Linux are:
766
767 1) Get dld 3.2.5 from a Linux site. Be careful here; the most
768 recent GNU version is 3.2.3, and doesn't support Linux; be sure to
769 get it from a Linux mirror, not a GNU mirror (3.2.4 should also
770 work). Compile it and install the library libdld.a somewhere; I
771 used /usr/local/lib.
772
773 Suitable URLs for the dld distribution are currently:
774 <URL:ftp://sunsite.unc.edu/pub/Linux/libs/dld-3.2.5.src.tar.gz> and
775 <URL:ftp://tsx-11.mit.edu/pub/linux/sources/libs/dld-3.2.5.src.tar.gz>.
776 There's also a binary distribution of it:
777 <URL:ftp://sunsite.unc.edu/pub/Linux/libs/dld-3.2.5.bin.tar.gz>.
778
779 2) Get Jack Jansen's DL library; its location is given in the
780 _Extending Python_ manual as <URL:ftp://ftp.cwi.nl/pub/dynload/>.
781 Compile it and install libdl.a wherever you put libdld.a.
782
783 3) Run Python's configure script, giving it the --with-dl-dld option,
784 which requires a parameter giving the directory where you put the
785 libraries.
786
787 4) Recompile Python.
788
7893.14. Q. Under Solaris 2.x, using GCC, how do I use shared libraries?
790
791A. Use the linker in /usr/ucb/ld, not the GNU linker. The latter
792cannot create shared libraries.
793
7943.15. Q. Errors when linking with a shared library containing C++ code.
795
796A. Link the main Python binary with C++. Change the definition of
797LINKCC in Modules/Makefile to be your C++ compiler. You may have to
798edit config.c slightly to make it compilable with C++.
Guido van Rossum07779351995-02-07 16:59:56 +0000799
Guido van Rossuma7925f11994-01-26 10:20:16 +0000800
8014. Programming in Python
802========================
803
Guido van Rossum24349991994-02-02 14:12:45 +00008044.1. Q. Is there a source code level debugger with breakpoints, step,
805etc.?
806
807A. Yes. Check out module pdb; pdb.help() prints the documentation (or
808you can read it as Lib/pdb.doc). If you use the STDWIN option,
809there's also a windowing interface, wdb. You can write your own
810debugger by using the code for pdb or wdb as an example.
811
8124.2. Q. Can I create an object class with some methods implemented in
Guido van Rossuma7925f11994-01-26 10:20:16 +0000813C and others in Python (e.g. through inheritance)? (Also phrased as:
814Can I use a built-in type as base class?)
815
816A. No, but you can easily create a Python class which serves as a
817wrapper around a built-in object, e.g. (for dictionaries):
818
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000819 # A user-defined class behaving almost identical
820 # to a built-in dictionary.
821 class UserDict:
822 def __init__(self): self.data = {}
823 def __repr__(self): return repr(self.data)
824 def __cmp__(self, dict):
825 if type(dict) == type(self.data):
826 return cmp(self.data, dict)
827 else:
828 return cmp(self.data, dict.data)
829 def __len__(self): return len(self.data)
830 def __getitem__(self, key): return self.data[key]
831 def __setitem__(self, key, item): self.data[key] = item
832 def __delitem__(self, key): del self.data[key]
833 def keys(self): return self.data.keys()
834 def items(self): return self.data.items()
835 def values(self): return self.data.values()
836 def has_key(self, key): return self.data.has_key(key)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000837
Guido van Rossum24349991994-02-02 14:12:45 +00008384.3. Q. Is there a curses/termcap package for Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000839
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000840A. Yes -- Lance Ellinghaus has written a module that interfaces to
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000841System V's "ncurses". If you know a little curses and some Python,
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000842it's straightforward to use. It is part of the standard Python
843distribution, but not configured by default -- you must enable it by
844editing Modules/Setup. It requires a System V curses implementation.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000845
846You could also consider using the "alfa" (== character cell) version
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000847of STDWIN. (Standard Window System Interface, a portable windowing
848system interface by myself <URL:ftp://ftp.cwi.nl/pub/stdwin/>.) This
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000849will also prepare your program for porting to windowing environments
850such as X11 or the Macintosh.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000851
Guido van Rossum24349991994-02-02 14:12:45 +00008524.4. Q. Is there an equivalent to C's onexit() in Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000853
854A. Yes, if you import sys and assign a function to sys.exitfunc, it
855will be called when your program exits, is killed by an unhandled
856exception, or (on UNIX) receives a SIGHUP or SIGTERM signal.
857
Guido van Rossum24349991994-02-02 14:12:45 +00008584.5. Q. When I define a function nested inside another function, the
Guido van Rossuma7925f11994-01-26 10:20:16 +0000859nested function seemingly can't access the local variables of the
860outer function. What is going on? How do I pass local data to a
861nested function?
862
863A. Python does not have arbitrarily nested scopes. When you need to
864create a function that needs to access some data which you have
865available locally, create a new class to hold the data and return a
866method of an instance of that class, e.g.:
867
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000868 class MultiplierClass:
869 def __init__(self, factor):
870 self.factor = factor
871 def multiplier(self, argument):
872 return argument * self.factor
Guido van Rossuma7925f11994-01-26 10:20:16 +0000873
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000874 def generate_multiplier(factor):
875 return MultiplierClass(factor).multiplier
Guido van Rossuma7925f11994-01-26 10:20:16 +0000876
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000877 twice = generate_multiplier(2)
878 print twice(10)
879 # Output: 20
880
881An alternative solution uses default arguments, e.g.:
882
883 def generate_multiplier(factor):
884 def multiplier(arg, fact = factor):
885 return arg*fact
886 return multiplier
887
888 twice = generate_multiplier(2)
889 print twice(10)
890 # Output: 20
Guido van Rossuma7925f11994-01-26 10:20:16 +0000891
Guido van Rossum24349991994-02-02 14:12:45 +00008924.6. Q. How do I iterate over a sequence in reverse order?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000893
894A. If it is a list, the fastest solution is
895
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000896 list.reverse()
897 try:
898 for x in list:
899 "do something with x"
900 finally:
901 list.reverse()
Guido van Rossuma7925f11994-01-26 10:20:16 +0000902
903This has the disadvantage that while you are in the loop, the list
904is temporarily reversed. If you don't like this, you can make a copy.
905This appears expensive but is actually faster than other solutions:
906
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000907 rev = list[:]
908 rev.reverse()
909 for x in rev:
910 <do something with x>
Guido van Rossuma7925f11994-01-26 10:20:16 +0000911
912If it isn't a list, a more general but slower solution is:
913
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000914 i = len(list)
915 while i > 0:
916 i = i-1
917 x = list[i]
918 <do something with x>
Guido van Rossuma7925f11994-01-26 10:20:16 +0000919
920A more elegant solution, is to define a class which acts as a sequence
921and yields the elements in reverse order (solution due to Steve
922Majewski):
923
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000924 class Rev:
925 def __init__(self, seq):
926 self.forw = seq
927 def __len__(self):
928 return len(self.forw)
929 def __getitem__(self, i):
930 return self.forw[-(i + 1)]
Guido van Rossuma7925f11994-01-26 10:20:16 +0000931
932You can now simply write:
933
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000934 for x in Rev(list):
935 <do something with x>
Guido van Rossuma7925f11994-01-26 10:20:16 +0000936
Guido van Rossum061f1821994-10-06 16:03:45 +0000937Unfortunately, this solution is slowest of all, due to the method
Guido van Rossuma7925f11994-01-26 10:20:16 +0000938call overhead...
939
Guido van Rossum24349991994-02-02 14:12:45 +00009404.7. Q. My program is too slow. How do I speed it up?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000941
942A. That's a tough one, in general. There are many tricks to speed up
943Python code; I would consider rewriting parts in C only as a last
944resort. One thing to notice is that function and (especially) method
945calls are rather expensive; if you have designed a purely OO interface
946with lots of tiny functions that don't do much more than get or set an
947instance variable or call another method, you may consider using a
948more direct way, e.g. directly accessing instance variables. Also see
949the standard module "profile" (described in the file
950"python/lib/profile.doc") which makes it possible to find out where
951your program is spending most of its time (if you have some patience
952-- the profiling itself can slow your program down by an order of
953magnitude).
954
Guido van Rossum24349991994-02-02 14:12:45 +00009554.8. Q. When I have imported a module, then edit it, and import it
Guido van Rossuma7925f11994-01-26 10:20:16 +0000956again (into the same Python process), the changes don't seem to take
957place. What is going on?
958
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000959A. For reasons of efficiency as well as consistency, Python only reads
Guido van Rossum796b2591995-01-20 23:05:52 +0000960the module file on the first time a module is imported. (Otherwise a
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000961program consisting of many modules, each of which imports the same
Guido van Rossum796b2591995-01-20 23:05:52 +0000962basic module, would read the basic module over and over again.) To
963force rereading of a changed module, do this:
Guido van Rossuma7925f11994-01-26 10:20:16 +0000964
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000965 import modname
966 reload(modname)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000967
968Warning: this technique is not 100% fool-proof. In particular,
969modules containing statements like
970
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000971 from modname import some_objects
Guido van Rossuma7925f11994-01-26 10:20:16 +0000972
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000973will continue to work with the old version of the imported objects.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000974
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00009754.9. Q. How do I find the current module name?
Guido van Rossum24349991994-02-02 14:12:45 +0000976
Guido van Rossum061f1821994-10-06 16:03:45 +0000977A. A module can find out its own module name by looking at the
Guido van Rossum24349991994-02-02 14:12:45 +0000978(predefined) global variable __name__. If this has the value
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000979'__main__' you are running as a script.
980
Guido van Rossuma6c707c1995-01-02 17:32:28 +00009814.10. Q. I have a module in which I want to execute some extra code
982when it is run as a script. How do I find out whether I am running as
983a script?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000984
985A. See the previous question. E.g. if you put the following on the
986last line of your module, main() is called only when your module is
987running as a script:
Guido van Rossum24349991994-02-02 14:12:45 +0000988
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000989 if __name__ == '__main__': main()
Guido van Rossum24349991994-02-02 14:12:45 +0000990
Guido van Rossuma6c707c1995-01-02 17:32:28 +00009914.11. Q. I try to run a program from the Demo directory but it fails
992with ImportError: No module named ...; what gives?
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000993
Guido van Rossum061f1821994-10-06 16:03:45 +0000994A. This is probably an optional module (written in C!) which hasn't
995been configured on your system. This especially happens with modules
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000996like "Tkinter", "stdwin", "gl", "Xt" or "Xm". For Tkinter, STDWIN and
997many other modules, see Modules/Setup.in for info on how to add these
998modules to your Python, if it is possible at all. Sometimes you will
999have to ftp and build another package first (e.g. STDWIN). Sometimes
1000the module only works on specific platforms (e.g. gl only works on SGI
1001machines).
1002
1003NOTE: if the complaint is about "Tkinter" (upper case T) and you have
1004already configured module "tkinter" (lower case t), the solution is
1005*not* to rename tkinter to Tkinter or vice versa. There is probably
Guido van Rossum796b2591995-01-20 23:05:52 +00001006something wrong with your module search path. Check out the value of
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001007sys.path.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001008
Guido van Rossum061f1821994-10-06 16:03:45 +00001009For X-related modules (Xt and Xm) you will have to do more work: they
1010are currently not part of the standard Python distribution. You will
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001011have to ftp the Extensions tar file, e.g.
Guido van Rossum7be34a81995-05-31 15:17:12 +00001012<URL:ftp://ftp.python.org/pub/python/src/extensions.tar.gz> and follow
1013the instructions there.
Guido van Rossum061f1821994-10-06 16:03:45 +00001014
1015See also the next question.
1016
10174.12. Q. I have successfully built Python with STDWIN but it can't
1018find some modules (e.g. stdwinevents).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001019
1020A. There's a subdirectory of the library directory named 'stdwin'
1021which should be in the default module search path. There's a line in
1022Modules/Setup(.in) that you have to enable for this purpose --
Guido van Rossum061f1821994-10-06 16:03:45 +00001023unfortunately in the latest release it's not near the other
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001024STDWIN-related lines so it's easy to miss it.
1025
10264.13. Q. What GUI toolkits exist for Python?
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001027
1028A. Depending on what platform(s) you are aiming at, there are several.
1029
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001030- There's a neat object-oriented interface to the Tcl/Tk widget set,
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001031called Tkinter. As of python 1.1, it is part of the standard Python
1032distribution -- all you need to do is enable it in Modules/Setup
1033(provided you have already installed Tk and Tcl). This is probably
1034the easiest to install and use, and the most complete widget set. It
1035is also very likely that in the future the standard Python GUI API
1036will be based on or at least look very much like the Tkinter
1037interface. For more info about Tk, including pointers to the source,
Guido van Rossumbf8e7d51995-08-28 03:09:13 +00001038see the Tcl/Tk home page <URL:http://www.sunlabs.com/research/tcl/>.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001039
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001040- The standard Python distribution comes with an interface to STDWIN,
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001041a platform-independent low-level windowing interface. You have to ftp
1042the source for STDWIN separately,
1043e.g. <URL:ftp://ftp.cwi.nl/pub/stdwin/> or gatekeeper.dec.com in
1044pub/misc/stdwin <URL:ftp://gatekeeper.dec.com/pub/misc/stdwin/>.
1045STDWIN runs under X11 or the Mac; a Windows port has been attempted
1046but I can't seem to get it working. Note that STDWIN is really not
1047powerful enough to implement a modern GUI (no widgets, etc.) and that
1048I don't have the time to maintain or extend it, so you may be better
1049off using Tkinter or the Motif interface, unless you require
1050portability to the Mac (which is also offered by SUIT, by the way --
1051see below).
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001052
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001053- For SGI IRIX only, there's an interface to the complete GL (Graphics
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001054Library -- low level but very good 3D capabilities) as well as to
1055FORMS (a buttons-and-sliders-etc package built on top of GL by Mark
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001056Overmars -- ftp'able from <URL:ftp://ftp.cs.ruu.nl/pub/SGI/FORMS/>).
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001057
1058- There's an interface to X11, including the Athena and Motif widget
1059sets (and a few individual widgets, like Mosaic's HTML widget and
1060SGI's GL widget) in the Extensions set, which is separately ftp'able
Guido van Rossum7be34a81995-05-31 15:17:12 +00001061<URL:ftp://ftp.python.org/pub/python/src/extensions.tar.gz>.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001062
1063- There's an interface to SUIT, the U of Virginia's Simple User
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001064Interface Toolkit; it can be ftp'ed from
1065<URL:ftp://uvacs.cs.virginia.edu/pub/suit/python/SUIT_python.tar.Z>.
1066A PC binary of Python 1.0.2 compiled with DJGPP and with SUIT support
1067built-in has been made available by Antonio Costa
1068<URL:ftp://asterix.inescn.pt/pub/PC/python/pyt102su.exe> (a
1069self-extracting archive). Note that the UVa people themselves have
1070expressed doubts about SUIT, and are planning to build a Python GUI
1071API based upon Tk (though not necessarily on Tkinter); see
1072<URL:http://server.cs.virginia.edu/~tnb2d/IT/IT.html>.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001073
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001074- There's an interface to WAFE, a Tcl interface to the X11 Motif and
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001075Athena widget sets. Last I heard about it it was included in the WAFE
10761.0 prerelease
1077<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 +00001078
Guido van Rossumbf8e7d51995-08-28 03:09:13 +00001079- The NT port by Mark Hammond (see question 7.4) includes an interface
1080to the Microsoft Foundation Classes and a Python programming
1081environment using it that's written mostly in Python. See
1082<URL:ftp://ftp.python.org/pub/python/nt/>.
1083
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00001084- There's an interface to wxWindows. wxWindows is a portable GUI
1085class library written in C++. It supports XView, Motif, MS-Windows as
1086targets. There is some support for Macs and CURSES as well. wxWindows
1087preserves the look and feel of the underlying graphics toolkit. See
1088the wxPython WWW page at
1089<URL:http://www.aiai.ed.ac.uk/~jacs/wx/wxpython/wxpython.html>.
1090
Guido van Rossum3fc9d731995-07-25 15:10:56 +00001091- There's an object-oriented GUI based on the Microsoft Foundation
1092Classes model called WPY. Programs written in WPY run unchanged and
1093with native look and feel on NT, Windows 3.1 (using win32s) and on
1094Unix (using Tk). Source and binaries for NT and Linux are available
1095in <URL:ftp://ftp.python.org/pub/python/wpy>.
1096
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00001097- Python has been mentioned on the "Futurism" subpage of the Fresco
1098home page <URL:http://www.faslab.com/fresco/HomePage.html>. "Pesto"
1099is a Python interface to the CORBA dynamic invocation interface, and
1100thus Fresco. A Pesto prototype is running and is currently being
Guido van Rossum7be34a81995-05-31 15:17:12 +00001101packaged up for inclusion in the Fresco snapshot. See also the Pesto
1102web pages: <URL:http://www.faslab.com/fresco/pesto/Index.html>.
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00001103
Guido van Rossum061f1821994-10-06 16:03:45 +000011044.14. Q. Are there any interfaces to database packages in Python?
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001105
1106A. There's an interface to SYBASE by John Redford
1107<jredford@lehman.com>.
1108
Guido van Rossum796b2591995-01-20 23:05:52 +00001109There's an interface to metalbase by Lance Ellinghaus
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001110<lance@markv.com>; it is part of the separate Extensions distribution
Guido van Rossum7be34a81995-05-31 15:17:12 +00001111<URL:ftp://ftp.python.org/pub/python/src/extensions.tar.gz>.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001112
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001113Anthony Baxter <anthony.baxter@aaii.oz.au> has written an interface to
Guido van Rossume530c581995-04-10 12:32:16 +00001114mSQL (mini-SQL).
Guido van Rossum7be34a81995-05-31 15:17:12 +00001115<URL:ftp://ftp.python.org/pub/python/contrib/PymSQL.tar.gz>.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001116
Guido van Rossum7be34a81995-05-31 15:17:12 +00001117Tom Culliton <culliton@clark.net> has written an Oracle module.
1118See <URL:ftp://ftp.clark.net/pub/culliton/oramod.tgz>.
Guido van Rossum796b2591995-01-20 23:05:52 +00001119
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000011204.15. Q. Is it possible to write obfuscated one-liners in Python?
Guido van Rossumc24da7c1994-09-23 14:08:41 +00001121
1122A. Yes. See the following three examples, due to Ulf Bartelt:
1123
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001124 # Primes < 1000
1125 print filter(None,map(lambda y:y*reduce(lambda x,y:x*y!=0,
1126 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 +00001127
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001128 # First 10 Fibonacci numbers
1129 print map(lambda x,f=lambda x,f:(x<=1) or (f(x-1,f)+f(x-2,f)): f(x,f),
1130 range(10))
Guido van Rossumc24da7c1994-09-23 14:08:41 +00001131
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001132 # Mandelbrot set
1133 print (lambda Ru,Ro,Iu,Io,IM,Sx,Sy:reduce(lambda x,y:x+y,map(lambda y,
1134 Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,Sy=Sy,L=lambda yc,Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,i=IM,
1135 Sx=Sx,Sy=Sy:reduce(lambda x,y:x+y,map(lambda x,xc=Ru,yc=yc,Ru=Ru,Ro=Ro,
1136 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
1137 >=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(
1138 64+F(Ru+x*(Ro-Ru)/Sx,yc,0,0,i)),range(Sx))):L(Iu+y*(Io-Iu)/Sy),range(Sy
1139 ))))(-2.1, 0.7, -1.2, 1.2, 30, 80, 24)
1140 # \___ ___/ \___ ___/ | | |__ lines on screen
1141 # V V | |______ columns on screen
1142 # | | |__________ maximum of "iterations"
1143 # | |_________________ range on y axis
1144 # |____________________________ range on x axis
Guido van Rossumc24da7c1994-09-23 14:08:41 +00001145
1146Don't try this at home, kids!
1147
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000011484.16. Q. Is there an equivalent of C's "?:" ternary operator?
Guido van Rossumc24da7c1994-09-23 14:08:41 +00001149
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001150A. Not directly. In many cases you can mimic a?b:c with "a and b or
1151c", but there's a flaw: if b is zero (or empty, or None -- anything
1152that tests false) then c will be selected instead. In many cases you
1153can prove by looking at the code that this can't happen (e.g. because
1154b is a constant or has a type that can never be false), but in general
1155this can be a problem.
1156
1157Steve Majewski (or was it Tim Peters?) suggested the following
1158solution: (a and [b] or [c])[0]. Because [b] is a singleton list it
1159is never false, so the wrong path is never taken; then applying [0] to
1160the whole thing gets the b or c that you really wanted. Ugly, but it
1161gets you there in the rare cases where it is really inconvenient to
1162rewrite your code using 'if'.
1163
11644.17. Q. My class defines __del__ but it is not called when I delete the
1165object.
1166
1167A. There are several possible reasons for this.
1168
1169- The del statement does not necessarily call __del__ -- it simply
1170decrements the object's reference count, and if this reaches zero
1171__del__ is called.
1172
1173- If your data structures contain circular links (e.g. a tree where
1174each child has a parent pointer and each parent has a list of
1175children) the reference counts will never go back to zero. You'll
Guido van Rossum061f1821994-10-06 16:03:45 +00001176have to define an explicit close() method which removes those
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001177pointers. Please don't ever call __del__ directly -- __del__ should
Guido van Rossum061f1821994-10-06 16:03:45 +00001178call close() and close() should make sure that it can be called more
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001179than once for the same object.
1180
1181- If the object has ever been a local variable (or argument, which is
1182really the same thing) to a function that caught an expression in an
1183except clause, chances are that a reference to the object still exists
1184in that function's stack frame as contained in the stack trace.
1185Normally, deleting (better: assigning None to) sys.exc_traceback will
1186take care of this. If you a stack was printed for an unhandled
1187exception in an interactive interpreter, delete sys.last_traceback
1188instead.
1189
1190- There is code that deletes all objects when the interpreter exits,
1191but if your Python has been configured to support threads, it is not
1192called (because other threads may still be active). You can define
Guido van Rossum061f1821994-10-06 16:03:45 +00001193your own cleanup function using sys.exitfunc (see question 4.4).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001194
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001195- Finally, there are some obscure bugs if your __del__ method does
1196complicated things such as updating dictionaries or lists or
1197references globals. I hope to have fixed these in release 1.2.
1198
11994.18. Q. How do I change the shell environment for programs called
1200using os.popen() or os.system()? Changing os.environ doesn't work.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001201
1202A. Modifying the environment passed to subshells was left out of the
1203interpreter because there seemed to be no well-established portable
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001204way to do it (in particular, some systems, have putenv(), others have
1205setenv(), and some have none at all).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001206
1207However if all you want is to pass environment variables to the
1208commands run by os.system() or os.popen(), there's a simple solution:
1209prefix the command string with a couple of variable assignments and
Guido van Rossum796b2591995-01-20 23:05:52 +00001210export statements. The following would be universal for popen:
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001211
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001212 import os
1213 from commands import mkarg # nifty routine to add shell quoting
1214 def epopen(cmd, mode, env = {}):
1215 # env is a dictionary of environment variables
1216 prefix = ''
Guido van Rossum796b2591995-01-20 23:05:52 +00001217 for key, value in env.items():
1218 prefix = prefix + '%s=%s\n' % (key, mkarg(value)[1:])
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001219 prefix = prefix + 'export %s\n' % key
1220 return os.popen(prefix + cmd, mode)
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001221
12224.19. Q. What is a class?
1223
1224A. A class is the particular object type that is created by executing
Guido van Rossum796b2591995-01-20 23:05:52 +00001225a class statement. Class objects are used as templates, to create
1226class instance objects, which embody both the data structure and
1227program routines specific to a datatype.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001228
12294.20. Q. What is a method?
1230
1231A. A method is a function that you normally call as
Guido van Rossum796b2591995-01-20 23:05:52 +00001232x.name(arguments...) for some object x. The term is used for methods
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001233of classes and class instances as well as for methods of built-in
Guido van Rossum796b2591995-01-20 23:05:52 +00001234objects. (The latter have a completely different implementation and
1235only share the way their calls look in Python code.) Methods of
1236classes (and class instances) are defined as functions inside the
1237class definition.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001238
12394.21. Q. What is self?
1240
1241A. Self is merely a conventional name for the first argument of a
1242method -- i.e. a function defined inside a class definition. A method
1243defined as meth(self, a, b, c) should be called as x.meth(a, b, c) for
1244some instance x of the class in which the definition occurs;
1245the called method will think it is called as meth(x, a, b, c).
1246
12474.22. Q. What is a unbound method?
1248
1249A. An unbound method is a method defined in a class that is not yet
1250bound to an instance. You get an unbound method if you ask for a
1251class attribute that happens to be a function. You get a bound method
1252if you ask for an instance attribute. A bound method knows which
Guido van Rossum061f1821994-10-06 16:03:45 +00001253instance it belongs to and calling it supplies the instance automatically;
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001254an unbound method only knows which class it wants for its first
1255argument (a derived class is also OK). Calling an unbound method
1256doesn't "magically" derive the first argument from the context -- you
1257have to provide it explicitly.
1258
Guido van Rossuma6c707c1995-01-02 17:32:28 +000012594.23. Q. How do I call a method defined in a base class from a derived
1260class that overrides it?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001261
1262A. If your class definition starts with "class Derived(Base): ..."
1263then you can call method meth defined in Base (or one of Base's base
1264classes) as Base.meth(self, arguments...). Here, Base.meth is an
1265unbound method (see previous question).
1266
Guido van Rossuma6c707c1995-01-02 17:32:28 +000012674.24. Q. How do I call a method from a base class without using the
1268name of the base class?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001269
1270A. DON'T DO THIS. REALLY. I MEAN IT. It appears that you could call
1271self.__class__.__bases__[0].meth(self, arguments...) but this fails when
Guido van Rossum061f1821994-10-06 16:03:45 +00001272a doubly-derived method is derived from your class: for its instances,
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001273self.__class__.__bases__[0] is your class, not its base class -- so
1274(assuming you are doing this from within Derived.meth) you would start
1275a recursive call.
1276
12774.25. Q. How can I organize my code to make it easier to change the base
1278class?
1279
1280A. You could define an alias for the base class, assign the real base
1281class to it before your class definition, and use the alias throughout
1282your class. Then all you have to change is the value assigned to the
1283alias. Incidentally, this trick is also handy if you want to decide
1284dynamically (e.g. depending on availability of resources) which base
1285class to use. Example:
1286
1287BaseAlias = <real base class>
1288class Derived(BaseAlias):
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001289 def meth(self):
1290 BaseAlias.meth(self)
1291 ...
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001292
12934.26. Q. How can I find the methods or attributes of an object?
1294
1295A. This depends on the object type.
1296
1297For an instance x of a user-defined class, instance attributes are
1298found in the dictionary x.__dict__, and methods and attributes defined
1299by its class are found in x.__class__.__bases__[i].__dict__ (for i in
1300range(len(x.__class__.__bases__))). You'll have to walk the tree of
1301base classes to find *all* class methods and attributes.
1302
1303Many, but not all built-in types define a list of their method names
1304in x.__methods__, and if they have data attributes, their names may be
1305found in x.__members__. However this is only a convention.
1306
1307For more information, read the source of the standard (but
1308undocumented) module newdir.
1309
13104.27. Q. I can't seem to use os.read() on a pipe created with os.popen().
1311
1312A. os.read() is a low-level function which takes a file descriptor (a
1313small integer). os.popen() creates a high-level file object -- the
1314same type used for sys.std{in,out,err} and returned by the builtin
1315open() function. Thus, to read n bytes from a pipe p created with
1316os.popen(), you need to use p.read(n).
Guido van Rossuma7925f11994-01-26 10:20:16 +00001317
Guido van Rossum061f1821994-10-06 16:03:45 +000013184.28. Q. How can I create a stand-alone binary from a Python script?
1319
1320The demo script "Demo/scripts/freeze.py" does what you want. (It's
1321actually not a demo but a support tool -- there is some extra code in
1322the interpreter to accommodate it.) It requires that you have the
1323Python build tree handy, complete with all the lib*.a files.
1324
1325This works by scanning your source recursively for import statements
1326(both forms) and looking for the modules on the standard Python path
1327as well as in the source directory (for built-in modules). It then
1328"compiles" the modules written in Python to C code (array initializers
1329that can be turned into code objects using the marshal module) and
1330creates a custom-made config file that only contains those built-in
1331modules which are actually used in the program. It then compiles the
1332generated C code and links it with the rest of the Python interpreter
1333to form a self-contained binary which acts exactly like your script.
1334
Guido van Rossum7be34a81995-05-31 15:17:12 +00001335Hint: the freeze program only works if your script's filename ends in
1336".py".
Guido van Rossum061f1821994-10-06 16:03:45 +00001337
Guido van Rossum7be34a81995-05-31 15:17:12 +000013384.29. Q. What WWW tools are there for Python?
Guido van Rossum061f1821994-10-06 16:03:45 +00001339
Guido van Rossum7be34a81995-05-31 15:17:12 +00001340A. There's a whole lot in the 1.2 release; see the section on
1341"Internet and WWW" in the Library Reference Manual. Highlights: a CGI
1342parser, a url retriever, an html parser.
Guido van Rossum061f1821994-10-06 16:03:45 +00001343
1344Steve Miale <smiale@cs.indiana.edu> has written a modular WWW browser
1345called Dancer. An alpha version can be FTP'ed from
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001346<URL:ftp://ftp.cs.indiana.edu/pub/smiale/dancer.tar.gz>. (There are a
1347few articles about Dancer in the (hyper)mail archive
1348<URL:http://www.cwi.nl/~guido/hypermail/python-1994q3/index.html>.)
Guido van Rossum061f1821994-10-06 16:03:45 +00001349
Guido van Rossum7be34a81995-05-31 15:17:12 +000013504.30. Q. How do I run a subprocess with pipes connected to both input
Guido van Rossum796b2591995-01-20 23:05:52 +00001351and output?
1352
1353A. This is really a UNIX question. Also, in general, it is unwise to
1354do so, because you can easily cause a deadlock where the parent
1355process is blocked waiting for output from the child, while the child
1356is blocked waiting for input from the child. This can be caused
1357because the parent expects the child to output more text than it does,
1358or it can be caused by data being stuck in stdio buffers due to lack
1359of flushing. The Python parent can of course explicitly flush the data
1360it sends to the child before it reads any output, but if the child is
1361a naive C program it can easily have been written to never explicitly
1362flush its output, even if it is interactive, since flushing is
1363normally automatic.
1364
1365In many cases, all you really need is to run some data through a
1366command and get the result back. Unless the data is infinite in size,
1367the easiest (and often the most efficient!) way to do this is to write
1368it to a temporary file and run the command with that temporary file as
1369input. The standard module tempfile exports a function mktemp() which
1370generates unique temporary file names.
1371
1372If after reading all of the above you still want to connect two pipes
1373to a subprocess's standard input and output, here's a simple solution,
1374due to Jack Jansen:
1375
1376 import os
1377 import sys
1378 import string
1379
1380 MAXFD = 100 # Max number of file descriptors in this system
1381
1382 def popen2(cmd):
1383 cmd = string.split(cmd)
1384 p2cread, p2cwrite = os.pipe()
1385 c2pread, c2pwrite = os.pipe()
1386 pid = os.fork()
1387 if pid == 0:
1388 # Child
1389 os.close(0)
1390 os.close(1)
1391 if os.dup(p2cread) <> 0:
1392 sys.stderr.write('popen2: bad read dup\n')
1393 if os.dup(c2pwrite) <> 1:
1394 sys.stderr.write('popen2: bad write dup\n')
1395 for i in range(3, MAXFD):
1396 try:
1397 os.close(i)
1398 except:
1399 pass
1400 try:
1401 os.execv(cmd[0], cmd)
1402 finally:
1403 os._exit(1)
1404 os.close(p2cread)
1405 tochild = os.fdopen(p2cwrite, 'w')
1406 os.close(c2pwrite)
1407 fromchild = os.fdopen(c2pread, 'r')
1408 return fromchild, tochild
1409
1410Note that many interactive programs (e.g. vi) don't work well with
1411pipes substituted for standard input and output. You will have to use
1412pseudo ttys ("ptys") instead of pipes. There is some undocumented
1413code to use these in the library module pty.py -- I'm afraid you're on
Guido van Rossum3fc9d731995-07-25 15:10:56 +00001414your own here.
1415
1416A different answer is a Python interface to Don Libes' "expect"
1417library. A prerelease of this is available on the Python ftp mirror
1418sites in the contrib subdirectory as expy-0.3.tar.gz, e.g.
1419<URL:ftp://ftp.python.org/pub/python/contrib/expy-0.3.tar.gz>.
Guido van Rossum796b2591995-01-20 23:05:52 +00001420
Guido van Rossum7be34a81995-05-31 15:17:12 +000014214.31. Q. How do I call a function if I have the arguments in a tuple?
Guido van Rossumac3f2121995-04-10 11:53:42 +00001422
1423A. Use the built-in function apply(). For instance,
1424
1425 func(1, 2, 3)
1426
1427is equivalent to
1428
1429 args = (1, 2, 3)
1430 apply(func, args)
1431
1432Note that func(args) is not the same -- it calls func() with exactly
1433one argument, the tuple args, instead of three arguments, the integers
14341, 2 and 3.
1435
Guido van Rossum7be34a81995-05-31 15:17:12 +000014364.32. Q. How do I enable font-lock-mode for Python in Emacs?
1437
1438A. Assuming you're already using python-mode and font-lock-mode
1439separately, all you need to do is put this in your .emacs file:
1440
1441 (defun my-python-mode-hook ()
1442 (setq font-lock-keywords python-font-lock-keywords)
1443 (font-lock-mode 1))
1444 (add-hook 'python-mode-hook 'my-python-mode-hook)
1445
14464.33. Q. Is there an inverse to the format operator (a la C's scanf())?
1447
1448A. Not as such.
1449
1450For simple input parsing, the easiest approach is usually to split
1451the line into whitespace-delimited words using string.split(), and to
1452convert decimal strings to numeric values using string.atoi(),
1453string.atol() or string.atof(). (Python's atoi() is 32-bit and its
1454atol() is arbitrary precision.) If you want to use another delimiter
1455than whitespace, use string.splitfield() (possibly combining it with
1456string.strip() which removes surrounding whitespace from a string).
1457
1458For more complicated input parsing, regular expressions (see module
1459regex) are better suited and more powerful than C's scanf().
1460
Guido van Rossum061f1821994-10-06 16:03:45 +00001461
Guido van Rossuma7925f11994-01-26 10:20:16 +000014625. Extending Python
1463===================
1464
14655.1. Q. Can I create my own functions in C?
1466
1467A. Yes, you can create built-in modules containing functions,
Guido van Rossum24349991994-02-02 14:12:45 +00001468variables, exceptions and even new types in C. This is explained in
1469the document "Extending and Embedding the Python Interpreter" (the
1470LaTeX file Doc/ext.tex). Also read the chapter on dynamic loading.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001471
14725.2. Q. Can I create my own functions in C++?
1473
1474A. Yes, using the C-compatibility features found in C++. Basically
1475you place extern "C" { ... } around the Python include files and put
1476extern "C" before each function that is going to be called by the
1477Python interpreter. Global or static C++ objects with constructors
1478are probably not a good idea.
1479
Guido van Rossum7ce61c11994-06-13 15:13:56 +000014805.3. Q. How can I execute arbitrary Python statements from C?
1481
1482A. The highest-level function to do this is run_command() which takes
1483a single string argument which is executed in the context of module
1484__main__ and returns 0 for success and -1 when an exception occurred
1485(including SyntaxError). If you want more control, use run_string();
1486see the source for run_command() in Python/pythonrun.c.
1487
14885.4. Q. How can I evaluate an arbitrary Python expression from C?
1489
1490A. Call the function run_string() from the previous question with the
1491start symbol eval_input; it then parses an expression, evaluates it
1492and returns its value. See exec_eval() in Python/bltinmodule.c.
1493
14945.5. Q. How do I extract C values from a Python object?
1495
1496A. That depends on the object's type. If it's a tuple,
1497gettuplesize(o) returns its length and gettupleitem(o, i) returns its
1498i'th item; similar for lists with getlistsize(o) and getlistitem(o,
1499i). For strings, getstringsize(o) returns its length and
1500getstringvalue(o) a pointer to its value (note that Python strings may
1501contain null bytes so strlen() is not safe). To test which type an
1502object is, first make sure it isn't NULL, and then use
1503is_stringobject(o), is_tupleobject(o), is_listobject(o) etc.
1504
15055.6. Q. How do I use mkvalue() to create a tuple of arbitrary length?
1506
1507A. You can't. Use t = newtupleobject(n) instead, and fill it with
1508objects using settupleitem(t, i, o) -- note that this "eats" a
1509reference count of o. Similar for lists with newlistobject(n) and
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001510setlistitem(l, i, o). Note that you *must* set all the tuple items to
1511some value before you pass the tuple to Python code --
1512newtupleobject(n) initializes them to NULL, which isn't a valid Python
1513value.
1514
Guido van Rossum796b2591995-01-20 23:05:52 +000015155.7. Q. What happened to mktuple(), featured in an example in the
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001516Extensions manual?
1517
1518A. It's a typo, I meant newtupleobject() (see previous question).
1519
15205.8. Q. How do I call an object's method from C?
1521
1522A. Here's a function (untested) that might become part of the next
1523release in some form. It uses <stdarg.h> to allow passing the
1524argument list on to vmkvalue():
1525
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001526 object *call_method(object *inst, char *methodname, char *format, ...)
1527 {
1528 object *method;
1529 object *args;
1530 object *result;
1531 va_list va;
1532 method = getattr(inst, methodname);
1533 if (method == NULL) return NULL;
1534 va_start(va, format);
1535 args = vmkvalue(format, va);
1536 va_end(va);
1537 if (args == NULL) {
1538 DECREF(method);
1539 return NULL;
1540 }
1541 result = call_object(method, args);
1542 DECREF(method);
1543 DECREF(args);
1544 return result;
1545 }
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001546
1547This works for any instance that has methods -- whether built-in or
1548user-defined. You are responsible for eventually DECREF'ing the
1549return value.
1550
1551To call, e.g., a file object's "seek" method with arguments 10, 0
1552(assuming the file object pointer is "f"):
1553
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001554 res = call_method(f, "seek", "(OO)", 10, 0);
1555 if (res == NULL) {
1556 ... an exception occurred ...
1557 }
1558 else {
1559 DECREF(res);
1560 }
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001561
1562Note that since call_object() *always* wants a tuple for the argument
1563list, to call a function without arguments, pass "()" for the format,
1564and to call a function with one argument, surround the argument in
1565parentheses, e.g. "(i)".
1566
15675.9. Q. How do I catch the output from print_error()?
1568
1569A. (Due to Mark Hammond):
1570
1571* in Python code, define an object that supports the "write()" method.
1572FWIW, there seems to be a small problem that requires the 'softspace'
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00001573attribute to be defined too (fixed in 1.2).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001574
1575* redirect sys.stdout and sys.stderr to this object.
1576
Guido van Rossum061f1821994-10-06 16:03:45 +00001577* call print_error, or just allow the standard traceback mechanism to
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001578work.
1579
Guido van Rossum061f1821994-10-06 16:03:45 +00001580Then, the output will go wherever your write() method sends it.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001581
15825.10. Q. How do I access a module written in Python from C?
1583
1584A. You can get a pointer to the module object as follows:
1585
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001586 module = import_module("<modulename>");
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001587
1588If the module hasn't been imported yet (i.e. it is not yet present in
1589sys.modules), this initializes the module; otherwise it simply returns
1590the value of sys.modules["<modulename>"]. Note that it doesn't enter
1591the module into any namespace -- it only ensures it has been
1592initialized and is stored in sys.modules.
1593
1594You can then access the module's attributes (i.e. any name defined in
1595the module) as follows:
1596
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001597 attr = getattr(module, "<attrname>");
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001598
1599Calling setattr(), to assign to variables in the module, also works.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001600
Guido van Rossuma7925f11994-01-26 10:20:16 +00001601
16026. Python's design
1603==================
1604
16056.1. Q. Why isn't there a generic copying operation for objects in
1606Python?
1607
1608A. Hmm. Maybe there should be one, but it's difficult to assign a
1609useful meaning to copying of open files, sockets and windows, or
1610recursive data structures. As long as you design all your classes
1611yourself you are of course free to define a standard base class that
1612defines an overridable copying operation for all the objects you care
1613about. (One practical point: it would have to be a built-in function,
1614not a standard method name, since not all built-in object types have
1615methods; e.g. strings, integers and tuples don't.)
1616
16176.2. Q. Why isn't there a generic way to implement persistent objects
1618in Python? (Persistent == automatically saved to and restored from
1619disk.)
1620
1621A. Hmm, hmm. Basically for the same reasons as why there is no
1622generic copying operation.
1623
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001624A partial solution will appear in release 1.2. This will also provide
1625a partial solution to the problem of a generic copying operation.
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00001626(Partial because it still won't cope with open files etc.; however it
1627does handle user-defined classes!)
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001628
Guido van Rossuma7925f11994-01-26 10:20:16 +000016296.3. Q. Why isn't there a switch or case statement in Python?
1630
1631A. You can do this easily enough with a sequence of
1632if... elif... elif... else. There have been some proposals for switch
Guido van Rossum061f1821994-10-06 16:03:45 +00001633statement syntax, but there is no consensus (yet) on whether and how
Guido van Rossuma7925f11994-01-26 10:20:16 +00001634to do range tests.
1635
Guido van Rossumc50158e1994-05-31 09:18:50 +000016366.4. Q. Why does Python use indentation for grouping of statements?
1637
1638A. Basically I believe that using indentation for grouping is
1639extremely elegant and contributes a lot to the clarity of the average
1640Python program. Most people learn to love this feature after a while.
1641Some arguments for it:
1642
1643- Since there are no begin/end brackets there cannot be a disagreement
1644between grouping perceived by the parser and the human reader. I
1645remember long ago seeing a C fragment like this:
1646
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001647 if (x <= y)
1648 x++;
1649 y--;
1650 z++;
Guido van Rossumc50158e1994-05-31 09:18:50 +00001651
1652and staring a long time at it wondering why y was being decremented
1653even for x > y... (And I wasn't a C newbie then either.)
1654
Guido van Rossum7be34a81995-05-31 15:17:12 +00001655- Since there are no begin/end brackets, Python is much less prone to
1656coding-style conflicts. In C there are loads of different ways to
1657place the braces (including the choice whether to place braces around
1658single statements in certain cases, for consistency). If you're used
1659to reading (and writing) code that uses one style, you will feel at
1660least slightly uneasy when reading (or being required to write)
1661another style.
Guido van Rossumc50158e1994-05-31 09:18:50 +00001662
1663- Many coding styles place begin/end brackets on a line by themself.
1664This makes programs considerably longer and wastes valuable screen
1665space, making it harder to get a good overview over a program.
1666Ideally, a function should fit on one basic tty screen (say, 20
1667lines). 20 lines of Python are worth a LOT more than 20 lines of C.
1668This is not solely due to the lack of begin/end brackets (the lack of
1669declarations also helps, and the powerful operations of course), but
1670it certainly helps!
1671
Guido van Rossum3de27361994-07-25 14:19:33 +000016726.5. Q. Why are Python strings immutable?
1673
1674A. There are two advantages. One is performance: knowing that a
1675string is immutable makes it easy to lay it out at construction time
1676-- fixed and unchanging storage requirements. (This is also one of
Guido van Rossum061f1821994-10-06 16:03:45 +00001677the reasons for the distinction between tuples and lists.) The
Guido van Rossum3de27361994-07-25 14:19:33 +00001678other is that strings in Python are considered as "elemental" as
1679numbers. No amount of activity will change the value 8 to anything
1680else, and in Python, no amount of activity will change the string
1681"eight" to anything else. (Adapted from Jim Roskind)
1682
16836.6. Q. Why don't strings have methods like index() or sort(), like
1684lists?
1685
1686A. Good question. Strings currently don't have methods at all
1687(likewise tuples and numbers). Long ago, it seemed unnecessary to
1688implement any of these functions in C, so a standard library module
1689"string" written in Python was created that performs string related
1690operations. Since then, the cry for performance has moved most of
1691them into the built-in module strop (this is imported by module
Guido van Rossumf8c76d01994-08-17 12:19:53 +00001692string, which is still the preferred interface, without loss of
Guido van Rossum3de27361994-07-25 14:19:33 +00001693performance except during initialization). Some of these functions
1694(e.g. index()) could easily be implemented as string methods instead,
1695but others (e.g. sort()) can't, since their interface prescribes that
1696they modify the object, while strings are immutable (see the previous
1697question).
1698
16996.7. Q. Why does Python use methods for some functionality
1700(e.g. list.index()) but functions for other (e.g. len(list))?
1701
1702A. Functions are used for those operations that are generic for a
1703group of types and which should work even for objects that don't have
1704methods at all (e.g. numbers, strings, tuples). Also, implementing
1705len(), max(), min() as a built-in function is actually less code than
1706implementing them as methods for each type. One can quibble about
1707individual cases but it's really too late to change such things
1708fundamentally now.
1709
17106.8. Q. Why can't I derive a class from built-in types (e.g. lists or
1711files)?
1712
1713A. This is caused by the relatively late addition of (user-defined)
1714classes to the language -- the implementation framework doesn't easily
1715allow it. See the answer to question 4.2 for a work-around. This
1716*may* be fixed in the (distant) future.
1717
17186.9. Q. Why must 'self' be declared and used explicitly in method
1719definitions and calls?
1720
1721A. By asking this question you reveal your C++ background. :-)
1722When I added classes, this was (again) the simplest way of
1723implementing methods without too many changes to the interpreter. I
1724borrowed the idea from Modula-3. It turns out to be very useful, for
1725a variety of reasons.
1726
1727First, it makes it more obvious that you are using a method or
1728instance attribute instead of a local variable. Reading "self.x" or
1729"self.meth()" makes it absolutely clear that an instance variable or
1730method is used even if you don't know the class definition by heart.
1731In C++, you can sort of tell by the lack of a local variable
Guido van Rossum061f1821994-10-06 16:03:45 +00001732declaration (assuming globals are rare or easily recognizable) -- but
Guido van Rossum3de27361994-07-25 14:19:33 +00001733in Python, there are no local variable declarations, so you'd have to
1734look up the class definition to be sure.
1735
1736Second, it means that no special syntax is necessary if you want to
1737explicitly reference or call the method from a particular class. In
1738C++, if you want to use a method from base class that is overridden in
1739a derived class, you have to use the :: operator -- in Python you can
1740write baseclass.methodname(self, <argument list>). This is
1741particularly useful for __init__() methods, and in general in cases
1742where a derived class method wants to extend the base class method of
1743the same name and thus has to call the base class method somehow.
1744
1745Lastly, for instance variables, it solves a syntactic problem with
1746assignment: since local variables in Python are (by definition!) those
1747variables to which a value assigned in a function body (and that
1748aren't explicitly declared global), there has to be some way to tell
1749the interpreter that an assignment was meant to assign to an instance
1750variable instead of to a local variable, and it should preferably be
1751syntactic (for efficiency reasons). C++ does this through
1752declarations, but Python doesn't have declarations and it would be a
1753pity having to introduce them just for this purpose. Using the
1754explicit "self.var" solves this nicely. Similarly, for using instance
1755variables, having to write "self.var" means that references to
1756unqualified names inside a method don't have to search the instance's
1757directories.
1758
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000017596.10. Q. Can't you emulate threads in the interpreter instead of
1760relying on an OS-specific thread implementation?
1761
1762A. Unfortunately, the interpreter pushes at least one C stack frame
1763for each Python stack frame. Also, extensions can call back into
1764Python at almost random moments. Therefore a complete threads
1765implementation requires thread support for C.
1766
Guido van Rossum061f1821994-10-06 16:03:45 +000017676.11. Q. Why can't lambda forms contain statements?
1768
1769A. Python lambda forms cannot contain statements because Python's
Guido van Rossum796b2591995-01-20 23:05:52 +00001770syntactic framework can't handle statements nested inside expressions.
Guido van Rossum061f1821994-10-06 16:03:45 +00001771
1772However, in Python, this is not a serious problem. Unlike lambda
1773forms in other languages, where they add functionality, Python lambdas
1774are only a shorthand notation if you're too lazy to define a function.
1775
1776Functions are already first class objects in Python, and can be
1777declared in a local scope. Therefore the only advantage of using a
1778lambda form instead of a locally-defined function is that you'll have
1779to invent a name for the function -- but that's just a local variable
1780to which the function object (which is exactly the same type of object
1781that a lambda form yields) is assigned!
1782
17836.12. Q. Why is there no more efficient way of iterating over a dictionary
1784than first constructing the list of keys()?
1785
1786A. Have you tried it? I bet it's fast enough for your purposes! In
1787most cases such a list takes only a few percent of the space occupied
1788by the dictionary -- it needs only 4 bytes (the size of a pointer) per
1789key -- a dictionary costs 8 bytes per key plus between 30 and 70
1790percent hash table overhead, plus the space for the keys and values --
1791by necessity all keys are unique objects and a string object (the most
1792common key type) costs at least 18 bytes plus the length of the
1793string. Add to that the values contained in the dictionary, and you
1794see that 4 bytes more per item really isn't that much more memory...
1795
1796A call to dict.keys() makes one fast scan over the dictionary
1797(internally, the iteration function does exist) copying the pointers
1798to the key objects into a pre-allocated list object of the right size.
1799The iteration time isn't lost (since you'll have to iterate anyway --
1800unless in the majority of cases your loop terminates very prematurely
1801(which I doubt since you're getting the keys in random order).
1802
1803I don't expose the dictionary iteration operation to Python
1804programmers because the dictionary shouldn't be modified during the
1805entire iteration -- if it is, there's a very small chance that the
1806dictionary is reorganized because the hash table becomes too full, and
1807then the iteration may miss some items and see others twice. Exactly
1808because this only occurs rarely, it would lead to hidden bugs in
1809programs: it's easy never to have it happen during test runs if you
1810only insert or delete a few items per iteration -- but your users will
1811surely hit upon it sooner or later.
1812
Guido van Rossuma7925f11994-01-26 10:20:16 +00001813
18147. Using Python on non-UNIX platforms
1815=====================================
1816
Guido van Rossum91f60831994-02-15 15:52:27 +000018177.1. Q. Is there a Mac version of Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +00001818
Guido van Rossume530c581995-04-10 12:32:16 +00001819A. Yes, see the "mac" subdirectory of the distribution sites,
Guido van Rossum7be34a81995-05-31 15:17:12 +00001820e.g. <URL:ftp://ftp.python.org/pub/python/mac/>.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001821
Guido van Rossum91f60831994-02-15 15:52:27 +000018227.2. Q. Is there a DOS version of Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +00001823
Guido van Rossume530c581995-04-10 12:32:16 +00001824A. Yes, see the "pc" subdirectory of the distribution sites,
Guido van Rossum7be34a81995-05-31 15:17:12 +00001825e.g. <URL:ftp://ftp.python.org/pub/python/pc/>.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001826
Guido van Rossume530c581995-04-10 12:32:16 +000018277.3. Q. Is there a Windows 3.1(1) version of Python?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001828
Guido van Rossume530c581995-04-10 12:32:16 +00001829A. Yes, also see the "pc" subdirectory of the distribution sites,
Guido van Rossum3fc9d731995-07-25 15:10:56 +00001830e.g. <URL:ftp://ftp.python.org/pub/python/pc/>. You may also be able
1831to run either of the Windows NT versions (see next question) if you
1832have Microsoft's "win32s".
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001833
Guido van Rossum91f60831994-02-15 15:52:27 +000018347.4. Q. Is there a Windows NT version of Python?
1835
Guido van Rossum3fc9d731995-07-25 15:10:56 +00001836A. There are two, both sporting DLL support for dynamic loading of
1837Python modules, and extensions to access the Win32 GUI API.
1838
1839Mark Hammond <MHammond@cmutual.com.au> maintains an NT port which
1840includes an interface to the Microsoft Foundation Classes and a Python
1841programming environment using it that's written mostly in Python. See
1842<URL:ftp://ftp.python.org/pub/python/nt/>.
1843
1844Jim Ahlstrom's WPY portable GUI runs on Windows NT and is modeled
1845after the Microsoft Foundation Classes. Source and binaries are
1846available in <URL:ftp://ftp.python.org/pub/python/wpy>.
Guido van Rossum91f60831994-02-15 15:52:27 +00001847
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001848Sam Rushing <rushing@squirl.oau.org> once announced he knows how to
1849build Python for the Windows NT on the DEC Alpha AXP.
Guido van Rossum061f1821994-10-06 16:03:45 +00001850
1851Note that currently there is no unified compilation environment for
1852all NT platforms -- hopefully Microsoft will fix this with the release
1853of Visual C++ 2.0.
1854
Guido van Rossume530c581995-04-10 12:32:16 +000018557.5. Q. Is there a Windows 95 version of Python?
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001856
Guido van Rossum3fc9d731995-07-25 15:10:56 +00001857A. The Windows NT versions might work, otherwise the Windows 3.1(1)
Guido van Rossume530c581995-04-10 12:32:16 +00001858version should work (isn't Windows 95 supposed to be backwards
1859compatible?).
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001860
Guido van Rossume530c581995-04-10 12:32:16 +000018617.6. Q. Is there an OS/2 version of Python?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001862
Guido van Rossume530c581995-04-10 12:32:16 +00001863A. Yes, also see the "pc" subdirectory of the distribution sites,
Guido van Rossum7be34a81995-05-31 15:17:12 +00001864e.g. <URL:ftp://ftp.python.org/pub/python/pc/>.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001865
Guido van Rossume530c581995-04-10 12:32:16 +000018667.7. Q. Is there a VMS version of Python?
1867
Guido van Rossum3fc9d731995-07-25 15:10:56 +00001868A. Donn Cave <donn@cac.washington.edu> did a partial port. The
1869results of his efforts are on public display in
Guido van Rossumbf8e7d51995-08-28 03:09:13 +00001870<<URL:ftp://ftp.python.org/pub/python/contrib/vms.tar.gz/>. Someone
1871else is working on a more complete port, for details watch the list.
Guido van Rossume530c581995-04-10 12:32:16 +00001872
Guido van Rossum3fc9d731995-07-25 15:10:56 +000018737.8. Q. What about IBM mainframes, or other non-UNIX platforms?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001874
Guido van Rossum3fc9d731995-07-25 15:10:56 +00001875A. I haven't heard about these, except I remember once hearing about
1876an attempt at an OS/9 port. If you're interested in any of this, go
Guido van Rossumbf8e7d51995-08-28 03:09:13 +00001877directly to the newsgroup and ask there, you may find exactly what you
1878need. For example, a port to MPE/iX 5.0 on HP3000 computers was just
1879announced, see <URL:http://www.allegro.com/software/>.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001880
Guido van Rossume530c581995-04-10 12:32:16 +000018817.9. Q. Where are the source or Makefiles for the non-UNIX versions?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001882
Guido van Rossume530c581995-04-10 12:32:16 +00001883A. The standard sources can (almost) be used. Additional sources can
1884be found in the platform-specific subdirectories of the distribution.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001885
Guido van Rossume530c581995-04-10 12:32:16 +000018867.10. Q. What is the status and support for the non-UNIX versions?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001887
1888A. I don't have access to most of these platforms, so in general I am
1889dependent on material submitted by volunteers(*). However I strive to
1890integrate all changes needed to get it to compile on a particular
1891platform back into the standard sources, so porting of the next
1892version to the various non-UNIX platforms should be easy.
1893
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001894(*) For the Macintosh, that volunteer is me, with help from Jack
1895Jansen <jack@cwi.nl>.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001896
Guido van Rossume530c581995-04-10 12:32:16 +000018977.11. Q. I have a PC version but it appears to be only a binary.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001898Where's the library?
Guido van Rossuma7925f11994-01-26 10:20:16 +00001899
1900A. You still need to copy the files from the distribution directory
Guido van Rossum91f60831994-02-15 15:52:27 +00001901"python/Lib" to your system. If you don't have the full distribution,
Guido van Rossume530c581995-04-10 12:32:16 +00001902you can get the file lib<version>.tar.gz from most ftp sites carrying
1903Python; this is a subset of the distribution containing just those
Guido van Rossum7be34a81995-05-31 15:17:12 +00001904files, e.g. <URL:ftp://ftp.python.org/pub/python/src/lib1.1.tar.gz>.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001905
Guido van Rossum5333c5d1994-04-11 11:06:22 +00001906Once you have installed the library, you need to point sys.path to it.
1907Assuming the library is in C:\misc\python\lib, the following commands
1908will point your Python interpreter to it (note the doubled backslashes
1909-- you can also use single forward slashes instead):
1910
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001911 >>> import sys
1912 >>> sys.path.insert(0, 'C:\\misc\\python\\lib')
1913 >>>
Guido van Rossum5333c5d1994-04-11 11:06:22 +00001914
1915For a more permanent effect, set the environment variable PYTHONPATH,
1916as follows (talking to a DOS prompt):
1917
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001918 C> SET PYTHONPATH=C:\misc\python\lib
Guido van Rossum5333c5d1994-04-11 11:06:22 +00001919
Guido van Rossume530c581995-04-10 12:32:16 +000019207.12. Q. Where's the documentation for the Mac or PC version?
Guido van Rossuma7925f11994-01-26 10:20:16 +00001921
Guido van Rossume530c581995-04-10 12:32:16 +00001922A. The documentation for the Unix version also applies to the Mac and
1923PC versions. Where applicable, differences are indicated in the text.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001924
Guido van Rossume530c581995-04-10 12:32:16 +000019257.13. Q. The Mac (PC) version doesn't seem to have any facilities for
Guido van Rossum91f60831994-02-15 15:52:27 +00001926creating or editing programs apart from entering it interactively, and
1927there seems to be no way to save code that was entered interactively.
1928How do I create a Python program on the Mac (PC)?
Guido van Rossuma7925f11994-01-26 10:20:16 +00001929
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001930A. Use an external editor. On the Mac, BBEdit seems to be a popular
1931no-frills text editor. I work like this: start the interpreter; edit
1932a module file using BBedit; import and test it in the interpreter;
1933edit again in BBedit; then use the built-in function reload() to
1934re-read the imported module; etc.
Guido van Rossum5333c5d1994-04-11 11:06:22 +00001935
1936Regarding the same question for the PC, Kurt Wm. Hemr writes: "While
1937anyone with a pulse could certainly figure out how to do the same on
1938MS-Windows, I would recommend the NotGNU Emacs clone for MS-Windows.
1939Not only can you easily resave and "reload()" from Python after making
1940changes, but since WinNot auto-copies to the clipboard any text you
1941select, you can simply select the entire procedure (function) which
1942you changed in WinNot, switch to QWPython, and shift-ins to reenter
1943the changed program unit."