blob: 5b2a499187d48cfd0264ae6bb7c64a281d90e54f [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 Rossum81300541996-09-06 16:37:56 +00004From: guido@cnri.reston.va.us (Guido van Rossum)
Guido van Rossum7be34a81995-05-31 15:17:12 +00005Reply-to: guido@cnri.reston.va.us (Guido van Rossum)
Guido van Rossum81300541996-09-06 16:37:56 +00006Expires: Fri, 1 Nov 1996 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 Rossum81300541996-09-06 16:37:56 +000011Submitted-by: Guido van Rossum <guido@cnri.reston.va.us>
Guido van Rossum8727df41996-09-09 15:16:39 +000012Version: 1.34
13Last-modified: 9 September 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
Guido van Rossum81300541996-09-06 16:37:56 +000027 Reston, VA 20191
Guido van Rossume7d92d51995-10-11 18:06:54 +000028 U.S.A.
Guido van Rossum81300541996-09-06 16:37:56 +000029Email: <guido@python.org>, <guido@cnri.reston.va.us>
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?
Guido van Rossuma7925f11994-01-26 10:20:16 +000079
80 2. Python in the real world
81 2.1. Q. How many people are using Python?
82 2.2. Q. Have any significant projects been done in Python?
83 2.3. Q. Are there any commercial projects going on using Python?
Guido van Rossum95f61a71994-01-26 17:23:37 +000084 2.4. Q. How stable is Python?
Guido van Rossum8727df41996-09-09 15:16:39 +000085 2.5. Q. What new developments are expected for Python in the future?
86 2.6. Q. Is it reasonable to propose incompatible changes to Python?
87 2.7. Q. What is the future of Python?
88 2.8. Q. What is the PSA, anyway?
89 2.9. Q. How do I join the PSA?
90 2.10. Q. What are the benefits of joining the PSA?
Guido van Rossuma7925f11994-01-26 10:20:16 +000091
Guido van Rossumb0a2ce51995-08-28 19:41:35 +000092 3. Building Python and Other Known Bugs
Guido van Rossum91f60831994-02-15 15:52:27 +000093 3.1. Q. Is there a test set?
94 3.2. Q. When running the test set, I get complaints about floating point
Guido van Rossuma7925f11994-01-26 10:20:16 +000095 operations, but when playing with floating point operations I cannot
96 find anything wrong with them.
Guido van Rossum7be34a81995-05-31 15:17:12 +000097 3.3. Q. Link errors after rerunning the configure script.
98 3.4. Q. The python interpreter complains about options passed to a
Guido van Rossum24349991994-02-02 14:12:45 +000099 script (after the script name).
Guido van Rossum7be34a81995-05-31 15:17:12 +0000100 3.5. Q. When building on the SGI, make tries to run python to create
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000101 glmodule.c, but python hasn't been built or installed yet.
Guido van Rossum8727df41996-09-09 15:16:39 +0000102 3.6. Q. I use VPATH but some targets are built in the source directory.
103 3.7. Q. Trouble building or linking with the GNU readline library.
104 3.8. Q. Trouble with socket I/O on older Linux 1.x versions.
105 3.9. Q. Trouble with prototypes on Ultrix.
106 3.10. Q. Trouble with posix.listdir on NeXTSTEP 3.2.
107 3.11. Q. Other trouble building Python on platform X.
108 3.12. Q. How to configure dynamic loading on Linux.
109 3.13. Q. Errors when linking with a shared library containing C++ code.
110 3.14. Q. I built with tkintermodule.c enabled but get "Tkinter not found".
111 3.15. Q. I built with Tk 4.0 but Tkinter complains about the Tk version.
112 3.16. Q. Link errors for Tcl/Tk symbols when linking with Tcl/Tk.
113 3.17. Q. I configured and built Python for Tcl/Tk but "import Tkinter"
114 fails.
115 3.18. Q. Tk doesn't work right on DEC Alpha.
116 3.19. Q. Several common system calls are missing from the posix module.
117 3.20. Q. ImportError: No module named string, on MS Windows.
118 3.21. Q. Core dump on SGI when using the gl module.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000119
120 4. Programming in Python
Guido van Rossum24349991994-02-02 14:12:45 +0000121 4.1. Q. Is there a source code level debugger with breakpoints, step,
122 etc.?
123 4.2. Q. Can I create an object class with some methods implemented in
Guido van Rossuma7925f11994-01-26 10:20:16 +0000124 C and others in Python (e.g. through inheritance)? (Also phrased as:
125 Can I use a built-in type as base class?)
Guido van Rossum24349991994-02-02 14:12:45 +0000126 4.3. Q. Is there a curses/termcap package for Python?
127 4.4. Q. Is there an equivalent to C's onexit() in Python?
128 4.5. Q. When I define a function nested inside another function, the
Guido van Rossuma7925f11994-01-26 10:20:16 +0000129 nested function seemingly can't access the local variables of the
130 outer function. What is going on? How do I pass local data to a
131 nested function?
Guido van Rossum24349991994-02-02 14:12:45 +0000132 4.6. Q. How do I iterate over a sequence in reverse order?
133 4.7. Q. My program is too slow. How do I speed it up?
134 4.8. Q. When I have imported a module, then edit it, and import it
Guido van Rossuma7925f11994-01-26 10:20:16 +0000135 again (into the same Python process), the changes don't seem to take
136 place. What is going on?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000137 4.9. Q. How do I find the current module name?
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000138 4.10. Q. I have a module in which I want to execute some extra code
139 when it is run as a script. How do I find out whether I am running as
140 a script?
141 4.11. Q. I try to run a program from the Demo directory but it fails
142 with ImportError: No module named ...; what gives?
Guido van Rossum061f1821994-10-06 16:03:45 +0000143 4.12. Q. I have successfully built Python with STDWIN but it can't
144 find some modules (e.g. stdwinevents).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000145 4.13. Q. What GUI toolkits exist for Python?
Guido van Rossum061f1821994-10-06 16:03:45 +0000146 4.14. Q. Are there any interfaces to database packages in Python?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000147 4.15. Q. Is it possible to write obfuscated one-liners in Python?
148 4.16. Q. Is there an equivalent of C's "?:" ternary operator?
149 4.17. Q. My class defines __del__ but it is not called when I delete the
150 object.
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000151 4.18. Q. How do I change the shell environment for programs called
152 using os.popen() or os.system()? Changing os.environ doesn't work.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000153 4.19. Q. What is a class?
154 4.20. Q. What is a method?
155 4.21. Q. What is self?
156 4.22. Q. What is a unbound method?
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000157 4.23. Q. How do I call a method defined in a base class from a derived
158 class that overrides it?
159 4.24. Q. How do I call a method from a base class without using the
160 name of the base class?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000161 4.25. Q. How can I organize my code to make it easier to change the base
162 class?
163 4.26. Q. How can I find the methods or attributes of an object?
Guido van Rossum061f1821994-10-06 16:03:45 +0000164 4.27. Q. I can't seem to use os.read() on a pipe created with os.popen().
165 4.28. Q. How can I create a stand-alone binary from a Python script?
Guido van Rossum7be34a81995-05-31 15:17:12 +0000166 4.29. Q. What WWW tools are there for Python?
167 4.30. Q. How do I run a subprocess with pipes connected to both input
Guido van Rossum796b2591995-01-20 23:05:52 +0000168 and output?
Guido van Rossum7be34a81995-05-31 15:17:12 +0000169 4.31. Q. How do I call a function if I have the arguments in a tuple?
170 4.32. Q. How do I enable font-lock-mode for Python in Emacs?
171 4.33. Q. Is there an inverse to the format operator (a la C's scanf())?
Guido van Rossumca318ec1996-06-26 19:50:09 +0000172 4.34. Q. Can I have Tk events handled while waiting for I/O?
173 4.35. Q. How do I write a function with output parameters (call by reference)?
Guido van Rossum0d20cfa1996-07-30 18:53:05 +0000174 4.36. Q. Please explain the rules for local and global variables in Python.
175 4.37. Q. How can I have modules that mutually import each other?
Guido van Rossum8727df41996-09-09 15:16:39 +0000176 4.38. Q. How do I copy an object in Python?
177 4.39. Q. How to implement persistent objects in Python? (Persistent ==
178 automatically saved to and restored from disk.)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000179
180 5. Extending Python
181 5.1. Q. Can I create my own functions in C?
182 5.2. Q. Can I create my own functions in C++?
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000183 5.3. Q. How can I execute arbitrary Python statements from C?
184 5.4. Q. How can I evaluate an arbitrary Python expression from C?
185 5.5. Q. How do I extract C values from a Python object?
186 5.6. Q. How do I use mkvalue() to create a tuple of arbitrary length?
Guido van Rossum8727df41996-09-09 15:16:39 +0000187 5.7. Q. How do I call an object's method from C?
188 5.8. Q. How do I catch the output from print_error()?
189 5.9. Q. How do I access a module written in Python from C?
190 5.10. Q. How do I interface to C++ objects from Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000191
192 6. Python's design
Guido van Rossum8727df41996-09-09 15:16:39 +0000193 6.1. Q. Why isn't there a switch or case statement in Python?
194 6.2. Q. Why does Python use indentation for grouping of statements?
195 6.3. Q. Why are Python strings immutable?
196 6.4. Q. Why don't strings have methods like index() or sort(), like
Guido van Rossum3de27361994-07-25 14:19:33 +0000197 lists?
Guido van Rossum8727df41996-09-09 15:16:39 +0000198 6.5. Q. Why does Python use methods for some functionality
Guido van Rossum3de27361994-07-25 14:19:33 +0000199 (e.g. list.index()) but functions for other (e.g. len(list))?
Guido van Rossum8727df41996-09-09 15:16:39 +0000200 6.6. Q. Why can't I derive a class from built-in types (e.g. lists or
Guido van Rossum3de27361994-07-25 14:19:33 +0000201 files)?
Guido van Rossum8727df41996-09-09 15:16:39 +0000202 6.7. Q. Why must 'self' be declared and used explicitly in method
Guido van Rossum3de27361994-07-25 14:19:33 +0000203 definitions and calls?
Guido van Rossum8727df41996-09-09 15:16:39 +0000204 6.8. Q. Can't you emulate threads in the interpreter instead of
205 relying on an OS-specific thread implementation?
206 6.9. Q. Why can't lambda forms contain statements?
207 6.10. Q. Why don't lambdas have access to variables defined in the
208 containing scope?
209 6.11. Q. Why can't recursive functions be defined inside other functions?
Guido van Rossum061f1821994-10-06 16:03:45 +0000210 6.12. Q. Why is there no more efficient way of iterating over a dictionary
211 than first constructing the list of keys()?
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000212 6.13. Q. Can Python be compiled to machine code, C or some other language?
Guido van Rossum05151e01995-09-28 13:24:46 +0000213 6.14. Q. Why doesn't Python use proper garbage collection?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000214
215 7. Using Python on non-UNIX platforms
Guido van Rossum91f60831994-02-15 15:52:27 +0000216 7.1. Q. Is there a Mac version of Python?
217 7.2. Q. Is there a DOS version of Python?
Guido van Rossume530c581995-04-10 12:32:16 +0000218 7.3. Q. Is there a Windows 3.1(1) version of Python?
Guido van Rossum91f60831994-02-15 15:52:27 +0000219 7.4. Q. Is there a Windows NT version of Python?
Guido van Rossume530c581995-04-10 12:32:16 +0000220 7.5. Q. Is there a Windows 95 version of Python?
221 7.6. Q. Is there an OS/2 version of Python?
222 7.7. Q. Is there a VMS version of Python?
Guido van Rossumbf8e7d51995-08-28 03:09:13 +0000223 7.8. Q. What about IBM mainframes, or other non-UNIX platforms?
Guido van Rossume530c581995-04-10 12:32:16 +0000224 7.9. Q. Where are the source or Makefiles for the non-UNIX versions?
225 7.10. Q. What is the status and support for the non-UNIX versions?
226 7.11. Q. I have a PC version but it appears to be only a binary.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000227 Where's the library?
Guido van Rossume530c581995-04-10 12:32:16 +0000228 7.12. Q. Where's the documentation for the Mac or PC version?
229 7.13. Q. The Mac (PC) version doesn't seem to have any facilities for
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000230 creating or editing programs apart from entering it interactively, and
231 there seems to be no way to save code that was entered interactively.
232 How do I create a Python program on the Mac (PC)?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000233
234To find a particular question, search for the question number followed
235by a dot, a space, and a Q at the beginning of a line (e.g. to find
236question 4.2 in vi, type /^4\.2\. Q/).
237
238
2391. General information and availability
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000240 =======================================
Guido van Rossuma7925f11994-01-26 10:20:16 +0000241
2421.1. Q. What is Python?
243
244A. Python is an interpreted, interactive, object-oriented programming
245language. It incorporates modules, exceptions, dynamic typing, very
246high level dynamic data types, and classes. Python combines
247remarkable power with very clear syntax. It has interfaces to many
248system calls and libraries, as well as to various window systems, and
249is extensible in C or C++. It is also usable as an extension language
250for applications that need a programmable interface. Finally, Python
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000251is portable: it runs on many brands of UNIX, on the Mac, and on PCs
252under MS-DOS, Windows, Windows NT, and OS/2.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000253
254To find out more, the best thing to do is to start reading the
255tutorial from the documentation set (see a few questions further
256down).
257
2581.2. Q. Why is it called Python?
259
Guido van Rossum796b2591995-01-20 23:05:52 +0000260A. Apart from being a computer scientist, I'm also a fan of "Monty
Guido van Rossuma7925f11994-01-26 10:20:16 +0000261Python's Flying Circus" (a BBC comedy series from the seventies, in
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000262the -- unlikely -- case you didn't know). It occurred to me one day
263that I needed a name that was short, unique, and slightly mysterious.
264And I happened to be reading some scripts from the series at the
265time... So then I decided to call my language Python. But Python is
266not a joke. And don't you associate it with dangerous reptiles
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000267either! (If you need an icon, use an image of the 16-ton weight from
268the TV series or of a can of SPAM :-)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000269
2701.3. Q. How do I obtain a copy of the Python source?
271
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000272A. The latest complete Python source distribution is always available
273by anonymous ftp, e.g.
Guido van Rossum8727df41996-09-09 15:16:39 +0000274<URL:ftp://ftp.python.org/pub/python/src/python1.3.tar.gz>. It is a
Guido van Rossume530c581995-04-10 12:32:16 +0000275gzipped tar file containing the complete C source, LaTeX
276documentation, Python library modules, example programs, and several
277useful pieces of freely distributable software. This will compile and
278run out of the box on most UNIX platforms. (See section 7 for
279non-UNIX information.)
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000280
Guido van Rossum8727df41996-09-09 15:16:39 +0000281Sometimes beta versions of a newer release are available; check the
282subdirectory "beta" of the above-mentioned URL (i.e.
283<URL:ftp://ftp.python.org/pub/python/src/beta/>). (At the time of
284writing, beta3 for Python 1.4 is available there, and should be
285checked before reporting problems with version 1.3.)
286
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000287Occasionally a set of patches is issued which has to be applied using
288the patch program. These patches are placed in the same directory,
Guido van Rossum7be34a81995-05-31 15:17:12 +0000289e.g. <URL:ftp://ftp.python.org/pub/python/src/patch1.1.1>. (At the time
Guido van Rossum8727df41996-09-09 15:16:39 +0000290of writing, no patches exist.)
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000291
292An index of said ftp directory can be found in the file INDEX. An
293HTML version of the index can be found in the file index.html,
Guido van Rossum7be34a81995-05-31 15:17:12 +0000294<URL:ftp://ftp.python.org/pub/python/index.html>.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000295
2961.4. Q. How do I get documentation on Python?
297
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000298A. The LaTeX source for the documentation is part of the source
299distribution. If you don't have LaTeX, the latest Python
300documentation set is always available by anonymous ftp, e.g.
Guido van Rossum7be34a81995-05-31 15:17:12 +0000301<URL:ftp://ftp.python.org/pub/python/doc/postscript.tar.gz>. It is a
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000302gzipped tar file containing PostScript files of the reference manual,
Guido van Rossum3de27361994-07-25 14:19:33 +0000303the library manual, and the tutorial. Note that the library manual is
304the most important one of the set, as much of Python's power stems
305from the standard or built-in types, functions and modules, all of
306which are described here. PostScript for a high-level description of
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000307Python is in the file nluug-paper.ps (a separate file on the ftp
308site).
Guido van Rossuma7925f11994-01-26 10:20:16 +0000309
Guido van Rossumc50158e1994-05-31 09:18:50 +00003101.5. Q. Are there other ftp sites that mirror the Python distribution?
311
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000312A. The following anonymous ftp sites keep mirrors of the Python
313distribution:
Guido van Rossuma7925f11994-01-26 10:20:16 +0000314
Guido van Rossume7d92d51995-10-11 18:06:54 +0000315USA:
316
317 <URL:ftp://ftp.python.org/pub/python/>
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000318 <URL:ftp://gatekeeper.dec.com/pub/plan/python/>
Guido van Rossume7d92d51995-10-11 18:06:54 +0000319 <URL:ftp://ftp.uu.net/languages/python/>
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000320 <URL:ftp://ftp.wustl.edu/graphics/graphics/sgi-stuff/python/>
Guido van Rossume7d92d51995-10-11 18:06:54 +0000321 <URL:ftp://ftp.sterling.com/programming/languages/python/>
322 <URL:ftp://uiarchive.cso.uiuc.edu/pub/lang/python/>
323 <URL:ftp://ftp.pht.com/mirrors/python/python/>
324
325Europe:
326
327 <URL:ftp://ftp.cwi.nl/pub/python/>
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000328 <URL:ftp://ftp.funet.fi/pub/languages/python/>
329 <URL:ftp://ftp.sunet.se/pub/lang/python/>
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000330 <URL:ftp://unix.hensa.ac.uk/mirrors/uunet/languages/python/>
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000331 <URL:ftp://ftp.ibp.fr/pub/python/>
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000332 <URL:ftp://ftp.switch.ch/software/sources/python/>
333 <URL:ftp://ftp.informatik.tu-muenchen.de/pub/comp/programming/languages/python/>
Guido van Rossuma7925f11994-01-26 10:20:16 +0000334
Guido van Rossume7d92d51995-10-11 18:06:54 +0000335Australia:
336
337 <URL:ftp://ftp.dstc.edu.au/pub/python/>
338
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000339Or try archie on the string "python".
Guido van Rossuma7925f11994-01-26 10:20:16 +0000340
Guido van Rossumc50158e1994-05-31 09:18:50 +00003411.6. Q. Is there a newsgroup or mailing list devoted to Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000342
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000343A. There is a newsgroup, comp.lang.python <URL:news:comp.lang.python>,
344and a mailing list. The newsgroup and mailing list are gatewayed into
345each other -- if you can read news it's unnecessary to subscribe to
346the mailing list. Send e-mail to <python-list-request@cwi.nl> to
Guido van Rossume530c581995-04-10 12:32:16 +0000347(un)subscribe to the mailing list. Hypermail archives of (nearly)
348everything posted to the mailing list (and thus the newsgroup) are
349available on our WWW server,
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000350<URL:http://www.cwi.nl/~guido/hypermail/index.html>. The raw archives
351are also available by ftp, e.g.
Guido van Rossum7be34a81995-05-31 15:17:12 +0000352<URL:ftp://ftp.python.org/pub/python/mail/mailinglist.gz>. The
Guido van Rossume530c581995-04-10 12:32:16 +0000353uncompressed versions of these files can be read with the standard
354UNIX Mail program ("Mail -f file") or with nn ("nn file"). To read
Guido van Rossumbf8e7d51995-08-28 03:09:13 +0000355them using MH, you could use "inc -file file". (The archival service
356has stopped archiving new articles around the end of April 1995. I
357hope to revive it on the PSA server www.python.org sometime in the
358future.)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000359
Guido van Rossuma6c707c1995-01-02 17:32:28 +00003601.7. Q. Is there a WWW page devoted to Python?
361
Guido van Rossum7be34a81995-05-31 15:17:12 +0000362A. Yes, <URL:http://www.python.org/> is the official Python home page.
363At the time of writing, this page is not yet completely operational;
364you may have a look at the old Python home page:
365<URL:http://www.cwi.nl/~guido/Python.html> or at the U.S. copy:
366<URL:http://www.python.org/~guido/Python.html>.
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000367
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00003681.8. Q. Is the Python documentation available on the WWW?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000369
Guido van Rossum7be34a81995-05-31 15:17:12 +0000370A. Yes, see <URL:http://www.python.org/> (Python's home page). It
371contains pointers to hypertext versions of the whole documentation set
372(as hypertext, not just PostScript).
Guido van Rossuma7925f11994-01-26 10:20:16 +0000373
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000374If you wish to browse this collection of HTML files on your own
375machine, it is available bundled up by anonymous ftp,
Guido van Rossum7be34a81995-05-31 15:17:12 +0000376e.g. <URL:ftp://ftp.python.org/pub/python/doc/html.tar.gz>.
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000377
378An Emacs-INFO set containing the library manual is also available by
Guido van Rossum7be34a81995-05-31 15:17:12 +0000379ftp, e.g. <URL:ftp://ftp.python.org/pub/python/doc/lib-info.tar.gz>.
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000380
3811.9. Q. Is there a book on Python, or will there be one out soon?
382
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000383A. Mark Lutz is writing a Python book for O'Reilly and Associates, to
384be published early 1996. See the outline (in PostScript):
385<URL:http://www.python.org/workshops/1995-05/outlinep.eps>.
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000386
3871.10. Q. Are there any published articles about Python that I can quote?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000388
389A. So far the only refereed and published article that describes
390Python in some detail is:
391
392 Guido van Rossum and Jelke de Boer, "Interactively Testing Remote
393 Servers Using the Python Programming Language", CWI Quarterly, Volume
394 4, Issue 4 (December 1991), Amsterdam, pp 283-303.
395
396LaTeX source for this paper is available as part of the Python source
397distribution.
398
Guido van Rossum05151e01995-09-28 13:24:46 +0000399See also the next section (supposedly Aaron Watters' paper has been
400refereed).
401
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00004021.11. Q. Are there short introductory papers or talks on Python?
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000403
Guido van Rossum05151e01995-09-28 13:24:46 +0000404A. A recent, very entertaining introduction to Python is the tutorial by
405Aaron Watters in UnixWorld Online:
406
407 Aaron R. Watters: "The What, Why, Who, and Where of Python",
408 <URL:http://www.wcmh.com/uworld/archives/95/tutorial/005.html>
409
410An olded paper is:
Guido van Rossuma7925f11994-01-26 10:20:16 +0000411
412 Guido van Rossum, "An Introduction to Python for UNIX/C
413 Programmers", in the proceedings of the NLUUG najaarsconferentie
Guido van Rossum061f1821994-10-06 16:03:45 +0000414 1993 (dutch UNIX users group meeting November 1993).
Guido van Rossuma7925f11994-01-26 10:20:16 +0000415
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000416PostScript for this paper and for the slides used for the accompanying
417presentation is available by ftp as
Guido van Rossum7be34a81995-05-31 15:17:12 +0000418<URL:ftp://ftp.python.org/pub/python/doc/nluug-paper.ps> and
419<URL:ftp://ftp.python.org/pub/python/doc/nluug-slides.ps>, respectively.
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000420
421Slides for a talk on Python that I gave at the Usenix Symposium on
Guido van Rossum05151e01995-09-28 13:24:46 +0000422Very High Level Languages in Santa Fe, NM, USA in October 1994 are
Guido van Rossum7be34a81995-05-31 15:17:12 +0000423available as <URL:ftp://ftp.python.org/pub/python/doc/vhll-slides.ps>.
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000424
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00004251.12. Q. How does the Python version numbering scheme work?
Guido van Rossum95f61a71994-01-26 17:23:37 +0000426
Guido van Rossum05151e01995-09-28 13:24:46 +0000427A. Python versions are numbered A.B.C or A.B. A is the major version
428number -- it is only incremented for major changes in functionality or
429source structure. B is the minor version number, incremented for less
Guido van Rossum95f61a71994-01-26 17:23:37 +0000430earth-shattering changes to a release. C is the patchlevel -- it is
Guido van Rossum05151e01995-09-28 13:24:46 +0000431incremented for each new patch release. Not all releases have patch
432releases. Note that in the past, patches have added significant
433changes; in fact the changeover from 0.9.9 to 1.0.0 was the first time
434that either A or B changed!
Guido van Rossum95f61a71994-01-26 17:23:37 +0000435
Guido van Rossum8727df41996-09-09 15:16:39 +0000436Beta versions have an additional suffix of "betaN" for some small
437number N. Note that (for instance) all versions labeled 1.4betaN
438*precede* the actual release of 1.4. 1.4b3 is short for 1.4beta3.
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000439
4401.13. Q. How do I get a beta test version of Python?
441
Guido van Rossum05151e01995-09-28 13:24:46 +0000442A. If there are any beta releases, they are published in the normal
443source directory (e.g. <URL:ftp://ftp.python.org/pub/python/src/>).
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000444
Guido van Rossum05151e01995-09-28 13:24:46 +00004451.14. Q. Are there copyright restrictions on the use of Python?
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000446
447A. Hardly. You can do anything you want with the source, as long as
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000448you leave the copyrights in, and display those copyrights in any
449documentation about Python that you produce. Also, don't use the
450author's institute's name in publicity without prior written
451permission, and don't hold them responsible for anything (read the
452actual copyright for a precise legal wording).
453
454In particular, if you honor the copyright rules, it's OK to use Python
455for commercial use, to sell copies of Python in source or binary form,
456or to sell products that enhance Python or incorporate Python (or part
457of it) in some form. I would still like to know about all commercial
458use of Python!
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000459
Guido van Rossum05151e01995-09-28 13:24:46 +00004601.15. Q. Why was Python created in the first place?
Guido van Rossum7be34a81995-05-31 15:17:12 +0000461
462A. Here's a *very* brief summary of what got me started:
463
464- I had extensive experience with implementing an interpreted language
465in the ABC group at CWI, and from working with this group I had
466learned a lot about language design. This is the origin of many
467Python features, including the use of indentation for statement
468grouping and the inclusion of very-high-level data types (although the
469details are all different in Python).
470
471- I had a number of gripes about the ABC language, but also liked many
472of its features. It was impossible to extend the ABC language (or its
473implementation) to remedy my complaints -- in fact its lack of
474extensibility was one of its biggest problems.
475
476- I had some experience with using Modula-2+ and talked with the
477designers of Modula-3 (and read the M3 report). M3 is the origin of
478the syntax and semantics used for exceptions, and some other Python
479features.
480
481- I was working in the Amoeba distributed operating system group at
482CWI. We needed a better way to do system administration than by
483writing either C programs or Bourne shell scripts, since Amoeba had
484its own system call interface which wasn't easily accessible from the
Guido van Rossumbf8e7d51995-08-28 03:09:13 +0000485Bourne shell. My experience with error handling in Amoeba made me
486acutely aware of the importance of exceptions as a programming
487language feature.
Guido van Rossum7be34a81995-05-31 15:17:12 +0000488
489- It occurred to me that a scripting language with a syntax like ABC
490but with access to the Amoeba system calls would fill the need. I
491realized that it would be foolish to write an Amoeba-specific
492language, so I decided that I needed a language that was generally
493extensible.
494
495- During the 1989 Christmas holidays, I had a lot of time on my hand,
496so I decided to give it a try. During the next year, while still
497mostly working on it in my own time, Python was used in the Amoeba
498project with increasing success, and the feedback from colleagues made
499me add many early improvements.
500
501- In February 1991, after just over a year of development, I decided
502to post to USENET. The rest is in the Misc/HISTORY file.
503
Guido van Rossuma7925f11994-01-26 10:20:16 +0000504
5052. Python in the real world
506===========================
507
5082.1. Q. How many people are using Python?
509
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000510A. I don't know, but the maximum number of simultaneous subscriptions
511to the Python mailing list before it was gatewayed into the newsgroup
512was about 180 (several of which were local redistribution lists). I
513believe that many active Python users don't bother to subscribe to the
514list, and now that there's a newsgroup the mailing list subscription
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000515is even less meaningful. I see new names on the newsgroup all the
516time and my best guess is that there are currently at least several
517thousands of users.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000518
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000519Another statistic is the number of accesses to the Python WWW server.
520Have a look at <URL:http://www.python.org/stats/>.
521
Guido van Rossuma7925f11994-01-26 10:20:16 +00005222.2. Q. Have any significant projects been done in Python?
523
524A. Here at CWI (the home of Python), we have written a 20,000 line
525authoring environment for transportable hypermedia presentations, a
Guido van Rossum5333c5d1994-04-11 11:06:22 +00005265,000 line multimedia teleconferencing tool, as well as many many
527smaller programs.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000528
529The University of Virginia uses Python to control a virtual reality
530engine. Contact: Matt Conway <conway@virginia.edu>.
531
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000532The ILU project at Xerox PARC can generate Python glue for ILU
533interfaces. See <URL:ftp://ftp.parc.xerox.com/pub/ilu/ilu.html>.
534
Guido van Rossumac3f2121995-04-10 11:53:42 +0000535The University of California, Irvine uses a student administration
536system called TELE-Vision written entirely in Python. Contact: Ray
537Price <rlprice@uci.edu>.
538
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000539See also the next question.
540
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000541If you have done a significant project in Python that you'd like to be
542included in the list above, send me email!
Guido van Rossuma7925f11994-01-26 10:20:16 +0000543
5442.3. Q. Are there any commercial projects going on using Python?
545
546A. Several companies have revealed to me that they are planning or
Guido van Rossum796b2591995-01-20 23:05:52 +0000547considering use of Python in a future product.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000548
Guido van Rossumbf8e7d51995-08-28 03:09:13 +0000549Sunrise Software has a product out using Python -- they use Python
Guido van Rossumac3f2121995-04-10 11:53:42 +0000550for a GUI management application and an SNMP network management
551application. Contact: <info@sunrise.com>.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000552
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000553Infoseek uses Python to implement their commercial WWW information
554retrieval service <URL:http://www.infoseek.com/>. Contact:
555<info@infoseek.com>.
Guido van Rossumf8c76d01994-08-17 12:19:53 +0000556
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000557Paul Everitt of Connecting Minds is planning a Lotus Notes gateway.
558Contact: <Paul.Everitt@cminds.com>. Or see their WWW server
559<URL:http://www.cminds.com/>.
560
Guido van Rossumac3f2121995-04-10 11:53:42 +0000561KaPRE in Boulder, CO is using Python for on-site customization of C++
562applications, rapid-prototyping/development,
563language-based-components, and possibly more. This is pretty solid:
564Python's being shipped with their tool-set now, to beta sites.
565Contact: <lutz@KaPRE.COM> (Mark Lutz).
566
567Individuals at many other companies are using Python for internal
568development or for as yet unannounced products (witness their
569contributions to the Python mailing list or newsgroup).
Guido van Rossuma7925f11994-01-26 10:20:16 +0000570
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000571SGI has advertised in the Python list looking for Python programmers
572for a project involving interactive television.
573
574See also the workshop minutes at
575<URL:http://www.python.org/workshops/> -- in general the WWW server is
576more up to date than the FAQ for these issues.
577
Guido van Rossuma7925f11994-01-26 10:20:16 +0000578Python has also been elected as an extension language by MADE, a
579consortium supported by the European Committee's ESPRIT program and
580consisting of Bull, CWI and some other European companies. Contact:
581Ivan Herman <ivan@cwi.nl>.
582
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000583If you'd like to be included in the list above, send me email!
584
Guido van Rossum95f61a71994-01-26 17:23:37 +00005852.4. Q. How stable is Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000586
Guido van Rossum3de27361994-07-25 14:19:33 +0000587A. Very stable. While the current version number would suggest it is
588in the early stages of development, in fact new, stable releases
Guido van Rossume7d92d51995-10-11 18:06:54 +0000589(numbered 0.9.x through 1.3) have been coming out roughly every 3 to
Guido van Rossuma6c707c1995-01-02 17:32:28 +00005906 months for the past four years.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000591
Guido van Rossum8727df41996-09-09 15:16:39 +00005922.5. Q. What new developments are expected for Python in the future?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000593
Guido van Rossum7be34a81995-05-31 15:17:12 +0000594A. See my Work-In-Progress web page, currently at
595<URL:http://www.python.org:~guido/WIP.html>, and the pages for the
596Second Python Workshop (best reached via the Python home page,
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000597<URL:http://www.python.org/>). Also follow the newsgroup discussions!
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000598
Guido van Rossum8727df41996-09-09 15:16:39 +00005992.6. Q. Is it reasonable to propose incompatible changes to Python?
Guido van Rossum3de27361994-07-25 14:19:33 +0000600
601A. In general, no. There are already millions of lines of Python code
602around the world, so any changes in the language that invalidates more
603than a very small fraction of existing programs has to be frowned
604upon. Even if you can provide a conversion program, there still is
605the problem of updating all documentation. Providing a gradual
606upgrade path is the only way if a feature has to be changed.
607
Guido van Rossum8727df41996-09-09 15:16:39 +00006082.7. Q. What is the future of Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000609
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000610A. If I knew, I'd be rich :-)
611
612Seriously, the formation of the PSA (Pyton Software Activity, see
613<URL:http://www.python.org/psa/>) ensures some kind of support even in
614the (unlikely! event that I'd be hit by a bus (actually, here in the
615US, a car accident would be more likely :-), were to join a nunnery,
616or would be head-hunted. A large number of Python users have become
617experts at Python programming as well as maintenance of the
618implementation, and would easily fill the vacuum created by my
619disappearance.
620
621In the mean time, I have no plans to disappear -- rather, I am
622committed to improving Python, and my current benefactor, CNRI (see
623<URL:http://www.cnri.reston.va.us>) is just as committed to continue
624its support of Python and the PSA. In fact, we have great plans for
625Python -- we just can't tell yet!
626
Guido van Rossum8727df41996-09-09 15:16:39 +00006272.8. Q. What is the PSA, anyway?
Guido van Rossum05151e01995-09-28 13:24:46 +0000628
629A. The Python Software Activity <URL:http://www.python.org/psa/> was
630created by a number of Python aficionados who want Python to be more
631than the product and responsibility of a single individual. It has
632found a home at CNRI <URL:http://www.cnri.reston.va.us>. Anybody who
633wishes Python well should join the PSA.
634
Guido van Rossum8727df41996-09-09 15:16:39 +00006352.9. Q. How do I join the PSA?
Guido van Rossum05151e01995-09-28 13:24:46 +0000636
637A. The full scoop is available on the web, see
638<URL:http://www.python.org/psa/Joining.html>. Summary: send a check
639of at least $50 to CNRI/PSA, 1895 Preston White Drive, Suite 100, in
Guido van Rossum81300541996-09-06 16:37:56 +0000640Reston, VA 20191. Full-time students pay $25. Companies can join for
Guido van Rossum05151e01995-09-28 13:24:46 +0000641a mere $500.
642
Guido van Rossum8727df41996-09-09 15:16:39 +00006432.10. Q. What are the benefits of joining the PSA?
Guido van Rossum05151e01995-09-28 13:24:46 +0000644
645A. Like National Public Radio, if not enough people join, Python will
646wither. Your name will be mentioned on the PSA's web server.
647Workshops organized by the PSA <URL:http://www.python.org/workshops/>
648are only accessible to PSA members (you can join at the door). The
649PSA is working on additional benefits, such as reduced prices for
650books and software, and early access to beta versions of Python.
651
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000652
6533. Building Python and Other Known Bugs
654=======================================
Guido van Rossuma7925f11994-01-26 10:20:16 +0000655
Guido van Rossum91f60831994-02-15 15:52:27 +00006563.1. Q. Is there a test set?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000657
658A. Yes, simply do "import testall" (or "import autotest" if you aren't
659interested in the output). The standard modules whose name begins
660with "test" together comprise the test. The test set doesn't test
661*all* features of Python but it goes a long way to confirm that a new
662port is actually working. The Makefile contains an entry "make test"
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000663which runs the autotest module. NOTE: if "make test" fails, run the
664tests manually ("import testall") to see what goes wrong before
665reporting the error.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000666
Guido van Rossum91f60831994-02-15 15:52:27 +00006673.2. Q. When running the test set, I get complaints about floating point
Guido van Rossuma7925f11994-01-26 10:20:16 +0000668operations, but when playing with floating point operations I cannot
669find anything wrong with them.
670
671A. The test set makes occasional unwarranted assumptions about the
672semantics of C floating point operations. Until someone donates a
673better floating point test set, you will have to comment out the
674offending floating point tests and execute similar tests manually.
675
Guido van Rossum7be34a81995-05-31 15:17:12 +00006763.3. Q. Link errors after rerunning the configure script.
Guido van Rossum24349991994-02-02 14:12:45 +0000677
678A. It is generally necessary to run "make clean" after a configuration
679change.
680
Guido van Rossum7be34a81995-05-31 15:17:12 +00006813.4. Q. The python interpreter complains about options passed to a
Guido van Rossum24349991994-02-02 14:12:45 +0000682script (after the script name).
683
684A. You are probably linking with GNU getopt, e.g. through -liberty.
Guido van Rossumf8c76d01994-08-17 12:19:53 +0000685Don't. The reason for the complaint is that GNU getopt, unlike System
686V getopt and other getopt implementations, doesn't consider a
687non-option to be the end of the option list. A quick (and compatible)
688fix for scripts is to add "--" to the interpreter, like this:
689
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000690 #! /usr/local/bin/python --
Guido van Rossumf8c76d01994-08-17 12:19:53 +0000691
692You can also use this interactively:
693
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000694 python -- script.py [options]
695
696Note that a working getopt implementation is provided in the Python
697distribution (in Python/getopt.c) but not automatically used.
Guido van Rossum24349991994-02-02 14:12:45 +0000698
Guido van Rossum7be34a81995-05-31 15:17:12 +00006993.5. Q. When building on the SGI, make tries to run python to create
Guido van Rossum24349991994-02-02 14:12:45 +0000700glmodule.c, but python hasn't been built or installed yet.
701
702A. Comment out the line mentioning glmodule.c in Setup and build a
703python without gl first; install it or make sure it is in your $PATH,
704then edit the Setup file again to turn on the gl module, and make
705again. You don't need to do "make clean"; you do need to run "make
706Makefile" in the Modules subdirectory (or just run "make" at the
707toplevel).
708
Guido van Rossum8727df41996-09-09 15:16:39 +00007093.6. Q. I use VPATH but some targets are built in the source directory.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000710
711A. On some systems (e.g. Sun), if the target already exists in the
712source directory, it is created there instead of in the build
713directory. This is usually because you have previously built without
714VPATH. Try running "make clobber" in the source directory.
715
Guido van Rossum8727df41996-09-09 15:16:39 +00007163.7. Q. Trouble building or linking with the GNU readline library.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000717
Guido van Rossumac3f2121995-04-10 11:53:42 +0000718A. Consider using readline 2.0. Some hints:
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000719
720- You can use the GNU readline library to improve the interactive user
721interface: this gives you line editing and command history when
Guido van Rossum796b2591995-01-20 23:05:52 +0000722calling python interactively. You need to configure and build the GNU
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000723readline library before running the configure script. Its sources are
724no longer distributed with Python; you can ftp them from any GNU
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000725mirror site, or from its home site
726<URL:ftp://slc2.ins.cwru.edu/pub/dist/readline-2.0.tar.gz> (or a
727higher version number -- using version 1.x is not recommended). Pass
728the Python configure script the option --with-readline=DIRECTORY where
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000729DIRECTORY is the absolute pathname of the directory where you've built
730the readline library. Some hints on building and using the readline
731library:
732
733- On SGI IRIX 5, you may have to add the following
734to rldefs.h:
735
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000736 #ifndef sigmask
737 #define sigmask(sig) (1L << ((sig)-1))
738 #endif
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000739
740- On most systems, you will have to add #include "rldefs.h" to the
741top of several source files, and if you use the VPATH feature, you
742will have to add dependencies of the form foo.o: foo.c to the
743Makefile for several values of foo.
744
745- The readline library requires use of the termcap library. A
746known problem with this is that it contains entry points which
Guido van Rossum061f1821994-10-06 16:03:45 +0000747cause conflicts with the STDWIN and SGI GL libraries. The STDWIN
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000748conflict can be solved (and will be, in the next release of
Guido van Rossum061f1821994-10-06 16:03:45 +0000749STDWIN) by adding a line saying '#define werase w_erase' to the
750stdwin.h file (in the STDWIN distribution, subdirectory H). The
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000751GL conflict has been solved in the Python configure script by a
752hack that forces use of the static version of the termcap library.
753
Guido van Rossumac3f2121995-04-10 11:53:42 +0000754- Check the newsgroup gnu.bash.bug <URL:news:gnu.bash.bug> for
755specific problems with the readline library (I don't read this group
756but I've been told that it is the place for readline bugs).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000757
Guido van Rossum8727df41996-09-09 15:16:39 +00007583.8. Q. Trouble with socket I/O on older Linux 1.x versions.
Guido van Rossumca318ec1996-06-26 19:50:09 +0000759
760A. Once you've built Python, use it to run the regen.py script in the
761Lib/linux1 directory. Apparently the files as distributed don't match
762the system headers on some Linux versions.
763
Guido van Rossum8727df41996-09-09 15:16:39 +00007643.9. Q. Trouble with prototypes on Ultrix.
Guido van Rossum72eb83c1994-10-07 11:33:28 +0000765
766A. Ultrix cc seems broken -- use gcc, or edit config.h to #undef
767HAVE_PROTOTYPES.
768
Guido van Rossum8727df41996-09-09 15:16:39 +00007693.10. Q. Trouble with posix.listdir on NeXTSTEP 3.2.
Guido van Rossum9351fdb1994-11-10 23:03:51 +0000770
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000771A. (This often manifests itself as a weird error from the
772compileall.py script run by "make libinstall".)
Guido van Rossum9351fdb1994-11-10 23:03:51 +0000773
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000774Don't use gcc, use the Next C compiler (cc). Even though it is
775derived from (an old version of) gcc, its interpretation of the
776"-posix" switch is different; in this particular case, cc is right and
777gcc is wrong.
Guido van Rossum9351fdb1994-11-10 23:03:51 +0000778
Guido van Rossum8727df41996-09-09 15:16:39 +00007793.11. Q. Other trouble building Python on platform X.
Guido van Rossum95f61a71994-01-26 17:23:37 +0000780
Guido van Rossum81300541996-09-06 16:37:56 +0000781A. Please email the details to <guido@cnri.reston.va.us> and I'll look
782into it. Please provide as many details as possible. In particular,
783if you don't tell me what type of computer and what operating system
784(and version) you are using it will be difficult for me to figure out
785what is the matter. If you get a specific error message, please email
786it to me too.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000787
Guido van Rossum8727df41996-09-09 15:16:39 +00007883.12. Q. How to configure dynamic loading on Linux.
Guido van Rossum07779351995-02-07 16:59:56 +0000789
Guido van Rossum8727df41996-09-09 15:16:39 +0000790A. This is now automatic as long as your Linux version uses the ELF
791object format (all recent Linuxes do).
Guido van Rossum07779351995-02-07 16:59:56 +0000792
Guido van Rossum8727df41996-09-09 15:16:39 +00007933.13. Q. Errors when linking with a shared library containing C++ code.
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000794
795A. Link the main Python binary with C++. Change the definition of
796LINKCC in Modules/Makefile to be your C++ compiler. You may have to
797edit config.c slightly to make it compilable with C++.
Guido van Rossum07779351995-02-07 16:59:56 +0000798
Guido van Rossum8727df41996-09-09 15:16:39 +00007993.14. Q. I built with tkintermodule.c enabled but get "Tkinter not found".
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000800
801A. Tkinter.py (note: upper case T) lives in a subdirectory of Lib,
802Lib/tkinter. If you are using the default module search path, you
803probably didn't enable the line in the Modules/Setup file defining
804TKPATH; if you use the environment variable PYTHONPATH, you'll have to
805add the proper tkinter subdirectory.
806
Guido van Rossum8727df41996-09-09 15:16:39 +00008073.15. Q. I built with Tk 4.0 but Tkinter complains about the Tk version.
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000808
809A. Several things could cause this. You most likely have a Tk 3.6
810installation that wasn't completely eradicated by the Tk 4.0
811installation (which tends to add "4.0" to its installed files). You
812may have the Tk 3.6 support library installed in the place where the
813Tk 4.0 support files should be (default /usr/local/lib/tk/); you may
814have compiled Python with the old tk.h header file (yes, this actually
815compiles!); you may actually have linked with Tk 3.6 even though Tk
8164.0 is also around. Similar for Tcl 7.4 vs. Tcl 7.3.
817
Guido van Rossum8727df41996-09-09 15:16:39 +00008183.16. Q. Link errors for Tcl/Tk symbols when linking with Tcl/Tk.
819
820Quite possibly, there's a version mismatch between the Tcl/Tk header
821files (tcl.h and tk.h) and the tck/tk libraries you are using (the
822"-ltk4.0" and "-ltcl7.4" arguments for _tkinter in the Setup file).
823If you have installed both versions 7.4/4.0 and 7.5/4.1 of Tcl/Tk,
824most likely your header files are for The newer versions, but the
825Setup line for _tkinter in some Python distributions references
8267.4/4.0 by default. Changing this to 7.5/4.1 should take care of
827this.
828
8293.17. Q. I configured and built Python for Tcl/Tk but "import Tkinter"
830fails.
831
832A. Most likely, you forgot to enable the line in Setup that says
833"TKPATH=:$(DESTLIB)/tkinter".
834
8353.18. Q. Tk doesn't work right on DEC Alpha.
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000836
837A. You probably compiled either Tcl, Tk or Python with gcc. Don't.
838For this platform, which has 64-bit integers, gcc is known to generate
839broken code. The standard cc (which comes bundled with the OS!)
840works. If you still prefer gcc, at least try recompiling with cc
841before reporting problems to the newsgroup or the author; if this
842fixes the problem, report the bug to the gcc developers instead. (As
843far as we know, there are no problem with gcc on other platforms --
844the instabilities seem to be restricted to the DEC Alpha.) See also
845question 3.6.
846
Guido van Rossum8727df41996-09-09 15:16:39 +00008473.19. Q. Several common system calls are missing from the posix module.
Guido van Rossumca318ec1996-06-26 19:50:09 +0000848
849A. Most likely, *all* test compilations run by the configure script
850are failing for some reason or another. Have a look in config.log to
851see what could be the reason. A common reason is specifying a
852directory to the --with-readline option that doesn't contain the
853libreadline.a file.
854
Guido van Rossum8727df41996-09-09 15:16:39 +00008553.20. Q. ImportError: No module named string, on MS Windows.
Guido van Rossumca318ec1996-06-26 19:50:09 +0000856
857A. Most likely, your PYTHONPATH environment variable should be set to
858something like:
859
860set PYTHONPATH=c:\python;c:\python\lib;c:\python\scripts
861
862(assuming Python was installed in c:\python)
863
Guido van Rossum8727df41996-09-09 15:16:39 +00008643.21. Q. Core dump on SGI when using the gl module.
Guido van Rossumca318ec1996-06-26 19:50:09 +0000865
866There are conflicts between entry points in the termcap and curses
867libraries and an entry point in the GL library. There's a hack of a
868fix for the termcap library if it's needed for the GNU readline
869library, but it doesn't work when you're using curses. Concluding,
870you can't build a Python binary containing both the curses and gl
871modules.
872
873
Guido van Rossuma7925f11994-01-26 10:20:16 +0000874
8754. Programming in Python
876========================
877
Guido van Rossum24349991994-02-02 14:12:45 +00008784.1. Q. Is there a source code level debugger with breakpoints, step,
879etc.?
880
881A. Yes. Check out module pdb; pdb.help() prints the documentation (or
882you can read it as Lib/pdb.doc). If you use the STDWIN option,
883there's also a windowing interface, wdb. You can write your own
884debugger by using the code for pdb or wdb as an example.
885
8864.2. Q. Can I create an object class with some methods implemented in
Guido van Rossuma7925f11994-01-26 10:20:16 +0000887C and others in Python (e.g. through inheritance)? (Also phrased as:
888Can I use a built-in type as base class?)
889
890A. No, but you can easily create a Python class which serves as a
891wrapper around a built-in object, e.g. (for dictionaries):
892
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000893 # A user-defined class behaving almost identical
894 # to a built-in dictionary.
895 class UserDict:
896 def __init__(self): self.data = {}
897 def __repr__(self): return repr(self.data)
898 def __cmp__(self, dict):
899 if type(dict) == type(self.data):
900 return cmp(self.data, dict)
901 else:
902 return cmp(self.data, dict.data)
903 def __len__(self): return len(self.data)
904 def __getitem__(self, key): return self.data[key]
905 def __setitem__(self, key, item): self.data[key] = item
906 def __delitem__(self, key): del self.data[key]
907 def keys(self): return self.data.keys()
908 def items(self): return self.data.items()
909 def values(self): return self.data.values()
910 def has_key(self, key): return self.data.has_key(key)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000911
Guido van Rossum24349991994-02-02 14:12:45 +00009124.3. Q. Is there a curses/termcap package for Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000913
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000914A. Yes -- Lance Ellinghaus has written a module that interfaces to
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000915System V's "ncurses". If you know a little curses and some Python,
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000916it's straightforward to use. It is part of the standard Python
917distribution, but not configured by default -- you must enable it by
918editing Modules/Setup. It requires a System V curses implementation.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000919
920You could also consider using the "alfa" (== character cell) version
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000921of STDWIN. (Standard Window System Interface, a portable windowing
922system interface by myself <URL:ftp://ftp.cwi.nl/pub/stdwin/>.) This
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000923will also prepare your program for porting to windowing environments
924such as X11 or the Macintosh.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000925
Guido van Rossum24349991994-02-02 14:12:45 +00009264.4. Q. Is there an equivalent to C's onexit() in Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000927
928A. Yes, if you import sys and assign a function to sys.exitfunc, it
929will be called when your program exits, is killed by an unhandled
930exception, or (on UNIX) receives a SIGHUP or SIGTERM signal.
931
Guido van Rossum24349991994-02-02 14:12:45 +00009324.5. Q. When I define a function nested inside another function, the
Guido van Rossuma7925f11994-01-26 10:20:16 +0000933nested function seemingly can't access the local variables of the
934outer function. What is going on? How do I pass local data to a
935nested function?
936
937A. Python does not have arbitrarily nested scopes. When you need to
938create a function that needs to access some data which you have
939available locally, create a new class to hold the data and return a
940method of an instance of that class, e.g.:
941
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000942 class MultiplierClass:
943 def __init__(self, factor):
944 self.factor = factor
945 def multiplier(self, argument):
946 return argument * self.factor
Guido van Rossuma7925f11994-01-26 10:20:16 +0000947
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000948 def generate_multiplier(factor):
949 return MultiplierClass(factor).multiplier
Guido van Rossuma7925f11994-01-26 10:20:16 +0000950
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000951 twice = generate_multiplier(2)
952 print twice(10)
953 # Output: 20
954
955An alternative solution uses default arguments, e.g.:
956
957 def generate_multiplier(factor):
958 def multiplier(arg, fact = factor):
959 return arg*fact
960 return multiplier
961
962 twice = generate_multiplier(2)
963 print twice(10)
964 # Output: 20
Guido van Rossuma7925f11994-01-26 10:20:16 +0000965
Guido van Rossum24349991994-02-02 14:12:45 +00009664.6. Q. How do I iterate over a sequence in reverse order?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000967
968A. If it is a list, the fastest solution is
969
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000970 list.reverse()
971 try:
972 for x in list:
973 "do something with x"
974 finally:
975 list.reverse()
Guido van Rossuma7925f11994-01-26 10:20:16 +0000976
977This has the disadvantage that while you are in the loop, the list
978is temporarily reversed. If you don't like this, you can make a copy.
979This appears expensive but is actually faster than other solutions:
980
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000981 rev = list[:]
982 rev.reverse()
983 for x in rev:
984 <do something with x>
Guido van Rossuma7925f11994-01-26 10:20:16 +0000985
986If it isn't a list, a more general but slower solution is:
987
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000988 i = len(list)
989 while i > 0:
990 i = i-1
991 x = list[i]
992 <do something with x>
Guido van Rossuma7925f11994-01-26 10:20:16 +0000993
994A more elegant solution, is to define a class which acts as a sequence
995and yields the elements in reverse order (solution due to Steve
996Majewski):
997
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000998 class Rev:
999 def __init__(self, seq):
1000 self.forw = seq
1001 def __len__(self):
1002 return len(self.forw)
1003 def __getitem__(self, i):
1004 return self.forw[-(i + 1)]
Guido van Rossuma7925f11994-01-26 10:20:16 +00001005
1006You can now simply write:
1007
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001008 for x in Rev(list):
1009 <do something with x>
Guido van Rossuma7925f11994-01-26 10:20:16 +00001010
Guido van Rossum061f1821994-10-06 16:03:45 +00001011Unfortunately, this solution is slowest of all, due to the method
Guido van Rossuma7925f11994-01-26 10:20:16 +00001012call overhead...
1013
Guido van Rossum24349991994-02-02 14:12:45 +000010144.7. Q. My program is too slow. How do I speed it up?
Guido van Rossuma7925f11994-01-26 10:20:16 +00001015
1016A. That's a tough one, in general. There are many tricks to speed up
1017Python code; I would consider rewriting parts in C only as a last
1018resort. One thing to notice is that function and (especially) method
1019calls are rather expensive; if you have designed a purely OO interface
1020with lots of tiny functions that don't do much more than get or set an
1021instance variable or call another method, you may consider using a
1022more direct way, e.g. directly accessing instance variables. Also see
1023the standard module "profile" (described in the file
1024"python/lib/profile.doc") which makes it possible to find out where
1025your program is spending most of its time (if you have some patience
1026-- the profiling itself can slow your program down by an order of
1027magnitude).
1028
Guido van Rossum24349991994-02-02 14:12:45 +000010294.8. Q. When I have imported a module, then edit it, and import it
Guido van Rossuma7925f11994-01-26 10:20:16 +00001030again (into the same Python process), the changes don't seem to take
1031place. What is going on?
1032
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001033A. For reasons of efficiency as well as consistency, Python only reads
Guido van Rossum796b2591995-01-20 23:05:52 +00001034the module file on the first time a module is imported. (Otherwise a
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001035program consisting of many modules, each of which imports the same
Guido van Rossum796b2591995-01-20 23:05:52 +00001036basic module, would read the basic module over and over again.) To
1037force rereading of a changed module, do this:
Guido van Rossuma7925f11994-01-26 10:20:16 +00001038
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001039 import modname
1040 reload(modname)
Guido van Rossuma7925f11994-01-26 10:20:16 +00001041
1042Warning: this technique is not 100% fool-proof. In particular,
1043modules containing statements like
1044
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001045 from modname import some_objects
Guido van Rossuma7925f11994-01-26 10:20:16 +00001046
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001047will continue to work with the old version of the imported objects.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001048
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000010494.9. Q. How do I find the current module name?
Guido van Rossum24349991994-02-02 14:12:45 +00001050
Guido van Rossum061f1821994-10-06 16:03:45 +00001051A. A module can find out its own module name by looking at the
Guido van Rossum24349991994-02-02 14:12:45 +00001052(predefined) global variable __name__. If this has the value
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001053'__main__' you are running as a script.
1054
Guido van Rossuma6c707c1995-01-02 17:32:28 +000010554.10. Q. I have a module in which I want to execute some extra code
1056when it is run as a script. How do I find out whether I am running as
1057a script?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001058
1059A. See the previous question. E.g. if you put the following on the
1060last line of your module, main() is called only when your module is
1061running as a script:
Guido van Rossum24349991994-02-02 14:12:45 +00001062
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001063 if __name__ == '__main__': main()
Guido van Rossum24349991994-02-02 14:12:45 +00001064
Guido van Rossuma6c707c1995-01-02 17:32:28 +000010654.11. Q. I try to run a program from the Demo directory but it fails
1066with ImportError: No module named ...; what gives?
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001067
Guido van Rossum061f1821994-10-06 16:03:45 +00001068A. This is probably an optional module (written in C!) which hasn't
1069been configured on your system. This especially happens with modules
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001070like "Tkinter", "stdwin", "gl", "Xt" or "Xm". For Tkinter, STDWIN and
1071many other modules, see Modules/Setup.in for info on how to add these
1072modules to your Python, if it is possible at all. Sometimes you will
1073have to ftp and build another package first (e.g. STDWIN). Sometimes
1074the module only works on specific platforms (e.g. gl only works on SGI
1075machines).
1076
1077NOTE: if the complaint is about "Tkinter" (upper case T) and you have
Guido van Rossumca318ec1996-06-26 19:50:09 +00001078already configured module "tkinter" (lower case t), the solution is
1079*not* to rename tkinter to Tkinter or vice versa. There is probably
1080something wrong with your module search path. Check out the value of
1081sys.path.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001082
Guido van Rossum061f1821994-10-06 16:03:45 +00001083For X-related modules (Xt and Xm) you will have to do more work: they
1084are currently not part of the standard Python distribution. You will
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001085have to ftp the Extensions tar file, e.g.
Guido van Rossum7be34a81995-05-31 15:17:12 +00001086<URL:ftp://ftp.python.org/pub/python/src/extensions.tar.gz> and follow
1087the instructions there.
Guido van Rossum061f1821994-10-06 16:03:45 +00001088
1089See also the next question.
1090
10914.12. Q. I have successfully built Python with STDWIN but it can't
1092find some modules (e.g. stdwinevents).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001093
1094A. There's a subdirectory of the library directory named 'stdwin'
1095which should be in the default module search path. There's a line in
1096Modules/Setup(.in) that you have to enable for this purpose --
Guido van Rossum061f1821994-10-06 16:03:45 +00001097unfortunately in the latest release it's not near the other
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001098STDWIN-related lines so it's easy to miss it.
1099
11004.13. Q. What GUI toolkits exist for Python?
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001101
1102A. Depending on what platform(s) you are aiming at, there are several.
1103
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001104- There's a neat object-oriented interface to the Tcl/Tk widget set,
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001105called Tkinter. As of python 1.1, it is part of the standard Python
1106distribution -- all you need to do is enable it in Modules/Setup
1107(provided you have already installed Tk and Tcl). This is probably
1108the easiest to install and use, and the most complete widget set. It
1109is also very likely that in the future the standard Python GUI API
1110will be based on or at least look very much like the Tkinter
1111interface. For more info about Tk, including pointers to the source,
Guido van Rossumbf8e7d51995-08-28 03:09:13 +00001112see the Tcl/Tk home page <URL:http://www.sunlabs.com/research/tcl/>.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001113
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001114- The standard Python distribution comes with an interface to STDWIN,
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001115a platform-independent low-level windowing interface. You have to ftp
1116the source for STDWIN separately,
1117e.g. <URL:ftp://ftp.cwi.nl/pub/stdwin/> or gatekeeper.dec.com in
1118pub/misc/stdwin <URL:ftp://gatekeeper.dec.com/pub/misc/stdwin/>.
1119STDWIN runs under X11 or the Mac; a Windows port has been attempted
1120but I can't seem to get it working. Note that STDWIN is really not
1121powerful enough to implement a modern GUI (no widgets, etc.) and that
1122I don't have the time to maintain or extend it, so you may be better
1123off using Tkinter or the Motif interface, unless you require
1124portability to the Mac (which is also offered by SUIT, by the way --
1125see below).
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001126
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001127- For SGI IRIX only, there's an interface to the complete GL (Graphics
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001128Library -- low level but very good 3D capabilities) as well as to
1129FORMS (a buttons-and-sliders-etc package built on top of GL by Mark
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001130Overmars -- ftp'able from <URL:ftp://ftp.cs.ruu.nl/pub/SGI/FORMS/>).
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001131
1132- There's an interface to X11, including the Athena and Motif widget
1133sets (and a few individual widgets, like Mosaic's HTML widget and
1134SGI's GL widget) in the Extensions set, which is separately ftp'able
Guido van Rossum7be34a81995-05-31 15:17:12 +00001135<URL:ftp://ftp.python.org/pub/python/src/extensions.tar.gz>.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001136
1137- There's an interface to SUIT, the U of Virginia's Simple User
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001138Interface Toolkit; it can be ftp'ed from
1139<URL:ftp://uvacs.cs.virginia.edu/pub/suit/python/SUIT_python.tar.Z>.
1140A PC binary of Python 1.0.2 compiled with DJGPP and with SUIT support
1141built-in has been made available by Antonio Costa
1142<URL:ftp://asterix.inescn.pt/pub/PC/python/pyt102su.exe> (a
1143self-extracting archive). Note that the UVa people themselves have
1144expressed doubts about SUIT, and are planning to build a Python GUI
1145API based upon Tk (though not necessarily on Tkinter); see
1146<URL:http://server.cs.virginia.edu/~tnb2d/IT/IT.html>.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001147
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001148- There's an interface to WAFE, a Tcl interface to the X11 Motif and
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001149Athena widget sets. Last I heard about it it was included in the WAFE
11501.0 prerelease
1151<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 +00001152
Guido van Rossumbf8e7d51995-08-28 03:09:13 +00001153- The NT port by Mark Hammond (see question 7.4) includes an interface
1154to the Microsoft Foundation Classes and a Python programming
1155environment using it that's written mostly in Python. See
1156<URL:ftp://ftp.python.org/pub/python/nt/>.
1157
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00001158- There's an interface to wxWindows. wxWindows is a portable GUI
1159class library written in C++. It supports XView, Motif, MS-Windows as
1160targets. There is some support for Macs and CURSES as well. wxWindows
1161preserves the look and feel of the underlying graphics toolkit. See
1162the wxPython WWW page at
1163<URL:http://www.aiai.ed.ac.uk/~jacs/wx/wxpython/wxpython.html>.
1164
Guido van Rossum3fc9d731995-07-25 15:10:56 +00001165- There's an object-oriented GUI based on the Microsoft Foundation
1166Classes model called WPY. Programs written in WPY run unchanged and
1167with native look and feel on NT, Windows 3.1 (using win32s) and on
1168Unix (using Tk). Source and binaries for NT and Linux are available
1169in <URL:ftp://ftp.python.org/pub/python/wpy>.
1170
Guido van Rossum0d20cfa1996-07-30 18:53:05 +00001171- (The Fresco port that was mentioned in earlier versions of this FAQ
1172no longer seems to exist. Inquire with Mark Linton.)
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00001173
Guido van Rossum061f1821994-10-06 16:03:45 +000011744.14. Q. Are there any interfaces to database packages in Python?
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001175
Guido van Rossum8727df41996-09-09 15:16:39 +00001176A. There's a whole collection of them in the contrib area of the ftp
1177server, see <URL:http://www.python.org/ftp/python/contrib/Database/>.
Guido van Rossum796b2591995-01-20 23:05:52 +00001178
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000011794.15. Q. Is it possible to write obfuscated one-liners in Python?
Guido van Rossumc24da7c1994-09-23 14:08:41 +00001180
1181A. Yes. See the following three examples, due to Ulf Bartelt:
1182
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001183 # Primes < 1000
1184 print filter(None,map(lambda y:y*reduce(lambda x,y:x*y!=0,
1185 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 +00001186
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001187 # First 10 Fibonacci numbers
1188 print map(lambda x,f=lambda x,f:(x<=1) or (f(x-1,f)+f(x-2,f)): f(x,f),
1189 range(10))
Guido van Rossumc24da7c1994-09-23 14:08:41 +00001190
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001191 # Mandelbrot set
1192 print (lambda Ru,Ro,Iu,Io,IM,Sx,Sy:reduce(lambda x,y:x+y,map(lambda y,
1193 Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,Sy=Sy,L=lambda yc,Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,i=IM,
1194 Sx=Sx,Sy=Sy:reduce(lambda x,y:x+y,map(lambda x,xc=Ru,yc=yc,Ru=Ru,Ro=Ro,
1195 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
1196 >=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(
1197 64+F(Ru+x*(Ro-Ru)/Sx,yc,0,0,i)),range(Sx))):L(Iu+y*(Io-Iu)/Sy),range(Sy
1198 ))))(-2.1, 0.7, -1.2, 1.2, 30, 80, 24)
1199 # \___ ___/ \___ ___/ | | |__ lines on screen
1200 # V V | |______ columns on screen
1201 # | | |__________ maximum of "iterations"
1202 # | |_________________ range on y axis
1203 # |____________________________ range on x axis
Guido van Rossumc24da7c1994-09-23 14:08:41 +00001204
1205Don't try this at home, kids!
1206
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000012074.16. Q. Is there an equivalent of C's "?:" ternary operator?
Guido van Rossumc24da7c1994-09-23 14:08:41 +00001208
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001209A. Not directly. In many cases you can mimic a?b:c with "a and b or
1210c", but there's a flaw: if b is zero (or empty, or None -- anything
1211that tests false) then c will be selected instead. In many cases you
1212can prove by looking at the code that this can't happen (e.g. because
1213b is a constant or has a type that can never be false), but in general
1214this can be a problem.
1215
1216Steve Majewski (or was it Tim Peters?) suggested the following
1217solution: (a and [b] or [c])[0]. Because [b] is a singleton list it
1218is never false, so the wrong path is never taken; then applying [0] to
1219the whole thing gets the b or c that you really wanted. Ugly, but it
1220gets you there in the rare cases where it is really inconvenient to
1221rewrite your code using 'if'.
1222
12234.17. Q. My class defines __del__ but it is not called when I delete the
1224object.
1225
1226A. There are several possible reasons for this.
1227
1228- The del statement does not necessarily call __del__ -- it simply
1229decrements the object's reference count, and if this reaches zero
1230__del__ is called.
1231
1232- If your data structures contain circular links (e.g. a tree where
1233each child has a parent pointer and each parent has a list of
1234children) the reference counts will never go back to zero. You'll
Guido van Rossum061f1821994-10-06 16:03:45 +00001235have to define an explicit close() method which removes those
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001236pointers. Please don't ever call __del__ directly -- __del__ should
Guido van Rossum061f1821994-10-06 16:03:45 +00001237call close() and close() should make sure that it can be called more
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001238than once for the same object.
1239
1240- If the object has ever been a local variable (or argument, which is
1241really the same thing) to a function that caught an expression in an
1242except clause, chances are that a reference to the object still exists
1243in that function's stack frame as contained in the stack trace.
1244Normally, deleting (better: assigning None to) sys.exc_traceback will
1245take care of this. If you a stack was printed for an unhandled
1246exception in an interactive interpreter, delete sys.last_traceback
1247instead.
1248
1249- There is code that deletes all objects when the interpreter exits,
1250but if your Python has been configured to support threads, it is not
1251called (because other threads may still be active). You can define
Guido van Rossum061f1821994-10-06 16:03:45 +00001252your own cleanup function using sys.exitfunc (see question 4.4).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001253
Guido van Rossum8727df41996-09-09 15:16:39 +00001254- Finally, if your __del__ method raises an exception, this will be
1255ignored. Starting with Python 1.4beta3, a warning message is printed
1256to sys.stderr when this happens.
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001257
12584.18. Q. How do I change the shell environment for programs called
1259using os.popen() or os.system()? Changing os.environ doesn't work.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001260
1261A. Modifying the environment passed to subshells was left out of the
1262interpreter because there seemed to be no well-established portable
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001263way to do it (in particular, some systems, have putenv(), others have
1264setenv(), and some have none at all).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001265
1266However if all you want is to pass environment variables to the
1267commands run by os.system() or os.popen(), there's a simple solution:
1268prefix the command string with a couple of variable assignments and
Guido van Rossum796b2591995-01-20 23:05:52 +00001269export statements. The following would be universal for popen:
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001270
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001271 import os
1272 from commands import mkarg # nifty routine to add shell quoting
1273 def epopen(cmd, mode, env = {}):
1274 # env is a dictionary of environment variables
1275 prefix = ''
Guido van Rossum796b2591995-01-20 23:05:52 +00001276 for key, value in env.items():
1277 prefix = prefix + '%s=%s\n' % (key, mkarg(value)[1:])
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001278 prefix = prefix + 'export %s\n' % key
1279 return os.popen(prefix + cmd, mode)
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001280
12814.19. Q. What is a class?
1282
1283A. A class is the particular object type that is created by executing
Guido van Rossum796b2591995-01-20 23:05:52 +00001284a class statement. Class objects are used as templates, to create
1285class instance objects, which embody both the data structure and
1286program routines specific to a datatype.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001287
12884.20. Q. What is a method?
1289
1290A. A method is a function that you normally call as
Guido van Rossum796b2591995-01-20 23:05:52 +00001291x.name(arguments...) for some object x. The term is used for methods
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001292of classes and class instances as well as for methods of built-in
Guido van Rossum796b2591995-01-20 23:05:52 +00001293objects. (The latter have a completely different implementation and
1294only share the way their calls look in Python code.) Methods of
1295classes (and class instances) are defined as functions inside the
1296class definition.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001297
12984.21. Q. What is self?
1299
1300A. Self is merely a conventional name for the first argument of a
1301method -- i.e. a function defined inside a class definition. A method
1302defined as meth(self, a, b, c) should be called as x.meth(a, b, c) for
1303some instance x of the class in which the definition occurs;
1304the called method will think it is called as meth(x, a, b, c).
1305
13064.22. Q. What is a unbound method?
1307
1308A. An unbound method is a method defined in a class that is not yet
1309bound to an instance. You get an unbound method if you ask for a
1310class attribute that happens to be a function. You get a bound method
1311if you ask for an instance attribute. A bound method knows which
Guido van Rossum061f1821994-10-06 16:03:45 +00001312instance it belongs to and calling it supplies the instance automatically;
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001313an unbound method only knows which class it wants for its first
1314argument (a derived class is also OK). Calling an unbound method
1315doesn't "magically" derive the first argument from the context -- you
1316have to provide it explicitly.
1317
Guido van Rossuma6c707c1995-01-02 17:32:28 +000013184.23. Q. How do I call a method defined in a base class from a derived
1319class that overrides it?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001320
1321A. If your class definition starts with "class Derived(Base): ..."
1322then you can call method meth defined in Base (or one of Base's base
1323classes) as Base.meth(self, arguments...). Here, Base.meth is an
1324unbound method (see previous question).
1325
Guido van Rossuma6c707c1995-01-02 17:32:28 +000013264.24. Q. How do I call a method from a base class without using the
1327name of the base class?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001328
1329A. DON'T DO THIS. REALLY. I MEAN IT. It appears that you could call
1330self.__class__.__bases__[0].meth(self, arguments...) but this fails when
Guido van Rossum061f1821994-10-06 16:03:45 +00001331a doubly-derived method is derived from your class: for its instances,
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001332self.__class__.__bases__[0] is your class, not its base class -- so
1333(assuming you are doing this from within Derived.meth) you would start
1334a recursive call.
1335
13364.25. Q. How can I organize my code to make it easier to change the base
1337class?
1338
1339A. You could define an alias for the base class, assign the real base
1340class to it before your class definition, and use the alias throughout
1341your class. Then all you have to change is the value assigned to the
1342alias. Incidentally, this trick is also handy if you want to decide
1343dynamically (e.g. depending on availability of resources) which base
1344class to use. Example:
1345
1346BaseAlias = <real base class>
1347class Derived(BaseAlias):
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001348 def meth(self):
1349 BaseAlias.meth(self)
1350 ...
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001351
13524.26. Q. How can I find the methods or attributes of an object?
1353
1354A. This depends on the object type.
1355
1356For an instance x of a user-defined class, instance attributes are
1357found in the dictionary x.__dict__, and methods and attributes defined
1358by its class are found in x.__class__.__bases__[i].__dict__ (for i in
1359range(len(x.__class__.__bases__))). You'll have to walk the tree of
1360base classes to find *all* class methods and attributes.
1361
1362Many, but not all built-in types define a list of their method names
1363in x.__methods__, and if they have data attributes, their names may be
1364found in x.__members__. However this is only a convention.
1365
1366For more information, read the source of the standard (but
1367undocumented) module newdir.
1368
13694.27. Q. I can't seem to use os.read() on a pipe created with os.popen().
1370
1371A. os.read() is a low-level function which takes a file descriptor (a
1372small integer). os.popen() creates a high-level file object -- the
1373same type used for sys.std{in,out,err} and returned by the builtin
1374open() function. Thus, to read n bytes from a pipe p created with
1375os.popen(), you need to use p.read(n).
Guido van Rossuma7925f11994-01-26 10:20:16 +00001376
Guido van Rossum061f1821994-10-06 16:03:45 +000013774.28. Q. How can I create a stand-alone binary from a Python script?
1378
1379The demo script "Demo/scripts/freeze.py" does what you want. (It's
1380actually not a demo but a support tool -- there is some extra code in
1381the interpreter to accommodate it.) It requires that you have the
1382Python build tree handy, complete with all the lib*.a files.
1383
1384This works by scanning your source recursively for import statements
1385(both forms) and looking for the modules on the standard Python path
1386as well as in the source directory (for built-in modules). It then
1387"compiles" the modules written in Python to C code (array initializers
1388that can be turned into code objects using the marshal module) and
1389creates a custom-made config file that only contains those built-in
1390modules which are actually used in the program. It then compiles the
1391generated C code and links it with the rest of the Python interpreter
1392to form a self-contained binary which acts exactly like your script.
1393
Guido van Rossum7be34a81995-05-31 15:17:12 +00001394Hint: the freeze program only works if your script's filename ends in
1395".py".
Guido van Rossum061f1821994-10-06 16:03:45 +00001396
Guido van Rossum7be34a81995-05-31 15:17:12 +000013974.29. Q. What WWW tools are there for Python?
Guido van Rossum061f1821994-10-06 16:03:45 +00001398
Guido van Rossum8727df41996-09-09 15:16:39 +00001399A. See the chapter titled "Internet and WWW" in the Library Reference
1400Manual. There's also a web browser written in Python, called Grail --
1401see <URL:http://monty.cnri.reston.va.us/grail/>.
Guido van Rossum061f1821994-10-06 16:03:45 +00001402
1403Steve Miale <smiale@cs.indiana.edu> has written a modular WWW browser
1404called Dancer. An alpha version can be FTP'ed from
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001405<URL:ftp://ftp.cs.indiana.edu/pub/smiale/dancer.tar.gz>. (There are a
1406few articles about Dancer in the (hyper)mail archive
1407<URL:http://www.cwi.nl/~guido/hypermail/python-1994q3/index.html>.)
Guido van Rossum061f1821994-10-06 16:03:45 +00001408
Guido van Rossum7be34a81995-05-31 15:17:12 +000014094.30. Q. How do I run a subprocess with pipes connected to both input
Guido van Rossum796b2591995-01-20 23:05:52 +00001410and output?
1411
1412A. This is really a UNIX question. Also, in general, it is unwise to
1413do so, because you can easily cause a deadlock where the parent
1414process is blocked waiting for output from the child, while the child
1415is blocked waiting for input from the child. This can be caused
1416because the parent expects the child to output more text than it does,
1417or it can be caused by data being stuck in stdio buffers due to lack
1418of flushing. The Python parent can of course explicitly flush the data
1419it sends to the child before it reads any output, but if the child is
1420a naive C program it can easily have been written to never explicitly
1421flush its output, even if it is interactive, since flushing is
1422normally automatic.
1423
1424In many cases, all you really need is to run some data through a
1425command and get the result back. Unless the data is infinite in size,
1426the easiest (and often the most efficient!) way to do this is to write
1427it to a temporary file and run the command with that temporary file as
1428input. The standard module tempfile exports a function mktemp() which
1429generates unique temporary file names.
1430
1431If after reading all of the above you still want to connect two pipes
1432to a subprocess's standard input and output, here's a simple solution,
1433due to Jack Jansen:
1434
Guido van Rossume7d92d51995-10-11 18:06:54 +00001435 import os
1436 import sys
1437 import string
Guido van Rossum796b2591995-01-20 23:05:52 +00001438
Guido van Rossume7d92d51995-10-11 18:06:54 +00001439 MAXFD = 100 # Max number of file descriptors in this system
Guido van Rossum796b2591995-01-20 23:05:52 +00001440
Guido van Rossume7d92d51995-10-11 18:06:54 +00001441 def popen2(cmd):
1442 cmd = string.split(cmd)
1443 p2cread, p2cwrite = os.pipe()
1444 c2pread, c2pwrite = os.pipe()
1445 pid = os.fork()
1446 if pid == 0:
1447 # Child
1448 os.close(0)
1449 os.close(1)
Guido van Rossumca318ec1996-06-26 19:50:09 +00001450 if os.dup(p2cread) != 0:
Guido van Rossume7d92d51995-10-11 18:06:54 +00001451 sys.stderr.write('popen2: bad read dup\n')
Guido van Rossumca318ec1996-06-26 19:50:09 +00001452 if os.dup(c2pwrite) != 1:
Guido van Rossume7d92d51995-10-11 18:06:54 +00001453 sys.stderr.write('popen2: bad write dup\n')
1454 for i in range(3, MAXFD):
1455 try:
1456 os.close(i)
1457 except:
1458 pass
1459 try:
1460 os.execv(cmd[0], cmd)
1461 finally:
1462 os._exit(1)
1463 os.close(p2cread)
1464 tochild = os.fdopen(p2cwrite, 'w')
1465 os.close(c2pwrite)
1466 fromchild = os.fdopen(c2pread, 'r')
1467 return fromchild, tochild
Guido van Rossum796b2591995-01-20 23:05:52 +00001468
1469Note that many interactive programs (e.g. vi) don't work well with
1470pipes substituted for standard input and output. You will have to use
1471pseudo ttys ("ptys") instead of pipes. There is some undocumented
1472code to use these in the library module pty.py -- I'm afraid you're on
Guido van Rossum3fc9d731995-07-25 15:10:56 +00001473your own here.
1474
1475A different answer is a Python interface to Don Libes' "expect"
1476library. A prerelease of this is available on the Python ftp mirror
1477sites in the contrib subdirectory as expy-0.3.tar.gz, e.g.
1478<URL:ftp://ftp.python.org/pub/python/contrib/expy-0.3.tar.gz>.
Guido van Rossum796b2591995-01-20 23:05:52 +00001479
Guido van Rossum7be34a81995-05-31 15:17:12 +000014804.31. Q. How do I call a function if I have the arguments in a tuple?
Guido van Rossumac3f2121995-04-10 11:53:42 +00001481
1482A. Use the built-in function apply(). For instance,
1483
1484 func(1, 2, 3)
1485
1486is equivalent to
1487
1488 args = (1, 2, 3)
1489 apply(func, args)
1490
1491Note that func(args) is not the same -- it calls func() with exactly
1492one argument, the tuple args, instead of three arguments, the integers
14931, 2 and 3.
1494
Guido van Rossum7be34a81995-05-31 15:17:12 +000014954.32. Q. How do I enable font-lock-mode for Python in Emacs?
1496
1497A. Assuming you're already using python-mode and font-lock-mode
1498separately, all you need to do is put this in your .emacs file:
1499
Guido van Rossume7d92d51995-10-11 18:06:54 +00001500 (defun my-python-mode-hook ()
1501 (setq font-lock-keywords python-font-lock-keywords)
1502 (font-lock-mode 1))
1503 (add-hook 'python-mode-hook 'my-python-mode-hook)
Guido van Rossum7be34a81995-05-31 15:17:12 +00001504
15054.33. Q. Is there an inverse to the format operator (a la C's scanf())?
1506
1507A. Not as such.
1508
1509For simple input parsing, the easiest approach is usually to split
1510the line into whitespace-delimited words using string.split(), and to
1511convert decimal strings to numeric values using string.atoi(),
1512string.atol() or string.atof(). (Python's atoi() is 32-bit and its
1513atol() is arbitrary precision.) If you want to use another delimiter
1514than whitespace, use string.splitfield() (possibly combining it with
1515string.strip() which removes surrounding whitespace from a string).
1516
1517For more complicated input parsing, regular expressions (see module
1518regex) are better suited and more powerful than C's scanf().
1519
Guido van Rossumca318ec1996-06-26 19:50:09 +000015204.34. Q. Can I have Tk events handled while waiting for I/O?
1521
1522A. Yes, and you don't even need threads! But you'll have to
1523restructure your I/O code a bit. Tk has the equivalent of Xt's
1524XtAddInput() call, which allows you to register a callback function
1525which will be called from the Tk mainloop when I/O is possible on a
1526file descriptor. Here's what you need:
1527
1528 from Tkinter import tkinter
1529 tkinter.createfilehandler(file, mask, callback)
1530
1531The file may be a Python file or socket object (actually, anything
1532with a fileno() method), or an integer file descriptor. The mask is
1533one of the constants tkinter.READABLE or tkinter.WRITABLE. The
1534callback is called as follows:
1535
1536 callback(file, mask)
1537
1538You must unregister the callback when you're done, using
1539
1540 tkinter.deletefilehandler(file)
1541
1542Note: since you don't know *how many bytes* are available for reading,
1543you can't use the Python file object's read or readline methods, since
1544these will insist on reading a predefined number of bytes. For
1545sockets, the recv() or recvfrom() methods will work fine; for other
1546files, use os.read(file.fileno(), maxbytecount).
1547
15484.35. Q. How do I write a function with output parameters (call by reference)?
1549
1550A. [Mark Lutz] The thing to remember is that arguments are passed by
1551assignment in Python. Since assignment just creates references to
1552objects, there's no alias between an argument name in the caller and
1553callee, and so no call-by-reference per se. But you can simulate it
1554in a number of ways:
1555
15561) By using global variables; but you probably shouldn't :-)
1557
15582) By passing a mutable (changeable in-place) object:
1559
1560 def func1(a):
1561 a[0] = 'new-value' # 'a' references a mutable list
1562 a[1] = a[1] + 1 # changes a shared object
1563
1564 args = ['old-value', 99]
1565 func1(args)
1566 print args[0], args[1] # output: new-value 100
1567
15683) By return a tuple, holding the final values of arguments:
1569
1570 def func2(a, b):
1571 a = 'new-value' # a and b are local names
1572 b = b + 1 # assigned to new objects
1573 return a, b # return new values
1574
1575 x, y = 'old-value', 99
1576 x, y = func2(x, y)
1577 print x, y # output: new-value 100
1578
15794) And other ideas that fall-out from Python's object model. For
1580 instance, it might be clearer to pass in a mutable dictionary:
1581
1582 def func3(args):
1583 args['a'] = 'new-value' # args is a mutable dictionary
1584 args['b'] = args['b'] + 1 # change it in-place
1585
1586 args = {'a':' old-value', 'b': 99}
1587 func3(args)
1588 print args['a'], args['b']
1589
15905) Or bundle-up values in a class instance:
1591
1592 class callByRef:
1593 def __init__(self, **args):
1594 for (key, value) in args.items():
1595 setattr(self, key, value)
1596
1597 def func4(args):
1598 args.a = 'new-value' # args is a mutable callByRef
1599 args.b = args.b + 1 # change object in-place
1600
1601 args = callByRef(a='old-value', b=99)
1602 func4(args)
1603 print args.a, args.b
1604
1605 But there's probably no good reason to get this complicated :-).
1606
1607[Python' author favors solution 3 in most cases.]
1608
Guido van Rossum0d20cfa1996-07-30 18:53:05 +000016094.36. Q. Please explain the rules for local and global variables in Python.
1610
1611A. [Ken Manheimer] In Python, procedure variables are implicitly
1612global, unless they assigned anywhere within the block. In that case
1613they are implicitly local, and you need to explicitly declare them as
1614'global'.
1615
1616Though a bit surprising at first, a moments consideration explains
1617this. On one hand, requirement of 'global' for assigned vars provides
1618a bar against unintended side-effects. On the other hand, if global
1619were required for all global references, you'd be using global all the
1620time. Eg, you'd have to declare as global every reference to a
1621builtin function, or to a component of an imported module. This
1622clutter would defeat the usefulness of the 'global' declaration for
1623identifying side-effects.
1624
16254.37. Q. How can I have modules that mutually import each other?
1626
1627A. Jim Roskind recommends the following order in each module:
1628
1629First: all exports (like globals, functions, and classes that don't
1630need imported bases classes).
1631
1632Then: all import statements.
1633
1634Finally: all active code (including globals that are initialized from
1635imported values).
1636
1637Python's author doesn't like this approach much because the imports
1638appear in a strange place, but has to admit that it works. His
1639recommended strategy is to avoid all uses of "from <module> import *"
1640(so everything from an imported module is referenced as
1641<module>.<name>) and to place all code inside functions.
1642Initializations of global variables and class variables should use
1643constants or built-in functions only.
1644
Guido van Rossum8727df41996-09-09 15:16:39 +000016454.38. Q. How do I copy an object in Python?
1646
1647A. There is no generic copying operation built into Python, however
1648most object types have some way to create a clone. Here's how for the
1649most common objects:
1650
1651- For immutable objects (numbers, strings, tuples), cloning is
1652unnecessary since their value can't change.
1653
1654- For lists (and generally for mutable sequence types), a clone is
1655created by the expression l[:].
1656
1657- For dictionaries, the following function returns a clone:
1658
1659 def dictclone(o):
1660 n = {}
1661 for k in o.keys(): n[k] = o[k]
1662 return n
1663
1664- Finally, for generic objects, the "copy" module defines two
1665functions for copying objects. copy.copy(x) returns a copy as shown
1666by the above rules. copy.deepcopy(x) also copies the elements of
1667composite objects. See the section on this module in the Library
1668Reference Manual.
1669
16704.39. Q. How to implement persistent objects in Python? (Persistent ==
1671automatically saved to and restored from disk.)
1672
1673A. The library module "pickle" now solves this in a very general way
1674(though you still can't store things like open files, sockests or
1675windows), and the library module "shelve" uses pickle and (g)dbm to
1676create presistent mappings containing arbitrary Python objects.
1677
Guido van Rossum061f1821994-10-06 16:03:45 +00001678
Guido van Rossuma7925f11994-01-26 10:20:16 +000016795. Extending Python
1680===================
1681
16825.1. Q. Can I create my own functions in C?
1683
1684A. Yes, you can create built-in modules containing functions,
Guido van Rossum24349991994-02-02 14:12:45 +00001685variables, exceptions and even new types in C. This is explained in
1686the document "Extending and Embedding the Python Interpreter" (the
1687LaTeX file Doc/ext.tex). Also read the chapter on dynamic loading.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001688
16895.2. Q. Can I create my own functions in C++?
1690
1691A. Yes, using the C-compatibility features found in C++. Basically
1692you place extern "C" { ... } around the Python include files and put
1693extern "C" before each function that is going to be called by the
1694Python interpreter. Global or static C++ objects with constructors
1695are probably not a good idea.
1696
Guido van Rossum7ce61c11994-06-13 15:13:56 +000016975.3. Q. How can I execute arbitrary Python statements from C?
1698
1699A. The highest-level function to do this is run_command() which takes
1700a single string argument which is executed in the context of module
1701__main__ and returns 0 for success and -1 when an exception occurred
1702(including SyntaxError). If you want more control, use run_string();
1703see the source for run_command() in Python/pythonrun.c.
1704
17055.4. Q. How can I evaluate an arbitrary Python expression from C?
1706
1707A. Call the function run_string() from the previous question with the
1708start symbol eval_input; it then parses an expression, evaluates it
1709and returns its value. See exec_eval() in Python/bltinmodule.c.
1710
17115.5. Q. How do I extract C values from a Python object?
1712
1713A. That depends on the object's type. If it's a tuple,
1714gettuplesize(o) returns its length and gettupleitem(o, i) returns its
1715i'th item; similar for lists with getlistsize(o) and getlistitem(o,
1716i). For strings, getstringsize(o) returns its length and
1717getstringvalue(o) a pointer to its value (note that Python strings may
1718contain null bytes so strlen() is not safe). To test which type an
1719object is, first make sure it isn't NULL, and then use
1720is_stringobject(o), is_tupleobject(o), is_listobject(o) etc.
1721
17225.6. Q. How do I use mkvalue() to create a tuple of arbitrary length?
1723
1724A. You can't. Use t = newtupleobject(n) instead, and fill it with
1725objects using settupleitem(t, i, o) -- note that this "eats" a
1726reference count of o. Similar for lists with newlistobject(n) and
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001727setlistitem(l, i, o). Note that you *must* set all the tuple items to
1728some value before you pass the tuple to Python code --
1729newtupleobject(n) initializes them to NULL, which isn't a valid Python
1730value.
1731
Guido van Rossum8727df41996-09-09 15:16:39 +000017325.7. Q. How do I call an object's method from C?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001733
1734A. Here's a function (untested) that might become part of the next
1735release in some form. It uses <stdarg.h> to allow passing the
1736argument list on to vmkvalue():
1737
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001738 object *call_method(object *inst, char *methodname, char *format, ...)
1739 {
1740 object *method;
1741 object *args;
1742 object *result;
1743 va_list va;
1744 method = getattr(inst, methodname);
1745 if (method == NULL) return NULL;
1746 va_start(va, format);
1747 args = vmkvalue(format, va);
1748 va_end(va);
1749 if (args == NULL) {
1750 DECREF(method);
1751 return NULL;
1752 }
1753 result = call_object(method, args);
1754 DECREF(method);
1755 DECREF(args);
1756 return result;
1757 }
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001758
1759This works for any instance that has methods -- whether built-in or
1760user-defined. You are responsible for eventually DECREF'ing the
1761return value.
1762
1763To call, e.g., a file object's "seek" method with arguments 10, 0
1764(assuming the file object pointer is "f"):
1765
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001766 res = call_method(f, "seek", "(OO)", 10, 0);
1767 if (res == NULL) {
1768 ... an exception occurred ...
1769 }
1770 else {
1771 DECREF(res);
1772 }
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001773
1774Note that since call_object() *always* wants a tuple for the argument
1775list, to call a function without arguments, pass "()" for the format,
1776and to call a function with one argument, surround the argument in
1777parentheses, e.g. "(i)".
1778
Guido van Rossum8727df41996-09-09 15:16:39 +000017795.8. Q. How do I catch the output from print_error()?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001780
1781A. (Due to Mark Hammond):
1782
1783* in Python code, define an object that supports the "write()" method.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001784
1785* redirect sys.stdout and sys.stderr to this object.
1786
Guido van Rossum061f1821994-10-06 16:03:45 +00001787* call print_error, or just allow the standard traceback mechanism to
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001788work.
1789
Guido van Rossum061f1821994-10-06 16:03:45 +00001790Then, the output will go wherever your write() method sends it.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001791
Guido van Rossum8727df41996-09-09 15:16:39 +000017925.9. Q. How do I access a module written in Python from C?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001793
1794A. You can get a pointer to the module object as follows:
1795
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001796 module = import_module("<modulename>");
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001797
1798If the module hasn't been imported yet (i.e. it is not yet present in
1799sys.modules), this initializes the module; otherwise it simply returns
1800the value of sys.modules["<modulename>"]. Note that it doesn't enter
1801the module into any namespace -- it only ensures it has been
1802initialized and is stored in sys.modules.
1803
1804You can then access the module's attributes (i.e. any name defined in
1805the module) as follows:
1806
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001807 attr = getattr(module, "<attrname>");
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001808
1809Calling setattr(), to assign to variables in the module, also works.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001810
Guido van Rossum8727df41996-09-09 15:16:39 +000018115.10. Q. How do I interface to C++ objects from Python?
Guido van Rossumb0a2ce51995-08-28 19:41:35 +00001812
1813A. Depending on your requirements, there are many approaches. Begin
1814by reading the "Extending and Embedding" document (Doc/ext.tex, see
1815also <URL:http://www.python.org/doc/>). Realize that for the Python
1816run-time system, there isn't a whole lot of difference between C and
1817C++ -- so the strategy to build a new Python type around a C structure
1818(pointer) type will also work for C++ objects.
1819
1820Automatic generation of interfaces between Python and C++ is still at
1821the horizon -- parsing C++ header files requires an almost complete
1822C++ parser, and many features aren't easily translated from C++ to
1823Python: certain forms of operator oveloading, function overloading
1824(best approached by a varargs function which explicitly type-checks
1825its arguments), and reference arguments are just a number of features
1826that are hard to translate correctly if at all.
1827
1828The hardest problem is to transparently translate the C++ class
1829hierarchy to Python, so that Python programs derive classes from C++
1830classes. Given suitable constraints, this may be possible, but it
1831would require more space than I have in this FAQ to explain how.
1832In any case, you can get quite a bit done without this, using just the
1833existing classes from Python.
1834
1835If this all seems rather daunting, that may be because it is -- C++
1836isn't exactly a baby to handle without gloves! However, people have
1837accomplished amazing feats of interfacing between Python and C++, and
1838a detailed question posted to the Python list is likely to elicit some
1839interesting and useful responses.
1840
Guido van Rossuma7925f11994-01-26 10:20:16 +00001841
18426. Python's design
1843==================
1844
Guido van Rossum8727df41996-09-09 15:16:39 +000018456.1. Q. Why isn't there a switch or case statement in Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +00001846
1847A. You can do this easily enough with a sequence of
1848if... elif... elif... else. There have been some proposals for switch
Guido van Rossum061f1821994-10-06 16:03:45 +00001849statement syntax, but there is no consensus (yet) on whether and how
Guido van Rossuma7925f11994-01-26 10:20:16 +00001850to do range tests.
1851
Guido van Rossum8727df41996-09-09 15:16:39 +000018526.2. Q. Why does Python use indentation for grouping of statements?
Guido van Rossumc50158e1994-05-31 09:18:50 +00001853
1854A. Basically I believe that using indentation for grouping is
1855extremely elegant and contributes a lot to the clarity of the average
1856Python program. Most people learn to love this feature after a while.
1857Some arguments for it:
1858
1859- Since there are no begin/end brackets there cannot be a disagreement
1860between grouping perceived by the parser and the human reader. I
1861remember long ago seeing a C fragment like this:
1862
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001863 if (x <= y)
1864 x++;
1865 y--;
1866 z++;
Guido van Rossumc50158e1994-05-31 09:18:50 +00001867
1868and staring a long time at it wondering why y was being decremented
1869even for x > y... (And I wasn't a C newbie then either.)
1870
Guido van Rossum7be34a81995-05-31 15:17:12 +00001871- Since there are no begin/end brackets, Python is much less prone to
1872coding-style conflicts. In C there are loads of different ways to
1873place the braces (including the choice whether to place braces around
1874single statements in certain cases, for consistency). If you're used
1875to reading (and writing) code that uses one style, you will feel at
1876least slightly uneasy when reading (or being required to write)
1877another style.
Guido van Rossumc50158e1994-05-31 09:18:50 +00001878
1879- Many coding styles place begin/end brackets on a line by themself.
1880This makes programs considerably longer and wastes valuable screen
1881space, making it harder to get a good overview over a program.
1882Ideally, a function should fit on one basic tty screen (say, 20
1883lines). 20 lines of Python are worth a LOT more than 20 lines of C.
1884This is not solely due to the lack of begin/end brackets (the lack of
1885declarations also helps, and the powerful operations of course), but
1886it certainly helps!
1887
Guido van Rossum8727df41996-09-09 15:16:39 +000018886.3. Q. Why are Python strings immutable?
Guido van Rossum3de27361994-07-25 14:19:33 +00001889
1890A. There are two advantages. One is performance: knowing that a
1891string is immutable makes it easy to lay it out at construction time
1892-- fixed and unchanging storage requirements. (This is also one of
Guido van Rossum061f1821994-10-06 16:03:45 +00001893the reasons for the distinction between tuples and lists.) The
Guido van Rossum3de27361994-07-25 14:19:33 +00001894other is that strings in Python are considered as "elemental" as
1895numbers. No amount of activity will change the value 8 to anything
1896else, and in Python, no amount of activity will change the string
1897"eight" to anything else. (Adapted from Jim Roskind)
1898
Guido van Rossum8727df41996-09-09 15:16:39 +000018996.4. Q. Why don't strings have methods like index() or sort(), like
Guido van Rossum3de27361994-07-25 14:19:33 +00001900lists?
1901
1902A. Good question. Strings currently don't have methods at all
1903(likewise tuples and numbers). Long ago, it seemed unnecessary to
1904implement any of these functions in C, so a standard library module
1905"string" written in Python was created that performs string related
1906operations. Since then, the cry for performance has moved most of
1907them into the built-in module strop (this is imported by module
Guido van Rossumf8c76d01994-08-17 12:19:53 +00001908string, which is still the preferred interface, without loss of
Guido van Rossum3de27361994-07-25 14:19:33 +00001909performance except during initialization). Some of these functions
1910(e.g. index()) could easily be implemented as string methods instead,
1911but others (e.g. sort()) can't, since their interface prescribes that
1912they modify the object, while strings are immutable (see the previous
1913question).
1914
Guido van Rossum8727df41996-09-09 15:16:39 +000019156.5. Q. Why does Python use methods for some functionality
Guido van Rossum3de27361994-07-25 14:19:33 +00001916(e.g. list.index()) but functions for other (e.g. len(list))?
1917
1918A. Functions are used for those operations that are generic for a
1919group of types and which should work even for objects that don't have
1920methods at all (e.g. numbers, strings, tuples). Also, implementing
1921len(), max(), min() as a built-in function is actually less code than
1922implementing them as methods for each type. One can quibble about
1923individual cases but it's really too late to change such things
1924fundamentally now.
1925
Guido van Rossum8727df41996-09-09 15:16:39 +000019266.6. Q. Why can't I derive a class from built-in types (e.g. lists or
Guido van Rossum3de27361994-07-25 14:19:33 +00001927files)?
1928
1929A. This is caused by the relatively late addition of (user-defined)
1930classes to the language -- the implementation framework doesn't easily
1931allow it. See the answer to question 4.2 for a work-around. This
1932*may* be fixed in the (distant) future.
1933
Guido van Rossum8727df41996-09-09 15:16:39 +000019346.7. Q. Why must 'self' be declared and used explicitly in method
Guido van Rossum3de27361994-07-25 14:19:33 +00001935definitions and calls?
1936
1937A. By asking this question you reveal your C++ background. :-)
1938When I added classes, this was (again) the simplest way of
1939implementing methods without too many changes to the interpreter. I
1940borrowed the idea from Modula-3. It turns out to be very useful, for
1941a variety of reasons.
1942
1943First, it makes it more obvious that you are using a method or
1944instance attribute instead of a local variable. Reading "self.x" or
1945"self.meth()" makes it absolutely clear that an instance variable or
1946method is used even if you don't know the class definition by heart.
1947In C++, you can sort of tell by the lack of a local variable
Guido van Rossum061f1821994-10-06 16:03:45 +00001948declaration (assuming globals are rare or easily recognizable) -- but
Guido van Rossum3de27361994-07-25 14:19:33 +00001949in Python, there are no local variable declarations, so you'd have to
1950look up the class definition to be sure.
1951
1952Second, it means that no special syntax is necessary if you want to
1953explicitly reference or call the method from a particular class. In
1954C++, if you want to use a method from base class that is overridden in
1955a derived class, you have to use the :: operator -- in Python you can
1956write baseclass.methodname(self, <argument list>). This is
1957particularly useful for __init__() methods, and in general in cases
1958where a derived class method wants to extend the base class method of
1959the same name and thus has to call the base class method somehow.
1960
1961Lastly, for instance variables, it solves a syntactic problem with
1962assignment: since local variables in Python are (by definition!) those
1963variables to which a value assigned in a function body (and that
1964aren't explicitly declared global), there has to be some way to tell
1965the interpreter that an assignment was meant to assign to an instance
1966variable instead of to a local variable, and it should preferably be
1967syntactic (for efficiency reasons). C++ does this through
1968declarations, but Python doesn't have declarations and it would be a
1969pity having to introduce them just for this purpose. Using the
1970explicit "self.var" solves this nicely. Similarly, for using instance
1971variables, having to write "self.var" means that references to
1972unqualified names inside a method don't have to search the instance's
1973directories.
1974
Guido van Rossum8727df41996-09-09 15:16:39 +000019756.8. Q. Can't you emulate threads in the interpreter instead of
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001976relying on an OS-specific thread implementation?
1977
1978A. Unfortunately, the interpreter pushes at least one C stack frame
1979for each Python stack frame. Also, extensions can call back into
1980Python at almost random moments. Therefore a complete threads
1981implementation requires thread support for C.
1982
Guido van Rossum8727df41996-09-09 15:16:39 +000019836.9. Q. Why can't lambda forms contain statements?
Guido van Rossum061f1821994-10-06 16:03:45 +00001984
1985A. Python lambda forms cannot contain statements because Python's
Guido van Rossum796b2591995-01-20 23:05:52 +00001986syntactic framework can't handle statements nested inside expressions.
Guido van Rossum061f1821994-10-06 16:03:45 +00001987
1988However, in Python, this is not a serious problem. Unlike lambda
1989forms in other languages, where they add functionality, Python lambdas
1990are only a shorthand notation if you're too lazy to define a function.
1991
1992Functions are already first class objects in Python, and can be
1993declared in a local scope. Therefore the only advantage of using a
1994lambda form instead of a locally-defined function is that you'll have
1995to invent a name for the function -- but that's just a local variable
1996to which the function object (which is exactly the same type of object
1997that a lambda form yields) is assigned!
1998
Guido van Rossum8727df41996-09-09 15:16:39 +000019996.10. Q. Why don't lambdas have access to variables defined in the
2000containing scope?
2001
2002A. Because they are implemented as ordinary functions. See question
20034.5 above.
2004
20056.11. Q. Why can't recursive functions be defined inside other functions?
2006
2007A. See question 4.5 above.
2008
Guido van Rossum061f1821994-10-06 16:03:45 +000020096.12. Q. Why is there no more efficient way of iterating over a dictionary
2010than first constructing the list of keys()?
2011
2012A. Have you tried it? I bet it's fast enough for your purposes! In
2013most cases such a list takes only a few percent of the space occupied
2014by the dictionary -- it needs only 4 bytes (the size of a pointer) per
2015key -- a dictionary costs 8 bytes per key plus between 30 and 70
2016percent hash table overhead, plus the space for the keys and values --
2017by necessity all keys are unique objects and a string object (the most
2018common key type) costs at least 18 bytes plus the length of the
2019string. Add to that the values contained in the dictionary, and you
2020see that 4 bytes more per item really isn't that much more memory...
2021
2022A call to dict.keys() makes one fast scan over the dictionary
2023(internally, the iteration function does exist) copying the pointers
2024to the key objects into a pre-allocated list object of the right size.
2025The iteration time isn't lost (since you'll have to iterate anyway --
2026unless in the majority of cases your loop terminates very prematurely
2027(which I doubt since you're getting the keys in random order).
2028
2029I don't expose the dictionary iteration operation to Python
2030programmers because the dictionary shouldn't be modified during the
2031entire iteration -- if it is, there's a very small chance that the
2032dictionary is reorganized because the hash table becomes too full, and
2033then the iteration may miss some items and see others twice. Exactly
2034because this only occurs rarely, it would lead to hidden bugs in
2035programs: it's easy never to have it happen during test runs if you
2036only insert or delete a few items per iteration -- but your users will
2037surely hit upon it sooner or later.
2038
Guido van Rossumb0a2ce51995-08-28 19:41:35 +000020396.13. Q. Can Python be compiled to machine code, C or some other language?
2040
2041A. Not easily. Python's high level data types, dynamic typing of
2042objects and run-time invocation of the interpreter (using eval() or
2043exec) together mean that a "compiled" Python program would probably
2044consist mostly of calls into the Python run-time system, even for
2045seemingly simple operations like "x+1". Thus, the performance gain
2046would probably be minimal.
2047
2048Internally, Python source code is always translated into a "virtual
2049machine code" or "byte code" representation before it is interpreted
2050(by the "Python virtual machine" or "bytecode interpreter"). In order
2051to avoid the overhead of parsing and translating modules that rarely
2052change over and over again, this byte code is written on a file whose
2053name ends in ".pyc" whenever a module is parsed (from a file whose
2054name ends in ".py"). When the corresponding .py file is changed, it
2055is parsed and translated again and the .pyc file is rewritten. There
2056is no performance difference once the .pyc file has been loaded (the
2057bytecode read from the .pyc file is exactly the same as the bytecode
2058created by direct translation). The only difference is that loading
2059code from a .pyc file is faster than parsing and translating a .py
2060file, so the presence of precompiled .pyc files will generally improve
2061start-up time of Python scripts. If desired, the Lib/compileall.py
2062module/script can be used to force creation of valid .pyc files for a
2063given set of modules.
2064
2065If you are looking for a way to translate Python programs in order to
2066distribute them in binary form, without the need to distribute the
2067interpreter and library as well, have a look at the freeze.py script
2068in the Tools/freeze directory. This creates a single binary file
2069incorporating your program, the Python interpreter, and those parts of
2070the Python library that are needed by your program. Of course, the
2071resulting binary will only run on the same type of platform as that
2072used to create it.
2073
2074Hints for proper usage of freeze.py:
2075
2076- the script must be in a file whose name ends in .py
2077
2078- you must have installed Python fully:
2079
Guido van Rossume7d92d51995-10-11 18:06:54 +00002080 make install
2081 make libinstall
2082 make inclinstall
2083 make libainstall
Guido van Rossumb0a2ce51995-08-28 19:41:35 +00002084
Guido van Rossum05151e01995-09-28 13:24:46 +000020856.14. Q. Why doesn't Python use proper garbage collection?
2086
2087A. It's looking less and less likely that Python will ever get
2088"automatic" garbage collection (GC). For one thing, unless this were
2089added to C as a standard feature, it's a portability pain in the ass.
2090And yes, I know about the Xerox library. It has bits of assembler
2091code for *most* *common* platforms. Not for all. And although it is
2092mostly transparent, it isn't completely transparent (when I once
2093linked Python with it, it dumped core).
2094
2095"Proper" GC also becomes a problem when Python gets embedded into
2096other applications. While in a stand-alone Python it may be fine to
2097replace the standard malloc() and free() with versions provided by the
2098GC library, an application embedding Python may want to have its *own*
2099substitute for malloc() and free(), and may not want Python's. Right
2100now, Python works with anything that implements malloc() and free()
2101properly.
2102
2103Besides, the predictability of destructor calls in Python is kind of
2104attractive. With GC, the following code (which is fine in current
2105Python) will run out of file descriptors long before it runs out of
2106memory:
2107
Guido van Rossume7d92d51995-10-11 18:06:54 +00002108 for file in <very long list of files>:
2109 f = open(file)
2110 c = file.read(1)
Guido van Rossum05151e01995-09-28 13:24:46 +00002111
2112Using the current reference counting and destructor scheme, each new
2113assignment to f closes the previous file. Using GC, this is not
2114guaranteed. Sure, you can think of ways to fix this. But it's not
2115off-the-shelf technology.
2116
Guido van Rossuma7925f11994-01-26 10:20:16 +00002117
21187. Using Python on non-UNIX platforms
2119=====================================
2120
Guido van Rossum91f60831994-02-15 15:52:27 +000021217.1. Q. Is there a Mac version of Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +00002122
Guido van Rossume530c581995-04-10 12:32:16 +00002123A. Yes, see the "mac" subdirectory of the distribution sites,
Guido van Rossum7be34a81995-05-31 15:17:12 +00002124e.g. <URL:ftp://ftp.python.org/pub/python/mac/>.
Guido van Rossuma7925f11994-01-26 10:20:16 +00002125
Guido van Rossum91f60831994-02-15 15:52:27 +000021267.2. Q. Is there a DOS version of Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +00002127
Guido van Rossume530c581995-04-10 12:32:16 +00002128A. Yes, see the "pc" subdirectory of the distribution sites,
Guido van Rossum7be34a81995-05-31 15:17:12 +00002129e.g. <URL:ftp://ftp.python.org/pub/python/pc/>.
Guido van Rossuma7925f11994-01-26 10:20:16 +00002130
Guido van Rossume530c581995-04-10 12:32:16 +000021317.3. Q. Is there a Windows 3.1(1) version of Python?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00002132
Guido van Rossume530c581995-04-10 12:32:16 +00002133A. Yes, also see the "pc" subdirectory of the distribution sites,
Guido van Rossum3fc9d731995-07-25 15:10:56 +00002134e.g. <URL:ftp://ftp.python.org/pub/python/pc/>. You may also be able
2135to run either of the Windows NT versions (see next question) if you
2136have Microsoft's "win32s".
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00002137
Guido van Rossum91f60831994-02-15 15:52:27 +000021387.4. Q. Is there a Windows NT version of Python?
2139
Guido van Rossum3fc9d731995-07-25 15:10:56 +00002140A. There are two, both sporting DLL support for dynamic loading of
2141Python modules, and extensions to access the Win32 GUI API.
2142
2143Mark Hammond <MHammond@cmutual.com.au> maintains an NT port which
2144includes an interface to the Microsoft Foundation Classes and a Python
2145programming environment using it that's written mostly in Python. See
2146<URL:ftp://ftp.python.org/pub/python/nt/>.
2147
2148Jim Ahlstrom's WPY portable GUI runs on Windows NT and is modeled
2149after the Microsoft Foundation Classes. Source and binaries are
2150available in <URL:ftp://ftp.python.org/pub/python/wpy>.
Guido van Rossum91f60831994-02-15 15:52:27 +00002151
Guido van Rossuma6c707c1995-01-02 17:32:28 +00002152Sam Rushing <rushing@squirl.oau.org> once announced he knows how to
2153build Python for the Windows NT on the DEC Alpha AXP.
Guido van Rossum061f1821994-10-06 16:03:45 +00002154
2155Note that currently there is no unified compilation environment for
2156all NT platforms -- hopefully Microsoft will fix this with the release
2157of Visual C++ 2.0.
2158
Guido van Rossume530c581995-04-10 12:32:16 +000021597.5. Q. Is there a Windows 95 version of Python?
Guido van Rossum7ce61c11994-06-13 15:13:56 +00002160
Guido van Rossum3fc9d731995-07-25 15:10:56 +00002161A. The Windows NT versions might work, otherwise the Windows 3.1(1)
Guido van Rossume530c581995-04-10 12:32:16 +00002162version should work (isn't Windows 95 supposed to be backwards
2163compatible?).
Guido van Rossum7ce61c11994-06-13 15:13:56 +00002164
Guido van Rossume530c581995-04-10 12:32:16 +000021657.6. Q. Is there an OS/2 version of Python?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00002166
Guido van Rossume530c581995-04-10 12:32:16 +00002167A. Yes, also see the "pc" subdirectory of the distribution sites,
Guido van Rossum7be34a81995-05-31 15:17:12 +00002168e.g. <URL:ftp://ftp.python.org/pub/python/pc/>.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00002169
Guido van Rossume530c581995-04-10 12:32:16 +000021707.7. Q. Is there a VMS version of Python?
2171
Guido van Rossum3fc9d731995-07-25 15:10:56 +00002172A. Donn Cave <donn@cac.washington.edu> did a partial port. The
2173results of his efforts are on public display in
Guido van Rossumbf8e7d51995-08-28 03:09:13 +00002174<<URL:ftp://ftp.python.org/pub/python/contrib/vms.tar.gz/>. Someone
2175else is working on a more complete port, for details watch the list.
Guido van Rossume530c581995-04-10 12:32:16 +00002176
Guido van Rossum3fc9d731995-07-25 15:10:56 +000021777.8. Q. What about IBM mainframes, or other non-UNIX platforms?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00002178
Guido van Rossumb0a2ce51995-08-28 19:41:35 +00002179A. I haven't heard about these, except I remember hearing about an
2180OS/9 port and a port to Vxworks (both operating systems for embedded
2181systems). If you're interested in any of this, go directly to the
2182newsgroup and ask there, you may find exactly what you need. For
2183example, a port to MPE/iX 5.0 on HP3000 computers was just announced,
2184see <URL:http://www.allegro.com/software/>.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00002185
Guido van Rossume530c581995-04-10 12:32:16 +000021867.9. Q. Where are the source or Makefiles for the non-UNIX versions?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00002187
Guido van Rossume530c581995-04-10 12:32:16 +00002188A. The standard sources can (almost) be used. Additional sources can
2189be found in the platform-specific subdirectories of the distribution.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00002190
Guido van Rossume530c581995-04-10 12:32:16 +000021917.10. Q. What is the status and support for the non-UNIX versions?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00002192
2193A. I don't have access to most of these platforms, so in general I am
2194dependent on material submitted by volunteers(*). However I strive to
2195integrate all changes needed to get it to compile on a particular
2196platform back into the standard sources, so porting of the next
2197version to the various non-UNIX platforms should be easy.
2198
Guido van Rossuma6c707c1995-01-02 17:32:28 +00002199(*) For the Macintosh, that volunteer is me, with help from Jack
2200Jansen <jack@cwi.nl>.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00002201
Guido van Rossume530c581995-04-10 12:32:16 +000022027.11. Q. I have a PC version but it appears to be only a binary.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00002203Where's the library?
Guido van Rossuma7925f11994-01-26 10:20:16 +00002204
2205A. You still need to copy the files from the distribution directory
Guido van Rossum91f60831994-02-15 15:52:27 +00002206"python/Lib" to your system. If you don't have the full distribution,
Guido van Rossume530c581995-04-10 12:32:16 +00002207you can get the file lib<version>.tar.gz from most ftp sites carrying
2208Python; this is a subset of the distribution containing just those
Guido van Rossum7be34a81995-05-31 15:17:12 +00002209files, e.g. <URL:ftp://ftp.python.org/pub/python/src/lib1.1.tar.gz>.
Guido van Rossuma7925f11994-01-26 10:20:16 +00002210
Guido van Rossum5333c5d1994-04-11 11:06:22 +00002211Once you have installed the library, you need to point sys.path to it.
2212Assuming the library is in C:\misc\python\lib, the following commands
2213will point your Python interpreter to it (note the doubled backslashes
2214-- you can also use single forward slashes instead):
2215
Guido van Rossuma6c707c1995-01-02 17:32:28 +00002216 >>> import sys
2217 >>> sys.path.insert(0, 'C:\\misc\\python\\lib')
2218 >>>
Guido van Rossum5333c5d1994-04-11 11:06:22 +00002219
2220For a more permanent effect, set the environment variable PYTHONPATH,
2221as follows (talking to a DOS prompt):
2222
Guido van Rossuma6c707c1995-01-02 17:32:28 +00002223 C> SET PYTHONPATH=C:\misc\python\lib
Guido van Rossum5333c5d1994-04-11 11:06:22 +00002224
Guido van Rossume530c581995-04-10 12:32:16 +000022257.12. Q. Where's the documentation for the Mac or PC version?
Guido van Rossuma7925f11994-01-26 10:20:16 +00002226
Guido van Rossume530c581995-04-10 12:32:16 +00002227A. The documentation for the Unix version also applies to the Mac and
2228PC versions. Where applicable, differences are indicated in the text.
Guido van Rossuma7925f11994-01-26 10:20:16 +00002229
Guido van Rossume530c581995-04-10 12:32:16 +000022307.13. Q. The Mac (PC) version doesn't seem to have any facilities for
Guido van Rossum91f60831994-02-15 15:52:27 +00002231creating or editing programs apart from entering it interactively, and
2232there seems to be no way to save code that was entered interactively.
2233How do I create a Python program on the Mac (PC)?
Guido van Rossuma7925f11994-01-26 10:20:16 +00002234
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00002235A. Use an external editor. On the Mac, BBEdit seems to be a popular
2236no-frills text editor. I work like this: start the interpreter; edit
2237a module file using BBedit; import and test it in the interpreter;
2238edit again in BBedit; then use the built-in function reload() to
2239re-read the imported module; etc.
Guido van Rossum5333c5d1994-04-11 11:06:22 +00002240
2241Regarding the same question for the PC, Kurt Wm. Hemr writes: "While
2242anyone with a pulse could certainly figure out how to do the same on
2243MS-Windows, I would recommend the NotGNU Emacs clone for MS-Windows.
2244Not only can you easily resave and "reload()" from Python after making
2245changes, but since WinNot auto-copies to the clipboard any text you
2246select, you can simply select the entire procedure (function) which
2247you changed in WinNot, switch to QWPython, and shift-ins to reenter
2248the changed program unit."