blob: fcc363722725b571afa2a619f4846f2eed81d2f7 [file] [log] [blame]
Guido van Rossum061f1821994-10-06 16:03:45 +00001NOTE -- THIS FAQ HAS NOT BEEN UPDATED TO REFLECT RELEASE 1.1 YET. FOR
21.1 RELATED PROBLEMS PLEASE WATCH THE NEWSGROUP / MAILING LIST OR
3CHECK THE FTP SITE.
4
Guido van Rossuma7925f11994-01-26 10:20:16 +00005Subject: FAQ: Python -- an object-oriented language
Guido van Rossum5333c5d1994-04-11 11:06:22 +00006Newsgroups: comp.lang.python,comp.answers,news.answers
7Followup-to: comp.lang.python
Guido van Rossuma7925f11994-01-26 10:20:16 +00008From: guido@cwi.nl (Guido van Rossum)
9Reply-to: guido@cwi.nl (Guido van Rossum)
10Approved: news-answers-request@MIT.Edu
11
12Archive-name: python-faq/part1
Guido van Rossum061f1821994-10-06 16:03:45 +000013Version: 1.15
14Last-modified: 6 October 1994
Guido van Rossuma7925f11994-01-26 10:20:16 +000015
16This article contains answers to Frequently Asked Questions about
17Python (an object-oriented interpreted programming language -- see
18the answer to question 1.1 for a short overview).
19
20Copyright 1993, 1994 Guido van Rossum. Unchanged electronic
21redistribution of this FAQ is allowed. Printed redistribution only
22with permission of the author. No warranties.
23
24Author's address:
25 Guido van Rossum
26 CWI, dept. CST
27 Kruislaan 413
28 P.O. Box 94079
29 1090 GB Amsterdam
30 The Netherlands
31Email: guido@cwi.nl
32
33The latest version of this FAQ is available by anonymous ftp from
Guido van Rossum7ce61c11994-06-13 15:13:56 +000034ftp.cwi.nl [192.16.191.128] in the directory /pub/python, with
Guido van Rossuma7925f11994-01-26 10:20:16 +000035filename python-FAQ. It will also be posted regularly to the
Guido van Rossum5333c5d1994-04-11 11:06:22 +000036newsgroups comp.answers and comp.lang.python.
Guido van Rossuma7925f11994-01-26 10:20:16 +000037
38Many FAQs, including this one, are available by anonymous ftp from
Guido van Rossumea2c6f71994-07-14 12:35:14 +000039rtfm.mit.edu [18.181.0.24] in the directory pub/usenet/news.answers.
Guido van Rossuma7925f11994-01-26 10:20:16 +000040The name under which a FAQ is archived appears in the Archive-name line
41at the top of the article. This FAQ is archived as python-faq/part1.
42
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
45message to mail-server@rtfm.mit.edu containing the single word help in
46the message body to receive instructions.
47
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000048Skip Montanaro <skip@automatrix.com> maintains an HTML version of this
Guido van Rossum061f1821994-10-06 16:03:45 +000049FAQ. The URL is <http://www.automatrix.com/~skip/python-faq.html>.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000050
Guido van Rossuma7925f11994-01-26 10:20:16 +000051This FAQ is divided in the following chapters:
52
53 1. General information and availability
54 2. Python in the real world
55 3. Building Python
56 4. Programming in Python
57 5. Extending Python
58 6. Python's design
59 7. Using Python on non-UNIX platforms
60
61To find the start of a particular chapter, search for the chapter number
62followed by a dot and a space at the beginning of a line (e.g. to
63find chapter 4 in vi, type /^4\. /).
64
65Here's an overview of the questions per chapter:
66
67 1. General information and availability
68 1.1. Q. What is Python?
69 1.2. Q. Why is it called Python?
70 1.3. Q. How do I obtain a copy of the Python source?
71 1.4. Q. How do I get documentation on Python?
Guido van Rossumc50158e1994-05-31 09:18:50 +000072 1.5. Q. Are there other ftp sites that mirror the Python distribution?
73 1.6. Q. Is there a newsgroup or mailing list devoted to Python?
74 1.7. Q. Is there a book on Python, or will there be one out soon?
75 1.8. Q. Are there any published articles about Python that I can quote?
76 1.9. Q. How does the Python version numbering scheme work?
77 1.10. Q. Are there other ftp sites that carry Python related material?
Guido van Rossum7ce61c11994-06-13 15:13:56 +000078 1.11. Q. Are there copyright restrictions on the use of Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +000079
80 2. Python in the real world
81 2.1. Q. How many people are using Python?
82 2.2. Q. Have any significant projects been done in Python?
83 2.3. Q. Are there any commercial projects going on using Python?
Guido van Rossum95f61a71994-01-26 17:23:37 +000084 2.4. Q. How stable is Python?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000085 2.5. Q. When will the next version be released?
86 2.6. Q. What new developments are expected for Python in the future?
87 2.7. Q. Is it reasonable to propose incompatible changes to Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +000088
89 3. Building Python
Guido van Rossum91f60831994-02-15 15:52:27 +000090 3.1. Q. Is there a test set?
91 3.2. Q. When running the test set, I get complaints about floating point
Guido van Rossuma7925f11994-01-26 10:20:16 +000092 operations, but when playing with floating point operations I cannot
93 find anything wrong with them.
Guido van Rossum91f60831994-02-15 15:52:27 +000094 3.3. Q. Link errors building Python with STDWIN on SGI IRIX.
95 3.4. Q. Link errors after rerunning the configure script.
96 3.5. Q. The python interpreter complains about options passed to a
Guido van Rossum24349991994-02-02 14:12:45 +000097 script (after the script name).
Guido van Rossum91f60831994-02-15 15:52:27 +000098 3.6. Q. When building on the SGI, make tries to run python to create
Guido van Rossum5333c5d1994-04-11 11:06:22 +000099 glmodule.c, but python hasn't been built or installed yet.
Guido van Rossum3de27361994-07-25 14:19:33 +0000100 3.7. Q. Python built with gcc for the DEC Alpha doesn't work.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000101 3.8. Q. I use VPATH but some targets are built in the source directory.
102 3.9. Q. Trouble building or linking with the GNU readline library.
103 3.10. Q. Trouble building Python on Linux.
104 3.11. Q. Other trouble building Python on platform X.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000105
106 4. Programming in Python
Guido van Rossum24349991994-02-02 14:12:45 +0000107 4.1. Q. Is there a source code level debugger with breakpoints, step,
108 etc.?
109 4.2. Q. Can I create an object class with some methods implemented in
Guido van Rossuma7925f11994-01-26 10:20:16 +0000110 C and others in Python (e.g. through inheritance)? (Also phrased as:
111 Can I use a built-in type as base class?)
Guido van Rossum24349991994-02-02 14:12:45 +0000112 4.3. Q. Is there a curses/termcap package for Python?
113 4.4. Q. Is there an equivalent to C's onexit() in Python?
114 4.5. Q. When I define a function nested inside another function, the
Guido van Rossuma7925f11994-01-26 10:20:16 +0000115 nested function seemingly can't access the local variables of the
116 outer function. What is going on? How do I pass local data to a
117 nested function?
Guido van Rossum24349991994-02-02 14:12:45 +0000118 4.6. Q. How do I iterate over a sequence in reverse order?
119 4.7. Q. My program is too slow. How do I speed it up?
120 4.8. Q. When I have imported a module, then edit it, and import it
Guido van Rossuma7925f11994-01-26 10:20:16 +0000121 again (into the same Python process), the changes don't seem to take
122 place. What is going on?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000123 4.9. Q. How do I find the current module name?
124 4.10. Q. I have a module in which I want to execute some extra code when it
125 is run as a script. How do I find out whether I am running as a
126 script?
127 4.11. Q. I try to run a program from the Demo directory but it fails with
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000128 ImportError: No module named ...; what gives?
Guido van Rossum061f1821994-10-06 16:03:45 +0000129 4.12. Q. I have successfully built Python with STDWIN but it can't
130 find some modules (e.g. stdwinevents).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000131 4.13. Q. What GUI toolkits exist for Python?
Guido van Rossum061f1821994-10-06 16:03:45 +0000132 4.14. Q. Are there any interfaces to database packages in Python?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000133 4.15. Q. Is it possible to write obfuscated one-liners in Python?
134 4.16. Q. Is there an equivalent of C's "?:" ternary operator?
135 4.17. Q. My class defines __del__ but it is not called when I delete the
136 object.
137 4.18. Q. How do I change the shell environment for programs called using
138 os.popen() or os.system()? Changing os.environ doesn't work.
139 4.19. Q. What is a class?
140 4.20. Q. What is a method?
141 4.21. Q. What is self?
142 4.22. Q. What is a unbound method?
143 4.23. Q. How do I call a method defined in a base class from a derived class
144 that overrides it?
145 4.24. Q. How do I call a method from a base class without using the name of
146 the base class?
147 4.25. Q. How can I organize my code to make it easier to change the base
148 class?
149 4.26. Q. How can I find the methods or attributes of an object?
Guido van Rossum061f1821994-10-06 16:03:45 +0000150 4.27. Q. I can't seem to use os.read() on a pipe created with os.popen().
151 4.28. Q. How can I create a stand-alone binary from a Python script?
152 4.29. Q. Is there a special lib for writing CGI scripts in Python?
153 4.30. Q. What other WWW tools are there for Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000154
155 5. Extending Python
156 5.1. Q. Can I create my own functions in C?
157 5.2. Q. Can I create my own functions in C++?
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000158 5.3. Q. How can I execute arbitrary Python statements from C?
159 5.4. Q. How can I evaluate an arbitrary Python expression from C?
160 5.5. Q. How do I extract C values from a Python object?
161 5.6. Q. How do I use mkvalue() to create a tuple of arbitrary length?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000162 5.7. Q. What happened to mktuple(), featuring in an example in the
163 Extensions manual?
164 5.8. Q. How do I call an object's method from C?
165 5.9. Q. How do I catch the output from print_error()?
166 5.10. Q. How do I access a module written in Python from C?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000167
168 6. Python's design
169 6.1. Q. Why isn't there a generic copying operation for objects in
170 Python?
171 6.2. Q. Why isn't there a generic way to implement persistent objects
172 in Python? (Persistent == automatically saved to and restored from
173 disk.)
174 6.3. Q. Why isn't there a switch or case statement in Python?
Guido van Rossumc50158e1994-05-31 09:18:50 +0000175 6.4. Q. Why does Python use indentation for grouping of statements?
Guido van Rossum3de27361994-07-25 14:19:33 +0000176 6.5. Q. Why are Python strings immutable?
177 6.6. Q. Why don't strings have methods like index() or sort(), like
178 lists?
179 6.7. Q. Why does Python use methods for some functionality
180 (e.g. list.index()) but functions for other (e.g. len(list))?
181 6.8. Q. Why can't I derive a class from built-in types (e.g. lists or
182 files)?
183 6.9. Q. Why must 'self' be declared and used explicitly in method
184 definitions and calls?
Guido van Rossum061f1821994-10-06 16:03:45 +0000185 6.10. Q. Can't you emulate threads in the interpreter instead of
186 relying on an OS-specific thread implementation?
187 6.11. Q. Why can't lambda forms contain statements?
188 6.12. Q. Why is there no more efficient way of iterating over a dictionary
189 than first constructing the list of keys()?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000190
191 7. Using Python on non-UNIX platforms
Guido van Rossum91f60831994-02-15 15:52:27 +0000192 7.1. Q. Is there a Mac version of Python?
193 7.2. Q. Is there a DOS version of Python?
194 7.3. Q. Is there a Windows version of Python?
195 7.4. Q. Is there a Windows NT version of Python?
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000196 7.5. Q. Is there an OS/2 version of Python?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000197 7.6. Q. Is there a VMS version of Python?
198 7.7. Q. What about IBM mainframes, or other esoteric non-UNIX
199 platforms?
200 7.8. Q. Where are the source or Makefiles for the non-UNIX versions?
201 7.9. Q. What is the status and support for the non-UNIX versions?
202 7.10. Q. I have the PC version but it appears to be only a binary.
203 Where's the library?
204 7.11. Q. Where's the documentation for the Mac or PC version?
205 7.12. Q. The Mac (PC) version doesn't seem to have any facilities for
206 creating or editing programs apart from entering it interactively, and
207 there seems to be no way to save code that was entered interactively.
208 How do I create a Python program on the Mac (PC)?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000209
210To find a particular question, search for the question number followed
211by a dot, a space, and a Q at the beginning of a line (e.g. to find
212question 4.2 in vi, type /^4\.2\. Q/).
213
214
2151. General information and availability
216=======================================
217
2181.1. Q. What is Python?
219
220A. Python is an interpreted, interactive, object-oriented programming
221language. It incorporates modules, exceptions, dynamic typing, very
222high level dynamic data types, and classes. Python combines
223remarkable power with very clear syntax. It has interfaces to many
224system calls and libraries, as well as to various window systems, and
225is extensible in C or C++. It is also usable as an extension language
226for applications that need a programmable interface. Finally, Python
227is portable: it runs on many brands of UNIX, on the Mac, and on
228MS-DOS.
229
230To find out more, the best thing to do is to start reading the
231tutorial from the documentation set (see a few questions further
232down).
233
2341.2. Q. Why is it called Python?
235
236A. Apart from being a computer wizard, I'm also a fan of "Monty
237Python's Flying Circus" (a BBC comedy series from the seventies, in
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000238the -- unlikely -- case you didn't know). It occurred to me one day
239that I needed a name that was short, unique, and slightly mysterious.
240And I happened to be reading some scripts from the series at the
241time... So then I decided to call my language Python. But Python is
242not a joke. And don't you associate it with dangerous reptiles
243either!
Guido van Rossuma7925f11994-01-26 10:20:16 +0000244
2451.3. Q. How do I obtain a copy of the Python source?
246
247A. The latest Python source distribution is always available by
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000248anonymous ftp from ftp.cwi.nl [192.16.191.128] in the directory
Guido van Rossum44b4cb11994-05-04 13:28:51 +0000249/pub/python, with filename python<version>.tar.gz. (Old versions may
250have an extension of .Z, indicating use of "compress" compression.)
251It is a gzip'ed tar file containing the complete C source, LaTeX
252documentation, Python library modules, example programs, and several
253useful pieces of freely distributable software. This will compile and
Guido van Rossum3de27361994-07-25 14:19:33 +0000254run out of the box on most UNIX platforms. (See section 7 for
255non-UNIX information.)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000256
2571.4. Q. How do I get documentation on Python?
258
259A. The latest Python documentation set is always available by
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000260anonymous ftp from ftp.cwi.nl [192.16.191.128] in the directory
Guido van Rossum44b4cb11994-05-04 13:28:51 +0000261/pub/python, with filename pythondoc-ps<version>.tar.gz. It is a
262gzip'ed tar file containing PostScript files of the reference manual,
Guido van Rossum3de27361994-07-25 14:19:33 +0000263the library manual, and the tutorial. Note that the library manual is
264the most important one of the set, as much of Python's power stems
265from the standard or built-in types, functions and modules, all of
266which are described here. PostScript for a high-level description of
267Python is in the file nluug-paper.ps.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000268
Guido van Rossumc50158e1994-05-31 09:18:50 +00002691.5. Q. Are there other ftp sites that mirror the Python distribution?
270
271A. The following sites keep mirrors of the Python distribution:
Guido van Rossuma7925f11994-01-26 10:20:16 +0000272
273Site IP address Directory
274
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000275gatekeeper.dec.com 16.1.0.2 /pub/plan/python
Guido van Rossuma7925f11994-01-26 10:20:16 +0000276ftp.uu.net 192.48.96.9 /languages/python
277ftp.wustl.edu 128.252.135.4 /graphics/graphics/sgi-stuff/python
Guido van Rossumc50158e1994-05-31 09:18:50 +0000278ftp.funet.fi 128.214.6.100 /pub/languages/python
Guido van Rossumf8c76d01994-08-17 12:19:53 +0000279ftp.fu-berlin.de 130.133.1.18 /unix/languages/python (*python* only)
Guido van Rossumc50158e1994-05-31 09:18:50 +0000280ftp.sunet.se 130.238.127.3 /pub/lang/python
Guido van Rossumea2c6f71994-07-14 12:35:14 +0000281unix.hensa.ac.uk 129.12.43.16 /uunet/languages/python
Guido van Rossum061f1821994-10-06 16:03:45 +0000282ftp.sterlng.com 192.124.9.3 /programming/languages/python
Guido van Rossuma7925f11994-01-26 10:20:16 +0000283
Guido van Rossum061f1821994-10-06 16:03:45 +0000284Or try archie on e.g. "python1." to locate the nearest copy of that
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000285version...
Guido van Rossuma7925f11994-01-26 10:20:16 +0000286
Guido van Rossumc50158e1994-05-31 09:18:50 +00002871.6. Q. Is there a newsgroup or mailing list devoted to Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000288
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000289A. There is a newsgroup, comp.lang.python, and a mailing list. The
290newsgroup and mailing list are gatewayed into each other -- if you can
Guido van Rossum061f1821994-10-06 16:03:45 +0000291read news it's unnecessary to subscribe to the mailing list. Send
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000292e-mail to python-list-request@cwi.nl to (un)subscribe to the mailing
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000293list. Once you're on, send e-mail tp python-list@cwi.nl to send mail
Guido van Rossum061f1821994-10-06 16:03:45 +0000294to the entire mailing list and newsgroup. Hypermail archives of
295(nearly) everything posted to the mailing list (and thus the
296newsgroup) are available -- the URL for the complete set of archives
297is <http://www.cwi.nl/~guido/hypermail/index.html>. The raw archives
298are also available by ftp from ftp.cwi.nl in /pub/python (and most
299mirrors), files mail*.gz. The uncompressed versions of these files
300can be read with the standard UNIX Mail program ("Mail -f file") or
301with nn ("nn file"). To read them using MH, you could use "inc -file
302file".
Guido van Rossuma7925f11994-01-26 10:20:16 +0000303
Guido van Rossumc50158e1994-05-31 09:18:50 +00003041.7. Q. Is there a book on Python, or will there be one out soon?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000305
Guido van Rossum061f1821994-10-06 16:03:45 +0000306A. I am writing one. Addison-Wesley is interested. Optimistically,
307it will be published by mid-1995. Other authors are also working on
308books... (Do you guys want your name mentioned here?)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000309
Guido van Rossumc50158e1994-05-31 09:18:50 +00003101.8. Q. Are there any published articles about Python that I can quote?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000311
312A. So far the only refereed and published article that describes
313Python in some detail is:
314
315 Guido van Rossum and Jelke de Boer, "Interactively Testing Remote
316 Servers Using the Python Programming Language", CWI Quarterly, Volume
317 4, Issue 4 (December 1991), Amsterdam, pp 283-303.
318
319LaTeX source for this paper is available as part of the Python source
320distribution.
321
322A more recent high-level description of Python is:
323
324 Guido van Rossum, "An Introduction to Python for UNIX/C
325 Programmers", in the proceedings of the NLUUG najaarsconferentie
Guido van Rossum061f1821994-10-06 16:03:45 +0000326 1993 (dutch UNIX users group meeting November 1993).
Guido van Rossuma7925f11994-01-26 10:20:16 +0000327
328PostScript for this paper and for the slides used for the accompanying
329presentation can be found in the ftp directory mentioned a few
330questions earlier, with filenames nluug-paper.ps and nluug-slides.ps,
331respectively.
332
Guido van Rossumc50158e1994-05-31 09:18:50 +00003331.9. Q. How does the Python version numbering scheme work?
Guido van Rossum95f61a71994-01-26 17:23:37 +0000334
335A. Python versions are numbered A.B.C. A is the major version number
336-- it is only incremented for major changes in functionality or source
337structure. B is the minor version number, incremented for less
338earth-shattering changes to a release. C is the patchlevel -- it is
339incremented for each new release. Note that in the past, patches have
340added significant changes; in fact the changeover from 0.9.9 to 1.0.0
341was the first time that either A or B changed!
342
Guido van Rossumc50158e1994-05-31 09:18:50 +00003431.10. Q. Are there other ftp sites that carry Python related material?
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000344
345A. An interesting ftp site for Python users is ftp.markv.com
346(192.122.251.1); the directory pub/python contains a growing
347collection of interesting Python scripts. To submit a script for
348inclusion, place it together with a readme file (with extension
349.readme) in the publicly writable directory /incoming/python. This
350service is maintained by Lance Ellinghouse <lance@markv.com>.
351
Guido van Rossum7ce61c11994-06-13 15:13:56 +00003521.11. Q. Are there copyright restrictions on the use of Python?
353
354A. Hardly. You can do anything you want with the source, as long as
355you leave the copyrights in, display those copyrights in any
356documentation about Python that you produce, don't use the author's
357institute's name in publicity without prior written permission, and
358don't hold them responsible for anything (read the actual copyright
359for a precise legal wording).
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000360
Guido van Rossuma7925f11994-01-26 10:20:16 +0000361
3622. Python in the real world
363===========================
364
3652.1. Q. How many people are using Python?
366
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000367A. I don't know, but the maximum number of simultaneous subscriptions
368to the Python mailing list before it was gatewayed into the newsgroup
369was about 180 (several of which were local redistribution lists). I
370believe that many active Python users don't bother to subscribe to the
371list, and now that there's a newsgroup the mailing list subscription
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000372is even less meaningful. I see new names on the newsgroup all the
373time and my best guess is that there are currently at least several
374thousands of users.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000375
3762.2. Q. Have any significant projects been done in Python?
377
378A. Here at CWI (the home of Python), we have written a 20,000 line
379authoring environment for transportable hypermedia presentations, a
Guido van Rossum5333c5d1994-04-11 11:06:22 +00003805,000 line multimedia teleconferencing tool, as well as many many
381smaller programs.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000382
383The University of Virginia uses Python to control a virtual reality
384engine. Contact: Matt Conway <conway@virginia.edu>.
385
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000386If you have done a significant project in Python that you'd like to be
387included in the list above, send me email!
Guido van Rossuma7925f11994-01-26 10:20:16 +0000388
3892.3. Q. Are there any commercial projects going on using Python?
390
391A. Several companies have revealed to me that they are planning or
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000392considering to use Python in a future product.
393
394The furthest is Sunrise Software, who already have a product out using
395Python -- they use Python for a GUI management application and an SNMP
Guido van Rossum061f1821994-10-06 16:03:45 +0000396network management application. Contact: <info@sunrise.com>.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000397
Guido van Rossum061f1821994-10-06 16:03:45 +0000398Infoseek is using Python to implement their commercial WWW information
Guido van Rossumf8c76d01994-08-17 12:19:53 +0000399retrieval service. Contact: <info@infoseek.com>.
400
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000401Michael Powers of daVinci Time & Space is "writing tons-o-python for
402interactive television entertainment." Contact: <powers@dvts.com>.
403
Guido van Rossuma7925f11994-01-26 10:20:16 +0000404Individuals at many other companies are using Python for
405internal development (witness their contributions to the Python
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000406mailing list or newsgroup).
Guido van Rossuma7925f11994-01-26 10:20:16 +0000407
408Python has also been elected as an extension language by MADE, a
409consortium supported by the European Committee's ESPRIT program and
410consisting of Bull, CWI and some other European companies. Contact:
411Ivan Herman <ivan@cwi.nl>.
412
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000413If you'd like to be included in the list above, send me email!
414
Guido van Rossum95f61a71994-01-26 17:23:37 +00004152.4. Q. How stable is Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000416
Guido van Rossum3de27361994-07-25 14:19:33 +0000417A. Very stable. While the current version number would suggest it is
418in the early stages of development, in fact new, stable releases
419(numbered 0.9.x and 1.0.x) have been coming out roughly every 3 to 6
Guido van Rossum95f61a71994-01-26 17:23:37 +0000420months for the past four years.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000421
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00004222.5. Q. When will the next version be released?
423
424A. I am planning to release 1.1 before October 15. It will contain
425tons of changes, including (again) improved portability, especially
426better support for DOS, Windows, Windows NT an the Macintosh. A few
427modules will have been converted to the new naming scheme. A working
428signal module will be present.
429
4302.6. Q. What new developments are expected for Python in the future?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000431
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000432A. A proposal is being worked out to change the semantics of operator
433overloading (__add__, __mul__ etc.) to make them more useful for
434implementing types that don't resemble numbers. Additions will be
435__call__ (to call an instance as if it were a function) and __eq__,
436_lt__ etc. (to override individual comparisons). A (new) pthreads
437interface has been submitted which will be integrated in the next
438release. The X11/Motif interface will be improved. There are ideas
439about built-in help using strings placed into function objects, and
440possibly a switch statement.
441
442There will be better ports to the Mac, DOS, Windows, Windows NT, and
Guido van Rossumf8c76d01994-08-17 12:19:53 +0000443OS/2. The Windows ports will support dynamically loaded modules using
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000444DLLs.
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000445
446Also planned is improved support for embedding Python in other
447applications, e.g. by renaming most global symbols to have a "Py"
448prefix and providing more documentation and threading support.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000449
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00004502.7. Q. Is it reasonable to propose incompatible changes to Python?
Guido van Rossum3de27361994-07-25 14:19:33 +0000451
452A. In general, no. There are already millions of lines of Python code
453around the world, so any changes in the language that invalidates more
454than a very small fraction of existing programs has to be frowned
455upon. Even if you can provide a conversion program, there still is
456the problem of updating all documentation. Providing a gradual
457upgrade path is the only way if a feature has to be changed.
458
Guido van Rossuma7925f11994-01-26 10:20:16 +0000459
4603. Building Python
461==================
462
Guido van Rossum91f60831994-02-15 15:52:27 +00004633.1. Q. Is there a test set?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000464
465A. Yes, simply do "import testall" (or "import autotest" if you aren't
466interested in the output). The standard modules whose name begins
467with "test" together comprise the test. The test set doesn't test
468*all* features of Python but it goes a long way to confirm that a new
469port is actually working. The Makefile contains an entry "make test"
470which runs the autotest module.
471
Guido van Rossum91f60831994-02-15 15:52:27 +00004723.2. Q. When running the test set, I get complaints about floating point
Guido van Rossuma7925f11994-01-26 10:20:16 +0000473operations, but when playing with floating point operations I cannot
474find anything wrong with them.
475
476A. The test set makes occasional unwarranted assumptions about the
477semantics of C floating point operations. Until someone donates a
478better floating point test set, you will have to comment out the
479offending floating point tests and execute similar tests manually.
480
Guido van Rossum061f1821994-10-06 16:03:45 +00004813.3. Q. Link errors building Python with STDWIN 0.9.8. on SGI IRIX.
Guido van Rossum24349991994-02-02 14:12:45 +0000482
Guido van Rossum061f1821994-10-06 16:03:45 +0000483A. Get STDWIN 0.9.9 from ftp://ftp.cwi.nl/pub/stdwin/stdwin0.9.9.tar.gz.
484
485Q. Link errors building Python with STDWIN 0.9.9.
486
487A. Probably routines liek 'tereate', 'tenew' etc. The STDWIN 0.9.9
488distribution requires that you add TWO libraries from stdwin to the
489line for stdwin in the Setupfile. Use something like this (all on one
490line!):
491
492stdwin stdwinmodule.c -I$(STDWIN)/H $(STDWIN)/Packs/textedit/libtextedit.a $(STDWIN)/Ports/x11/libstdwin.a -lX11
Guido van Rossum24349991994-02-02 14:12:45 +0000493
Guido van Rossum91f60831994-02-15 15:52:27 +00004943.4. Q. Link errors after rerunning the configure script.
Guido van Rossum24349991994-02-02 14:12:45 +0000495
496A. It is generally necessary to run "make clean" after a configuration
497change.
498
Guido van Rossum91f60831994-02-15 15:52:27 +00004993.5. Q. The python interpreter complains about options passed to a
Guido van Rossum24349991994-02-02 14:12:45 +0000500script (after the script name).
501
502A. You are probably linking with GNU getopt, e.g. through -liberty.
Guido van Rossumf8c76d01994-08-17 12:19:53 +0000503Don't. The reason for the complaint is that GNU getopt, unlike System
504V getopt and other getopt implementations, doesn't consider a
505non-option to be the end of the option list. A quick (and compatible)
506fix for scripts is to add "--" to the interpreter, like this:
507
508 #! /usr/local/bin/python --
509
510You can also use this interactively:
511
512 python -- script.py [options]
Guido van Rossum24349991994-02-02 14:12:45 +0000513
Guido van Rossum91f60831994-02-15 15:52:27 +00005143.6. Q. When building on the SGI, make tries to run python to create
Guido van Rossum24349991994-02-02 14:12:45 +0000515glmodule.c, but python hasn't been built or installed yet.
516
517A. Comment out the line mentioning glmodule.c in Setup and build a
518python without gl first; install it or make sure it is in your $PATH,
519then edit the Setup file again to turn on the gl module, and make
520again. You don't need to do "make clean"; you do need to run "make
521Makefile" in the Modules subdirectory (or just run "make" at the
522toplevel).
523
Guido van Rossum3de27361994-07-25 14:19:33 +00005243.7. Q. Python built with gcc for the DEC Alpha doesn't work.
525
526People have reported problems with both gcc 2.5.8 and 2.6.0. The DEC
527OSF/1 cc compiler does not have these problems so it's probably gcc's
528fault. One person reported that the problem went away when using -g
529instead of -O so this may be an option if you insist on using gcc. If
530someone tracks it down more completely I'd like to hear about it!
531
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00005323.8. Q. I use VPATH but some targets are built in the source directory.
533
534A. On some systems (e.g. Sun), if the target already exists in the
535source directory, it is created there instead of in the build
536directory. This is usually because you have previously built without
537VPATH. Try running "make clobber" in the source directory.
538
5393.9. Q. Trouble building or linking with the GNU readline library.
540
541A. Consider using readline 2.0. From the Python 1.1 README:
542
543- You can use the GNU readline library to improve the interactive user
544interface: this gives you line editing and command history when
545calling python interactively. You need to configure build the GNU
546readline library before running the configure script. Its sources are
547no longer distributed with Python; you can ftp them from any GNU
548mirror site, or from its home site:
549ftp://slc2.ins.cwru.edu/pub/dist/readline-2.0.tar.gz (or a higher
550version number -- using version 1.x is not recommended). Pass the
551Python configure script the option --with-readline=DIRECTORY where
552DIRECTORY is the absolute pathname of the directory where you've built
553the readline library. Some hints on building and using the readline
554library:
555
556- On SGI IRIX 5, you may have to add the following
557to rldefs.h:
558
559#ifndef sigmask
560#define sigmask(sig) (1L << ((sig)-1))
561#endif
562
563- On most systems, you will have to add #include "rldefs.h" to the
564top of several source files, and if you use the VPATH feature, you
565will have to add dependencies of the form foo.o: foo.c to the
566Makefile for several values of foo.
567
568- The readline library requires use of the termcap library. A
569known problem with this is that it contains entry points which
Guido van Rossum061f1821994-10-06 16:03:45 +0000570cause conflicts with the STDWIN and SGI GL libraries. The STDWIN
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000571conflict can be solved (and will be, in the next release of
Guido van Rossum061f1821994-10-06 16:03:45 +0000572STDWIN) by adding a line saying '#define werase w_erase' to the
573stdwin.h file (in the STDWIN distribution, subdirectory H). The
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000574GL conflict has been solved in the Python configure script by a
575hack that forces use of the static version of the termcap library.
576
577- Check the newsgroup gnu.bash.bugs for specific problems with the
578readline library (I don't get this group here but I've been told
579that it is the place for readline bugs.)
580
5813.10. Q. Trouble building Python on Linux.
582
583A. There shouldn't be any -- I've seen several complaints but more
584reports on successful "out-of-the-box" ports on Linux. The standard
585configure script runs just fine on Linux.
586
5873.11. Q. Other trouble building Python on platform X.
Guido van Rossum95f61a71994-01-26 17:23:37 +0000588
589A. Please email the details to <guido@cwi.nl> and I'll look into it.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000590
591
5924. Programming in Python
593========================
594
Guido van Rossum24349991994-02-02 14:12:45 +00005954.1. Q. Is there a source code level debugger with breakpoints, step,
596etc.?
597
598A. Yes. Check out module pdb; pdb.help() prints the documentation (or
599you can read it as Lib/pdb.doc). If you use the STDWIN option,
600there's also a windowing interface, wdb. You can write your own
601debugger by using the code for pdb or wdb as an example.
602
6034.2. Q. Can I create an object class with some methods implemented in
Guido van Rossuma7925f11994-01-26 10:20:16 +0000604C and others in Python (e.g. through inheritance)? (Also phrased as:
605Can I use a built-in type as base class?)
606
607A. No, but you can easily create a Python class which serves as a
608wrapper around a built-in object, e.g. (for dictionaries):
609
610 # A user-defined class behaving almost identical
611 # to a built-in dictionary.
612 class UserDict:
613 def __init__(self): self.data = {}
614 def __repr__(self): return repr(self.data)
615 def __cmp__(self, dict):
616 if type(dict) == type(self.data):
617 return cmp(self.data, dict)
618 else:
619 return cmp(self.data, dict.data)
620 def __len__(self): return len(self.data)
621 def __getitem__(self, key): return self.data[key]
622 def __setitem__(self, key, item): self.data[key] = item
623 def __delitem__(self, key): del self.data[key]
624 def keys(self): return self.data.keys()
625 def items(self): return self.data.items()
626 def values(self): return self.data.values()
627 def has_key(self, key): return self.data.has_key(key)
628
Guido van Rossum24349991994-02-02 14:12:45 +00006294.3. Q. Is there a curses/termcap package for Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000630
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000631A. Yes -- Lance Ellinghouse has written a module that interfaces to
632System V's "ncurses". If you know a little curses and some Python,
633it's straightforward to use.
634
635You could also consider using the "alfa" (== character cell) version
636of STDWIN. (STDWIN == Standard Windows, a portable windowing system
637interface by the same author, URL: "ftp://ftp.cwi.nl/pub/stdwin".) This
638will also prepare your program for porting to windowing environments
639such as X11 or the Macintosh.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000640
Guido van Rossum24349991994-02-02 14:12:45 +00006414.4. Q. Is there an equivalent to C's onexit() in Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000642
643A. Yes, if you import sys and assign a function to sys.exitfunc, it
644will be called when your program exits, is killed by an unhandled
645exception, or (on UNIX) receives a SIGHUP or SIGTERM signal.
646
Guido van Rossum24349991994-02-02 14:12:45 +00006474.5. Q. When I define a function nested inside another function, the
Guido van Rossuma7925f11994-01-26 10:20:16 +0000648nested function seemingly can't access the local variables of the
649outer function. What is going on? How do I pass local data to a
650nested function?
651
652A. Python does not have arbitrarily nested scopes. When you need to
653create a function that needs to access some data which you have
654available locally, create a new class to hold the data and return a
655method of an instance of that class, e.g.:
656
657 class MultiplierClass:
658 def __init__(self, factor):
659 self.factor = factor
660 def multiplier(self, argument):
661 return argument * self.factor
662
663 def generate_multiplier(factor):
664 return MultiplierClass(factor).multiplier
665
666 twice = generate_multiplier(2)
667 print twice(10)
668 # Output: 20
669
Guido van Rossum24349991994-02-02 14:12:45 +00006704.6. Q. How do I iterate over a sequence in reverse order?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000671
672A. If it is a list, the fastest solution is
673
674 list.reverse()
675 try:
676 for x in list:
677 "do something with x"
678 finally:
679 list.reverse()
680
681This has the disadvantage that while you are in the loop, the list
682is temporarily reversed. If you don't like this, you can make a copy.
683This appears expensive but is actually faster than other solutions:
684
685 rev = list[:]
686 rev.reverse()
687 for x in rev:
688 <do something with x>
689
690If it isn't a list, a more general but slower solution is:
691
692 i = len(list)
693 while i > 0:
694 i = i-1
695 x = list[i]
696 <do something with x>
697
698A more elegant solution, is to define a class which acts as a sequence
699and yields the elements in reverse order (solution due to Steve
700Majewski):
701
702 class Rev:
703 def __init__(self, seq):
704 self.forw = seq
705 def __len__(self):
706 return len(self.forw)
707 def __getitem__(self, i):
708 return self.forw[-(i + 1)]
709
710You can now simply write:
711
712 for x in Rev(list):
713 <do something with x>
714
Guido van Rossum061f1821994-10-06 16:03:45 +0000715Unfortunately, this solution is slowest of all, due to the method
Guido van Rossuma7925f11994-01-26 10:20:16 +0000716call overhead...
717
Guido van Rossum24349991994-02-02 14:12:45 +00007184.7. Q. My program is too slow. How do I speed it up?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000719
720A. That's a tough one, in general. There are many tricks to speed up
721Python code; I would consider rewriting parts in C only as a last
722resort. One thing to notice is that function and (especially) method
723calls are rather expensive; if you have designed a purely OO interface
724with lots of tiny functions that don't do much more than get or set an
725instance variable or call another method, you may consider using a
726more direct way, e.g. directly accessing instance variables. Also see
727the standard module "profile" (described in the file
728"python/lib/profile.doc") which makes it possible to find out where
729your program is spending most of its time (if you have some patience
730-- the profiling itself can slow your program down by an order of
731magnitude).
732
Guido van Rossum24349991994-02-02 14:12:45 +00007334.8. Q. When I have imported a module, then edit it, and import it
Guido van Rossuma7925f11994-01-26 10:20:16 +0000734again (into the same Python process), the changes don't seem to take
735place. What is going on?
736
737A. For efficiency reasons, Python only reads the module file on the
738first time a module is imported (otherwise a program consisting of
739many modules, each of which imports the same basic module, would read
740the basic module over and over again). To force a changed module
741being read again, do this:
742
743 import modname
744 reload(modname)
745
746Warning: this technique is not 100% fool-proof. In particular,
747modules containing statements like
748
749 from modname import some_objects
750
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000751will continue to work with the old version of the imported objects.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000752
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00007534.9. Q. How do I find the current module name?
Guido van Rossum24349991994-02-02 14:12:45 +0000754
Guido van Rossum061f1821994-10-06 16:03:45 +0000755A. A module can find out its own module name by looking at the
Guido van Rossum24349991994-02-02 14:12:45 +0000756(predefined) global variable __name__. If this has the value
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000757'__main__' you are running as a script.
758
7594.10. Q. I have a module in which I want to execute some extra code when it
760is run as a script. How do I find out whether I am running as a
761script?
762
763A. See the previous question. E.g. if you put the following on the
764last line of your module, main() is called only when your module is
765running as a script:
Guido van Rossum24349991994-02-02 14:12:45 +0000766
767 if __name__ == '__main__': main()
768
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00007694.11. Q. I try to run a program from the Demo directory but it fails with
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000770ImportError: No module named ...; what gives?
771
Guido van Rossum061f1821994-10-06 16:03:45 +0000772A. This is probably an optional module (written in C!) which hasn't
773been configured on your system. This especially happens with modules
774like "stdwin", "gl", "Xt" or "Xm". For STDWIN and many other modules,
775see Modules/Setup.in for info on how to add these modules to your
776Python, if it is possible at all. Sometimes you will have to ftp and
777build another package first (e.g. STDWIN). Sometimes the module only
778works on specific platforms (e.g. gl only works on SGI machines).
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000779
Guido van Rossum061f1821994-10-06 16:03:45 +0000780For X-related modules (Xt and Xm) you will have to do more work: they
781are currently not part of the standard Python distribution. You will
782have to ftp the file "extensions.tar.gz" file from a Python ftp
783repository (e.g. ftp.cwi.nl) and follow the instructions there. Note:
784the X related modules are still somewhat flakey, so don't try this
785unless you alread know a bit or two about building X applications on
786your platform.
787
788See also the next question.
789
7904.12. Q. I have successfully built Python with STDWIN but it can't
791find some modules (e.g. stdwinevents).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000792
793A. There's a subdirectory of the library directory named 'stdwin'
794which should be in the default module search path. There's a line in
795Modules/Setup(.in) that you have to enable for this purpose --
Guido van Rossum061f1821994-10-06 16:03:45 +0000796unfortunately in the latest release it's not near the other
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000797STDWIN-related lines so it's easy to miss it.
798
7994.13. Q. What GUI toolkits exist for Python?
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000800
801A. Depending on what platform(s) you are aiming at, there are several.
802
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000803- There's a neat object-oriented interface to the Tcl/Tk widget set,
804called Tkinter. You can ftp it from ftp.cwi.nl as
805pub/python/tkinter.tar.gz. This is probably the easiest to install
806and use, and the most complete widget set.
807
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000808- The standard Python distribution comes with an interface to STDWIN,
809a platform-independent low-level windowing interface (you have to ftp
Guido van Rossum061f1821994-10-06 16:03:45 +0000810the source for STDWIN separately, e.g. from ftp.cwi.nl in pub/stdwin
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000811or gatekeeper.dec.com in pub/misc/stdwin). STDWIN runs under X11 or
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000812the Mac; a Windows port has been attempted but I can't seem to get it
813working. Note that STDWIN is really not powerful enough to implement
814a modern GUI (no widgets, etc.) and that I don't have the time to
815maintain or extend it, so you may be better off using Tkinter or the
816Motif interface, unless you require portability to the Mac (which is
817also offered by SUIT, by the way -- see below).
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000818
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000819- For SGI IRIX only, there's an interface to the complete GL (Graphics
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000820Library -- low level but very good 3D capabilities) as well as to
821FORMS (a buttons-and-sliders-etc package built on top of GL by Mark
822Overmars -- ftp'able from ftp.cs.ruu.nl in pub/SGI/FORMS).
823
824- There's an interface to X11, including the Athena and Motif widget
825sets (and a few individual widgets, like Mosaic's HTML widget and
826SGI's GL widget) in the Extensions set, which is separately ftp'able
827from ftp.cwi.nl as pub/python/extensions.tar.gz.
828
829- There's an interface to SUIT, the U of Virginia's Simple User
830Interface Toolkit; it can be ftp'ed from uvacs.cs.virginia.edu as
831pub/suit/python/SUIT_python.tar.Z. A PC binary of Python 1.0.2
832compiled with DJGPP and with SUIT support built-in has been made
833available by Antonio Costa on ftp site asterix.inescn.pt, directory
834pub/PC/python, file pyt102su.exe (a self-extracting archive).
835
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000836- There's an interface to WAFE, a Tcl interface to the X11 Motif and
837Athena widget sets. Last I heard about it it was included in the
838WAFE 1.0 prerelease, ftp'able from ftp.wu-wien.ac.at as
839pub/src/X11/wafe/wafe-1.0.tar.gz-prerelease.
840
Guido van Rossum061f1821994-10-06 16:03:45 +00008414.14. Q. Are there any interfaces to database packages in Python?
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000842
843A. There's an interface to SYBASE by John Redford
844<jredford@lehman.com>.
845
846There's also an interface to metalbase by Lance Ellinghouse
847<lance@markv.com>.
848
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000849Anthony Baxter <anthony.baxter@aaii.oz.au> has written an interface to
850mSQL (mini-SQL). Ftp it from ftp.cwi.nl:/pub/python/PymSQL.tar.gz.
851
8524.15. Q. Is it possible to write obfuscated one-liners in Python?
Guido van Rossumc24da7c1994-09-23 14:08:41 +0000853
854A. Yes. See the following three examples, due to Ulf Bartelt:
855
856# Primes < 1000
857print filter(None,map(lambda y:y*reduce(lambda x,y:x*y!=0,\
858map(lambda x,y=y:y%x,range(2,int(pow(y,0.5)+1))),1),range(2,1000)))
859
860# First 10 Fibonacci numbers
861print map(lambda x,f=lambda x,f:(x<=1) or (f(x-1,f)+f(x-2,f)): f(x,f),\
862range(10))
863
864# Mandelbrot set
865print (lambda Ru,Ro,Iu,Io,IM,Sx,Sy:reduce(lambda x,y:x+y,map(lambda y,\
866Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,Sy=Sy,L=lambda yc,Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,i=IM,\
867Sx=Sx,Sy=Sy:reduce(lambda x,y:x+y,map(lambda x,xc=Ru,yc=yc,Ru=Ru,Ro=Ro,\
868i=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\
869>=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(\
87064+F(Ru+x*(Ro-Ru)/Sx,yc,0,0,i)),range(Sx))):L(Iu+y*(Io-Iu)/Sy),range(Sy\
871))))(-2.1, 0.7, -1.2, 1.2, 30, 80, 24)
872# \___ ___/ \___ ___/ | | |__ lines on screen
873# V V | |______ columns on screen
874# | | |__________ maximum of "iterations"
875# | |_________________ range on y axis
876# |____________________________ range on x axis
877
878Don't try this at home, kids!
879
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00008804.16. Q. Is there an equivalent of C's "?:" ternary operator?
Guido van Rossumc24da7c1994-09-23 14:08:41 +0000881
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000882A. Not directly. In many cases you can mimic a?b:c with "a and b or
883c", but there's a flaw: if b is zero (or empty, or None -- anything
884that tests false) then c will be selected instead. In many cases you
885can prove by looking at the code that this can't happen (e.g. because
886b is a constant or has a type that can never be false), but in general
887this can be a problem.
888
889Steve Majewski (or was it Tim Peters?) suggested the following
890solution: (a and [b] or [c])[0]. Because [b] is a singleton list it
891is never false, so the wrong path is never taken; then applying [0] to
892the whole thing gets the b or c that you really wanted. Ugly, but it
893gets you there in the rare cases where it is really inconvenient to
894rewrite your code using 'if'.
895
8964.17. Q. My class defines __del__ but it is not called when I delete the
897object.
898
899A. There are several possible reasons for this.
900
901- The del statement does not necessarily call __del__ -- it simply
902decrements the object's reference count, and if this reaches zero
903__del__ is called.
904
905- If your data structures contain circular links (e.g. a tree where
906each child has a parent pointer and each parent has a list of
907children) the reference counts will never go back to zero. You'll
Guido van Rossum061f1821994-10-06 16:03:45 +0000908have to define an explicit close() method which removes those
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000909pointers. Please don't ever call __del__ directly -- __del__ should
Guido van Rossum061f1821994-10-06 16:03:45 +0000910call close() and close() should make sure that it can be called more
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000911than once for the same object.
912
913- If the object has ever been a local variable (or argument, which is
914really the same thing) to a function that caught an expression in an
915except clause, chances are that a reference to the object still exists
916in that function's stack frame as contained in the stack trace.
917Normally, deleting (better: assigning None to) sys.exc_traceback will
918take care of this. If you a stack was printed for an unhandled
919exception in an interactive interpreter, delete sys.last_traceback
920instead.
921
922- There is code that deletes all objects when the interpreter exits,
923but if your Python has been configured to support threads, it is not
924called (because other threads may still be active). You can define
Guido van Rossum061f1821994-10-06 16:03:45 +0000925your own cleanup function using sys.exitfunc (see question 4.4).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000926
9274.18. Q. How do I change the shell environment for programs called using
928os.popen() or os.system()? Changing os.environ doesn't work.
929
930A. Modifying the environment passed to subshells was left out of the
931interpreter because there seemed to be no well-established portable
932way to do it.
933
934However if all you want is to pass environment variables to the
935commands run by os.system() or os.popen(), there's a simple solution:
936prefix the command string with a couple of variable assignments and
937export statements. I guess the following would be universal for popen
938(untested):
939
940import os
941from commands import mkarg # nifty routine to add shell quoting
942def epopen(cmd, mode, env = {}):
943 # env is a dictionary of environment variables
944 prefix = ''
945 for key, value in env.values():
946 prefix = prefix + '%s=%s\n' % (key, mkarg(value))
947 prefix = prefix + 'export %s\n' % key
948 return os.popen(prefix + cmd, mode)
949
9504.19. Q. What is a class?
951
952A. A class is the particular object type that is created by executing
953a class statement.
954
9554.20. Q. What is a method?
956
957A. A method is a function that you normally call as
958x.name(arguments...) for some object x. The word is used for methods
959of classes and class instances as well as for methods of built-in
960objects. The latter have a completely different implementation and
961only share the way their calls look in Python code.
962
9634.21. Q. What is self?
964
965A. Self is merely a conventional name for the first argument of a
966method -- i.e. a function defined inside a class definition. A method
967defined as meth(self, a, b, c) should be called as x.meth(a, b, c) for
968some instance x of the class in which the definition occurs;
969the called method will think it is called as meth(x, a, b, c).
970
9714.22. Q. What is a unbound method?
972
973A. An unbound method is a method defined in a class that is not yet
974bound to an instance. You get an unbound method if you ask for a
975class attribute that happens to be a function. You get a bound method
976if you ask for an instance attribute. A bound method knows which
Guido van Rossum061f1821994-10-06 16:03:45 +0000977instance it belongs to and calling it supplies the instance automatically;
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000978an unbound method only knows which class it wants for its first
979argument (a derived class is also OK). Calling an unbound method
980doesn't "magically" derive the first argument from the context -- you
981have to provide it explicitly.
982
9834.23. Q. How do I call a method defined in a base class from a derived class
984that overrides it?
985
986A. If your class definition starts with "class Derived(Base): ..."
987then you can call method meth defined in Base (or one of Base's base
988classes) as Base.meth(self, arguments...). Here, Base.meth is an
989unbound method (see previous question).
990
9914.24. Q. How do I call a method from a base class without using the name of
992the base class?
993
994A. DON'T DO THIS. REALLY. I MEAN IT. It appears that you could call
995self.__class__.__bases__[0].meth(self, arguments...) but this fails when
Guido van Rossum061f1821994-10-06 16:03:45 +0000996a doubly-derived method is derived from your class: for its instances,
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000997self.__class__.__bases__[0] is your class, not its base class -- so
998(assuming you are doing this from within Derived.meth) you would start
999a recursive call.
1000
10014.25. Q. How can I organize my code to make it easier to change the base
1002class?
1003
1004A. You could define an alias for the base class, assign the real base
1005class to it before your class definition, and use the alias throughout
1006your class. Then all you have to change is the value assigned to the
1007alias. Incidentally, this trick is also handy if you want to decide
1008dynamically (e.g. depending on availability of resources) which base
1009class to use. Example:
1010
1011BaseAlias = <real base class>
1012class Derived(BaseAlias):
1013 def meth(self):
1014 BaseAlias.meth(self)
1015 ...
1016
10174.26. Q. How can I find the methods or attributes of an object?
1018
1019A. This depends on the object type.
1020
1021For an instance x of a user-defined class, instance attributes are
1022found in the dictionary x.__dict__, and methods and attributes defined
1023by its class are found in x.__class__.__bases__[i].__dict__ (for i in
1024range(len(x.__class__.__bases__))). You'll have to walk the tree of
1025base classes to find *all* class methods and attributes.
1026
1027Many, but not all built-in types define a list of their method names
1028in x.__methods__, and if they have data attributes, their names may be
1029found in x.__members__. However this is only a convention.
1030
1031For more information, read the source of the standard (but
1032undocumented) module newdir.
1033
10344.27. Q. I can't seem to use os.read() on a pipe created with os.popen().
1035
1036A. os.read() is a low-level function which takes a file descriptor (a
1037small integer). os.popen() creates a high-level file object -- the
1038same type used for sys.std{in,out,err} and returned by the builtin
1039open() function. Thus, to read n bytes from a pipe p created with
1040os.popen(), you need to use p.read(n).
Guido van Rossuma7925f11994-01-26 10:20:16 +00001041
Guido van Rossum061f1821994-10-06 16:03:45 +000010424.28. Q. How can I create a stand-alone binary from a Python script?
1043
1044The demo script "Demo/scripts/freeze.py" does what you want. (It's
1045actually not a demo but a support tool -- there is some extra code in
1046the interpreter to accommodate it.) It requires that you have the
1047Python build tree handy, complete with all the lib*.a files.
1048
1049This works by scanning your source recursively for import statements
1050(both forms) and looking for the modules on the standard Python path
1051as well as in the source directory (for built-in modules). It then
1052"compiles" the modules written in Python to C code (array initializers
1053that can be turned into code objects using the marshal module) and
1054creates a custom-made config file that only contains those built-in
1055modules which are actually used in the program. It then compiles the
1056generated C code and links it with the rest of the Python interpreter
1057to form a self-contained binary which acts exactly like your script.
1058
1059Unfortunately, the current version is very platform-specific, because
1060each platform has its own compilation flags and libraries to link
1061with. You will probably have to edit the freeze.py file to point it
1062to the right directories and tell it about the compilation and link
1063flags for your platform. A new version will be released with Python
10641.1 -- if you want a peek write to my colleague <Jack.Jansen@cwi.nl>.
1065
10664.29. Q. Is there a special lib for writing CGI scripts in Python?
1067
1068A. There's documentation and code for a cgi.py module by Michael McLay
1069<mclay@eeel.nist.gov> available from:
1070
1071 http://www.eeel.nist.gov/python/
1072
1073(For the curious: CGI or Common Gateway Interface is the protocol
1074between HTTP servers (WWW servers) and programs/scripts they run to
1075perform queries and other tasks that require returning a dynamically
1076generated document.)
1077
10784.30. Q. What other WWW tools are there for Python?
1079
1080A. The standard library has a module urllib, which can retrieve most
1081commonly used URL types (file, ftp, http, gopher).
1082
1083The Demo2/www directory (Demo2 has to be retrieved separately from the
1084Python ftp sites) contains some (really old) code to parse HTML and to
1085display it.
1086
1087Steve Miale <smiale@cs.indiana.edu> has written a modular WWW browser
1088called Dancer. An alpha version can be FTP'ed from
1089ftp.cs.indiana.edu:/pub/smiale/dancer.tar.gz. (There are a few
1090articles about Dancer in the (hyper)mail archive.)
1091
1092
Guido van Rossuma7925f11994-01-26 10:20:16 +000010935. Extending Python
1094===================
1095
10965.1. Q. Can I create my own functions in C?
1097
1098A. Yes, you can create built-in modules containing functions,
Guido van Rossum24349991994-02-02 14:12:45 +00001099variables, exceptions and even new types in C. This is explained in
1100the document "Extending and Embedding the Python Interpreter" (the
1101LaTeX file Doc/ext.tex). Also read the chapter on dynamic loading.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001102
11035.2. Q. Can I create my own functions in C++?
1104
1105A. Yes, using the C-compatibility features found in C++. Basically
1106you place extern "C" { ... } around the Python include files and put
1107extern "C" before each function that is going to be called by the
1108Python interpreter. Global or static C++ objects with constructors
1109are probably not a good idea.
1110
Guido van Rossum7ce61c11994-06-13 15:13:56 +000011115.3. Q. How can I execute arbitrary Python statements from C?
1112
1113A. The highest-level function to do this is run_command() which takes
1114a single string argument which is executed in the context of module
1115__main__ and returns 0 for success and -1 when an exception occurred
1116(including SyntaxError). If you want more control, use run_string();
1117see the source for run_command() in Python/pythonrun.c.
1118
11195.4. Q. How can I evaluate an arbitrary Python expression from C?
1120
1121A. Call the function run_string() from the previous question with the
1122start symbol eval_input; it then parses an expression, evaluates it
1123and returns its value. See exec_eval() in Python/bltinmodule.c.
1124
11255.5. Q. How do I extract C values from a Python object?
1126
1127A. That depends on the object's type. If it's a tuple,
1128gettuplesize(o) returns its length and gettupleitem(o, i) returns its
1129i'th item; similar for lists with getlistsize(o) and getlistitem(o,
1130i). For strings, getstringsize(o) returns its length and
1131getstringvalue(o) a pointer to its value (note that Python strings may
1132contain null bytes so strlen() is not safe). To test which type an
1133object is, first make sure it isn't NULL, and then use
1134is_stringobject(o), is_tupleobject(o), is_listobject(o) etc.
1135
11365.6. Q. How do I use mkvalue() to create a tuple of arbitrary length?
1137
1138A. You can't. Use t = newtupleobject(n) instead, and fill it with
1139objects using settupleitem(t, i, o) -- note that this "eats" a
1140reference count of o. Similar for lists with newlistobject(n) and
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001141setlistitem(l, i, o). Note that you *must* set all the tuple items to
1142some value before you pass the tuple to Python code --
1143newtupleobject(n) initializes them to NULL, which isn't a valid Python
1144value.
1145
11465.7. Q. What happened to mktuple(), featuring in an example in the
1147Extensions manual?
1148
1149A. It's a typo, I meant newtupleobject() (see previous question).
1150
11515.8. Q. How do I call an object's method from C?
1152
1153A. Here's a function (untested) that might become part of the next
1154release in some form. It uses <stdarg.h> to allow passing the
1155argument list on to vmkvalue():
1156
1157object *call_method(object *inst, char *methodname, char *format, ...)
1158{
1159 object *method;
1160 object *args;
1161 object *result;
1162 va_list va;
1163 method = getattr(inst, methodname);
1164 if (method == NULL) return NULL;
1165 va_start(va, format);
1166 args = vmkvalue(format, va);
1167 va_end(va);
1168 if (args == NULL) {
1169 DECREF(method);
1170 return NULL;
1171 }
1172 result = call_object(method, args);
1173 DECREF(method);
1174 DECREF(args);
1175 return result;
1176}
1177
1178This works for any instance that has methods -- whether built-in or
1179user-defined. You are responsible for eventually DECREF'ing the
1180return value.
1181
1182To call, e.g., a file object's "seek" method with arguments 10, 0
1183(assuming the file object pointer is "f"):
1184
1185res = call_method(f, "seek", "(OO)", 10, 0);
1186if (res == NULL) {
1187 ... an exception occurred ...
1188}
1189else {
1190 DECREF(res);
1191}
1192
1193Note that since call_object() *always* wants a tuple for the argument
1194list, to call a function without arguments, pass "()" for the format,
1195and to call a function with one argument, surround the argument in
1196parentheses, e.g. "(i)".
1197
11985.9. Q. How do I catch the output from print_error()?
1199
1200A. (Due to Mark Hammond):
1201
1202* in Python code, define an object that supports the "write()" method.
1203FWIW, there seems to be a small problem that requires the 'softspace'
Guido van Rossum061f1821994-10-06 16:03:45 +00001204attribute to be defined too (I can't remember exact details of the
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001205problem).
1206
1207* redirect sys.stdout and sys.stderr to this object.
1208
Guido van Rossum061f1821994-10-06 16:03:45 +00001209* call print_error, or just allow the standard traceback mechanism to
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001210work.
1211
Guido van Rossum061f1821994-10-06 16:03:45 +00001212Then, the output will go wherever your write() method sends it.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001213
12145.10. Q. How do I access a module written in Python from C?
1215
1216A. You can get a pointer to the module object as follows:
1217
1218 module = import_module("<modulename>");
1219
1220If the module hasn't been imported yet (i.e. it is not yet present in
1221sys.modules), this initializes the module; otherwise it simply returns
1222the value of sys.modules["<modulename>"]. Note that it doesn't enter
1223the module into any namespace -- it only ensures it has been
1224initialized and is stored in sys.modules.
1225
1226You can then access the module's attributes (i.e. any name defined in
1227the module) as follows:
1228
1229 attr = getattr(module, "<attrname>");
1230
1231Calling setattr(), to assign to variables in the module, also works.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001232
Guido van Rossuma7925f11994-01-26 10:20:16 +00001233
12346. Python's design
1235==================
1236
12376.1. Q. Why isn't there a generic copying operation for objects in
1238Python?
1239
1240A. Hmm. Maybe there should be one, but it's difficult to assign a
1241useful meaning to copying of open files, sockets and windows, or
1242recursive data structures. As long as you design all your classes
1243yourself you are of course free to define a standard base class that
1244defines an overridable copying operation for all the objects you care
1245about. (One practical point: it would have to be a built-in function,
1246not a standard method name, since not all built-in object types have
1247methods; e.g. strings, integers and tuples don't.)
1248
12496.2. Q. Why isn't there a generic way to implement persistent objects
1250in Python? (Persistent == automatically saved to and restored from
1251disk.)
1252
1253A. Hmm, hmm. Basically for the same reasons as why there is no
1254generic copying operation.
1255
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001256However, since there is a real desire to have persistent operations,
1257I'm thinking of extending the marshal module to support object sharing
1258(and hence recursive objects) and to extend the list of supported
1259types considerably. For user-defined classes, hooks with __*__ names
1260will allow the class to modify the way their instances are dumped and
1261loaded. Built-in types (including those defined by new extensions)
1262may also define dump/load hooks. There are several problems still to
1263solve, e.g. how do you reliably find the class of which an object is
1264an instance at load time -- assuming the class itself is loaded from
1265some module, and not part of the dumped data. It is also necessary to
1266separate the functionality of converting a set of objects into a byte
1267stream from which they can be reloaded, from the ability to save these
1268byte streams as files and being able to reference an object by a
1269persistent global name.
1270
Guido van Rossuma7925f11994-01-26 10:20:16 +000012716.3. Q. Why isn't there a switch or case statement in Python?
1272
1273A. You can do this easily enough with a sequence of
1274if... elif... elif... else. There have been some proposals for switch
Guido van Rossum061f1821994-10-06 16:03:45 +00001275statement syntax, but there is no consensus (yet) on whether and how
Guido van Rossuma7925f11994-01-26 10:20:16 +00001276to do range tests.
1277
Guido van Rossumc50158e1994-05-31 09:18:50 +000012786.4. Q. Why does Python use indentation for grouping of statements?
1279
1280A. Basically I believe that using indentation for grouping is
1281extremely elegant and contributes a lot to the clarity of the average
1282Python program. Most people learn to love this feature after a while.
1283Some arguments for it:
1284
1285- Since there are no begin/end brackets there cannot be a disagreement
1286between grouping perceived by the parser and the human reader. I
1287remember long ago seeing a C fragment like this:
1288
1289 if (x <= y)
1290 x++;
1291 y--;
1292 z++;
1293
1294and staring a long time at it wondering why y was being decremented
1295even for x > y... (And I wasn't a C newbie then either.)
1296
1297- Since there are no begin/end brackets there can be no conflicting
1298coding styles. In C there are loads of different ways to place the
1299braces (including the choice whether to place braces around single
1300statements in certain cases, for consistency). If you're used to
1301reading (and writing) code that uses one style, you will feel at least
1302slightly uneasy when reading (or being required to write) another
1303style.
1304
1305- Many coding styles place begin/end brackets on a line by themself.
1306This makes programs considerably longer and wastes valuable screen
1307space, making it harder to get a good overview over a program.
1308Ideally, a function should fit on one basic tty screen (say, 20
1309lines). 20 lines of Python are worth a LOT more than 20 lines of C.
1310This is not solely due to the lack of begin/end brackets (the lack of
1311declarations also helps, and the powerful operations of course), but
1312it certainly helps!
1313
Guido van Rossum3de27361994-07-25 14:19:33 +000013146.5. Q. Why are Python strings immutable?
1315
1316A. There are two advantages. One is performance: knowing that a
1317string is immutable makes it easy to lay it out at construction time
1318-- fixed and unchanging storage requirements. (This is also one of
Guido van Rossum061f1821994-10-06 16:03:45 +00001319the reasons for the distinction between tuples and lists.) The
Guido van Rossum3de27361994-07-25 14:19:33 +00001320other is that strings in Python are considered as "elemental" as
1321numbers. No amount of activity will change the value 8 to anything
1322else, and in Python, no amount of activity will change the string
1323"eight" to anything else. (Adapted from Jim Roskind)
1324
13256.6. Q. Why don't strings have methods like index() or sort(), like
1326lists?
1327
1328A. Good question. Strings currently don't have methods at all
1329(likewise tuples and numbers). Long ago, it seemed unnecessary to
1330implement any of these functions in C, so a standard library module
1331"string" written in Python was created that performs string related
1332operations. Since then, the cry for performance has moved most of
1333them into the built-in module strop (this is imported by module
Guido van Rossumf8c76d01994-08-17 12:19:53 +00001334string, which is still the preferred interface, without loss of
Guido van Rossum3de27361994-07-25 14:19:33 +00001335performance except during initialization). Some of these functions
1336(e.g. index()) could easily be implemented as string methods instead,
1337but others (e.g. sort()) can't, since their interface prescribes that
1338they modify the object, while strings are immutable (see the previous
1339question).
1340
13416.7. Q. Why does Python use methods for some functionality
1342(e.g. list.index()) but functions for other (e.g. len(list))?
1343
1344A. Functions are used for those operations that are generic for a
1345group of types and which should work even for objects that don't have
1346methods at all (e.g. numbers, strings, tuples). Also, implementing
1347len(), max(), min() as a built-in function is actually less code than
1348implementing them as methods for each type. One can quibble about
1349individual cases but it's really too late to change such things
1350fundamentally now.
1351
13526.8. Q. Why can't I derive a class from built-in types (e.g. lists or
1353files)?
1354
1355A. This is caused by the relatively late addition of (user-defined)
1356classes to the language -- the implementation framework doesn't easily
1357allow it. See the answer to question 4.2 for a work-around. This
1358*may* be fixed in the (distant) future.
1359
13606.9. Q. Why must 'self' be declared and used explicitly in method
1361definitions and calls?
1362
1363A. By asking this question you reveal your C++ background. :-)
1364When I added classes, this was (again) the simplest way of
1365implementing methods without too many changes to the interpreter. I
1366borrowed the idea from Modula-3. It turns out to be very useful, for
1367a variety of reasons.
1368
1369First, it makes it more obvious that you are using a method or
1370instance attribute instead of a local variable. Reading "self.x" or
1371"self.meth()" makes it absolutely clear that an instance variable or
1372method is used even if you don't know the class definition by heart.
1373In C++, you can sort of tell by the lack of a local variable
Guido van Rossum061f1821994-10-06 16:03:45 +00001374declaration (assuming globals are rare or easily recognizable) -- but
Guido van Rossum3de27361994-07-25 14:19:33 +00001375in Python, there are no local variable declarations, so you'd have to
1376look up the class definition to be sure.
1377
1378Second, it means that no special syntax is necessary if you want to
1379explicitly reference or call the method from a particular class. In
1380C++, if you want to use a method from base class that is overridden in
1381a derived class, you have to use the :: operator -- in Python you can
1382write baseclass.methodname(self, <argument list>). This is
1383particularly useful for __init__() methods, and in general in cases
1384where a derived class method wants to extend the base class method of
1385the same name and thus has to call the base class method somehow.
1386
1387Lastly, for instance variables, it solves a syntactic problem with
1388assignment: since local variables in Python are (by definition!) those
1389variables to which a value assigned in a function body (and that
1390aren't explicitly declared global), there has to be some way to tell
1391the interpreter that an assignment was meant to assign to an instance
1392variable instead of to a local variable, and it should preferably be
1393syntactic (for efficiency reasons). C++ does this through
1394declarations, but Python doesn't have declarations and it would be a
1395pity having to introduce them just for this purpose. Using the
1396explicit "self.var" solves this nicely. Similarly, for using instance
1397variables, having to write "self.var" means that references to
1398unqualified names inside a method don't have to search the instance's
1399directories.
1400
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000014016.10. Q. Can't you emulate threads in the interpreter instead of
1402relying on an OS-specific thread implementation?
1403
1404A. Unfortunately, the interpreter pushes at least one C stack frame
1405for each Python stack frame. Also, extensions can call back into
1406Python at almost random moments. Therefore a complete threads
1407implementation requires thread support for C.
1408
Guido van Rossum061f1821994-10-06 16:03:45 +000014096.11. Q. Why can't lambda forms contain statements?
1410
1411A. Python lambda forms cannot contain statements because Python's
1412syntactic framework can't handle statements nested inside functions.
1413
1414However, in Python, this is not a serious problem. Unlike lambda
1415forms in other languages, where they add functionality, Python lambdas
1416are only a shorthand notation if you're too lazy to define a function.
1417
1418Functions are already first class objects in Python, and can be
1419declared in a local scope. Therefore the only advantage of using a
1420lambda form instead of a locally-defined function is that you'll have
1421to invent a name for the function -- but that's just a local variable
1422to which the function object (which is exactly the same type of object
1423that a lambda form yields) is assigned!
1424
14256.12. Q. Why is there no more efficient way of iterating over a dictionary
1426than first constructing the list of keys()?
1427
1428A. Have you tried it? I bet it's fast enough for your purposes! In
1429most cases such a list takes only a few percent of the space occupied
1430by the dictionary -- it needs only 4 bytes (the size of a pointer) per
1431key -- a dictionary costs 8 bytes per key plus between 30 and 70
1432percent hash table overhead, plus the space for the keys and values --
1433by necessity all keys are unique objects and a string object (the most
1434common key type) costs at least 18 bytes plus the length of the
1435string. Add to that the values contained in the dictionary, and you
1436see that 4 bytes more per item really isn't that much more memory...
1437
1438A call to dict.keys() makes one fast scan over the dictionary
1439(internally, the iteration function does exist) copying the pointers
1440to the key objects into a pre-allocated list object of the right size.
1441The iteration time isn't lost (since you'll have to iterate anyway --
1442unless in the majority of cases your loop terminates very prematurely
1443(which I doubt since you're getting the keys in random order).
1444
1445I don't expose the dictionary iteration operation to Python
1446programmers because the dictionary shouldn't be modified during the
1447entire iteration -- if it is, there's a very small chance that the
1448dictionary is reorganized because the hash table becomes too full, and
1449then the iteration may miss some items and see others twice. Exactly
1450because this only occurs rarely, it would lead to hidden bugs in
1451programs: it's easy never to have it happen during test runs if you
1452only insert or delete a few items per iteration -- but your users will
1453surely hit upon it sooner or later.
1454
Guido van Rossuma7925f11994-01-26 10:20:16 +00001455
14567. Using Python on non-UNIX platforms
1457=====================================
1458
Guido van Rossum91f60831994-02-15 15:52:27 +000014597.1. Q. Is there a Mac version of Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +00001460
Guido van Rossum91f60831994-02-15 15:52:27 +00001461A. Yes. It is on most ftp sites carrying Python as python.sea.hqx --
1462this is a self-extracting archive containing the application binary as
1463well as the Lib modules.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001464
Guido van Rossum91f60831994-02-15 15:52:27 +000014657.2. Q. Is there a DOS version of Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +00001466
Guido van Rossum91f60831994-02-15 15:52:27 +00001467A. Yes. More than one, actually: 16python.exe runs in standard DOS
1468mode on 186 CPUs or higher; 32python.exe uses a DOS extender and only
1469runs on a 386 or higher CPUs. Although 16python.exe does not pass the
1470test set because test_grammar is too big for the parser, it actually
1471has about 270 kbyte of allocatable heap space, which is sufficient for
1472fairly large programs. 32python.exe is distributed as a tar file
1473containing the required DOS extended and 387 emulator. Both are on
1474most ftp sites carrying Python.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001475
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001476The file dosbuild.tar.gz on the standard ftp sites
1477(e.g. ftp.cwi.nl:/pub/python/) contains rudimentary Makefiles and
1478instructions.
1479
Guido van Rossum91f60831994-02-15 15:52:27 +000014807.3. Q. Is there a Windows version of Python?
1481
1482A. Yes. Use qwpython.exe. The only problem with it: ^C
1483unconditionally kills the entire program -- it does not raise
1484KeyboardInterrupt. You can also run 16python.exe or 32python.exe in a
1485"DOS box", but qwpython.exe appears to be slightly faster.
1486
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001487There aren't any Makefiles at this moment. Sorry.
1488
1489Using Win32s (a free NT compatibility package by Microsoft) you can
1490also use the NT version by Mark Hammond -- the Win32s package is also
1491in that directory (you'll need several MB of disk space to install
1492it). See the next question.
1493
Guido van Rossum91f60831994-02-15 15:52:27 +000014947.4. Q. Is there a Windows NT version of Python?
1495
1496A. Yes. Use ntpython.exe. This is for Intel CPUs. If you want a
1497Windows user interface, use qwpython.exe.
1498
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001499Mark Hammond <MHammond@cmutual.com.au> is building a better NT port.
1500This supports using DLLs for dynamic loading of Python modules, and
1501includes an interface to the Microsoft Foundation Classes and a Python
1502programming environment using it that's written mostly in Python. A
1503prerelease (source and binaries) can be ftp'ed from
1504ftp.cwi.nl:/pub/python/nt/ -- most mirrors will also have this. A
1505thread module is also planned but currently low on Mark's list of
1506priorities.
1507
Guido van Rossum061f1821994-10-06 16:03:45 +00001508To build Python for the Windows NT on the DEC Alpha AXP, retrieve a
1509zipfile with Makefiles for NT from the following URL:
1510"ftp://ftp.ksc.nasa.gov/pub.win3.private.proto/python-make.zip". This
1511was contributed by Sam Rushing <rushing@squirl.oau.org>.
1512
1513Note that currently there is no unified compilation environment for
1514all NT platforms -- hopefully Microsoft will fix this with the release
1515of Visual C++ 2.0.
1516
Guido van Rossum7ce61c11994-06-13 15:13:56 +000015177.5. Q. Is there an OS/2 version of Python?
1518
1519A. Yes. You can ftp it (from ftp.cwi.nl in pub/python, or from the
1520mirror sites) as pyth_os2.zip. This contains both an executable and
1521Makefiles for those fortunate enough to have a C compiler.
1522
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000015237.6. Q. Is there a VMS version of Python?
1524
1525A. I think not (yet). This question has been asked on the list
1526several times and I've never seen an answer. Maybe someone with a VMS
1527C compiler could attempt a port? Probably coming up with proper
1528Makefiles, config.h and config.c should be sufficient. If you succeed
1529(or even if you get stuck halfway), please let me know! (Info as of
153023 September 1994.)
1531
15327.7. Q. What about IBM mainframes, or other esoteric non-UNIX
1533platforms?
1534
1535A. Basically, the same story as for VMS... (Info as of 23 September
15361994.)
1537
1538For ports of Windows NT to non-Intel platforms, the normal NT port
1539should work except you may have to use a different Makefile.
1540(Unconfirmed suggestions from the Python list.)
1541
15427.8. Q. Where are the source or Makefiles for the non-UNIX versions?
1543
1544A. The standard sources can (almost) be used. See the previous
1545questions for availability of Makefiles/projects or patches. If you
1546find things in the standard sources that don't work on your particular
1547platform, please let me know and I'll integrate a solution into the
1548next release of the standard sources. If you submit a fix, please use
1549some kind of #ifdef so as to keep the source working for other
1550platforms. In particular, if the patch works around the availability
1551of a particular function of header file, you should mimic the
1552HAVE_... style used by the configure script -- you can then submit a
1553config.h file for a particular platform so there are no absolutely
1554platform-specific #ifdefs in the rest of the sources.
1555
15567.9. Q. What is the status and support for the non-UNIX versions?
1557
1558A. I don't have access to most of these platforms, so in general I am
1559dependent on material submitted by volunteers(*). However I strive to
1560integrate all changes needed to get it to compile on a particular
1561platform back into the standard sources, so porting of the next
1562version to the various non-UNIX platforms should be easy.
1563
1564(*) For the Macintosh, that volunteer is me.
1565
15667.10. Q. I have the PC version but it appears to be only a binary.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001567Where's the library?
Guido van Rossuma7925f11994-01-26 10:20:16 +00001568
1569A. You still need to copy the files from the distribution directory
Guido van Rossum91f60831994-02-15 15:52:27 +00001570"python/Lib" to your system. If you don't have the full distribution,
Guido van Rossum3de27361994-07-25 14:19:33 +00001571you can get the file pythonlib<version>.tar.gz from most ftp sites
1572carrying Python; this is a subset of the distribution containing just
1573those file.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001574
Guido van Rossum5333c5d1994-04-11 11:06:22 +00001575Once you have installed the library, you need to point sys.path to it.
1576Assuming the library is in C:\misc\python\lib, the following commands
1577will point your Python interpreter to it (note the doubled backslashes
1578-- you can also use single forward slashes instead):
1579
1580 >>> import sys
1581 >>> sys.path.insert(0, 'C:\\misc\\python\\lib')
1582 >>>
1583
1584For a more permanent effect, set the environment variable PYTHONPATH,
1585as follows (talking to a DOS prompt):
1586
1587 C> SET PYTHONPATH=C:\misc\python\lib
1588
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000015897.11. Q. Where's the documentation for the Mac or PC version?
Guido van Rossuma7925f11994-01-26 10:20:16 +00001590
1591A. There isn't any. The documentation for the Unix version also
Guido van Rossum91f60831994-02-15 15:52:27 +00001592applies to the Mac and PC versions. Where applicable, differences
Guido van Rossuma7925f11994-01-26 10:20:16 +00001593are indicated in the text.
1594
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000015957.12. Q. The Mac (PC) version doesn't seem to have any facilities for
Guido van Rossum91f60831994-02-15 15:52:27 +00001596creating or editing programs apart from entering it interactively, and
1597there seems to be no way to save code that was entered interactively.
1598How do I create a Python program on the Mac (PC)?
Guido van Rossuma7925f11994-01-26 10:20:16 +00001599
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001600A. Use an external editor. On the Mac, BBEdit seems to be a popular
1601no-frills text editor. I work like this: start the interpreter; edit
1602a module file using BBedit; import and test it in the interpreter;
1603edit again in BBedit; then use the built-in function reload() to
1604re-read the imported module; etc.
Guido van Rossum5333c5d1994-04-11 11:06:22 +00001605
1606Regarding the same question for the PC, Kurt Wm. Hemr writes: "While
1607anyone with a pulse could certainly figure out how to do the same on
1608MS-Windows, I would recommend the NotGNU Emacs clone for MS-Windows.
1609Not only can you easily resave and "reload()" from Python after making
1610changes, but since WinNot auto-copies to the clipboard any text you
1611select, you can simply select the entire procedure (function) which
1612you changed in WinNot, switch to QWPython, and shift-ins to reenter
1613the changed program unit."