blob: a93f3adf86b55b364e083b34ed4812ffb88d1917 [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 Rossum05151e01995-09-28 13:24:46 +00006Expires: Wed, 1 Nov 1995 00:00:00 GMT
Guido van Rossume7d92d51995-10-11 18:06:54 +00007Supersedes: <DFMAv8.3Hp@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 Rossumca318ec1996-06-26 19:50:09 +000012Version: 1.31
13Last-modified: 26 June 1996
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 Rossume7d92d51995-10-11 18:06:54 +000024 Guido van Rossum
25 C.N.R.I.
26 1895 Preston White Drive
27 Reston, VA 22091
28 U.S.A.
Guido van Rossumb0a2ce51995-08-28 19:41:35 +000029Email: <guido@python.org>, <guido@cnri.reston.va.us>, <guido@cwi.nl>
Guido van Rossuma7925f11994-01-26 10:20:16 +000030
31The latest version of this FAQ is available by anonymous ftp from
Guido van Rossumca318ec1996-06-26 19:50:09 +000032<URL:ftp://ftp.python.org/pub/python/doc/FAQ>. It will also be posted
Guido van Rossuma6c707c1995-01-02 17:32:28 +000033regularly to the newsgroups comp.answers <URL:news:comp.answers> and
34comp.lang.python <URL:news:comp.lang.python>.
Guido van Rossuma7925f11994-01-26 10:20:16 +000035
Guido van Rossuma6c707c1995-01-02 17:32:28 +000036Many FAQs, including this one, are available by anonymous ftp
37<URL:ftp://rtfm.mit.edu/pub/usenet/news.answers/>. The name under
38which a FAQ is archived appears in the Archive-name line at the top of
39the article. This FAQ is archived as python-faq/part1
40<URL:ftp://rtfm.mit.edu/pub/usenet/news.answers/python-faq/part1>.
Guido van Rossuma7925f11994-01-26 10:20:16 +000041
42There's a mail server on that machine which will send you files from
43the archive by e-mail if you have no ftp access. You send a e-mail
Guido van Rossuma6c707c1995-01-02 17:32:28 +000044message to <mail-server@rtfm.mit.edu> containing the single word help
45in the message body to receive instructions.
Guido van Rossuma7925f11994-01-26 10:20:16 +000046
47This FAQ is divided in the following chapters:
48
49 1. General information and availability
50 2. Python in the real world
Guido van Rossumb0a2ce51995-08-28 19:41:35 +000051 3. Building Python and Other Known Bugs
Guido van Rossuma7925f11994-01-26 10:20:16 +000052 4. Programming in Python
53 5. Extending Python
54 6. Python's design
55 7. Using Python on non-UNIX platforms
56
57To find the start of a particular chapter, search for the chapter number
58followed by a dot and a space at the beginning of a line (e.g. to
59find chapter 4 in vi, type /^4\. /).
60
61Here's an overview of the questions per chapter:
62
63 1. General information and availability
64 1.1. Q. What is Python?
65 1.2. Q. Why is it called Python?
66 1.3. Q. How do I obtain a copy of the Python source?
67 1.4. Q. How do I get documentation on Python?
Guido van Rossumc50158e1994-05-31 09:18:50 +000068 1.5. Q. Are there other ftp sites that mirror the Python distribution?
69 1.6. Q. Is there a newsgroup or mailing list devoted to Python?
Guido van Rossuma6c707c1995-01-02 17:32:28 +000070 1.7. Q. Is there a WWW page devoted to Python?
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +000071 1.8. Q. Is the Python documentation available on the WWW?
72 1.9. Q. Is there a book on Python, or will there be one out soon?
73 1.10. Q. Are there any published articles about Python that I can quote?
74 1.11. Q. Are there short introductory papers or talks on Python?
75 1.12. Q. How does the Python version numbering scheme work?
76 1.13. Q. How do I get a beta test version of Python?
Guido van Rossum05151e01995-09-28 13:24:46 +000077 1.14. Q. Are there copyright restrictions on the use of Python?
78 1.15. Q. Why was Python created in the first place?
79 1.16. Q. What happened to Tim Peters (the author of python-mode.el, and many
Guido van Rossum7be34a81995-05-31 15:17:12 +000080 entertaining and enlightening pieces of Python email)?
Guido van Rossuma7925f11994-01-26 10:20:16 +000081
82 2. Python in the real world
83 2.1. Q. How many people are using Python?
84 2.2. Q. Have any significant projects been done in Python?
85 2.3. Q. Are there any commercial projects going on using Python?
Guido van Rossum95f61a71994-01-26 17:23:37 +000086 2.4. Q. How stable is Python?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000087 2.5. Q. When will the next version be released?
88 2.6. Q. What new developments are expected for Python in the future?
89 2.7. Q. Is it reasonable to propose incompatible changes to Python?
Guido van Rossumb0a2ce51995-08-28 19:41:35 +000090 2.8. Q. What is the future of Python?
Guido van Rossum05151e01995-09-28 13:24:46 +000091 2.9. Q. What is the PSA, anyway?
92 2.10. Q. How do I join the PSA?
93 2.11. Q. What are the benefits of joining the PSA?
Guido van Rossuma7925f11994-01-26 10:20:16 +000094
Guido van Rossumb0a2ce51995-08-28 19:41:35 +000095 3. Building Python and Other Known Bugs
Guido van Rossum91f60831994-02-15 15:52:27 +000096 3.1. Q. Is there a test set?
97 3.2. Q. When running the test set, I get complaints about floating point
Guido van Rossuma7925f11994-01-26 10:20:16 +000098 operations, but when playing with floating point operations I cannot
99 find anything wrong with them.
Guido van Rossum7be34a81995-05-31 15:17:12 +0000100 3.3. Q. Link errors after rerunning the configure script.
101 3.4. Q. The python interpreter complains about options passed to a
Guido van Rossum24349991994-02-02 14:12:45 +0000102 script (after the script name).
Guido van Rossum7be34a81995-05-31 15:17:12 +0000103 3.5. Q. When building on the SGI, make tries to run python to create
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000104 glmodule.c, but python hasn't been built or installed yet.
Guido van Rossum7be34a81995-05-31 15:17:12 +0000105 3.6. Q. Python built with gcc for the DEC Alpha doesn't work.
106 3.7. Q. I use VPATH but some targets are built in the source directory.
107 3.8. Q. Trouble building or linking with the GNU readline library.
108 3.9. Q. Trouble building Python on Linux.
Guido van Rossumca318ec1996-06-26 19:50:09 +0000109 3.10. Q. Trouble with socket I/O on Linux.
110 3.11. Q. Trouble with prototypes on Ultrix.
111 3.12. Q. Trouble with posix.listdir on NeXTSTEP 3.2.
112 3.13. Q. Other trouble building Python on platform X.
113 3.14. Q. How to configure dynamic loading on Linux.
114 3.15. Q. Under Solaris 2.x, using GCC, how do I use shared libraries?
115 3.16. Q. Errors when linking with a shared library containing C++ code.
116 3.17. Q. Tk menus or radiobuttons don't work properly in Python 1.2.
117 3.18. Q. I built with tkintermodule.c enabled but get "Tkinter not found".
118 3.19. Q. I built with Tk 4.0 but Tkinter complains about the Tk version.
119 3.20. Q. Tk doesn't work right on DEC Alpha.
120 3.21. Q. Several common system calls are missing from the posix module.
121 3.22. Q. ImportError: No module named string, on MS Windows.
122 3.23. Q. Core dump on SGI when using the gl module.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000123
124 4. Programming in Python
Guido van Rossum24349991994-02-02 14:12:45 +0000125 4.1. Q. Is there a source code level debugger with breakpoints, step,
126 etc.?
127 4.2. Q. Can I create an object class with some methods implemented in
Guido van Rossuma7925f11994-01-26 10:20:16 +0000128 C and others in Python (e.g. through inheritance)? (Also phrased as:
129 Can I use a built-in type as base class?)
Guido van Rossum24349991994-02-02 14:12:45 +0000130 4.3. Q. Is there a curses/termcap package for Python?
131 4.4. Q. Is there an equivalent to C's onexit() in Python?
132 4.5. Q. When I define a function nested inside another function, the
Guido van Rossuma7925f11994-01-26 10:20:16 +0000133 nested function seemingly can't access the local variables of the
134 outer function. What is going on? How do I pass local data to a
135 nested function?
Guido van Rossum24349991994-02-02 14:12:45 +0000136 4.6. Q. How do I iterate over a sequence in reverse order?
137 4.7. Q. My program is too slow. How do I speed it up?
138 4.8. Q. When I have imported a module, then edit it, and import it
Guido van Rossuma7925f11994-01-26 10:20:16 +0000139 again (into the same Python process), the changes don't seem to take
140 place. What is going on?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000141 4.9. Q. How do I find the current module name?
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000142 4.10. Q. I have a module in which I want to execute some extra code
143 when it is run as a script. How do I find out whether I am running as
144 a script?
145 4.11. Q. I try to run a program from the Demo directory but it fails
146 with ImportError: No module named ...; what gives?
Guido van Rossum061f1821994-10-06 16:03:45 +0000147 4.12. Q. I have successfully built Python with STDWIN but it can't
148 find some modules (e.g. stdwinevents).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000149 4.13. Q. What GUI toolkits exist for Python?
Guido van Rossum061f1821994-10-06 16:03:45 +0000150 4.14. Q. Are there any interfaces to database packages in Python?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000151 4.15. Q. Is it possible to write obfuscated one-liners in Python?
152 4.16. Q. Is there an equivalent of C's "?:" ternary operator?
153 4.17. Q. My class defines __del__ but it is not called when I delete the
154 object.
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000155 4.18. Q. How do I change the shell environment for programs called
156 using os.popen() or os.system()? Changing os.environ doesn't work.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000157 4.19. Q. What is a class?
158 4.20. Q. What is a method?
159 4.21. Q. What is self?
160 4.22. Q. What is a unbound method?
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000161 4.23. Q. How do I call a method defined in a base class from a derived
162 class that overrides it?
163 4.24. Q. How do I call a method from a base class without using the
164 name of the base class?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000165 4.25. Q. How can I organize my code to make it easier to change the base
166 class?
167 4.26. Q. How can I find the methods or attributes of an object?
Guido van Rossum061f1821994-10-06 16:03:45 +0000168 4.27. Q. I can't seem to use os.read() on a pipe created with os.popen().
169 4.28. Q. How can I create a stand-alone binary from a Python script?
Guido van Rossum7be34a81995-05-31 15:17:12 +0000170 4.29. Q. What WWW tools are there for Python?
171 4.30. Q. How do I run a subprocess with pipes connected to both input
Guido van Rossum796b2591995-01-20 23:05:52 +0000172 and output?
Guido van Rossum7be34a81995-05-31 15:17:12 +0000173 4.31. Q. How do I call a function if I have the arguments in a tuple?
174 4.32. Q. How do I enable font-lock-mode for Python in Emacs?
175 4.33. Q. Is there an inverse to the format operator (a la C's scanf())?
Guido van Rossumca318ec1996-06-26 19:50:09 +0000176 4.34. Q. Can I have Tk events handled while waiting for I/O?
177 4.35. Q. How do I write a function with output parameters (call by reference)?
Guido van Rossum0d20cfa1996-07-30 18:53:05 +0000178 4.36. Q. Please explain the rules for local and global variables in Python.
179 4.37. Q. How can I have modules that mutually import each other?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000180
181 5. Extending Python
182 5.1. Q. Can I create my own functions in C?
183 5.2. Q. Can I create my own functions in C++?
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000184 5.3. Q. How can I execute arbitrary Python statements from C?
185 5.4. Q. How can I evaluate an arbitrary Python expression from C?
186 5.5. Q. How do I extract C values from a Python object?
187 5.6. Q. How do I use mkvalue() to create a tuple of arbitrary length?
Guido van Rossum796b2591995-01-20 23:05:52 +0000188 5.7. Q. What happened to mktuple(), featured in an example in the
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000189 Extensions manual?
190 5.8. Q. How do I call an object's method from C?
191 5.9. Q. How do I catch the output from print_error()?
192 5.10. Q. How do I access a module written in Python from C?
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000193 5.11. Q. How do I interface to C++ objects from Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000194
195 6. Python's design
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000196 6.1. Q. Why isn't there a generic copying operation for objects in Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000197 6.2. Q. Why isn't there a generic way to implement persistent objects
198 in Python? (Persistent == automatically saved to and restored from
199 disk.)
200 6.3. Q. Why isn't there a switch or case statement in Python?
Guido van Rossumc50158e1994-05-31 09:18:50 +0000201 6.4. Q. Why does Python use indentation for grouping of statements?
Guido van Rossum3de27361994-07-25 14:19:33 +0000202 6.5. Q. Why are Python strings immutable?
203 6.6. Q. Why don't strings have methods like index() or sort(), like
204 lists?
205 6.7. Q. Why does Python use methods for some functionality
206 (e.g. list.index()) but functions for other (e.g. len(list))?
207 6.8. Q. Why can't I derive a class from built-in types (e.g. lists or
208 files)?
209 6.9. Q. Why must 'self' be declared and used explicitly in method
210 definitions and calls?
Guido van Rossum061f1821994-10-06 16:03:45 +0000211 6.10. Q. Can't you emulate threads in the interpreter instead of
212 relying on an OS-specific thread implementation?
213 6.11. Q. Why can't lambda forms contain statements?
214 6.12. Q. Why is there no more efficient way of iterating over a dictionary
215 than first constructing the list of keys()?
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000216 6.13. Q. Can Python be compiled to machine code, C or some other language?
Guido van Rossum05151e01995-09-28 13:24:46 +0000217 6.14. Q. Why doesn't Python use proper garbage collection?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000218
219 7. Using Python on non-UNIX platforms
Guido van Rossum91f60831994-02-15 15:52:27 +0000220 7.1. Q. Is there a Mac version of Python?
221 7.2. Q. Is there a DOS version of Python?
Guido van Rossume530c581995-04-10 12:32:16 +0000222 7.3. Q. Is there a Windows 3.1(1) version of Python?
Guido van Rossum91f60831994-02-15 15:52:27 +0000223 7.4. Q. Is there a Windows NT version of Python?
Guido van Rossume530c581995-04-10 12:32:16 +0000224 7.5. Q. Is there a Windows 95 version of Python?
225 7.6. Q. Is there an OS/2 version of Python?
226 7.7. Q. Is there a VMS version of Python?
Guido van Rossumbf8e7d51995-08-28 03:09:13 +0000227 7.8. Q. What about IBM mainframes, or other non-UNIX platforms?
Guido van Rossume530c581995-04-10 12:32:16 +0000228 7.9. Q. Where are the source or Makefiles for the non-UNIX versions?
229 7.10. Q. What is the status and support for the non-UNIX versions?
230 7.11. Q. I have a PC version but it appears to be only a binary.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000231 Where's the library?
Guido van Rossume530c581995-04-10 12:32:16 +0000232 7.12. Q. Where's the documentation for the Mac or PC version?
233 7.13. Q. The Mac (PC) version doesn't seem to have any facilities for
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000234 creating or editing programs apart from entering it interactively, and
235 there seems to be no way to save code that was entered interactively.
236 How do I create a Python program on the Mac (PC)?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000237
238To find a particular question, search for the question number followed
239by a dot, a space, and a Q at the beginning of a line (e.g. to find
240question 4.2 in vi, type /^4\.2\. Q/).
241
242
2431. General information and availability
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000244 =======================================
Guido van Rossuma7925f11994-01-26 10:20:16 +0000245
2461.1. Q. What is Python?
247
248A. Python is an interpreted, interactive, object-oriented programming
249language. It incorporates modules, exceptions, dynamic typing, very
250high level dynamic data types, and classes. Python combines
251remarkable power with very clear syntax. It has interfaces to many
252system calls and libraries, as well as to various window systems, and
253is extensible in C or C++. It is also usable as an extension language
254for applications that need a programmable interface. Finally, Python
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000255is portable: it runs on many brands of UNIX, on the Mac, and on PCs
256under MS-DOS, Windows, Windows NT, and OS/2.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000257
258To find out more, the best thing to do is to start reading the
259tutorial from the documentation set (see a few questions further
260down).
261
2621.2. Q. Why is it called Python?
263
Guido van Rossum796b2591995-01-20 23:05:52 +0000264A. Apart from being a computer scientist, I'm also a fan of "Monty
Guido van Rossuma7925f11994-01-26 10:20:16 +0000265Python's Flying Circus" (a BBC comedy series from the seventies, in
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000266the -- unlikely -- case you didn't know). It occurred to me one day
267that I needed a name that was short, unique, and slightly mysterious.
268And I happened to be reading some scripts from the series at the
269time... So then I decided to call my language Python. But Python is
270not a joke. And don't you associate it with dangerous reptiles
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000271either! (If you need an icon, use an image of the 16-ton weight from
272the TV series or of a can of SPAM :-)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000273
2741.3. Q. How do I obtain a copy of the Python source?
275
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000276A. The latest complete Python source distribution is always available
277by anonymous ftp, e.g.
Guido van Rossum7be34a81995-05-31 15:17:12 +0000278<URL:ftp://ftp.python.org/pub/python/src/python1.2.tar.gz>. It is a
Guido van Rossume530c581995-04-10 12:32:16 +0000279gzipped tar file containing the complete C source, LaTeX
280documentation, Python library modules, example programs, and several
281useful pieces of freely distributable software. This will compile and
282run out of the box on most UNIX platforms. (See section 7 for
283non-UNIX information.)
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000284
285Occasionally a set of patches is issued which has to be applied using
286the patch program. These patches are placed in the same directory,
Guido van Rossum7be34a81995-05-31 15:17:12 +0000287e.g. <URL:ftp://ftp.python.org/pub/python/src/patch1.1.1>. (At the time
Guido van Rossume530c581995-04-10 12:32:16 +0000288of writing, no patches exist for 1.2.)
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000289
290An index of said ftp directory can be found in the file INDEX. An
291HTML version of the index can be found in the file index.html,
Guido van Rossum7be34a81995-05-31 15:17:12 +0000292<URL:ftp://ftp.python.org/pub/python/index.html>.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000293
2941.4. Q. How do I get documentation on Python?
295
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000296A. The LaTeX source for the documentation is part of the source
297distribution. If you don't have LaTeX, the latest Python
298documentation set is always available by anonymous ftp, e.g.
Guido van Rossum7be34a81995-05-31 15:17:12 +0000299<URL:ftp://ftp.python.org/pub/python/doc/postscript.tar.gz>. It is a
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000300gzipped tar file containing PostScript files of the reference manual,
Guido van Rossum3de27361994-07-25 14:19:33 +0000301the library manual, and the tutorial. Note that the library manual is
302the most important one of the set, as much of Python's power stems
303from the standard or built-in types, functions and modules, all of
304which are described here. PostScript for a high-level description of
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000305Python is in the file nluug-paper.ps (a separate file on the ftp
306site).
Guido van Rossuma7925f11994-01-26 10:20:16 +0000307
Guido van Rossumc50158e1994-05-31 09:18:50 +00003081.5. Q. Are there other ftp sites that mirror the Python distribution?
309
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000310A. The following anonymous ftp sites keep mirrors of the Python
311distribution:
Guido van Rossuma7925f11994-01-26 10:20:16 +0000312
Guido van Rossume7d92d51995-10-11 18:06:54 +0000313USA:
314
315 <URL:ftp://ftp.python.org/pub/python/>
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000316 <URL:ftp://gatekeeper.dec.com/pub/plan/python/>
Guido van Rossume7d92d51995-10-11 18:06:54 +0000317 <URL:ftp://ftp.uu.net/languages/python/>
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000318 <URL:ftp://ftp.wustl.edu/graphics/graphics/sgi-stuff/python/>
Guido van Rossume7d92d51995-10-11 18:06:54 +0000319 <URL:ftp://ftp.sterling.com/programming/languages/python/>
320 <URL:ftp://uiarchive.cso.uiuc.edu/pub/lang/python/>
321 <URL:ftp://ftp.pht.com/mirrors/python/python/>
322
323Europe:
324
325 <URL:ftp://ftp.cwi.nl/pub/python/>
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000326 <URL:ftp://ftp.funet.fi/pub/languages/python/>
327 <URL:ftp://ftp.sunet.se/pub/lang/python/>
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000328 <URL:ftp://unix.hensa.ac.uk/mirrors/uunet/languages/python/>
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000329 <URL:ftp://ftp.ibp.fr/pub/python/>
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000330 <URL:ftp://ftp.switch.ch/software/sources/python/>
331 <URL:ftp://ftp.informatik.tu-muenchen.de/pub/comp/programming/languages/python/>
Guido van Rossuma7925f11994-01-26 10:20:16 +0000332
Guido van Rossume7d92d51995-10-11 18:06:54 +0000333Australia:
334
335 <URL:ftp://ftp.dstc.edu.au/pub/python/>
336
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000337Or try archie on the string "python".
Guido van Rossuma7925f11994-01-26 10:20:16 +0000338
Guido van Rossumc50158e1994-05-31 09:18:50 +00003391.6. Q. Is there a newsgroup or mailing list devoted to Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000340
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000341A. There is a newsgroup, comp.lang.python <URL:news:comp.lang.python>,
342and a mailing list. The newsgroup and mailing list are gatewayed into
343each other -- if you can read news it's unnecessary to subscribe to
344the mailing list. Send e-mail to <python-list-request@cwi.nl> to
Guido van Rossume530c581995-04-10 12:32:16 +0000345(un)subscribe to the mailing list. Hypermail archives of (nearly)
346everything posted to the mailing list (and thus the newsgroup) are
347available on our WWW server,
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000348<URL:http://www.cwi.nl/~guido/hypermail/index.html>. The raw archives
349are also available by ftp, e.g.
Guido van Rossum7be34a81995-05-31 15:17:12 +0000350<URL:ftp://ftp.python.org/pub/python/mail/mailinglist.gz>. The
Guido van Rossume530c581995-04-10 12:32:16 +0000351uncompressed versions of these files can be read with the standard
352UNIX Mail program ("Mail -f file") or with nn ("nn file"). To read
Guido van Rossumbf8e7d51995-08-28 03:09:13 +0000353them using MH, you could use "inc -file file". (The archival service
354has stopped archiving new articles around the end of April 1995. I
355hope to revive it on the PSA server www.python.org sometime in the
356future.)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000357
Guido van Rossuma6c707c1995-01-02 17:32:28 +00003581.7. Q. Is there a WWW page devoted to Python?
359
Guido van Rossum7be34a81995-05-31 15:17:12 +0000360A. Yes, <URL:http://www.python.org/> is the official Python home page.
361At the time of writing, this page is not yet completely operational;
362you may have a look at the old Python home page:
363<URL:http://www.cwi.nl/~guido/Python.html> or at the U.S. copy:
364<URL:http://www.python.org/~guido/Python.html>.
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000365
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00003661.8. Q. Is the Python documentation available on the WWW?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000367
Guido van Rossum7be34a81995-05-31 15:17:12 +0000368A. Yes, see <URL:http://www.python.org/> (Python's home page). It
369contains pointers to hypertext versions of the whole documentation set
370(as hypertext, not just PostScript).
Guido van Rossuma7925f11994-01-26 10:20:16 +0000371
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000372If you wish to browse this collection of HTML files on your own
373machine, it is available bundled up by anonymous ftp,
Guido van Rossum7be34a81995-05-31 15:17:12 +0000374e.g. <URL:ftp://ftp.python.org/pub/python/doc/html.tar.gz>.
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000375
376An Emacs-INFO set containing the library manual is also available by
Guido van Rossum7be34a81995-05-31 15:17:12 +0000377ftp, e.g. <URL:ftp://ftp.python.org/pub/python/doc/lib-info.tar.gz>.
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000378
3791.9. Q. Is there a book on Python, or will there be one out soon?
380
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000381A. Mark Lutz is writing a Python book for O'Reilly and Associates, to
382be published early 1996. See the outline (in PostScript):
383<URL:http://www.python.org/workshops/1995-05/outlinep.eps>.
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000384
3851.10. Q. Are there any published articles about Python that I can quote?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000386
387A. So far the only refereed and published article that describes
388Python in some detail is:
389
390 Guido van Rossum and Jelke de Boer, "Interactively Testing Remote
391 Servers Using the Python Programming Language", CWI Quarterly, Volume
392 4, Issue 4 (December 1991), Amsterdam, pp 283-303.
393
394LaTeX source for this paper is available as part of the Python source
395distribution.
396
Guido van Rossum05151e01995-09-28 13:24:46 +0000397See also the next section (supposedly Aaron Watters' paper has been
398refereed).
399
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00004001.11. Q. Are there short introductory papers or talks on Python?
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000401
Guido van Rossum05151e01995-09-28 13:24:46 +0000402A. A recent, very entertaining introduction to Python is the tutorial by
403Aaron Watters in UnixWorld Online:
404
405 Aaron R. Watters: "The What, Why, Who, and Where of Python",
406 <URL:http://www.wcmh.com/uworld/archives/95/tutorial/005.html>
407
408An olded paper is:
Guido van Rossuma7925f11994-01-26 10:20:16 +0000409
410 Guido van Rossum, "An Introduction to Python for UNIX/C
411 Programmers", in the proceedings of the NLUUG najaarsconferentie
Guido van Rossum061f1821994-10-06 16:03:45 +0000412 1993 (dutch UNIX users group meeting November 1993).
Guido van Rossuma7925f11994-01-26 10:20:16 +0000413
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000414PostScript for this paper and for the slides used for the accompanying
415presentation is available by ftp as
Guido van Rossum7be34a81995-05-31 15:17:12 +0000416<URL:ftp://ftp.python.org/pub/python/doc/nluug-paper.ps> and
417<URL:ftp://ftp.python.org/pub/python/doc/nluug-slides.ps>, respectively.
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000418
419Slides for a talk on Python that I gave at the Usenix Symposium on
Guido van Rossum05151e01995-09-28 13:24:46 +0000420Very High Level Languages in Santa Fe, NM, USA in October 1994 are
Guido van Rossum7be34a81995-05-31 15:17:12 +0000421available as <URL:ftp://ftp.python.org/pub/python/doc/vhll-slides.ps>.
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000422
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00004231.12. Q. How does the Python version numbering scheme work?
Guido van Rossum95f61a71994-01-26 17:23:37 +0000424
Guido van Rossum05151e01995-09-28 13:24:46 +0000425A. Python versions are numbered A.B.C or A.B. A is the major version
426number -- it is only incremented for major changes in functionality or
427source structure. B is the minor version number, incremented for less
Guido van Rossum95f61a71994-01-26 17:23:37 +0000428earth-shattering changes to a release. C is the patchlevel -- it is
Guido van Rossum05151e01995-09-28 13:24:46 +0000429incremented for each new patch release. Not all releases have patch
430releases. Note that in the past, patches have added significant
431changes; in fact the changeover from 0.9.9 to 1.0.0 was the first time
432that either A or B changed!
Guido van Rossum95f61a71994-01-26 17:23:37 +0000433
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000434Beta versions have an additional suffix of "-beta-N" for some small
435number N. Note that (for instance) all versions labeled 1.2-beta-N
Guido van Rossum05151e01995-09-28 13:24:46 +0000436*precede* the actual release of 1.2. 1.3b1 is short for 1.3-beta-1.
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000437
4381.13. Q. How do I get a beta test version of Python?
439
Guido van Rossum05151e01995-09-28 13:24:46 +0000440A. If there are any beta releases, they are published in the normal
441source directory (e.g. <URL:ftp://ftp.python.org/pub/python/src/>).
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000442
Guido van Rossum05151e01995-09-28 13:24:46 +00004431.14. Q. Are there copyright restrictions on the use of Python?
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000444
445A. Hardly. You can do anything you want with the source, as long as
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000446you leave the copyrights in, and display those copyrights in any
447documentation about Python that you produce. Also, don't use the
448author's institute's name in publicity without prior written
449permission, and don't hold them responsible for anything (read the
450actual copyright for a precise legal wording).
451
452In particular, if you honor the copyright rules, it's OK to use Python
453for commercial use, to sell copies of Python in source or binary form,
454or to sell products that enhance Python or incorporate Python (or part
455of it) in some form. I would still like to know about all commercial
456use of Python!
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000457
Guido van Rossum05151e01995-09-28 13:24:46 +00004581.15. Q. Why was Python created in the first place?
Guido van Rossum7be34a81995-05-31 15:17:12 +0000459
460A. Here's a *very* brief summary of what got me started:
461
462- I had extensive experience with implementing an interpreted language
463in the ABC group at CWI, and from working with this group I had
464learned a lot about language design. This is the origin of many
465Python features, including the use of indentation for statement
466grouping and the inclusion of very-high-level data types (although the
467details are all different in Python).
468
469- I had a number of gripes about the ABC language, but also liked many
470of its features. It was impossible to extend the ABC language (or its
471implementation) to remedy my complaints -- in fact its lack of
472extensibility was one of its biggest problems.
473
474- I had some experience with using Modula-2+ and talked with the
475designers of Modula-3 (and read the M3 report). M3 is the origin of
476the syntax and semantics used for exceptions, and some other Python
477features.
478
479- I was working in the Amoeba distributed operating system group at
480CWI. We needed a better way to do system administration than by
481writing either C programs or Bourne shell scripts, since Amoeba had
482its own system call interface which wasn't easily accessible from the
Guido van Rossumbf8e7d51995-08-28 03:09:13 +0000483Bourne shell. My experience with error handling in Amoeba made me
484acutely aware of the importance of exceptions as a programming
485language feature.
Guido van Rossum7be34a81995-05-31 15:17:12 +0000486
487- It occurred to me that a scripting language with a syntax like ABC
488but with access to the Amoeba system calls would fill the need. I
489realized that it would be foolish to write an Amoeba-specific
490language, so I decided that I needed a language that was generally
491extensible.
492
493- During the 1989 Christmas holidays, I had a lot of time on my hand,
494so I decided to give it a try. During the next year, while still
495mostly working on it in my own time, Python was used in the Amoeba
496project with increasing success, and the feedback from colleagues made
497me add many early improvements.
498
499- In February 1991, after just over a year of development, I decided
500to post to USENET. The rest is in the Misc/HISTORY file.
501
Guido van Rossum05151e01995-09-28 13:24:46 +00005021.16. Q. What happened to Tim Peters (the author of python-mode.el, and many
Guido van Rossum7be34a81995-05-31 15:17:12 +0000503entertaining and enlightening pieces of Python email)?
504
505A. He worked at KSR (Kendall Square Research, a start-up building a
506new kind of massively parallel processor). When KSR folded down a
507couple of years ago, Tim lost his email access. He hasn't surfaced
508on the net since then.
509
Guido van Rossume7d92d51995-10-11 18:06:54 +0000510 Missing-him-too-ly yours...
Guido van Rossum7be34a81995-05-31 15:17:12 +0000511
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000512PS: support for Python's Emacs mode (Misc/python-mode.el in the
513distribution) has been taken up by Barry Warsaw. Questions about it
514should be mailed to <python-mode@python.org>.
515
Guido van Rossuma7925f11994-01-26 10:20:16 +0000516
5172. Python in the real world
518===========================
519
5202.1. Q. How many people are using Python?
521
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000522A. I don't know, but the maximum number of simultaneous subscriptions
523to the Python mailing list before it was gatewayed into the newsgroup
524was about 180 (several of which were local redistribution lists). I
525believe that many active Python users don't bother to subscribe to the
526list, and now that there's a newsgroup the mailing list subscription
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000527is even less meaningful. I see new names on the newsgroup all the
528time and my best guess is that there are currently at least several
529thousands of users.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000530
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000531Another statistic is the number of accesses to the Python WWW server.
532Have a look at <URL:http://www.python.org/stats/>.
533
Guido van Rossuma7925f11994-01-26 10:20:16 +00005342.2. Q. Have any significant projects been done in Python?
535
536A. Here at CWI (the home of Python), we have written a 20,000 line
537authoring environment for transportable hypermedia presentations, a
Guido van Rossum5333c5d1994-04-11 11:06:22 +00005385,000 line multimedia teleconferencing tool, as well as many many
539smaller programs.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000540
541The University of Virginia uses Python to control a virtual reality
542engine. Contact: Matt Conway <conway@virginia.edu>.
543
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000544The ILU project at Xerox PARC can generate Python glue for ILU
545interfaces. See <URL:ftp://ftp.parc.xerox.com/pub/ilu/ilu.html>.
546
Guido van Rossumac3f2121995-04-10 11:53:42 +0000547The University of California, Irvine uses a student administration
548system called TELE-Vision written entirely in Python. Contact: Ray
549Price <rlprice@uci.edu>.
550
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000551See also the next question.
552
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000553If you have done a significant project in Python that you'd like to be
554included in the list above, send me email!
Guido van Rossuma7925f11994-01-26 10:20:16 +0000555
5562.3. Q. Are there any commercial projects going on using Python?
557
558A. Several companies have revealed to me that they are planning or
Guido van Rossum796b2591995-01-20 23:05:52 +0000559considering use of Python in a future product.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000560
Guido van Rossumbf8e7d51995-08-28 03:09:13 +0000561Sunrise Software has a product out using Python -- they use Python
Guido van Rossumac3f2121995-04-10 11:53:42 +0000562for a GUI management application and an SNMP network management
563application. Contact: <info@sunrise.com>.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000564
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000565Infoseek uses Python to implement their commercial WWW information
566retrieval service <URL:http://www.infoseek.com/>. Contact:
567<info@infoseek.com>.
Guido van Rossumf8c76d01994-08-17 12:19:53 +0000568
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000569Paul Everitt of Connecting Minds is planning a Lotus Notes gateway.
570Contact: <Paul.Everitt@cminds.com>. Or see their WWW server
571<URL:http://www.cminds.com/>.
572
Guido van Rossumac3f2121995-04-10 11:53:42 +0000573KaPRE in Boulder, CO is using Python for on-site customization of C++
574applications, rapid-prototyping/development,
575language-based-components, and possibly more. This is pretty solid:
576Python's being shipped with their tool-set now, to beta sites.
577Contact: <lutz@KaPRE.COM> (Mark Lutz).
578
579Individuals at many other companies are using Python for internal
580development or for as yet unannounced products (witness their
581contributions to the Python mailing list or newsgroup).
Guido van Rossuma7925f11994-01-26 10:20:16 +0000582
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000583SGI has advertised in the Python list looking for Python programmers
584for a project involving interactive television.
585
586See also the workshop minutes at
587<URL:http://www.python.org/workshops/> -- in general the WWW server is
588more up to date than the FAQ for these issues.
589
Guido van Rossuma7925f11994-01-26 10:20:16 +0000590Python has also been elected as an extension language by MADE, a
591consortium supported by the European Committee's ESPRIT program and
592consisting of Bull, CWI and some other European companies. Contact:
593Ivan Herman <ivan@cwi.nl>.
594
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000595If you'd like to be included in the list above, send me email!
596
Guido van Rossum95f61a71994-01-26 17:23:37 +00005972.4. Q. How stable is Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000598
Guido van Rossum3de27361994-07-25 14:19:33 +0000599A. Very stable. While the current version number would suggest it is
600in the early stages of development, in fact new, stable releases
Guido van Rossume7d92d51995-10-11 18:06:54 +0000601(numbered 0.9.x through 1.3) have been coming out roughly every 3 to
Guido van Rossuma6c707c1995-01-02 17:32:28 +00006026 months for the past four years.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000603
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00006042.5. Q. When will the next version be released?
605
Guido van Rossume7d92d51995-10-11 18:06:54 +0000606A. Version 1.3 is being released on 13 October 1995. It is too early
607to predict when the next release will be necessary, but you can expect
608something awesome within half a year!
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000609
6102.6. Q. What new developments are expected for Python in the future?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000611
Guido van Rossum7be34a81995-05-31 15:17:12 +0000612A. See my Work-In-Progress web page, currently at
613<URL:http://www.python.org:~guido/WIP.html>, and the pages for the
614Second Python Workshop (best reached via the Python home page,
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000615<URL:http://www.python.org/>). Also follow the newsgroup discussions!
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000616
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00006172.7. Q. Is it reasonable to propose incompatible changes to Python?
Guido van Rossum3de27361994-07-25 14:19:33 +0000618
619A. In general, no. There are already millions of lines of Python code
620around the world, so any changes in the language that invalidates more
621than a very small fraction of existing programs has to be frowned
622upon. Even if you can provide a conversion program, there still is
623the problem of updating all documentation. Providing a gradual
624upgrade path is the only way if a feature has to be changed.
625
Guido van Rossumb0a2ce51995-08-28 19:41:35 +00006262.8. Q. What is the future of Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000627
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000628A. If I knew, I'd be rich :-)
629
630Seriously, the formation of the PSA (Pyton Software Activity, see
631<URL:http://www.python.org/psa/>) ensures some kind of support even in
632the (unlikely! event that I'd be hit by a bus (actually, here in the
633US, a car accident would be more likely :-), were to join a nunnery,
634or would be head-hunted. A large number of Python users have become
635experts at Python programming as well as maintenance of the
636implementation, and would easily fill the vacuum created by my
637disappearance.
638
639In the mean time, I have no plans to disappear -- rather, I am
640committed to improving Python, and my current benefactor, CNRI (see
641<URL:http://www.cnri.reston.va.us>) is just as committed to continue
642its support of Python and the PSA. In fact, we have great plans for
643Python -- we just can't tell yet!
644
Guido van Rossum05151e01995-09-28 13:24:46 +00006452.9. Q. What is the PSA, anyway?
646
647A. The Python Software Activity <URL:http://www.python.org/psa/> was
648created by a number of Python aficionados who want Python to be more
649than the product and responsibility of a single individual. It has
650found a home at CNRI <URL:http://www.cnri.reston.va.us>. Anybody who
651wishes Python well should join the PSA.
652
6532.10. Q. How do I join the PSA?
654
655A. The full scoop is available on the web, see
656<URL:http://www.python.org/psa/Joining.html>. Summary: send a check
657of at least $50 to CNRI/PSA, 1895 Preston White Drive, Suite 100, in
658Reston, VA 22091. Full-time students pay $25. Companies can join for
659a mere $500.
660
6612.11. Q. What are the benefits of joining the PSA?
662
663A. Like National Public Radio, if not enough people join, Python will
664wither. Your name will be mentioned on the PSA's web server.
665Workshops organized by the PSA <URL:http://www.python.org/workshops/>
666are only accessible to PSA members (you can join at the door). The
667PSA is working on additional benefits, such as reduced prices for
668books and software, and early access to beta versions of Python.
669
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000670
6713. Building Python and Other Known Bugs
672=======================================
Guido van Rossuma7925f11994-01-26 10:20:16 +0000673
Guido van Rossum91f60831994-02-15 15:52:27 +00006743.1. Q. Is there a test set?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000675
676A. Yes, simply do "import testall" (or "import autotest" if you aren't
677interested in the output). The standard modules whose name begins
678with "test" together comprise the test. The test set doesn't test
679*all* features of Python but it goes a long way to confirm that a new
680port is actually working. The Makefile contains an entry "make test"
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000681which runs the autotest module. NOTE: if "make test" fails, run the
682tests manually ("import testall") to see what goes wrong before
683reporting the error.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000684
Guido van Rossum91f60831994-02-15 15:52:27 +00006853.2. Q. When running the test set, I get complaints about floating point
Guido van Rossuma7925f11994-01-26 10:20:16 +0000686operations, but when playing with floating point operations I cannot
687find anything wrong with them.
688
689A. The test set makes occasional unwarranted assumptions about the
690semantics of C floating point operations. Until someone donates a
691better floating point test set, you will have to comment out the
692offending floating point tests and execute similar tests manually.
693
Guido van Rossum7be34a81995-05-31 15:17:12 +00006943.3. Q. Link errors after rerunning the configure script.
Guido van Rossum24349991994-02-02 14:12:45 +0000695
696A. It is generally necessary to run "make clean" after a configuration
697change.
698
Guido van Rossum7be34a81995-05-31 15:17:12 +00006993.4. Q. The python interpreter complains about options passed to a
Guido van Rossum24349991994-02-02 14:12:45 +0000700script (after the script name).
701
702A. You are probably linking with GNU getopt, e.g. through -liberty.
Guido van Rossumf8c76d01994-08-17 12:19:53 +0000703Don't. The reason for the complaint is that GNU getopt, unlike System
704V getopt and other getopt implementations, doesn't consider a
705non-option to be the end of the option list. A quick (and compatible)
706fix for scripts is to add "--" to the interpreter, like this:
707
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000708 #! /usr/local/bin/python --
Guido van Rossumf8c76d01994-08-17 12:19:53 +0000709
710You can also use this interactively:
711
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000712 python -- script.py [options]
713
714Note that a working getopt implementation is provided in the Python
715distribution (in Python/getopt.c) but not automatically used.
Guido van Rossum24349991994-02-02 14:12:45 +0000716
Guido van Rossum7be34a81995-05-31 15:17:12 +00007173.5. Q. When building on the SGI, make tries to run python to create
Guido van Rossum24349991994-02-02 14:12:45 +0000718glmodule.c, but python hasn't been built or installed yet.
719
720A. Comment out the line mentioning glmodule.c in Setup and build a
721python without gl first; install it or make sure it is in your $PATH,
722then edit the Setup file again to turn on the gl module, and make
723again. You don't need to do "make clean"; you do need to run "make
724Makefile" in the Modules subdirectory (or just run "make" at the
725toplevel).
726
Guido van Rossum7be34a81995-05-31 15:17:12 +00007273.6. Q. Python built with gcc for the DEC Alpha doesn't work.
Guido van Rossum3de27361994-07-25 14:19:33 +0000728
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000729People have reported problems with gcc 2.5.8 up to 2.6.3. The DEC
730OSF/1 cc compiler does not have these problems so it's likely a gcc
Guido van Rossume530c581995-04-10 12:32:16 +0000731bug. The latest news is that this has been fixed in Python 1.2 by a
732source change (I gave up waiting for a fixed gcc).
Guido van Rossum3de27361994-07-25 14:19:33 +0000733
Guido van Rossum7be34a81995-05-31 15:17:12 +00007343.7. Q. I use VPATH but some targets are built in the source directory.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000735
736A. On some systems (e.g. Sun), if the target already exists in the
737source directory, it is created there instead of in the build
738directory. This is usually because you have previously built without
739VPATH. Try running "make clobber" in the source directory.
740
Guido van Rossum7be34a81995-05-31 15:17:12 +00007413.8. Q. Trouble building or linking with the GNU readline library.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000742
Guido van Rossumac3f2121995-04-10 11:53:42 +0000743A. Consider using readline 2.0. Some hints:
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000744
745- You can use the GNU readline library to improve the interactive user
746interface: this gives you line editing and command history when
Guido van Rossum796b2591995-01-20 23:05:52 +0000747calling python interactively. You need to configure and build the GNU
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000748readline library before running the configure script. Its sources are
749no longer distributed with Python; you can ftp them from any GNU
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000750mirror site, or from its home site
751<URL:ftp://slc2.ins.cwru.edu/pub/dist/readline-2.0.tar.gz> (or a
752higher version number -- using version 1.x is not recommended). Pass
753the Python configure script the option --with-readline=DIRECTORY where
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000754DIRECTORY is the absolute pathname of the directory where you've built
755the readline library. Some hints on building and using the readline
756library:
757
758- On SGI IRIX 5, you may have to add the following
759to rldefs.h:
760
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000761 #ifndef sigmask
762 #define sigmask(sig) (1L << ((sig)-1))
763 #endif
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000764
765- On most systems, you will have to add #include "rldefs.h" to the
766top of several source files, and if you use the VPATH feature, you
767will have to add dependencies of the form foo.o: foo.c to the
768Makefile for several values of foo.
769
770- The readline library requires use of the termcap library. A
771known problem with this is that it contains entry points which
Guido van Rossum061f1821994-10-06 16:03:45 +0000772cause conflicts with the STDWIN and SGI GL libraries. The STDWIN
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000773conflict can be solved (and will be, in the next release of
Guido van Rossum061f1821994-10-06 16:03:45 +0000774STDWIN) by adding a line saying '#define werase w_erase' to the
775stdwin.h file (in the STDWIN distribution, subdirectory H). The
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000776GL conflict has been solved in the Python configure script by a
777hack that forces use of the static version of the termcap library.
778
Guido van Rossumac3f2121995-04-10 11:53:42 +0000779- Check the newsgroup gnu.bash.bug <URL:news:gnu.bash.bug> for
780specific problems with the readline library (I don't read this group
781but I've been told that it is the place for readline bugs).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000782
Guido van Rossum7be34a81995-05-31 15:17:12 +00007833.9. Q. Trouble building Python on Linux.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000784
Guido van Rossumac3f2121995-04-10 11:53:42 +0000785A. If you're building Python 1.2, Slackware 2.2 has a buggy bash
786(version 1.14.3) which breaks a sed script that is used to build
787Modules/Makefile. Replace /bin/sh with /bin/ash in both makesetup and
788Makefile.pre.in.
789
790In 1.1 and 1.1.1, there's a bug in the reference counting logic of
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000791ternary pow() which is only tripped by very picky mallocs, like the
792GNU malloc on Linux. This has been fixed in 1.2. To continue the
Guido van Rossumac3f2121995-04-10 11:53:42 +0000793tests in 1.1(.1), just disable the tests of pow() with three arguments
794from Lib/test/test_b2.py.
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000795
Guido van Rossumac3f2121995-04-10 11:53:42 +0000796Apart from this, Python builds and runs fine on most Linux versions
797(if you run into trouble on an old Linux version, consider upgrading).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000798
Guido van Rossumca318ec1996-06-26 19:50:09 +00007993.10. Q. Trouble with socket I/O on Linux.
800
801A. Once you've built Python, use it to run the regen.py script in the
802Lib/linux1 directory. Apparently the files as distributed don't match
803the system headers on some Linux versions.
804
8053.11. Q. Trouble with prototypes on Ultrix.
Guido van Rossum72eb83c1994-10-07 11:33:28 +0000806
807A. Ultrix cc seems broken -- use gcc, or edit config.h to #undef
808HAVE_PROTOTYPES.
809
Guido van Rossumca318ec1996-06-26 19:50:09 +00008103.12. Q. Trouble with posix.listdir on NeXTSTEP 3.2.
Guido van Rossum9351fdb1994-11-10 23:03:51 +0000811
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000812A. (This often manifests itself as a weird error from the
813compileall.py script run by "make libinstall".)
Guido van Rossum9351fdb1994-11-10 23:03:51 +0000814
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000815Don't use gcc, use the Next C compiler (cc). Even though it is
816derived from (an old version of) gcc, its interpretation of the
817"-posix" switch is different; in this particular case, cc is right and
818gcc is wrong.
Guido van Rossum9351fdb1994-11-10 23:03:51 +0000819
Guido van Rossumca318ec1996-06-26 19:50:09 +00008203.13. Q. Other trouble building Python on platform X.
Guido van Rossum95f61a71994-01-26 17:23:37 +0000821
Guido van Rossum796b2591995-01-20 23:05:52 +0000822A. Please email the details to <guido@cwi.nl> and I'll look into it.
823Please provide as many details as possible. In particular, if you
824don't tell me what type of computer and what operating system (and
825version) you are using it will be difficult for me to figure out what
826is the matter. If you get a specific error message, please email it
827to me too.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000828
Guido van Rossumca318ec1996-06-26 19:50:09 +00008293.14. Q. How to configure dynamic loading on Linux.
Guido van Rossum07779351995-02-07 16:59:56 +0000830
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000831A. There are two answers, depending on whether you are using the new
832ELF object format or not.
Guido van Rossum07779351995-02-07 16:59:56 +0000833
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000834For ELF, this seems to be the procedure (thanks to Martin von Loewis):
Guido van Rossum07779351995-02-07 16:59:56 +0000835
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000836Compile Python to an ELF binary.
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000837
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000838In addition, you have to use the following flags:
Guido van Rossum07779351995-02-07 16:59:56 +0000839
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000840 - when linking python: -rdynamic -ldl
841 - when compiling an object that goes into a shared module: -fPIC
842 - when linking a shared module: -shared -ldl
Guido van Rossum07779351995-02-07 16:59:56 +0000843
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000844Furthermore, it appears that some Python releases did not understand
845that Linux has dynamic linking. Python 1.2 did it right, but you
846should check wether the generated config.h indicates the use of -ldl
847(i.e. dlopen, dlsym). Finally, you can load a shared module by saying
848'use foo'. Make sure the module is in your PYTHONPATH.
Guido van Rossum07779351995-02-07 16:59:56 +0000849
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000850For pre-ELF systems (thanks to Andrew Kuchling):
Guido van Rossum07779351995-02-07 16:59:56 +0000851
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000852Pre-ELF Linux requires that you use the GNU DLD library. The stages
853of using dynamic libraries on Linux are:
854
855 1) Get dld 3.2.5 from a Linux site. Be careful here; the most
856 recent GNU version is 3.2.3, and doesn't support Linux; be sure to
857 get it from a Linux mirror, not a GNU mirror (3.2.4 should also
858 work). Compile it and install the library libdld.a somewhere; I
859 used /usr/local/lib.
860
861 Suitable URLs for the dld distribution are currently:
862 <URL:ftp://sunsite.unc.edu/pub/Linux/libs/dld-3.2.5.src.tar.gz> and
863 <URL:ftp://tsx-11.mit.edu/pub/linux/sources/libs/dld-3.2.5.src.tar.gz>.
864 There's also a binary distribution of it:
865 <URL:ftp://sunsite.unc.edu/pub/Linux/libs/dld-3.2.5.bin.tar.gz>.
866
867 2) Get Jack Jansen's DL library; its location is given in the
868 _Extending Python_ manual as <URL:ftp://ftp.cwi.nl/pub/dynload/>.
869 Compile it and install libdl.a wherever you put libdld.a.
870
871 3) Run Python's configure script, giving it the --with-dl-dld option,
872 which requires a parameter giving the directory where you put the
873 libraries.
874
875 4) Recompile Python.
876
Guido van Rossumca318ec1996-06-26 19:50:09 +00008773.15. Q. Under Solaris 2.x, using GCC, how do I use shared libraries?
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000878
879A. Use the linker in /usr/ucb/ld, not the GNU linker. The latter
880cannot create shared libraries.
881
Guido van Rossumca318ec1996-06-26 19:50:09 +00008823.16. Q. Errors when linking with a shared library containing C++ code.
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000883
884A. Link the main Python binary with C++. Change the definition of
885LINKCC in Modules/Makefile to be your C++ compiler. You may have to
886edit config.c slightly to make it compilable with C++.
Guido van Rossum07779351995-02-07 16:59:56 +0000887
Guido van Rossumca318ec1996-06-26 19:50:09 +00008883.17. Q. Tk menus or radiobuttons don't work properly in Python 1.2.
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000889
890A. There's a bug in Tkinter.py; remove ClassType and InstanceType from
891the definition of CallableTypes near the top of Lib/tkinter/Tkinter.py.
892
Guido van Rossumca318ec1996-06-26 19:50:09 +00008933.18. Q. I built with tkintermodule.c enabled but get "Tkinter not found".
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000894
895A. Tkinter.py (note: upper case T) lives in a subdirectory of Lib,
896Lib/tkinter. If you are using the default module search path, you
897probably didn't enable the line in the Modules/Setup file defining
898TKPATH; if you use the environment variable PYTHONPATH, you'll have to
899add the proper tkinter subdirectory.
900
Guido van Rossumca318ec1996-06-26 19:50:09 +00009013.19. Q. I built with Tk 4.0 but Tkinter complains about the Tk version.
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000902
903A. Several things could cause this. You most likely have a Tk 3.6
904installation that wasn't completely eradicated by the Tk 4.0
905installation (which tends to add "4.0" to its installed files). You
906may have the Tk 3.6 support library installed in the place where the
907Tk 4.0 support files should be (default /usr/local/lib/tk/); you may
908have compiled Python with the old tk.h header file (yes, this actually
909compiles!); you may actually have linked with Tk 3.6 even though Tk
9104.0 is also around. Similar for Tcl 7.4 vs. Tcl 7.3.
911
Guido van Rossumca318ec1996-06-26 19:50:09 +00009123.20. Q. Tk doesn't work right on DEC Alpha.
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000913
914A. You probably compiled either Tcl, Tk or Python with gcc. Don't.
915For this platform, which has 64-bit integers, gcc is known to generate
916broken code. The standard cc (which comes bundled with the OS!)
917works. If you still prefer gcc, at least try recompiling with cc
918before reporting problems to the newsgroup or the author; if this
919fixes the problem, report the bug to the gcc developers instead. (As
920far as we know, there are no problem with gcc on other platforms --
921the instabilities seem to be restricted to the DEC Alpha.) See also
922question 3.6.
923
Guido van Rossumca318ec1996-06-26 19:50:09 +00009243.21. Q. Several common system calls are missing from the posix module.
925
926A. Most likely, *all* test compilations run by the configure script
927are failing for some reason or another. Have a look in config.log to
928see what could be the reason. A common reason is specifying a
929directory to the --with-readline option that doesn't contain the
930libreadline.a file.
931
9323.22. Q. ImportError: No module named string, on MS Windows.
933
934A. Most likely, your PYTHONPATH environment variable should be set to
935something like:
936
937set PYTHONPATH=c:\python;c:\python\lib;c:\python\scripts
938
939(assuming Python was installed in c:\python)
940
9413.23. Q. Core dump on SGI when using the gl module.
942
943There are conflicts between entry points in the termcap and curses
944libraries and an entry point in the GL library. There's a hack of a
945fix for the termcap library if it's needed for the GNU readline
946library, but it doesn't work when you're using curses. Concluding,
947you can't build a Python binary containing both the curses and gl
948modules.
949
950
Guido van Rossuma7925f11994-01-26 10:20:16 +0000951
9524. Programming in Python
953========================
954
Guido van Rossum24349991994-02-02 14:12:45 +00009554.1. Q. Is there a source code level debugger with breakpoints, step,
956etc.?
957
958A. Yes. Check out module pdb; pdb.help() prints the documentation (or
959you can read it as Lib/pdb.doc). If you use the STDWIN option,
960there's also a windowing interface, wdb. You can write your own
961debugger by using the code for pdb or wdb as an example.
962
9634.2. Q. Can I create an object class with some methods implemented in
Guido van Rossuma7925f11994-01-26 10:20:16 +0000964C and others in Python (e.g. through inheritance)? (Also phrased as:
965Can I use a built-in type as base class?)
966
967A. No, but you can easily create a Python class which serves as a
968wrapper around a built-in object, e.g. (for dictionaries):
969
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000970 # A user-defined class behaving almost identical
971 # to a built-in dictionary.
972 class UserDict:
973 def __init__(self): self.data = {}
974 def __repr__(self): return repr(self.data)
975 def __cmp__(self, dict):
976 if type(dict) == type(self.data):
977 return cmp(self.data, dict)
978 else:
979 return cmp(self.data, dict.data)
980 def __len__(self): return len(self.data)
981 def __getitem__(self, key): return self.data[key]
982 def __setitem__(self, key, item): self.data[key] = item
983 def __delitem__(self, key): del self.data[key]
984 def keys(self): return self.data.keys()
985 def items(self): return self.data.items()
986 def values(self): return self.data.values()
987 def has_key(self, key): return self.data.has_key(key)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000988
Guido van Rossum24349991994-02-02 14:12:45 +00009894.3. Q. Is there a curses/termcap package for Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000990
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000991A. Yes -- Lance Ellinghaus has written a module that interfaces to
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000992System V's "ncurses". If you know a little curses and some Python,
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000993it's straightforward to use. It is part of the standard Python
994distribution, but not configured by default -- you must enable it by
995editing Modules/Setup. It requires a System V curses implementation.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000996
997You could also consider using the "alfa" (== character cell) version
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000998of STDWIN. (Standard Window System Interface, a portable windowing
999system interface by myself <URL:ftp://ftp.cwi.nl/pub/stdwin/>.) This
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001000will also prepare your program for porting to windowing environments
1001such as X11 or the Macintosh.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001002
Guido van Rossum24349991994-02-02 14:12:45 +000010034.4. Q. Is there an equivalent to C's onexit() in Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +00001004
1005A. Yes, if you import sys and assign a function to sys.exitfunc, it
1006will be called when your program exits, is killed by an unhandled
1007exception, or (on UNIX) receives a SIGHUP or SIGTERM signal.
1008
Guido van Rossum24349991994-02-02 14:12:45 +000010094.5. Q. When I define a function nested inside another function, the
Guido van Rossuma7925f11994-01-26 10:20:16 +00001010nested function seemingly can't access the local variables of the
1011outer function. What is going on? How do I pass local data to a
1012nested function?
1013
1014A. Python does not have arbitrarily nested scopes. When you need to
1015create a function that needs to access some data which you have
1016available locally, create a new class to hold the data and return a
1017method of an instance of that class, e.g.:
1018
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001019 class MultiplierClass:
1020 def __init__(self, factor):
1021 self.factor = factor
1022 def multiplier(self, argument):
1023 return argument * self.factor
Guido van Rossuma7925f11994-01-26 10:20:16 +00001024
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001025 def generate_multiplier(factor):
1026 return MultiplierClass(factor).multiplier
Guido van Rossuma7925f11994-01-26 10:20:16 +00001027
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001028 twice = generate_multiplier(2)
1029 print twice(10)
1030 # Output: 20
1031
1032An alternative solution uses default arguments, e.g.:
1033
1034 def generate_multiplier(factor):
1035 def multiplier(arg, fact = factor):
1036 return arg*fact
1037 return multiplier
1038
1039 twice = generate_multiplier(2)
1040 print twice(10)
1041 # Output: 20
Guido van Rossuma7925f11994-01-26 10:20:16 +00001042
Guido van Rossum24349991994-02-02 14:12:45 +000010434.6. Q. How do I iterate over a sequence in reverse order?
Guido van Rossuma7925f11994-01-26 10:20:16 +00001044
1045A. If it is a list, the fastest solution is
1046
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001047 list.reverse()
1048 try:
1049 for x in list:
1050 "do something with x"
1051 finally:
1052 list.reverse()
Guido van Rossuma7925f11994-01-26 10:20:16 +00001053
1054This has the disadvantage that while you are in the loop, the list
1055is temporarily reversed. If you don't like this, you can make a copy.
1056This appears expensive but is actually faster than other solutions:
1057
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001058 rev = list[:]
1059 rev.reverse()
1060 for x in rev:
1061 <do something with x>
Guido van Rossuma7925f11994-01-26 10:20:16 +00001062
1063If it isn't a list, a more general but slower solution is:
1064
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001065 i = len(list)
1066 while i > 0:
1067 i = i-1
1068 x = list[i]
1069 <do something with x>
Guido van Rossuma7925f11994-01-26 10:20:16 +00001070
1071A more elegant solution, is to define a class which acts as a sequence
1072and yields the elements in reverse order (solution due to Steve
1073Majewski):
1074
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001075 class Rev:
1076 def __init__(self, seq):
1077 self.forw = seq
1078 def __len__(self):
1079 return len(self.forw)
1080 def __getitem__(self, i):
1081 return self.forw[-(i + 1)]
Guido van Rossuma7925f11994-01-26 10:20:16 +00001082
1083You can now simply write:
1084
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001085 for x in Rev(list):
1086 <do something with x>
Guido van Rossuma7925f11994-01-26 10:20:16 +00001087
Guido van Rossum061f1821994-10-06 16:03:45 +00001088Unfortunately, this solution is slowest of all, due to the method
Guido van Rossuma7925f11994-01-26 10:20:16 +00001089call overhead...
1090
Guido van Rossum24349991994-02-02 14:12:45 +000010914.7. Q. My program is too slow. How do I speed it up?
Guido van Rossuma7925f11994-01-26 10:20:16 +00001092
1093A. That's a tough one, in general. There are many tricks to speed up
1094Python code; I would consider rewriting parts in C only as a last
1095resort. One thing to notice is that function and (especially) method
1096calls are rather expensive; if you have designed a purely OO interface
1097with lots of tiny functions that don't do much more than get or set an
1098instance variable or call another method, you may consider using a
1099more direct way, e.g. directly accessing instance variables. Also see
1100the standard module "profile" (described in the file
1101"python/lib/profile.doc") which makes it possible to find out where
1102your program is spending most of its time (if you have some patience
1103-- the profiling itself can slow your program down by an order of
1104magnitude).
1105
Guido van Rossum24349991994-02-02 14:12:45 +000011064.8. Q. When I have imported a module, then edit it, and import it
Guido van Rossuma7925f11994-01-26 10:20:16 +00001107again (into the same Python process), the changes don't seem to take
1108place. What is going on?
1109
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001110A. For reasons of efficiency as well as consistency, Python only reads
Guido van Rossum796b2591995-01-20 23:05:52 +00001111the module file on the first time a module is imported. (Otherwise a
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001112program consisting of many modules, each of which imports the same
Guido van Rossum796b2591995-01-20 23:05:52 +00001113basic module, would read the basic module over and over again.) To
1114force rereading of a changed module, do this:
Guido van Rossuma7925f11994-01-26 10:20:16 +00001115
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001116 import modname
1117 reload(modname)
Guido van Rossuma7925f11994-01-26 10:20:16 +00001118
1119Warning: this technique is not 100% fool-proof. In particular,
1120modules containing statements like
1121
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001122 from modname import some_objects
Guido van Rossuma7925f11994-01-26 10:20:16 +00001123
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001124will continue to work with the old version of the imported objects.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001125
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000011264.9. Q. How do I find the current module name?
Guido van Rossum24349991994-02-02 14:12:45 +00001127
Guido van Rossum061f1821994-10-06 16:03:45 +00001128A. A module can find out its own module name by looking at the
Guido van Rossum24349991994-02-02 14:12:45 +00001129(predefined) global variable __name__. If this has the value
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001130'__main__' you are running as a script.
1131
Guido van Rossuma6c707c1995-01-02 17:32:28 +000011324.10. Q. I have a module in which I want to execute some extra code
1133when it is run as a script. How do I find out whether I am running as
1134a script?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001135
1136A. See the previous question. E.g. if you put the following on the
1137last line of your module, main() is called only when your module is
1138running as a script:
Guido van Rossum24349991994-02-02 14:12:45 +00001139
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001140 if __name__ == '__main__': main()
Guido van Rossum24349991994-02-02 14:12:45 +00001141
Guido van Rossuma6c707c1995-01-02 17:32:28 +000011424.11. Q. I try to run a program from the Demo directory but it fails
1143with ImportError: No module named ...; what gives?
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001144
Guido van Rossum061f1821994-10-06 16:03:45 +00001145A. This is probably an optional module (written in C!) which hasn't
1146been configured on your system. This especially happens with modules
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001147like "Tkinter", "stdwin", "gl", "Xt" or "Xm". For Tkinter, STDWIN and
1148many other modules, see Modules/Setup.in for info on how to add these
1149modules to your Python, if it is possible at all. Sometimes you will
1150have to ftp and build another package first (e.g. STDWIN). Sometimes
1151the module only works on specific platforms (e.g. gl only works on SGI
1152machines).
1153
1154NOTE: if the complaint is about "Tkinter" (upper case T) and you have
Guido van Rossumca318ec1996-06-26 19:50:09 +00001155already configured module "tkinter" (lower case t), the solution is
1156*not* to rename tkinter to Tkinter or vice versa. There is probably
1157something wrong with your module search path. Check out the value of
1158sys.path.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001159
Guido van Rossum061f1821994-10-06 16:03:45 +00001160For X-related modules (Xt and Xm) you will have to do more work: they
1161are currently not part of the standard Python distribution. You will
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001162have to ftp the Extensions tar file, e.g.
Guido van Rossum7be34a81995-05-31 15:17:12 +00001163<URL:ftp://ftp.python.org/pub/python/src/extensions.tar.gz> and follow
1164the instructions there.
Guido van Rossum061f1821994-10-06 16:03:45 +00001165
1166See also the next question.
1167
11684.12. Q. I have successfully built Python with STDWIN but it can't
1169find some modules (e.g. stdwinevents).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001170
1171A. There's a subdirectory of the library directory named 'stdwin'
1172which should be in the default module search path. There's a line in
1173Modules/Setup(.in) that you have to enable for this purpose --
Guido van Rossum061f1821994-10-06 16:03:45 +00001174unfortunately in the latest release it's not near the other
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001175STDWIN-related lines so it's easy to miss it.
1176
11774.13. Q. What GUI toolkits exist for Python?
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001178
1179A. Depending on what platform(s) you are aiming at, there are several.
1180
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001181- There's a neat object-oriented interface to the Tcl/Tk widget set,
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001182called Tkinter. As of python 1.1, it is part of the standard Python
1183distribution -- all you need to do is enable it in Modules/Setup
1184(provided you have already installed Tk and Tcl). This is probably
1185the easiest to install and use, and the most complete widget set. It
1186is also very likely that in the future the standard Python GUI API
1187will be based on or at least look very much like the Tkinter
1188interface. For more info about Tk, including pointers to the source,
Guido van Rossumbf8e7d51995-08-28 03:09:13 +00001189see the Tcl/Tk home page <URL:http://www.sunlabs.com/research/tcl/>.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001190
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001191- The standard Python distribution comes with an interface to STDWIN,
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001192a platform-independent low-level windowing interface. You have to ftp
1193the source for STDWIN separately,
1194e.g. <URL:ftp://ftp.cwi.nl/pub/stdwin/> or gatekeeper.dec.com in
1195pub/misc/stdwin <URL:ftp://gatekeeper.dec.com/pub/misc/stdwin/>.
1196STDWIN runs under X11 or the Mac; a Windows port has been attempted
1197but I can't seem to get it working. Note that STDWIN is really not
1198powerful enough to implement a modern GUI (no widgets, etc.) and that
1199I don't have the time to maintain or extend it, so you may be better
1200off using Tkinter or the Motif interface, unless you require
1201portability to the Mac (which is also offered by SUIT, by the way --
1202see below).
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001203
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001204- For SGI IRIX only, there's an interface to the complete GL (Graphics
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001205Library -- low level but very good 3D capabilities) as well as to
1206FORMS (a buttons-and-sliders-etc package built on top of GL by Mark
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001207Overmars -- ftp'able from <URL:ftp://ftp.cs.ruu.nl/pub/SGI/FORMS/>).
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001208
1209- There's an interface to X11, including the Athena and Motif widget
1210sets (and a few individual widgets, like Mosaic's HTML widget and
1211SGI's GL widget) in the Extensions set, which is separately ftp'able
Guido van Rossum7be34a81995-05-31 15:17:12 +00001212<URL:ftp://ftp.python.org/pub/python/src/extensions.tar.gz>.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001213
1214- There's an interface to SUIT, the U of Virginia's Simple User
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001215Interface Toolkit; it can be ftp'ed from
1216<URL:ftp://uvacs.cs.virginia.edu/pub/suit/python/SUIT_python.tar.Z>.
1217A PC binary of Python 1.0.2 compiled with DJGPP and with SUIT support
1218built-in has been made available by Antonio Costa
1219<URL:ftp://asterix.inescn.pt/pub/PC/python/pyt102su.exe> (a
1220self-extracting archive). Note that the UVa people themselves have
1221expressed doubts about SUIT, and are planning to build a Python GUI
1222API based upon Tk (though not necessarily on Tkinter); see
1223<URL:http://server.cs.virginia.edu/~tnb2d/IT/IT.html>.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001224
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001225- There's an interface to WAFE, a Tcl interface to the X11 Motif and
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001226Athena widget sets. Last I heard about it it was included in the WAFE
12271.0 prerelease
1228<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 +00001229
Guido van Rossumbf8e7d51995-08-28 03:09:13 +00001230- The NT port by Mark Hammond (see question 7.4) includes an interface
1231to the Microsoft Foundation Classes and a Python programming
1232environment using it that's written mostly in Python. See
1233<URL:ftp://ftp.python.org/pub/python/nt/>.
1234
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00001235- There's an interface to wxWindows. wxWindows is a portable GUI
1236class library written in C++. It supports XView, Motif, MS-Windows as
1237targets. There is some support for Macs and CURSES as well. wxWindows
1238preserves the look and feel of the underlying graphics toolkit. See
1239the wxPython WWW page at
1240<URL:http://www.aiai.ed.ac.uk/~jacs/wx/wxpython/wxpython.html>.
1241
Guido van Rossum3fc9d731995-07-25 15:10:56 +00001242- There's an object-oriented GUI based on the Microsoft Foundation
1243Classes model called WPY. Programs written in WPY run unchanged and
1244with native look and feel on NT, Windows 3.1 (using win32s) and on
1245Unix (using Tk). Source and binaries for NT and Linux are available
1246in <URL:ftp://ftp.python.org/pub/python/wpy>.
1247
Guido van Rossum0d20cfa1996-07-30 18:53:05 +00001248- (The Fresco port that was mentioned in earlier versions of this FAQ
1249no longer seems to exist. Inquire with Mark Linton.)
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00001250
Guido van Rossum061f1821994-10-06 16:03:45 +000012514.14. Q. Are there any interfaces to database packages in Python?
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001252
1253A. There's an interface to SYBASE by John Redford
1254<jredford@lehman.com>.
1255
Guido van Rossum796b2591995-01-20 23:05:52 +00001256There's an interface to metalbase by Lance Ellinghaus
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001257<lance@markv.com>; it is part of the separate Extensions distribution
Guido van Rossum7be34a81995-05-31 15:17:12 +00001258<URL:ftp://ftp.python.org/pub/python/src/extensions.tar.gz>.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001259
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001260Anthony Baxter <anthony.baxter@aaii.oz.au> has written an interface to
Guido van Rossume530c581995-04-10 12:32:16 +00001261mSQL (mini-SQL).
Guido van Rossum7be34a81995-05-31 15:17:12 +00001262<URL:ftp://ftp.python.org/pub/python/contrib/PymSQL.tar.gz>.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001263
Guido van Rossum7be34a81995-05-31 15:17:12 +00001264Tom Culliton <culliton@clark.net> has written an Oracle module.
1265See <URL:ftp://ftp.clark.net/pub/culliton/oramod.tgz>.
Guido van Rossum796b2591995-01-20 23:05:52 +00001266
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000012674.15. Q. Is it possible to write obfuscated one-liners in Python?
Guido van Rossumc24da7c1994-09-23 14:08:41 +00001268
1269A. Yes. See the following three examples, due to Ulf Bartelt:
1270
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001271 # Primes < 1000
1272 print filter(None,map(lambda y:y*reduce(lambda x,y:x*y!=0,
1273 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 +00001274
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001275 # First 10 Fibonacci numbers
1276 print map(lambda x,f=lambda x,f:(x<=1) or (f(x-1,f)+f(x-2,f)): f(x,f),
1277 range(10))
Guido van Rossumc24da7c1994-09-23 14:08:41 +00001278
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001279 # Mandelbrot set
1280 print (lambda Ru,Ro,Iu,Io,IM,Sx,Sy:reduce(lambda x,y:x+y,map(lambda y,
1281 Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,Sy=Sy,L=lambda yc,Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,i=IM,
1282 Sx=Sx,Sy=Sy:reduce(lambda x,y:x+y,map(lambda x,xc=Ru,yc=yc,Ru=Ru,Ro=Ro,
1283 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
1284 >=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(
1285 64+F(Ru+x*(Ro-Ru)/Sx,yc,0,0,i)),range(Sx))):L(Iu+y*(Io-Iu)/Sy),range(Sy
1286 ))))(-2.1, 0.7, -1.2, 1.2, 30, 80, 24)
1287 # \___ ___/ \___ ___/ | | |__ lines on screen
1288 # V V | |______ columns on screen
1289 # | | |__________ maximum of "iterations"
1290 # | |_________________ range on y axis
1291 # |____________________________ range on x axis
Guido van Rossumc24da7c1994-09-23 14:08:41 +00001292
1293Don't try this at home, kids!
1294
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000012954.16. Q. Is there an equivalent of C's "?:" ternary operator?
Guido van Rossumc24da7c1994-09-23 14:08:41 +00001296
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001297A. Not directly. In many cases you can mimic a?b:c with "a and b or
1298c", but there's a flaw: if b is zero (or empty, or None -- anything
1299that tests false) then c will be selected instead. In many cases you
1300can prove by looking at the code that this can't happen (e.g. because
1301b is a constant or has a type that can never be false), but in general
1302this can be a problem.
1303
1304Steve Majewski (or was it Tim Peters?) suggested the following
1305solution: (a and [b] or [c])[0]. Because [b] is a singleton list it
1306is never false, so the wrong path is never taken; then applying [0] to
1307the whole thing gets the b or c that you really wanted. Ugly, but it
1308gets you there in the rare cases where it is really inconvenient to
1309rewrite your code using 'if'.
1310
13114.17. Q. My class defines __del__ but it is not called when I delete the
1312object.
1313
1314A. There are several possible reasons for this.
1315
1316- The del statement does not necessarily call __del__ -- it simply
1317decrements the object's reference count, and if this reaches zero
1318__del__ is called.
1319
1320- If your data structures contain circular links (e.g. a tree where
1321each child has a parent pointer and each parent has a list of
1322children) the reference counts will never go back to zero. You'll
Guido van Rossum061f1821994-10-06 16:03:45 +00001323have to define an explicit close() method which removes those
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001324pointers. Please don't ever call __del__ directly -- __del__ should
Guido van Rossum061f1821994-10-06 16:03:45 +00001325call close() and close() should make sure that it can be called more
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001326than once for the same object.
1327
1328- If the object has ever been a local variable (or argument, which is
1329really the same thing) to a function that caught an expression in an
1330except clause, chances are that a reference to the object still exists
1331in that function's stack frame as contained in the stack trace.
1332Normally, deleting (better: assigning None to) sys.exc_traceback will
1333take care of this. If you a stack was printed for an unhandled
1334exception in an interactive interpreter, delete sys.last_traceback
1335instead.
1336
1337- There is code that deletes all objects when the interpreter exits,
1338but if your Python has been configured to support threads, it is not
1339called (because other threads may still be active). You can define
Guido van Rossum061f1821994-10-06 16:03:45 +00001340your own cleanup function using sys.exitfunc (see question 4.4).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001341
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001342- Finally, there are some obscure bugs if your __del__ method does
1343complicated things such as updating dictionaries or lists or
1344references globals. I hope to have fixed these in release 1.2.
1345
13464.18. Q. How do I change the shell environment for programs called
1347using os.popen() or os.system()? Changing os.environ doesn't work.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001348
1349A. Modifying the environment passed to subshells was left out of the
1350interpreter because there seemed to be no well-established portable
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001351way to do it (in particular, some systems, have putenv(), others have
1352setenv(), and some have none at all).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001353
1354However if all you want is to pass environment variables to the
1355commands run by os.system() or os.popen(), there's a simple solution:
1356prefix the command string with a couple of variable assignments and
Guido van Rossum796b2591995-01-20 23:05:52 +00001357export statements. The following would be universal for popen:
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001358
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001359 import os
1360 from commands import mkarg # nifty routine to add shell quoting
1361 def epopen(cmd, mode, env = {}):
1362 # env is a dictionary of environment variables
1363 prefix = ''
Guido van Rossum796b2591995-01-20 23:05:52 +00001364 for key, value in env.items():
1365 prefix = prefix + '%s=%s\n' % (key, mkarg(value)[1:])
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001366 prefix = prefix + 'export %s\n' % key
1367 return os.popen(prefix + cmd, mode)
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001368
13694.19. Q. What is a class?
1370
1371A. A class is the particular object type that is created by executing
Guido van Rossum796b2591995-01-20 23:05:52 +00001372a class statement. Class objects are used as templates, to create
1373class instance objects, which embody both the data structure and
1374program routines specific to a datatype.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001375
13764.20. Q. What is a method?
1377
1378A. A method is a function that you normally call as
Guido van Rossum796b2591995-01-20 23:05:52 +00001379x.name(arguments...) for some object x. The term is used for methods
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001380of classes and class instances as well as for methods of built-in
Guido van Rossum796b2591995-01-20 23:05:52 +00001381objects. (The latter have a completely different implementation and
1382only share the way their calls look in Python code.) Methods of
1383classes (and class instances) are defined as functions inside the
1384class definition.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001385
13864.21. Q. What is self?
1387
1388A. Self is merely a conventional name for the first argument of a
1389method -- i.e. a function defined inside a class definition. A method
1390defined as meth(self, a, b, c) should be called as x.meth(a, b, c) for
1391some instance x of the class in which the definition occurs;
1392the called method will think it is called as meth(x, a, b, c).
1393
13944.22. Q. What is a unbound method?
1395
1396A. An unbound method is a method defined in a class that is not yet
1397bound to an instance. You get an unbound method if you ask for a
1398class attribute that happens to be a function. You get a bound method
1399if you ask for an instance attribute. A bound method knows which
Guido van Rossum061f1821994-10-06 16:03:45 +00001400instance it belongs to and calling it supplies the instance automatically;
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001401an unbound method only knows which class it wants for its first
1402argument (a derived class is also OK). Calling an unbound method
1403doesn't "magically" derive the first argument from the context -- you
1404have to provide it explicitly.
1405
Guido van Rossuma6c707c1995-01-02 17:32:28 +000014064.23. Q. How do I call a method defined in a base class from a derived
1407class that overrides it?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001408
1409A. If your class definition starts with "class Derived(Base): ..."
1410then you can call method meth defined in Base (or one of Base's base
1411classes) as Base.meth(self, arguments...). Here, Base.meth is an
1412unbound method (see previous question).
1413
Guido van Rossuma6c707c1995-01-02 17:32:28 +000014144.24. Q. How do I call a method from a base class without using the
1415name of the base class?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001416
1417A. DON'T DO THIS. REALLY. I MEAN IT. It appears that you could call
1418self.__class__.__bases__[0].meth(self, arguments...) but this fails when
Guido van Rossum061f1821994-10-06 16:03:45 +00001419a doubly-derived method is derived from your class: for its instances,
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001420self.__class__.__bases__[0] is your class, not its base class -- so
1421(assuming you are doing this from within Derived.meth) you would start
1422a recursive call.
1423
14244.25. Q. How can I organize my code to make it easier to change the base
1425class?
1426
1427A. You could define an alias for the base class, assign the real base
1428class to it before your class definition, and use the alias throughout
1429your class. Then all you have to change is the value assigned to the
1430alias. Incidentally, this trick is also handy if you want to decide
1431dynamically (e.g. depending on availability of resources) which base
1432class to use. Example:
1433
1434BaseAlias = <real base class>
1435class Derived(BaseAlias):
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001436 def meth(self):
1437 BaseAlias.meth(self)
1438 ...
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001439
14404.26. Q. How can I find the methods or attributes of an object?
1441
1442A. This depends on the object type.
1443
1444For an instance x of a user-defined class, instance attributes are
1445found in the dictionary x.__dict__, and methods and attributes defined
1446by its class are found in x.__class__.__bases__[i].__dict__ (for i in
1447range(len(x.__class__.__bases__))). You'll have to walk the tree of
1448base classes to find *all* class methods and attributes.
1449
1450Many, but not all built-in types define a list of their method names
1451in x.__methods__, and if they have data attributes, their names may be
1452found in x.__members__. However this is only a convention.
1453
1454For more information, read the source of the standard (but
1455undocumented) module newdir.
1456
14574.27. Q. I can't seem to use os.read() on a pipe created with os.popen().
1458
1459A. os.read() is a low-level function which takes a file descriptor (a
1460small integer). os.popen() creates a high-level file object -- the
1461same type used for sys.std{in,out,err} and returned by the builtin
1462open() function. Thus, to read n bytes from a pipe p created with
1463os.popen(), you need to use p.read(n).
Guido van Rossuma7925f11994-01-26 10:20:16 +00001464
Guido van Rossum061f1821994-10-06 16:03:45 +000014654.28. Q. How can I create a stand-alone binary from a Python script?
1466
1467The demo script "Demo/scripts/freeze.py" does what you want. (It's
1468actually not a demo but a support tool -- there is some extra code in
1469the interpreter to accommodate it.) It requires that you have the
1470Python build tree handy, complete with all the lib*.a files.
1471
1472This works by scanning your source recursively for import statements
1473(both forms) and looking for the modules on the standard Python path
1474as well as in the source directory (for built-in modules). It then
1475"compiles" the modules written in Python to C code (array initializers
1476that can be turned into code objects using the marshal module) and
1477creates a custom-made config file that only contains those built-in
1478modules which are actually used in the program. It then compiles the
1479generated C code and links it with the rest of the Python interpreter
1480to form a self-contained binary which acts exactly like your script.
1481
Guido van Rossum7be34a81995-05-31 15:17:12 +00001482Hint: the freeze program only works if your script's filename ends in
1483".py".
Guido van Rossum061f1821994-10-06 16:03:45 +00001484
Guido van Rossum7be34a81995-05-31 15:17:12 +000014854.29. Q. What WWW tools are there for Python?
Guido van Rossum061f1821994-10-06 16:03:45 +00001486
Guido van Rossum7be34a81995-05-31 15:17:12 +00001487A. There's a whole lot in the 1.2 release; see the section on
1488"Internet and WWW" in the Library Reference Manual. Highlights: a CGI
1489parser, a url retriever, an html parser.
Guido van Rossum061f1821994-10-06 16:03:45 +00001490
1491Steve Miale <smiale@cs.indiana.edu> has written a modular WWW browser
1492called Dancer. An alpha version can be FTP'ed from
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001493<URL:ftp://ftp.cs.indiana.edu/pub/smiale/dancer.tar.gz>. (There are a
1494few articles about Dancer in the (hyper)mail archive
1495<URL:http://www.cwi.nl/~guido/hypermail/python-1994q3/index.html>.)
Guido van Rossum061f1821994-10-06 16:03:45 +00001496
Guido van Rossum7be34a81995-05-31 15:17:12 +000014974.30. Q. How do I run a subprocess with pipes connected to both input
Guido van Rossum796b2591995-01-20 23:05:52 +00001498and output?
1499
1500A. This is really a UNIX question. Also, in general, it is unwise to
1501do so, because you can easily cause a deadlock where the parent
1502process is blocked waiting for output from the child, while the child
1503is blocked waiting for input from the child. This can be caused
1504because the parent expects the child to output more text than it does,
1505or it can be caused by data being stuck in stdio buffers due to lack
1506of flushing. The Python parent can of course explicitly flush the data
1507it sends to the child before it reads any output, but if the child is
1508a naive C program it can easily have been written to never explicitly
1509flush its output, even if it is interactive, since flushing is
1510normally automatic.
1511
1512In many cases, all you really need is to run some data through a
1513command and get the result back. Unless the data is infinite in size,
1514the easiest (and often the most efficient!) way to do this is to write
1515it to a temporary file and run the command with that temporary file as
1516input. The standard module tempfile exports a function mktemp() which
1517generates unique temporary file names.
1518
1519If after reading all of the above you still want to connect two pipes
1520to a subprocess's standard input and output, here's a simple solution,
1521due to Jack Jansen:
1522
Guido van Rossume7d92d51995-10-11 18:06:54 +00001523 import os
1524 import sys
1525 import string
Guido van Rossum796b2591995-01-20 23:05:52 +00001526
Guido van Rossume7d92d51995-10-11 18:06:54 +00001527 MAXFD = 100 # Max number of file descriptors in this system
Guido van Rossum796b2591995-01-20 23:05:52 +00001528
Guido van Rossume7d92d51995-10-11 18:06:54 +00001529 def popen2(cmd):
1530 cmd = string.split(cmd)
1531 p2cread, p2cwrite = os.pipe()
1532 c2pread, c2pwrite = os.pipe()
1533 pid = os.fork()
1534 if pid == 0:
1535 # Child
1536 os.close(0)
1537 os.close(1)
Guido van Rossumca318ec1996-06-26 19:50:09 +00001538 if os.dup(p2cread) != 0:
Guido van Rossume7d92d51995-10-11 18:06:54 +00001539 sys.stderr.write('popen2: bad read dup\n')
Guido van Rossumca318ec1996-06-26 19:50:09 +00001540 if os.dup(c2pwrite) != 1:
Guido van Rossume7d92d51995-10-11 18:06:54 +00001541 sys.stderr.write('popen2: bad write dup\n')
1542 for i in range(3, MAXFD):
1543 try:
1544 os.close(i)
1545 except:
1546 pass
1547 try:
1548 os.execv(cmd[0], cmd)
1549 finally:
1550 os._exit(1)
1551 os.close(p2cread)
1552 tochild = os.fdopen(p2cwrite, 'w')
1553 os.close(c2pwrite)
1554 fromchild = os.fdopen(c2pread, 'r')
1555 return fromchild, tochild
Guido van Rossum796b2591995-01-20 23:05:52 +00001556
1557Note that many interactive programs (e.g. vi) don't work well with
1558pipes substituted for standard input and output. You will have to use
1559pseudo ttys ("ptys") instead of pipes. There is some undocumented
1560code to use these in the library module pty.py -- I'm afraid you're on
Guido van Rossum3fc9d731995-07-25 15:10:56 +00001561your own here.
1562
1563A different answer is a Python interface to Don Libes' "expect"
1564library. A prerelease of this is available on the Python ftp mirror
1565sites in the contrib subdirectory as expy-0.3.tar.gz, e.g.
1566<URL:ftp://ftp.python.org/pub/python/contrib/expy-0.3.tar.gz>.
Guido van Rossum796b2591995-01-20 23:05:52 +00001567
Guido van Rossum7be34a81995-05-31 15:17:12 +000015684.31. Q. How do I call a function if I have the arguments in a tuple?
Guido van Rossumac3f2121995-04-10 11:53:42 +00001569
1570A. Use the built-in function apply(). For instance,
1571
1572 func(1, 2, 3)
1573
1574is equivalent to
1575
1576 args = (1, 2, 3)
1577 apply(func, args)
1578
1579Note that func(args) is not the same -- it calls func() with exactly
1580one argument, the tuple args, instead of three arguments, the integers
15811, 2 and 3.
1582
Guido van Rossum7be34a81995-05-31 15:17:12 +000015834.32. Q. How do I enable font-lock-mode for Python in Emacs?
1584
1585A. Assuming you're already using python-mode and font-lock-mode
1586separately, all you need to do is put this in your .emacs file:
1587
Guido van Rossume7d92d51995-10-11 18:06:54 +00001588 (defun my-python-mode-hook ()
1589 (setq font-lock-keywords python-font-lock-keywords)
1590 (font-lock-mode 1))
1591 (add-hook 'python-mode-hook 'my-python-mode-hook)
Guido van Rossum7be34a81995-05-31 15:17:12 +00001592
15934.33. Q. Is there an inverse to the format operator (a la C's scanf())?
1594
1595A. Not as such.
1596
1597For simple input parsing, the easiest approach is usually to split
1598the line into whitespace-delimited words using string.split(), and to
1599convert decimal strings to numeric values using string.atoi(),
1600string.atol() or string.atof(). (Python's atoi() is 32-bit and its
1601atol() is arbitrary precision.) If you want to use another delimiter
1602than whitespace, use string.splitfield() (possibly combining it with
1603string.strip() which removes surrounding whitespace from a string).
1604
1605For more complicated input parsing, regular expressions (see module
1606regex) are better suited and more powerful than C's scanf().
1607
Guido van Rossumca318ec1996-06-26 19:50:09 +000016084.34. Q. Can I have Tk events handled while waiting for I/O?
1609
1610A. Yes, and you don't even need threads! But you'll have to
1611restructure your I/O code a bit. Tk has the equivalent of Xt's
1612XtAddInput() call, which allows you to register a callback function
1613which will be called from the Tk mainloop when I/O is possible on a
1614file descriptor. Here's what you need:
1615
1616 from Tkinter import tkinter
1617 tkinter.createfilehandler(file, mask, callback)
1618
1619The file may be a Python file or socket object (actually, anything
1620with a fileno() method), or an integer file descriptor. The mask is
1621one of the constants tkinter.READABLE or tkinter.WRITABLE. The
1622callback is called as follows:
1623
1624 callback(file, mask)
1625
1626You must unregister the callback when you're done, using
1627
1628 tkinter.deletefilehandler(file)
1629
1630Note: since you don't know *how many bytes* are available for reading,
1631you can't use the Python file object's read or readline methods, since
1632these will insist on reading a predefined number of bytes. For
1633sockets, the recv() or recvfrom() methods will work fine; for other
1634files, use os.read(file.fileno(), maxbytecount).
1635
16364.35. Q. How do I write a function with output parameters (call by reference)?
1637
1638A. [Mark Lutz] The thing to remember is that arguments are passed by
1639assignment in Python. Since assignment just creates references to
1640objects, there's no alias between an argument name in the caller and
1641callee, and so no call-by-reference per se. But you can simulate it
1642in a number of ways:
1643
16441) By using global variables; but you probably shouldn't :-)
1645
16462) By passing a mutable (changeable in-place) object:
1647
1648 def func1(a):
1649 a[0] = 'new-value' # 'a' references a mutable list
1650 a[1] = a[1] + 1 # changes a shared object
1651
1652 args = ['old-value', 99]
1653 func1(args)
1654 print args[0], args[1] # output: new-value 100
1655
16563) By return a tuple, holding the final values of arguments:
1657
1658 def func2(a, b):
1659 a = 'new-value' # a and b are local names
1660 b = b + 1 # assigned to new objects
1661 return a, b # return new values
1662
1663 x, y = 'old-value', 99
1664 x, y = func2(x, y)
1665 print x, y # output: new-value 100
1666
16674) And other ideas that fall-out from Python's object model. For
1668 instance, it might be clearer to pass in a mutable dictionary:
1669
1670 def func3(args):
1671 args['a'] = 'new-value' # args is a mutable dictionary
1672 args['b'] = args['b'] + 1 # change it in-place
1673
1674 args = {'a':' old-value', 'b': 99}
1675 func3(args)
1676 print args['a'], args['b']
1677
16785) Or bundle-up values in a class instance:
1679
1680 class callByRef:
1681 def __init__(self, **args):
1682 for (key, value) in args.items():
1683 setattr(self, key, value)
1684
1685 def func4(args):
1686 args.a = 'new-value' # args is a mutable callByRef
1687 args.b = args.b + 1 # change object in-place
1688
1689 args = callByRef(a='old-value', b=99)
1690 func4(args)
1691 print args.a, args.b
1692
1693 But there's probably no good reason to get this complicated :-).
1694
1695[Python' author favors solution 3 in most cases.]
1696
Guido van Rossum0d20cfa1996-07-30 18:53:05 +000016974.36. Q. Please explain the rules for local and global variables in Python.
1698
1699A. [Ken Manheimer] In Python, procedure variables are implicitly
1700global, unless they assigned anywhere within the block. In that case
1701they are implicitly local, and you need to explicitly declare them as
1702'global'.
1703
1704Though a bit surprising at first, a moments consideration explains
1705this. On one hand, requirement of 'global' for assigned vars provides
1706a bar against unintended side-effects. On the other hand, if global
1707were required for all global references, you'd be using global all the
1708time. Eg, you'd have to declare as global every reference to a
1709builtin function, or to a component of an imported module. This
1710clutter would defeat the usefulness of the 'global' declaration for
1711identifying side-effects.
1712
17134.37. Q. How can I have modules that mutually import each other?
1714
1715A. Jim Roskind recommends the following order in each module:
1716
1717First: all exports (like globals, functions, and classes that don't
1718need imported bases classes).
1719
1720Then: all import statements.
1721
1722Finally: all active code (including globals that are initialized from
1723imported values).
1724
1725Python's author doesn't like this approach much because the imports
1726appear in a strange place, but has to admit that it works. His
1727recommended strategy is to avoid all uses of "from <module> import *"
1728(so everything from an imported module is referenced as
1729<module>.<name>) and to place all code inside functions.
1730Initializations of global variables and class variables should use
1731constants or built-in functions only.
1732
Guido van Rossum061f1821994-10-06 16:03:45 +00001733
Guido van Rossuma7925f11994-01-26 10:20:16 +000017345. Extending Python
1735===================
1736
17375.1. Q. Can I create my own functions in C?
1738
1739A. Yes, you can create built-in modules containing functions,
Guido van Rossum24349991994-02-02 14:12:45 +00001740variables, exceptions and even new types in C. This is explained in
1741the document "Extending and Embedding the Python Interpreter" (the
1742LaTeX file Doc/ext.tex). Also read the chapter on dynamic loading.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001743
17445.2. Q. Can I create my own functions in C++?
1745
1746A. Yes, using the C-compatibility features found in C++. Basically
1747you place extern "C" { ... } around the Python include files and put
1748extern "C" before each function that is going to be called by the
1749Python interpreter. Global or static C++ objects with constructors
1750are probably not a good idea.
1751
Guido van Rossum7ce61c11994-06-13 15:13:56 +000017525.3. Q. How can I execute arbitrary Python statements from C?
1753
1754A. The highest-level function to do this is run_command() which takes
1755a single string argument which is executed in the context of module
1756__main__ and returns 0 for success and -1 when an exception occurred
1757(including SyntaxError). If you want more control, use run_string();
1758see the source for run_command() in Python/pythonrun.c.
1759
17605.4. Q. How can I evaluate an arbitrary Python expression from C?
1761
1762A. Call the function run_string() from the previous question with the
1763start symbol eval_input; it then parses an expression, evaluates it
1764and returns its value. See exec_eval() in Python/bltinmodule.c.
1765
17665.5. Q. How do I extract C values from a Python object?
1767
1768A. That depends on the object's type. If it's a tuple,
1769gettuplesize(o) returns its length and gettupleitem(o, i) returns its
1770i'th item; similar for lists with getlistsize(o) and getlistitem(o,
1771i). For strings, getstringsize(o) returns its length and
1772getstringvalue(o) a pointer to its value (note that Python strings may
1773contain null bytes so strlen() is not safe). To test which type an
1774object is, first make sure it isn't NULL, and then use
1775is_stringobject(o), is_tupleobject(o), is_listobject(o) etc.
1776
17775.6. Q. How do I use mkvalue() to create a tuple of arbitrary length?
1778
1779A. You can't. Use t = newtupleobject(n) instead, and fill it with
1780objects using settupleitem(t, i, o) -- note that this "eats" a
1781reference count of o. Similar for lists with newlistobject(n) and
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001782setlistitem(l, i, o). Note that you *must* set all the tuple items to
1783some value before you pass the tuple to Python code --
1784newtupleobject(n) initializes them to NULL, which isn't a valid Python
1785value.
1786
Guido van Rossum796b2591995-01-20 23:05:52 +000017875.7. Q. What happened to mktuple(), featured in an example in the
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001788Extensions manual?
1789
1790A. It's a typo, I meant newtupleobject() (see previous question).
1791
17925.8. Q. How do I call an object's method from C?
1793
1794A. Here's a function (untested) that might become part of the next
1795release in some form. It uses <stdarg.h> to allow passing the
1796argument list on to vmkvalue():
1797
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001798 object *call_method(object *inst, char *methodname, char *format, ...)
1799 {
1800 object *method;
1801 object *args;
1802 object *result;
1803 va_list va;
1804 method = getattr(inst, methodname);
1805 if (method == NULL) return NULL;
1806 va_start(va, format);
1807 args = vmkvalue(format, va);
1808 va_end(va);
1809 if (args == NULL) {
1810 DECREF(method);
1811 return NULL;
1812 }
1813 result = call_object(method, args);
1814 DECREF(method);
1815 DECREF(args);
1816 return result;
1817 }
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001818
1819This works for any instance that has methods -- whether built-in or
1820user-defined. You are responsible for eventually DECREF'ing the
1821return value.
1822
1823To call, e.g., a file object's "seek" method with arguments 10, 0
1824(assuming the file object pointer is "f"):
1825
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001826 res = call_method(f, "seek", "(OO)", 10, 0);
1827 if (res == NULL) {
1828 ... an exception occurred ...
1829 }
1830 else {
1831 DECREF(res);
1832 }
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001833
1834Note that since call_object() *always* wants a tuple for the argument
1835list, to call a function without arguments, pass "()" for the format,
1836and to call a function with one argument, surround the argument in
1837parentheses, e.g. "(i)".
1838
18395.9. Q. How do I catch the output from print_error()?
1840
1841A. (Due to Mark Hammond):
1842
1843* in Python code, define an object that supports the "write()" method.
1844FWIW, there seems to be a small problem that requires the 'softspace'
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00001845attribute to be defined too (fixed in 1.2).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001846
1847* redirect sys.stdout and sys.stderr to this object.
1848
Guido van Rossum061f1821994-10-06 16:03:45 +00001849* call print_error, or just allow the standard traceback mechanism to
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001850work.
1851
Guido van Rossum061f1821994-10-06 16:03:45 +00001852Then, the output will go wherever your write() method sends it.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001853
18545.10. Q. How do I access a module written in Python from C?
1855
1856A. You can get a pointer to the module object as follows:
1857
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001858 module = import_module("<modulename>");
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001859
1860If the module hasn't been imported yet (i.e. it is not yet present in
1861sys.modules), this initializes the module; otherwise it simply returns
1862the value of sys.modules["<modulename>"]. Note that it doesn't enter
1863the module into any namespace -- it only ensures it has been
1864initialized and is stored in sys.modules.
1865
1866You can then access the module's attributes (i.e. any name defined in
1867the module) as follows:
1868
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001869 attr = getattr(module, "<attrname>");
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001870
1871Calling setattr(), to assign to variables in the module, also works.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001872
Guido van Rossumb0a2ce51995-08-28 19:41:35 +000018735.11. Q. How do I interface to C++ objects from Python?
1874
1875A. Depending on your requirements, there are many approaches. Begin
1876by reading the "Extending and Embedding" document (Doc/ext.tex, see
1877also <URL:http://www.python.org/doc/>). Realize that for the Python
1878run-time system, there isn't a whole lot of difference between C and
1879C++ -- so the strategy to build a new Python type around a C structure
1880(pointer) type will also work for C++ objects.
1881
1882Automatic generation of interfaces between Python and C++ is still at
1883the horizon -- parsing C++ header files requires an almost complete
1884C++ parser, and many features aren't easily translated from C++ to
1885Python: certain forms of operator oveloading, function overloading
1886(best approached by a varargs function which explicitly type-checks
1887its arguments), and reference arguments are just a number of features
1888that are hard to translate correctly if at all.
1889
1890The hardest problem is to transparently translate the C++ class
1891hierarchy to Python, so that Python programs derive classes from C++
1892classes. Given suitable constraints, this may be possible, but it
1893would require more space than I have in this FAQ to explain how.
1894In any case, you can get quite a bit done without this, using just the
1895existing classes from Python.
1896
1897If this all seems rather daunting, that may be because it is -- C++
1898isn't exactly a baby to handle without gloves! However, people have
1899accomplished amazing feats of interfacing between Python and C++, and
1900a detailed question posted to the Python list is likely to elicit some
1901interesting and useful responses.
1902
Guido van Rossuma7925f11994-01-26 10:20:16 +00001903
19046. Python's design
1905==================
1906
Guido van Rossumb0a2ce51995-08-28 19:41:35 +000019076.1. Q. Why isn't there a generic copying operation for objects in Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +00001908
Guido van Rossumb0a2ce51995-08-28 19:41:35 +00001909A. Historically, there wasn't. However, starting in Python 1.2,
1910there's a library module "copy" which supports both shallow and deep
1911copying of most common Python objects (though not things like open
1912files, sockets or windows), including an extensible mechanism to copy
1913class instances.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001914
19156.2. Q. Why isn't there a generic way to implement persistent objects
1916in Python? (Persistent == automatically saved to and restored from
1917disk.)
1918
Guido van Rossumb0a2ce51995-08-28 19:41:35 +00001919A. Like the previous question, historically, there wasn't. The
1920library module "pickle" now solves this in a very general way (though
1921you still can't store things like open files, sockests or windows),
1922and the library module "shelve" uses pickle and (g)dbm to create
1923presistent mappings containing arbitrary Python objects. There are
1924some problems with shelve when using gdbm which will be solved in
1925Python 1.3.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001926
Guido van Rossuma7925f11994-01-26 10:20:16 +000019276.3. Q. Why isn't there a switch or case statement in Python?
1928
1929A. You can do this easily enough with a sequence of
1930if... elif... elif... else. There have been some proposals for switch
Guido van Rossum061f1821994-10-06 16:03:45 +00001931statement syntax, but there is no consensus (yet) on whether and how
Guido van Rossuma7925f11994-01-26 10:20:16 +00001932to do range tests.
1933
Guido van Rossumc50158e1994-05-31 09:18:50 +000019346.4. Q. Why does Python use indentation for grouping of statements?
1935
1936A. Basically I believe that using indentation for grouping is
1937extremely elegant and contributes a lot to the clarity of the average
1938Python program. Most people learn to love this feature after a while.
1939Some arguments for it:
1940
1941- Since there are no begin/end brackets there cannot be a disagreement
1942between grouping perceived by the parser and the human reader. I
1943remember long ago seeing a C fragment like this:
1944
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001945 if (x <= y)
1946 x++;
1947 y--;
1948 z++;
Guido van Rossumc50158e1994-05-31 09:18:50 +00001949
1950and staring a long time at it wondering why y was being decremented
1951even for x > y... (And I wasn't a C newbie then either.)
1952
Guido van Rossum7be34a81995-05-31 15:17:12 +00001953- Since there are no begin/end brackets, Python is much less prone to
1954coding-style conflicts. In C there are loads of different ways to
1955place the braces (including the choice whether to place braces around
1956single statements in certain cases, for consistency). If you're used
1957to reading (and writing) code that uses one style, you will feel at
1958least slightly uneasy when reading (or being required to write)
1959another style.
Guido van Rossumc50158e1994-05-31 09:18:50 +00001960
1961- Many coding styles place begin/end brackets on a line by themself.
1962This makes programs considerably longer and wastes valuable screen
1963space, making it harder to get a good overview over a program.
1964Ideally, a function should fit on one basic tty screen (say, 20
1965lines). 20 lines of Python are worth a LOT more than 20 lines of C.
1966This is not solely due to the lack of begin/end brackets (the lack of
1967declarations also helps, and the powerful operations of course), but
1968it certainly helps!
1969
Guido van Rossum3de27361994-07-25 14:19:33 +000019706.5. Q. Why are Python strings immutable?
1971
1972A. There are two advantages. One is performance: knowing that a
1973string is immutable makes it easy to lay it out at construction time
1974-- fixed and unchanging storage requirements. (This is also one of
Guido van Rossum061f1821994-10-06 16:03:45 +00001975the reasons for the distinction between tuples and lists.) The
Guido van Rossum3de27361994-07-25 14:19:33 +00001976other is that strings in Python are considered as "elemental" as
1977numbers. No amount of activity will change the value 8 to anything
1978else, and in Python, no amount of activity will change the string
1979"eight" to anything else. (Adapted from Jim Roskind)
1980
19816.6. Q. Why don't strings have methods like index() or sort(), like
1982lists?
1983
1984A. Good question. Strings currently don't have methods at all
1985(likewise tuples and numbers). Long ago, it seemed unnecessary to
1986implement any of these functions in C, so a standard library module
1987"string" written in Python was created that performs string related
1988operations. Since then, the cry for performance has moved most of
1989them into the built-in module strop (this is imported by module
Guido van Rossumf8c76d01994-08-17 12:19:53 +00001990string, which is still the preferred interface, without loss of
Guido van Rossum3de27361994-07-25 14:19:33 +00001991performance except during initialization). Some of these functions
1992(e.g. index()) could easily be implemented as string methods instead,
1993but others (e.g. sort()) can't, since their interface prescribes that
1994they modify the object, while strings are immutable (see the previous
1995question).
1996
19976.7. Q. Why does Python use methods for some functionality
1998(e.g. list.index()) but functions for other (e.g. len(list))?
1999
2000A. Functions are used for those operations that are generic for a
2001group of types and which should work even for objects that don't have
2002methods at all (e.g. numbers, strings, tuples). Also, implementing
2003len(), max(), min() as a built-in function is actually less code than
2004implementing them as methods for each type. One can quibble about
2005individual cases but it's really too late to change such things
2006fundamentally now.
2007
20086.8. Q. Why can't I derive a class from built-in types (e.g. lists or
2009files)?
2010
2011A. This is caused by the relatively late addition of (user-defined)
2012classes to the language -- the implementation framework doesn't easily
2013allow it. See the answer to question 4.2 for a work-around. This
2014*may* be fixed in the (distant) future.
2015
20166.9. Q. Why must 'self' be declared and used explicitly in method
2017definitions and calls?
2018
2019A. By asking this question you reveal your C++ background. :-)
2020When I added classes, this was (again) the simplest way of
2021implementing methods without too many changes to the interpreter. I
2022borrowed the idea from Modula-3. It turns out to be very useful, for
2023a variety of reasons.
2024
2025First, it makes it more obvious that you are using a method or
2026instance attribute instead of a local variable. Reading "self.x" or
2027"self.meth()" makes it absolutely clear that an instance variable or
2028method is used even if you don't know the class definition by heart.
2029In C++, you can sort of tell by the lack of a local variable
Guido van Rossum061f1821994-10-06 16:03:45 +00002030declaration (assuming globals are rare or easily recognizable) -- but
Guido van Rossum3de27361994-07-25 14:19:33 +00002031in Python, there are no local variable declarations, so you'd have to
2032look up the class definition to be sure.
2033
2034Second, it means that no special syntax is necessary if you want to
2035explicitly reference or call the method from a particular class. In
2036C++, if you want to use a method from base class that is overridden in
2037a derived class, you have to use the :: operator -- in Python you can
2038write baseclass.methodname(self, <argument list>). This is
2039particularly useful for __init__() methods, and in general in cases
2040where a derived class method wants to extend the base class method of
2041the same name and thus has to call the base class method somehow.
2042
2043Lastly, for instance variables, it solves a syntactic problem with
2044assignment: since local variables in Python are (by definition!) those
2045variables to which a value assigned in a function body (and that
2046aren't explicitly declared global), there has to be some way to tell
2047the interpreter that an assignment was meant to assign to an instance
2048variable instead of to a local variable, and it should preferably be
2049syntactic (for efficiency reasons). C++ does this through
2050declarations, but Python doesn't have declarations and it would be a
2051pity having to introduce them just for this purpose. Using the
2052explicit "self.var" solves this nicely. Similarly, for using instance
2053variables, having to write "self.var" means that references to
2054unqualified names inside a method don't have to search the instance's
2055directories.
2056
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000020576.10. Q. Can't you emulate threads in the interpreter instead of
2058relying on an OS-specific thread implementation?
2059
2060A. Unfortunately, the interpreter pushes at least one C stack frame
2061for each Python stack frame. Also, extensions can call back into
2062Python at almost random moments. Therefore a complete threads
2063implementation requires thread support for C.
2064
Guido van Rossum061f1821994-10-06 16:03:45 +000020656.11. Q. Why can't lambda forms contain statements?
2066
2067A. Python lambda forms cannot contain statements because Python's
Guido van Rossum796b2591995-01-20 23:05:52 +00002068syntactic framework can't handle statements nested inside expressions.
Guido van Rossum061f1821994-10-06 16:03:45 +00002069
2070However, in Python, this is not a serious problem. Unlike lambda
2071forms in other languages, where they add functionality, Python lambdas
2072are only a shorthand notation if you're too lazy to define a function.
2073
2074Functions are already first class objects in Python, and can be
2075declared in a local scope. Therefore the only advantage of using a
2076lambda form instead of a locally-defined function is that you'll have
2077to invent a name for the function -- but that's just a local variable
2078to which the function object (which is exactly the same type of object
2079that a lambda form yields) is assigned!
2080
20816.12. Q. Why is there no more efficient way of iterating over a dictionary
2082than first constructing the list of keys()?
2083
2084A. Have you tried it? I bet it's fast enough for your purposes! In
2085most cases such a list takes only a few percent of the space occupied
2086by the dictionary -- it needs only 4 bytes (the size of a pointer) per
2087key -- a dictionary costs 8 bytes per key plus between 30 and 70
2088percent hash table overhead, plus the space for the keys and values --
2089by necessity all keys are unique objects and a string object (the most
2090common key type) costs at least 18 bytes plus the length of the
2091string. Add to that the values contained in the dictionary, and you
2092see that 4 bytes more per item really isn't that much more memory...
2093
2094A call to dict.keys() makes one fast scan over the dictionary
2095(internally, the iteration function does exist) copying the pointers
2096to the key objects into a pre-allocated list object of the right size.
2097The iteration time isn't lost (since you'll have to iterate anyway --
2098unless in the majority of cases your loop terminates very prematurely
2099(which I doubt since you're getting the keys in random order).
2100
2101I don't expose the dictionary iteration operation to Python
2102programmers because the dictionary shouldn't be modified during the
2103entire iteration -- if it is, there's a very small chance that the
2104dictionary is reorganized because the hash table becomes too full, and
2105then the iteration may miss some items and see others twice. Exactly
2106because this only occurs rarely, it would lead to hidden bugs in
2107programs: it's easy never to have it happen during test runs if you
2108only insert or delete a few items per iteration -- but your users will
2109surely hit upon it sooner or later.
2110
Guido van Rossumb0a2ce51995-08-28 19:41:35 +000021116.13. Q. Can Python be compiled to machine code, C or some other language?
2112
2113A. Not easily. Python's high level data types, dynamic typing of
2114objects and run-time invocation of the interpreter (using eval() or
2115exec) together mean that a "compiled" Python program would probably
2116consist mostly of calls into the Python run-time system, even for
2117seemingly simple operations like "x+1". Thus, the performance gain
2118would probably be minimal.
2119
2120Internally, Python source code is always translated into a "virtual
2121machine code" or "byte code" representation before it is interpreted
2122(by the "Python virtual machine" or "bytecode interpreter"). In order
2123to avoid the overhead of parsing and translating modules that rarely
2124change over and over again, this byte code is written on a file whose
2125name ends in ".pyc" whenever a module is parsed (from a file whose
2126name ends in ".py"). When the corresponding .py file is changed, it
2127is parsed and translated again and the .pyc file is rewritten. There
2128is no performance difference once the .pyc file has been loaded (the
2129bytecode read from the .pyc file is exactly the same as the bytecode
2130created by direct translation). The only difference is that loading
2131code from a .pyc file is faster than parsing and translating a .py
2132file, so the presence of precompiled .pyc files will generally improve
2133start-up time of Python scripts. If desired, the Lib/compileall.py
2134module/script can be used to force creation of valid .pyc files for a
2135given set of modules.
2136
2137If you are looking for a way to translate Python programs in order to
2138distribute them in binary form, without the need to distribute the
2139interpreter and library as well, have a look at the freeze.py script
2140in the Tools/freeze directory. This creates a single binary file
2141incorporating your program, the Python interpreter, and those parts of
2142the Python library that are needed by your program. Of course, the
2143resulting binary will only run on the same type of platform as that
2144used to create it.
2145
2146Hints for proper usage of freeze.py:
2147
2148- the script must be in a file whose name ends in .py
2149
2150- you must have installed Python fully:
2151
Guido van Rossume7d92d51995-10-11 18:06:54 +00002152 make install
2153 make libinstall
2154 make inclinstall
2155 make libainstall
Guido van Rossumb0a2ce51995-08-28 19:41:35 +00002156
Guido van Rossum05151e01995-09-28 13:24:46 +000021576.14. Q. Why doesn't Python use proper garbage collection?
2158
2159A. It's looking less and less likely that Python will ever get
2160"automatic" garbage collection (GC). For one thing, unless this were
2161added to C as a standard feature, it's a portability pain in the ass.
2162And yes, I know about the Xerox library. It has bits of assembler
2163code for *most* *common* platforms. Not for all. And although it is
2164mostly transparent, it isn't completely transparent (when I once
2165linked Python with it, it dumped core).
2166
2167"Proper" GC also becomes a problem when Python gets embedded into
2168other applications. While in a stand-alone Python it may be fine to
2169replace the standard malloc() and free() with versions provided by the
2170GC library, an application embedding Python may want to have its *own*
2171substitute for malloc() and free(), and may not want Python's. Right
2172now, Python works with anything that implements malloc() and free()
2173properly.
2174
2175Besides, the predictability of destructor calls in Python is kind of
2176attractive. With GC, the following code (which is fine in current
2177Python) will run out of file descriptors long before it runs out of
2178memory:
2179
Guido van Rossume7d92d51995-10-11 18:06:54 +00002180 for file in <very long list of files>:
2181 f = open(file)
2182 c = file.read(1)
Guido van Rossum05151e01995-09-28 13:24:46 +00002183
2184Using the current reference counting and destructor scheme, each new
2185assignment to f closes the previous file. Using GC, this is not
2186guaranteed. Sure, you can think of ways to fix this. But it's not
2187off-the-shelf technology.
2188
Guido van Rossuma7925f11994-01-26 10:20:16 +00002189
21907. Using Python on non-UNIX platforms
2191=====================================
2192
Guido van Rossum91f60831994-02-15 15:52:27 +000021937.1. Q. Is there a Mac version of Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +00002194
Guido van Rossume530c581995-04-10 12:32:16 +00002195A. Yes, see the "mac" subdirectory of the distribution sites,
Guido van Rossum7be34a81995-05-31 15:17:12 +00002196e.g. <URL:ftp://ftp.python.org/pub/python/mac/>.
Guido van Rossuma7925f11994-01-26 10:20:16 +00002197
Guido van Rossum91f60831994-02-15 15:52:27 +000021987.2. Q. Is there a DOS version of Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +00002199
Guido van Rossume530c581995-04-10 12:32:16 +00002200A. Yes, see the "pc" subdirectory of the distribution sites,
Guido van Rossum7be34a81995-05-31 15:17:12 +00002201e.g. <URL:ftp://ftp.python.org/pub/python/pc/>.
Guido van Rossuma7925f11994-01-26 10:20:16 +00002202
Guido van Rossume530c581995-04-10 12:32:16 +000022037.3. Q. Is there a Windows 3.1(1) version of Python?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00002204
Guido van Rossume530c581995-04-10 12:32:16 +00002205A. Yes, also see the "pc" subdirectory of the distribution sites,
Guido van Rossum3fc9d731995-07-25 15:10:56 +00002206e.g. <URL:ftp://ftp.python.org/pub/python/pc/>. You may also be able
2207to run either of the Windows NT versions (see next question) if you
2208have Microsoft's "win32s".
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00002209
Guido van Rossum91f60831994-02-15 15:52:27 +000022107.4. Q. Is there a Windows NT version of Python?
2211
Guido van Rossum3fc9d731995-07-25 15:10:56 +00002212A. There are two, both sporting DLL support for dynamic loading of
2213Python modules, and extensions to access the Win32 GUI API.
2214
2215Mark Hammond <MHammond@cmutual.com.au> maintains an NT port which
2216includes an interface to the Microsoft Foundation Classes and a Python
2217programming environment using it that's written mostly in Python. See
2218<URL:ftp://ftp.python.org/pub/python/nt/>.
2219
2220Jim Ahlstrom's WPY portable GUI runs on Windows NT and is modeled
2221after the Microsoft Foundation Classes. Source and binaries are
2222available in <URL:ftp://ftp.python.org/pub/python/wpy>.
Guido van Rossum91f60831994-02-15 15:52:27 +00002223
Guido van Rossuma6c707c1995-01-02 17:32:28 +00002224Sam Rushing <rushing@squirl.oau.org> once announced he knows how to
2225build Python for the Windows NT on the DEC Alpha AXP.
Guido van Rossum061f1821994-10-06 16:03:45 +00002226
2227Note that currently there is no unified compilation environment for
2228all NT platforms -- hopefully Microsoft will fix this with the release
2229of Visual C++ 2.0.
2230
Guido van Rossume530c581995-04-10 12:32:16 +000022317.5. Q. Is there a Windows 95 version of Python?
Guido van Rossum7ce61c11994-06-13 15:13:56 +00002232
Guido van Rossum3fc9d731995-07-25 15:10:56 +00002233A. The Windows NT versions might work, otherwise the Windows 3.1(1)
Guido van Rossume530c581995-04-10 12:32:16 +00002234version should work (isn't Windows 95 supposed to be backwards
2235compatible?).
Guido van Rossum7ce61c11994-06-13 15:13:56 +00002236
Guido van Rossume530c581995-04-10 12:32:16 +000022377.6. Q. Is there an OS/2 version of Python?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00002238
Guido van Rossume530c581995-04-10 12:32:16 +00002239A. Yes, also see the "pc" subdirectory of the distribution sites,
Guido van Rossum7be34a81995-05-31 15:17:12 +00002240e.g. <URL:ftp://ftp.python.org/pub/python/pc/>.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00002241
Guido van Rossume530c581995-04-10 12:32:16 +000022427.7. Q. Is there a VMS version of Python?
2243
Guido van Rossum3fc9d731995-07-25 15:10:56 +00002244A. Donn Cave <donn@cac.washington.edu> did a partial port. The
2245results of his efforts are on public display in
Guido van Rossumbf8e7d51995-08-28 03:09:13 +00002246<<URL:ftp://ftp.python.org/pub/python/contrib/vms.tar.gz/>. Someone
2247else is working on a more complete port, for details watch the list.
Guido van Rossume530c581995-04-10 12:32:16 +00002248
Guido van Rossum3fc9d731995-07-25 15:10:56 +000022497.8. Q. What about IBM mainframes, or other non-UNIX platforms?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00002250
Guido van Rossumb0a2ce51995-08-28 19:41:35 +00002251A. I haven't heard about these, except I remember hearing about an
2252OS/9 port and a port to Vxworks (both operating systems for embedded
2253systems). If you're interested in any of this, go directly to the
2254newsgroup and ask there, you may find exactly what you need. For
2255example, a port to MPE/iX 5.0 on HP3000 computers was just announced,
2256see <URL:http://www.allegro.com/software/>.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00002257
Guido van Rossume530c581995-04-10 12:32:16 +000022587.9. Q. Where are the source or Makefiles for the non-UNIX versions?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00002259
Guido van Rossume530c581995-04-10 12:32:16 +00002260A. The standard sources can (almost) be used. Additional sources can
2261be found in the platform-specific subdirectories of the distribution.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00002262
Guido van Rossume530c581995-04-10 12:32:16 +000022637.10. Q. What is the status and support for the non-UNIX versions?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00002264
2265A. I don't have access to most of these platforms, so in general I am
2266dependent on material submitted by volunteers(*). However I strive to
2267integrate all changes needed to get it to compile on a particular
2268platform back into the standard sources, so porting of the next
2269version to the various non-UNIX platforms should be easy.
2270
Guido van Rossuma6c707c1995-01-02 17:32:28 +00002271(*) For the Macintosh, that volunteer is me, with help from Jack
2272Jansen <jack@cwi.nl>.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00002273
Guido van Rossume530c581995-04-10 12:32:16 +000022747.11. Q. I have a PC version but it appears to be only a binary.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00002275Where's the library?
Guido van Rossuma7925f11994-01-26 10:20:16 +00002276
2277A. You still need to copy the files from the distribution directory
Guido van Rossum91f60831994-02-15 15:52:27 +00002278"python/Lib" to your system. If you don't have the full distribution,
Guido van Rossume530c581995-04-10 12:32:16 +00002279you can get the file lib<version>.tar.gz from most ftp sites carrying
2280Python; this is a subset of the distribution containing just those
Guido van Rossum7be34a81995-05-31 15:17:12 +00002281files, e.g. <URL:ftp://ftp.python.org/pub/python/src/lib1.1.tar.gz>.
Guido van Rossuma7925f11994-01-26 10:20:16 +00002282
Guido van Rossum5333c5d1994-04-11 11:06:22 +00002283Once you have installed the library, you need to point sys.path to it.
2284Assuming the library is in C:\misc\python\lib, the following commands
2285will point your Python interpreter to it (note the doubled backslashes
2286-- you can also use single forward slashes instead):
2287
Guido van Rossuma6c707c1995-01-02 17:32:28 +00002288 >>> import sys
2289 >>> sys.path.insert(0, 'C:\\misc\\python\\lib')
2290 >>>
Guido van Rossum5333c5d1994-04-11 11:06:22 +00002291
2292For a more permanent effect, set the environment variable PYTHONPATH,
2293as follows (talking to a DOS prompt):
2294
Guido van Rossuma6c707c1995-01-02 17:32:28 +00002295 C> SET PYTHONPATH=C:\misc\python\lib
Guido van Rossum5333c5d1994-04-11 11:06:22 +00002296
Guido van Rossume530c581995-04-10 12:32:16 +000022977.12. Q. Where's the documentation for the Mac or PC version?
Guido van Rossuma7925f11994-01-26 10:20:16 +00002298
Guido van Rossume530c581995-04-10 12:32:16 +00002299A. The documentation for the Unix version also applies to the Mac and
2300PC versions. Where applicable, differences are indicated in the text.
Guido van Rossuma7925f11994-01-26 10:20:16 +00002301
Guido van Rossume530c581995-04-10 12:32:16 +000023027.13. Q. The Mac (PC) version doesn't seem to have any facilities for
Guido van Rossum91f60831994-02-15 15:52:27 +00002303creating or editing programs apart from entering it interactively, and
2304there seems to be no way to save code that was entered interactively.
2305How do I create a Python program on the Mac (PC)?
Guido van Rossuma7925f11994-01-26 10:20:16 +00002306
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00002307A. Use an external editor. On the Mac, BBEdit seems to be a popular
2308no-frills text editor. I work like this: start the interpreter; edit
2309a module file using BBedit; import and test it in the interpreter;
2310edit again in BBedit; then use the built-in function reload() to
2311re-read the imported module; etc.
Guido van Rossum5333c5d1994-04-11 11:06:22 +00002312
2313Regarding the same question for the PC, Kurt Wm. Hemr writes: "While
2314anyone with a pulse could certainly figure out how to do the same on
2315MS-Windows, I would recommend the NotGNU Emacs clone for MS-Windows.
2316Not only can you easily resave and "reload()" from Python after making
2317changes, but since WinNot auto-copies to the clipboard any text you
2318select, you can simply select the entire procedure (function) which
2319you changed in WinNot, switch to QWPython, and shift-ins to reenter
2320the changed program unit."