blob: a7aff6c891ef22310fb1898482c0f756a8ffaff0 [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 Rossumc458e941996-09-11 15:43:13 +00006Expires: Sun, 1 Dec 1996 00:00:00 GMT
7Supersedes: <DxJ3t1.CJv@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 Rossumc458e941996-09-11 15:43:13 +000012Version: $Revision$
13Last-modified: 11 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 Rossumc458e941996-09-11 15:43:13 +000019Copyright 1993-1996 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.
Guido van Rossum8a913021996-10-08 17:18:30 +0000106 3.10. Q. Other trouble building Python on platform X.
107 3.11. Q. How to configure dynamic loading on Linux.
108 3.12. Q. Errors when linking with a shared library containing C++ code.
109 3.13. Q. I built with tkintermodule.c enabled but get "Tkinter not found".
110 3.14. Q. I built with Tk 4.0 but Tkinter complains about the Tk version.
111 3.15. Q. Link errors for Tcl/Tk symbols when linking with Tcl/Tk.
112 3.16. Q. I configured and built Python for Tcl/Tk but "import Tkinter"
Guido van Rossum8727df41996-09-09 15:16:39 +0000113 fails.
Guido van Rossum8a913021996-10-08 17:18:30 +0000114 3.17. Q. Tk doesn't work right on DEC Alpha.
115 3.18. Q. Several common system calls are missing from the posix module.
116 3.19. Q. ImportError: No module named string, on MS Windows.
117 3.20. Q. Core dump on SGI when using the gl module.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000118
119 4. Programming in Python
Guido van Rossum24349991994-02-02 14:12:45 +0000120 4.1. Q. Is there a source code level debugger with breakpoints, step,
121 etc.?
122 4.2. Q. Can I create an object class with some methods implemented in
Guido van Rossuma7925f11994-01-26 10:20:16 +0000123 C and others in Python (e.g. through inheritance)? (Also phrased as:
124 Can I use a built-in type as base class?)
Guido van Rossum24349991994-02-02 14:12:45 +0000125 4.3. Q. Is there a curses/termcap package for Python?
126 4.4. Q. Is there an equivalent to C's onexit() in Python?
127 4.5. Q. When I define a function nested inside another function, the
Guido van Rossuma7925f11994-01-26 10:20:16 +0000128 nested function seemingly can't access the local variables of the
129 outer function. What is going on? How do I pass local data to a
130 nested function?
Guido van Rossum24349991994-02-02 14:12:45 +0000131 4.6. Q. How do I iterate over a sequence in reverse order?
132 4.7. Q. My program is too slow. How do I speed it up?
133 4.8. Q. When I have imported a module, then edit it, and import it
Guido van Rossuma7925f11994-01-26 10:20:16 +0000134 again (into the same Python process), the changes don't seem to take
135 place. What is going on?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000136 4.9. Q. How do I find the current module name?
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000137 4.10. Q. I have a module in which I want to execute some extra code
138 when it is run as a script. How do I find out whether I am running as
139 a script?
140 4.11. Q. I try to run a program from the Demo directory but it fails
141 with ImportError: No module named ...; what gives?
Guido van Rossum061f1821994-10-06 16:03:45 +0000142 4.12. Q. I have successfully built Python with STDWIN but it can't
143 find some modules (e.g. stdwinevents).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000144 4.13. Q. What GUI toolkits exist for Python?
Guido van Rossum061f1821994-10-06 16:03:45 +0000145 4.14. Q. Are there any interfaces to database packages in Python?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000146 4.15. Q. Is it possible to write obfuscated one-liners in Python?
147 4.16. Q. Is there an equivalent of C's "?:" ternary operator?
148 4.17. Q. My class defines __del__ but it is not called when I delete the
149 object.
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000150 4.18. Q. How do I change the shell environment for programs called
151 using os.popen() or os.system()? Changing os.environ doesn't work.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000152 4.19. Q. What is a class?
153 4.20. Q. What is a method?
154 4.21. Q. What is self?
155 4.22. Q. What is a unbound method?
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000156 4.23. Q. How do I call a method defined in a base class from a derived
157 class that overrides it?
158 4.24. Q. How do I call a method from a base class without using the
159 name of the base class?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000160 4.25. Q. How can I organize my code to make it easier to change the base
161 class?
162 4.26. Q. How can I find the methods or attributes of an object?
Guido van Rossum061f1821994-10-06 16:03:45 +0000163 4.27. Q. I can't seem to use os.read() on a pipe created with os.popen().
164 4.28. Q. How can I create a stand-alone binary from a Python script?
Guido van Rossum7be34a81995-05-31 15:17:12 +0000165 4.29. Q. What WWW tools are there for Python?
166 4.30. Q. How do I run a subprocess with pipes connected to both input
Guido van Rossum796b2591995-01-20 23:05:52 +0000167 and output?
Guido van Rossum7be34a81995-05-31 15:17:12 +0000168 4.31. Q. How do I call a function if I have the arguments in a tuple?
169 4.32. Q. How do I enable font-lock-mode for Python in Emacs?
170 4.33. Q. Is there an inverse to the format operator (a la C's scanf())?
Guido van Rossumca318ec1996-06-26 19:50:09 +0000171 4.34. Q. Can I have Tk events handled while waiting for I/O?
172 4.35. Q. How do I write a function with output parameters (call by reference)?
Guido van Rossum0d20cfa1996-07-30 18:53:05 +0000173 4.36. Q. Please explain the rules for local and global variables in Python.
174 4.37. Q. How can I have modules that mutually import each other?
Guido van Rossum8727df41996-09-09 15:16:39 +0000175 4.38. Q. How do I copy an object in Python?
176 4.39. Q. How to implement persistent objects in Python? (Persistent ==
177 automatically saved to and restored from disk.)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000178
179 5. Extending Python
180 5.1. Q. Can I create my own functions in C?
181 5.2. Q. Can I create my own functions in C++?
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000182 5.3. Q. How can I execute arbitrary Python statements from C?
183 5.4. Q. How can I evaluate an arbitrary Python expression from C?
184 5.5. Q. How do I extract C values from a Python object?
185 5.6. Q. How do I use mkvalue() to create a tuple of arbitrary length?
Guido van Rossum8727df41996-09-09 15:16:39 +0000186 5.7. Q. How do I call an object's method from C?
187 5.8. Q. How do I catch the output from print_error()?
188 5.9. Q. How do I access a module written in Python from C?
189 5.10. Q. How do I interface to C++ objects from Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000190
191 6. Python's design
Guido van Rossum8727df41996-09-09 15:16:39 +0000192 6.1. Q. Why isn't there a switch or case statement in Python?
193 6.2. Q. Why does Python use indentation for grouping of statements?
194 6.3. Q. Why are Python strings immutable?
195 6.4. Q. Why don't strings have methods like index() or sort(), like
Guido van Rossum3de27361994-07-25 14:19:33 +0000196 lists?
Guido van Rossum8727df41996-09-09 15:16:39 +0000197 6.5. Q. Why does Python use methods for some functionality
Guido van Rossum3de27361994-07-25 14:19:33 +0000198 (e.g. list.index()) but functions for other (e.g. len(list))?
Guido van Rossum8727df41996-09-09 15:16:39 +0000199 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 +0000200 files)?
Guido van Rossum8727df41996-09-09 15:16:39 +0000201 6.7. Q. Why must 'self' be declared and used explicitly in method
Guido van Rossum3de27361994-07-25 14:19:33 +0000202 definitions and calls?
Guido van Rossum8727df41996-09-09 15:16:39 +0000203 6.8. Q. Can't you emulate threads in the interpreter instead of
204 relying on an OS-specific thread implementation?
205 6.9. Q. Why can't lambda forms contain statements?
206 6.10. Q. Why don't lambdas have access to variables defined in the
207 containing scope?
208 6.11. Q. Why can't recursive functions be defined inside other functions?
Guido van Rossum061f1821994-10-06 16:03:45 +0000209 6.12. Q. Why is there no more efficient way of iterating over a dictionary
210 than first constructing the list of keys()?
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000211 6.13. Q. Can Python be compiled to machine code, C or some other language?
Guido van Rossum05151e01995-09-28 13:24:46 +0000212 6.14. Q. Why doesn't Python use proper garbage collection?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000213
214 7. Using Python on non-UNIX platforms
Guido van Rossum91f60831994-02-15 15:52:27 +0000215 7.1. Q. Is there a Mac version of Python?
Guido van Rossumc458e941996-09-11 15:43:13 +0000216 7.2. Q. Are there DOS and Windows versions of Python?
217 7.3. Q. Is there an OS/2 version of Python?
218 7.4. Q. Is there a VMS version of Python?
219 7.5. Q. What about IBM mainframes, or other non-UNIX platforms?
220 7.6. Q. Where are the source or Makefiles for the non-UNIX versions?
221 7.7. Q. What is the status and support for the non-UNIX versions?
222 7.8. Q. I have a PC version but it appears to be only a binary.
223 Where's the library?
224 7.9. Q. Where's the documentation for the Mac or PC version?
225 7.10. Q. The Mac (PC) version doesn't seem to have any facilities for
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000226 creating or editing programs apart from entering it interactively, and
227 there seems to be no way to save code that was entered interactively.
228 How do I create a Python program on the Mac (PC)?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000229
230To find a particular question, search for the question number followed
231by a dot, a space, and a Q at the beginning of a line (e.g. to find
232question 4.2 in vi, type /^4\.2\. Q/).
233
234
2351. General information and availability
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000236 =======================================
Guido van Rossuma7925f11994-01-26 10:20:16 +0000237
2381.1. Q. What is Python?
239
240A. Python is an interpreted, interactive, object-oriented programming
241language. It incorporates modules, exceptions, dynamic typing, very
242high level dynamic data types, and classes. Python combines
243remarkable power with very clear syntax. It has interfaces to many
244system calls and libraries, as well as to various window systems, and
245is extensible in C or C++. It is also usable as an extension language
246for applications that need a programmable interface. Finally, Python
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000247is portable: it runs on many brands of UNIX, on the Mac, and on PCs
248under MS-DOS, Windows, Windows NT, and OS/2.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000249
250To find out more, the best thing to do is to start reading the
251tutorial from the documentation set (see a few questions further
252down).
253
2541.2. Q. Why is it called Python?
255
Guido van Rossum796b2591995-01-20 23:05:52 +0000256A. Apart from being a computer scientist, I'm also a fan of "Monty
Guido van Rossuma7925f11994-01-26 10:20:16 +0000257Python's Flying Circus" (a BBC comedy series from the seventies, in
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000258the -- unlikely -- case you didn't know). It occurred to me one day
259that I needed a name that was short, unique, and slightly mysterious.
260And I happened to be reading some scripts from the series at the
261time... So then I decided to call my language Python. But Python is
262not a joke. And don't you associate it with dangerous reptiles
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000263either! (If you need an icon, use an image of the 16-ton weight from
264the TV series or of a can of SPAM :-)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000265
2661.3. Q. How do I obtain a copy of the Python source?
267
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000268A. The latest complete Python source distribution is always available
269by anonymous ftp, e.g.
Guido van Rossum8727df41996-09-09 15:16:39 +0000270<URL:ftp://ftp.python.org/pub/python/src/python1.3.tar.gz>. It is a
Guido van Rossume530c581995-04-10 12:32:16 +0000271gzipped tar file containing the complete C source, LaTeX
272documentation, Python library modules, example programs, and several
273useful pieces of freely distributable software. This will compile and
274run out of the box on most UNIX platforms. (See section 7 for
275non-UNIX information.)
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000276
Guido van Rossum8727df41996-09-09 15:16:39 +0000277Sometimes beta versions of a newer release are available; check the
278subdirectory "beta" of the above-mentioned URL (i.e.
279<URL:ftp://ftp.python.org/pub/python/src/beta/>). (At the time of
280writing, beta3 for Python 1.4 is available there, and should be
281checked before reporting problems with version 1.3.)
282
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000283Occasionally a set of patches is issued which has to be applied using
284the patch program. These patches are placed in the same directory,
Guido van Rossum7be34a81995-05-31 15:17:12 +0000285e.g. <URL:ftp://ftp.python.org/pub/python/src/patch1.1.1>. (At the time
Guido van Rossum8727df41996-09-09 15:16:39 +0000286of writing, no patches exist.)
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000287
288An index of said ftp directory can be found in the file INDEX. An
289HTML version of the index can be found in the file index.html,
Guido van Rossum7be34a81995-05-31 15:17:12 +0000290<URL:ftp://ftp.python.org/pub/python/index.html>.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000291
2921.4. Q. How do I get documentation on Python?
293
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000294A. The LaTeX source for the documentation is part of the source
295distribution. If you don't have LaTeX, the latest Python
296documentation set is always available by anonymous ftp, e.g.
Guido van Rossum7be34a81995-05-31 15:17:12 +0000297<URL:ftp://ftp.python.org/pub/python/doc/postscript.tar.gz>. It is a
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000298gzipped tar file containing PostScript files of the reference manual,
Guido van Rossum3de27361994-07-25 14:19:33 +0000299the library manual, and the tutorial. Note that the library manual is
300the most important one of the set, as much of Python's power stems
301from the standard or built-in types, functions and modules, all of
302which are described here. PostScript for a high-level description of
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000303Python is in the file nluug-paper.ps (a separate file on the ftp
304site).
Guido van Rossuma7925f11994-01-26 10:20:16 +0000305
Guido van Rossumc50158e1994-05-31 09:18:50 +00003061.5. Q. Are there other ftp sites that mirror the Python distribution?
307
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000308A. The following anonymous ftp sites keep mirrors of the Python
309distribution:
Guido van Rossuma7925f11994-01-26 10:20:16 +0000310
Guido van Rossume7d92d51995-10-11 18:06:54 +0000311USA:
312
313 <URL:ftp://ftp.python.org/pub/python/>
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000314 <URL:ftp://gatekeeper.dec.com/pub/plan/python/>
Guido van Rossume7d92d51995-10-11 18:06:54 +0000315 <URL:ftp://ftp.uu.net/languages/python/>
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000316 <URL:ftp://ftp.wustl.edu/graphics/graphics/sgi-stuff/python/>
Guido van Rossume7d92d51995-10-11 18:06:54 +0000317 <URL:ftp://ftp.sterling.com/programming/languages/python/>
318 <URL:ftp://uiarchive.cso.uiuc.edu/pub/lang/python/>
319 <URL:ftp://ftp.pht.com/mirrors/python/python/>
320
321Europe:
322
323 <URL:ftp://ftp.cwi.nl/pub/python/>
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000324 <URL:ftp://ftp.funet.fi/pub/languages/python/>
325 <URL:ftp://ftp.sunet.se/pub/lang/python/>
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000326 <URL:ftp://unix.hensa.ac.uk/mirrors/uunet/languages/python/>
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000327 <URL:ftp://ftp.ibp.fr/pub/python/>
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000328 <URL:ftp://ftp.switch.ch/software/sources/python/>
329 <URL:ftp://ftp.informatik.tu-muenchen.de/pub/comp/programming/languages/python/>
Guido van Rossuma7925f11994-01-26 10:20:16 +0000330
Guido van Rossume7d92d51995-10-11 18:06:54 +0000331Australia:
332
333 <URL:ftp://ftp.dstc.edu.au/pub/python/>
334
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000335Or try archie on the string "python".
Guido van Rossuma7925f11994-01-26 10:20:16 +0000336
Guido van Rossumc50158e1994-05-31 09:18:50 +00003371.6. Q. Is there a newsgroup or mailing list devoted to Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000338
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000339A. There is a newsgroup, comp.lang.python <URL:news:comp.lang.python>,
340and a mailing list. The newsgroup and mailing list are gatewayed into
341each other -- if you can read news it's unnecessary to subscribe to
342the mailing list. Send e-mail to <python-list-request@cwi.nl> to
Guido van Rossume530c581995-04-10 12:32:16 +0000343(un)subscribe to the mailing list. Hypermail archives of (nearly)
344everything posted to the mailing list (and thus the newsgroup) are
345available on our WWW server,
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000346<URL:http://www.cwi.nl/~guido/hypermail/index.html>. The raw archives
347are also available by ftp, e.g.
Guido van Rossum7be34a81995-05-31 15:17:12 +0000348<URL:ftp://ftp.python.org/pub/python/mail/mailinglist.gz>. The
Guido van Rossume530c581995-04-10 12:32:16 +0000349uncompressed versions of these files can be read with the standard
350UNIX Mail program ("Mail -f file") or with nn ("nn file"). To read
Guido van Rossumbf8e7d51995-08-28 03:09:13 +0000351them using MH, you could use "inc -file file". (The archival service
352has stopped archiving new articles around the end of April 1995. I
353hope to revive it on the PSA server www.python.org sometime in the
354future.)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000355
Guido van Rossuma6c707c1995-01-02 17:32:28 +00003561.7. Q. Is there a WWW page devoted to Python?
357
Guido van Rossum7be34a81995-05-31 15:17:12 +0000358A. Yes, <URL:http://www.python.org/> is the official Python home page.
359At the time of writing, this page is not yet completely operational;
360you may have a look at the old Python home page:
361<URL:http://www.cwi.nl/~guido/Python.html> or at the U.S. copy:
362<URL:http://www.python.org/~guido/Python.html>.
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000363
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00003641.8. Q. Is the Python documentation available on the WWW?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000365
Guido van Rossum7be34a81995-05-31 15:17:12 +0000366A. Yes, see <URL:http://www.python.org/> (Python's home page). It
367contains pointers to hypertext versions of the whole documentation set
368(as hypertext, not just PostScript).
Guido van Rossuma7925f11994-01-26 10:20:16 +0000369
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000370If you wish to browse this collection of HTML files on your own
371machine, it is available bundled up by anonymous ftp,
Guido van Rossum7be34a81995-05-31 15:17:12 +0000372e.g. <URL:ftp://ftp.python.org/pub/python/doc/html.tar.gz>.
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000373
374An Emacs-INFO set containing the library manual is also available by
Guido van Rossum7be34a81995-05-31 15:17:12 +0000375ftp, e.g. <URL:ftp://ftp.python.org/pub/python/doc/lib-info.tar.gz>.
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000376
3771.9. Q. Is there a book on Python, or will there be one out soon?
378
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000379A. Mark Lutz is writing a Python book for O'Reilly and Associates, to
380be published early 1996. See the outline (in PostScript):
381<URL:http://www.python.org/workshops/1995-05/outlinep.eps>.
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000382
3831.10. Q. Are there any published articles about Python that I can quote?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000384
385A. So far the only refereed and published article that describes
386Python in some detail is:
387
388 Guido van Rossum and Jelke de Boer, "Interactively Testing Remote
389 Servers Using the Python Programming Language", CWI Quarterly, Volume
390 4, Issue 4 (December 1991), Amsterdam, pp 283-303.
391
392LaTeX source for this paper is available as part of the Python source
393distribution.
394
Guido van Rossum05151e01995-09-28 13:24:46 +0000395See also the next section (supposedly Aaron Watters' paper has been
396refereed).
397
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00003981.11. Q. Are there short introductory papers or talks on Python?
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000399
Guido van Rossum05151e01995-09-28 13:24:46 +0000400A. A recent, very entertaining introduction to Python is the tutorial by
401Aaron Watters in UnixWorld Online:
402
403 Aaron R. Watters: "The What, Why, Who, and Where of Python",
404 <URL:http://www.wcmh.com/uworld/archives/95/tutorial/005.html>
405
406An olded paper is:
Guido van Rossuma7925f11994-01-26 10:20:16 +0000407
408 Guido van Rossum, "An Introduction to Python for UNIX/C
409 Programmers", in the proceedings of the NLUUG najaarsconferentie
Guido van Rossum061f1821994-10-06 16:03:45 +0000410 1993 (dutch UNIX users group meeting November 1993).
Guido van Rossuma7925f11994-01-26 10:20:16 +0000411
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000412PostScript for this paper and for the slides used for the accompanying
413presentation is available by ftp as
Guido van Rossum7be34a81995-05-31 15:17:12 +0000414<URL:ftp://ftp.python.org/pub/python/doc/nluug-paper.ps> and
415<URL:ftp://ftp.python.org/pub/python/doc/nluug-slides.ps>, respectively.
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000416
417Slides for a talk on Python that I gave at the Usenix Symposium on
Guido van Rossum05151e01995-09-28 13:24:46 +0000418Very High Level Languages in Santa Fe, NM, USA in October 1994 are
Guido van Rossum7be34a81995-05-31 15:17:12 +0000419available as <URL:ftp://ftp.python.org/pub/python/doc/vhll-slides.ps>.
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000420
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00004211.12. Q. How does the Python version numbering scheme work?
Guido van Rossum95f61a71994-01-26 17:23:37 +0000422
Guido van Rossum05151e01995-09-28 13:24:46 +0000423A. Python versions are numbered A.B.C or A.B. A is the major version
424number -- it is only incremented for major changes in functionality or
425source structure. B is the minor version number, incremented for less
Guido van Rossum95f61a71994-01-26 17:23:37 +0000426earth-shattering changes to a release. C is the patchlevel -- it is
Guido van Rossum05151e01995-09-28 13:24:46 +0000427incremented for each new patch release. Not all releases have patch
428releases. Note that in the past, patches have added significant
429changes; in fact the changeover from 0.9.9 to 1.0.0 was the first time
430that either A or B changed!
Guido van Rossum95f61a71994-01-26 17:23:37 +0000431
Guido van Rossum8727df41996-09-09 15:16:39 +0000432Beta versions have an additional suffix of "betaN" for some small
433number N. Note that (for instance) all versions labeled 1.4betaN
434*precede* the actual release of 1.4. 1.4b3 is short for 1.4beta3.
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000435
4361.13. Q. How do I get a beta test version of Python?
437
Guido van Rossum05151e01995-09-28 13:24:46 +0000438A. If there are any beta releases, they are published in the normal
439source directory (e.g. <URL:ftp://ftp.python.org/pub/python/src/>).
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000440
Guido van Rossum05151e01995-09-28 13:24:46 +00004411.14. Q. Are there copyright restrictions on the use of Python?
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000442
443A. Hardly. You can do anything you want with the source, as long as
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000444you leave the copyrights in, and display those copyrights in any
445documentation about Python that you produce. Also, don't use the
446author's institute's name in publicity without prior written
447permission, and don't hold them responsible for anything (read the
448actual copyright for a precise legal wording).
449
450In particular, if you honor the copyright rules, it's OK to use Python
451for commercial use, to sell copies of Python in source or binary form,
452or to sell products that enhance Python or incorporate Python (or part
453of it) in some form. I would still like to know about all commercial
454use of Python!
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000455
Guido van Rossum05151e01995-09-28 13:24:46 +00004561.15. Q. Why was Python created in the first place?
Guido van Rossum7be34a81995-05-31 15:17:12 +0000457
458A. Here's a *very* brief summary of what got me started:
459
460- I had extensive experience with implementing an interpreted language
461in the ABC group at CWI, and from working with this group I had
462learned a lot about language design. This is the origin of many
463Python features, including the use of indentation for statement
464grouping and the inclusion of very-high-level data types (although the
465details are all different in Python).
466
467- I had a number of gripes about the ABC language, but also liked many
468of its features. It was impossible to extend the ABC language (or its
469implementation) to remedy my complaints -- in fact its lack of
470extensibility was one of its biggest problems.
471
472- I had some experience with using Modula-2+ and talked with the
473designers of Modula-3 (and read the M3 report). M3 is the origin of
474the syntax and semantics used for exceptions, and some other Python
475features.
476
477- I was working in the Amoeba distributed operating system group at
478CWI. We needed a better way to do system administration than by
479writing either C programs or Bourne shell scripts, since Amoeba had
480its own system call interface which wasn't easily accessible from the
Guido van Rossumbf8e7d51995-08-28 03:09:13 +0000481Bourne shell. My experience with error handling in Amoeba made me
482acutely aware of the importance of exceptions as a programming
483language feature.
Guido van Rossum7be34a81995-05-31 15:17:12 +0000484
485- It occurred to me that a scripting language with a syntax like ABC
486but with access to the Amoeba system calls would fill the need. I
487realized that it would be foolish to write an Amoeba-specific
488language, so I decided that I needed a language that was generally
489extensible.
490
491- During the 1989 Christmas holidays, I had a lot of time on my hand,
492so I decided to give it a try. During the next year, while still
493mostly working on it in my own time, Python was used in the Amoeba
494project with increasing success, and the feedback from colleagues made
495me add many early improvements.
496
497- In February 1991, after just over a year of development, I decided
498to post to USENET. The rest is in the Misc/HISTORY file.
499
Guido van Rossuma7925f11994-01-26 10:20:16 +0000500
5012. Python in the real world
502===========================
503
5042.1. Q. How many people are using Python?
505
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000506A. I don't know, but the maximum number of simultaneous subscriptions
507to the Python mailing list before it was gatewayed into the newsgroup
508was about 180 (several of which were local redistribution lists). I
509believe that many active Python users don't bother to subscribe to the
510list, and now that there's a newsgroup the mailing list subscription
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000511is even less meaningful. I see new names on the newsgroup all the
512time and my best guess is that there are currently at least several
513thousands of users.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000514
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000515Another statistic is the number of accesses to the Python WWW server.
516Have a look at <URL:http://www.python.org/stats/>.
517
Guido van Rossuma7925f11994-01-26 10:20:16 +00005182.2. Q. Have any significant projects been done in Python?
519
520A. Here at CWI (the home of Python), we have written a 20,000 line
521authoring environment for transportable hypermedia presentations, a
Guido van Rossum5333c5d1994-04-11 11:06:22 +00005225,000 line multimedia teleconferencing tool, as well as many many
523smaller programs.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000524
525The University of Virginia uses Python to control a virtual reality
526engine. Contact: Matt Conway <conway@virginia.edu>.
527
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000528The ILU project at Xerox PARC can generate Python glue for ILU
529interfaces. See <URL:ftp://ftp.parc.xerox.com/pub/ilu/ilu.html>.
530
Guido van Rossumac3f2121995-04-10 11:53:42 +0000531The University of California, Irvine uses a student administration
532system called TELE-Vision written entirely in Python. Contact: Ray
533Price <rlprice@uci.edu>.
534
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000535See also the next question.
536
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000537If you have done a significant project in Python that you'd like to be
538included in the list above, send me email!
Guido van Rossuma7925f11994-01-26 10:20:16 +0000539
5402.3. Q. Are there any commercial projects going on using Python?
541
542A. Several companies have revealed to me that they are planning or
Guido van Rossum796b2591995-01-20 23:05:52 +0000543considering use of Python in a future product.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000544
Guido van Rossumbf8e7d51995-08-28 03:09:13 +0000545Sunrise Software has a product out using Python -- they use Python
Guido van Rossumac3f2121995-04-10 11:53:42 +0000546for a GUI management application and an SNMP network management
547application. Contact: <info@sunrise.com>.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000548
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000549Infoseek uses Python to implement their commercial WWW information
550retrieval service <URL:http://www.infoseek.com/>. Contact:
551<info@infoseek.com>.
Guido van Rossumf8c76d01994-08-17 12:19:53 +0000552
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000553Paul Everitt of Connecting Minds is planning a Lotus Notes gateway.
554Contact: <Paul.Everitt@cminds.com>. Or see their WWW server
555<URL:http://www.cminds.com/>.
556
Guido van Rossumac3f2121995-04-10 11:53:42 +0000557KaPRE in Boulder, CO is using Python for on-site customization of C++
558applications, rapid-prototyping/development,
559language-based-components, and possibly more. This is pretty solid:
560Python's being shipped with their tool-set now, to beta sites.
561Contact: <lutz@KaPRE.COM> (Mark Lutz).
562
563Individuals at many other companies are using Python for internal
564development or for as yet unannounced products (witness their
565contributions to the Python mailing list or newsgroup).
Guido van Rossuma7925f11994-01-26 10:20:16 +0000566
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000567SGI has advertised in the Python list looking for Python programmers
568for a project involving interactive television.
569
570See also the workshop minutes at
571<URL:http://www.python.org/workshops/> -- in general the WWW server is
572more up to date than the FAQ for these issues.
573
Guido van Rossuma7925f11994-01-26 10:20:16 +0000574Python has also been elected as an extension language by MADE, a
575consortium supported by the European Committee's ESPRIT program and
576consisting of Bull, CWI and some other European companies. Contact:
577Ivan Herman <ivan@cwi.nl>.
578
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000579If you'd like to be included in the list above, send me email!
580
Guido van Rossum95f61a71994-01-26 17:23:37 +00005812.4. Q. How stable is Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000582
Guido van Rossum3de27361994-07-25 14:19:33 +0000583A. Very stable. While the current version number would suggest it is
584in the early stages of development, in fact new, stable releases
Guido van Rossume7d92d51995-10-11 18:06:54 +0000585(numbered 0.9.x through 1.3) have been coming out roughly every 3 to
Guido van Rossuma6c707c1995-01-02 17:32:28 +00005866 months for the past four years.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000587
Guido van Rossum8727df41996-09-09 15:16:39 +00005882.5. Q. What new developments are expected for Python in the future?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000589
Guido van Rossum7be34a81995-05-31 15:17:12 +0000590A. See my Work-In-Progress web page, currently at
591<URL:http://www.python.org:~guido/WIP.html>, and the pages for the
592Second Python Workshop (best reached via the Python home page,
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000593<URL:http://www.python.org/>). Also follow the newsgroup discussions!
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000594
Guido van Rossum8727df41996-09-09 15:16:39 +00005952.6. Q. Is it reasonable to propose incompatible changes to Python?
Guido van Rossum3de27361994-07-25 14:19:33 +0000596
597A. In general, no. There are already millions of lines of Python code
598around the world, so any changes in the language that invalidates more
599than a very small fraction of existing programs has to be frowned
600upon. Even if you can provide a conversion program, there still is
601the problem of updating all documentation. Providing a gradual
602upgrade path is the only way if a feature has to be changed.
603
Guido van Rossum8727df41996-09-09 15:16:39 +00006042.7. Q. What is the future of Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000605
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000606A. If I knew, I'd be rich :-)
607
608Seriously, the formation of the PSA (Pyton Software Activity, see
609<URL:http://www.python.org/psa/>) ensures some kind of support even in
610the (unlikely! event that I'd be hit by a bus (actually, here in the
611US, a car accident would be more likely :-), were to join a nunnery,
612or would be head-hunted. A large number of Python users have become
613experts at Python programming as well as maintenance of the
614implementation, and would easily fill the vacuum created by my
615disappearance.
616
617In the mean time, I have no plans to disappear -- rather, I am
618committed to improving Python, and my current benefactor, CNRI (see
619<URL:http://www.cnri.reston.va.us>) is just as committed to continue
620its support of Python and the PSA. In fact, we have great plans for
621Python -- we just can't tell yet!
622
Guido van Rossum8727df41996-09-09 15:16:39 +00006232.8. Q. What is the PSA, anyway?
Guido van Rossum05151e01995-09-28 13:24:46 +0000624
625A. The Python Software Activity <URL:http://www.python.org/psa/> was
626created by a number of Python aficionados who want Python to be more
627than the product and responsibility of a single individual. It has
628found a home at CNRI <URL:http://www.cnri.reston.va.us>. Anybody who
629wishes Python well should join the PSA.
630
Guido van Rossum8727df41996-09-09 15:16:39 +00006312.9. Q. How do I join the PSA?
Guido van Rossum05151e01995-09-28 13:24:46 +0000632
633A. The full scoop is available on the web, see
634<URL:http://www.python.org/psa/Joining.html>. Summary: send a check
635of at least $50 to CNRI/PSA, 1895 Preston White Drive, Suite 100, in
Guido van Rossum81300541996-09-06 16:37:56 +0000636Reston, VA 20191. Full-time students pay $25. Companies can join for
Guido van Rossum05151e01995-09-28 13:24:46 +0000637a mere $500.
638
Guido van Rossum8727df41996-09-09 15:16:39 +00006392.10. Q. What are the benefits of joining the PSA?
Guido van Rossum05151e01995-09-28 13:24:46 +0000640
641A. Like National Public Radio, if not enough people join, Python will
642wither. Your name will be mentioned on the PSA's web server.
643Workshops organized by the PSA <URL:http://www.python.org/workshops/>
644are only accessible to PSA members (you can join at the door). The
645PSA is working on additional benefits, such as reduced prices for
646books and software, and early access to beta versions of Python.
647
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000648
6493. Building Python and Other Known Bugs
650=======================================
Guido van Rossuma7925f11994-01-26 10:20:16 +0000651
Guido van Rossum91f60831994-02-15 15:52:27 +00006523.1. Q. Is there a test set?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000653
654A. Yes, simply do "import testall" (or "import autotest" if you aren't
655interested in the output). The standard modules whose name begins
656with "test" together comprise the test. The test set doesn't test
657*all* features of Python but it goes a long way to confirm that a new
658port is actually working. The Makefile contains an entry "make test"
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000659which runs the autotest module. NOTE: if "make test" fails, run the
660tests manually ("import testall") to see what goes wrong before
661reporting the error.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000662
Guido van Rossum91f60831994-02-15 15:52:27 +00006633.2. Q. When running the test set, I get complaints about floating point
Guido van Rossuma7925f11994-01-26 10:20:16 +0000664operations, but when playing with floating point operations I cannot
665find anything wrong with them.
666
667A. The test set makes occasional unwarranted assumptions about the
668semantics of C floating point operations. Until someone donates a
669better floating point test set, you will have to comment out the
670offending floating point tests and execute similar tests manually.
671
Guido van Rossum7be34a81995-05-31 15:17:12 +00006723.3. Q. Link errors after rerunning the configure script.
Guido van Rossum24349991994-02-02 14:12:45 +0000673
674A. It is generally necessary to run "make clean" after a configuration
675change.
676
Guido van Rossum7be34a81995-05-31 15:17:12 +00006773.4. Q. The python interpreter complains about options passed to a
Guido van Rossum24349991994-02-02 14:12:45 +0000678script (after the script name).
679
680A. You are probably linking with GNU getopt, e.g. through -liberty.
Guido van Rossumf8c76d01994-08-17 12:19:53 +0000681Don't. The reason for the complaint is that GNU getopt, unlike System
682V getopt and other getopt implementations, doesn't consider a
683non-option to be the end of the option list. A quick (and compatible)
684fix for scripts is to add "--" to the interpreter, like this:
685
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000686 #! /usr/local/bin/python --
Guido van Rossumf8c76d01994-08-17 12:19:53 +0000687
688You can also use this interactively:
689
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000690 python -- script.py [options]
691
692Note that a working getopt implementation is provided in the Python
693distribution (in Python/getopt.c) but not automatically used.
Guido van Rossum24349991994-02-02 14:12:45 +0000694
Guido van Rossum7be34a81995-05-31 15:17:12 +00006953.5. Q. When building on the SGI, make tries to run python to create
Guido van Rossum24349991994-02-02 14:12:45 +0000696glmodule.c, but python hasn't been built or installed yet.
697
698A. Comment out the line mentioning glmodule.c in Setup and build a
699python without gl first; install it or make sure it is in your $PATH,
700then edit the Setup file again to turn on the gl module, and make
701again. You don't need to do "make clean"; you do need to run "make
702Makefile" in the Modules subdirectory (or just run "make" at the
703toplevel).
704
Guido van Rossum8727df41996-09-09 15:16:39 +00007053.6. Q. I use VPATH but some targets are built in the source directory.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000706
707A. On some systems (e.g. Sun), if the target already exists in the
708source directory, it is created there instead of in the build
709directory. This is usually because you have previously built without
710VPATH. Try running "make clobber" in the source directory.
711
Guido van Rossum8727df41996-09-09 15:16:39 +00007123.7. Q. Trouble building or linking with the GNU readline library.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000713
Guido van Rossumac3f2121995-04-10 11:53:42 +0000714A. Consider using readline 2.0. Some hints:
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000715
716- You can use the GNU readline library to improve the interactive user
717interface: this gives you line editing and command history when
Guido van Rossum796b2591995-01-20 23:05:52 +0000718calling python interactively. You need to configure and build the GNU
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000719readline library before running the configure script. Its sources are
720no longer distributed with Python; you can ftp them from any GNU
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000721mirror site, or from its home site
722<URL:ftp://slc2.ins.cwru.edu/pub/dist/readline-2.0.tar.gz> (or a
723higher version number -- using version 1.x is not recommended). Pass
724the Python configure script the option --with-readline=DIRECTORY where
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000725DIRECTORY is the absolute pathname of the directory where you've built
726the readline library. Some hints on building and using the readline
727library:
728
729- On SGI IRIX 5, you may have to add the following
730to rldefs.h:
731
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000732 #ifndef sigmask
733 #define sigmask(sig) (1L << ((sig)-1))
734 #endif
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000735
736- On most systems, you will have to add #include "rldefs.h" to the
737top of several source files, and if you use the VPATH feature, you
738will have to add dependencies of the form foo.o: foo.c to the
739Makefile for several values of foo.
740
741- The readline library requires use of the termcap library. A
742known problem with this is that it contains entry points which
Guido van Rossum061f1821994-10-06 16:03:45 +0000743cause conflicts with the STDWIN and SGI GL libraries. The STDWIN
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000744conflict can be solved (and will be, in the next release of
Guido van Rossum061f1821994-10-06 16:03:45 +0000745STDWIN) by adding a line saying '#define werase w_erase' to the
746stdwin.h file (in the STDWIN distribution, subdirectory H). The
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000747GL conflict has been solved in the Python configure script by a
748hack that forces use of the static version of the termcap library.
749
Guido van Rossumac3f2121995-04-10 11:53:42 +0000750- Check the newsgroup gnu.bash.bug <URL:news:gnu.bash.bug> for
751specific problems with the readline library (I don't read this group
752but I've been told that it is the place for readline bugs).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000753
Guido van Rossum8727df41996-09-09 15:16:39 +00007543.8. Q. Trouble with socket I/O on older Linux 1.x versions.
Guido van Rossumca318ec1996-06-26 19:50:09 +0000755
756A. Once you've built Python, use it to run the regen.py script in the
757Lib/linux1 directory. Apparently the files as distributed don't match
758the system headers on some Linux versions.
759
Guido van Rossum8727df41996-09-09 15:16:39 +00007603.9. Q. Trouble with prototypes on Ultrix.
Guido van Rossum72eb83c1994-10-07 11:33:28 +0000761
762A. Ultrix cc seems broken -- use gcc, or edit config.h to #undef
763HAVE_PROTOTYPES.
764
Guido van Rossum8a913021996-10-08 17:18:30 +00007653.10. Q. Other trouble building Python on platform X.
Guido van Rossum95f61a71994-01-26 17:23:37 +0000766
Guido van Rossum81300541996-09-06 16:37:56 +0000767A. Please email the details to <guido@cnri.reston.va.us> and I'll look
768into it. Please provide as many details as possible. In particular,
769if you don't tell me what type of computer and what operating system
770(and version) you are using it will be difficult for me to figure out
771what is the matter. If you get a specific error message, please email
772it to me too.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000773
Guido van Rossum8a913021996-10-08 17:18:30 +00007743.11. Q. How to configure dynamic loading on Linux.
Guido van Rossum07779351995-02-07 16:59:56 +0000775
Guido van Rossum8727df41996-09-09 15:16:39 +0000776A. This is now automatic as long as your Linux version uses the ELF
777object format (all recent Linuxes do).
Guido van Rossum07779351995-02-07 16:59:56 +0000778
Guido van Rossum8a913021996-10-08 17:18:30 +00007793.12. Q. Errors when linking with a shared library containing C++ code.
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000780
781A. Link the main Python binary with C++. Change the definition of
782LINKCC in Modules/Makefile to be your C++ compiler. You may have to
783edit config.c slightly to make it compilable with C++.
Guido van Rossum07779351995-02-07 16:59:56 +0000784
Guido van Rossum8a913021996-10-08 17:18:30 +00007853.13. Q. I built with tkintermodule.c enabled but get "Tkinter not found".
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000786
787A. Tkinter.py (note: upper case T) lives in a subdirectory of Lib,
788Lib/tkinter. If you are using the default module search path, you
789probably didn't enable the line in the Modules/Setup file defining
790TKPATH; if you use the environment variable PYTHONPATH, you'll have to
791add the proper tkinter subdirectory.
792
Guido van Rossum8a913021996-10-08 17:18:30 +00007933.14. Q. I built with Tk 4.0 but Tkinter complains about the Tk version.
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000794
795A. Several things could cause this. You most likely have a Tk 3.6
796installation that wasn't completely eradicated by the Tk 4.0
797installation (which tends to add "4.0" to its installed files). You
798may have the Tk 3.6 support library installed in the place where the
799Tk 4.0 support files should be (default /usr/local/lib/tk/); you may
800have compiled Python with the old tk.h header file (yes, this actually
801compiles!); you may actually have linked with Tk 3.6 even though Tk
8024.0 is also around. Similar for Tcl 7.4 vs. Tcl 7.3.
803
Guido van Rossum8a913021996-10-08 17:18:30 +00008043.15. Q. Link errors for Tcl/Tk symbols when linking with Tcl/Tk.
Guido van Rossum8727df41996-09-09 15:16:39 +0000805
806Quite possibly, there's a version mismatch between the Tcl/Tk header
807files (tcl.h and tk.h) and the tck/tk libraries you are using (the
808"-ltk4.0" and "-ltcl7.4" arguments for _tkinter in the Setup file).
809If you have installed both versions 7.4/4.0 and 7.5/4.1 of Tcl/Tk,
810most likely your header files are for The newer versions, but the
811Setup line for _tkinter in some Python distributions references
8127.4/4.0 by default. Changing this to 7.5/4.1 should take care of
813this.
814
Guido van Rossum8a913021996-10-08 17:18:30 +00008153.16. Q. I configured and built Python for Tcl/Tk but "import Tkinter"
Guido van Rossum8727df41996-09-09 15:16:39 +0000816fails.
817
818A. Most likely, you forgot to enable the line in Setup that says
819"TKPATH=:$(DESTLIB)/tkinter".
820
Guido van Rossum8a913021996-10-08 17:18:30 +00008213.17. Q. Tk doesn't work right on DEC Alpha.
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000822
823A. You probably compiled either Tcl, Tk or Python with gcc. Don't.
824For this platform, which has 64-bit integers, gcc is known to generate
825broken code. The standard cc (which comes bundled with the OS!)
826works. If you still prefer gcc, at least try recompiling with cc
827before reporting problems to the newsgroup or the author; if this
828fixes the problem, report the bug to the gcc developers instead. (As
829far as we know, there are no problem with gcc on other platforms --
830the instabilities seem to be restricted to the DEC Alpha.) See also
831question 3.6.
832
Guido van Rossum8a913021996-10-08 17:18:30 +00008333.18. Q. Several common system calls are missing from the posix module.
Guido van Rossumca318ec1996-06-26 19:50:09 +0000834
835A. Most likely, *all* test compilations run by the configure script
836are failing for some reason or another. Have a look in config.log to
837see what could be the reason. A common reason is specifying a
838directory to the --with-readline option that doesn't contain the
839libreadline.a file.
840
Guido van Rossum8a913021996-10-08 17:18:30 +00008413.19. Q. ImportError: No module named string, on MS Windows.
Guido van Rossumca318ec1996-06-26 19:50:09 +0000842
843A. Most likely, your PYTHONPATH environment variable should be set to
844something like:
845
846set PYTHONPATH=c:\python;c:\python\lib;c:\python\scripts
847
848(assuming Python was installed in c:\python)
849
Guido van Rossum8a913021996-10-08 17:18:30 +00008503.20. Q. Core dump on SGI when using the gl module.
Guido van Rossumca318ec1996-06-26 19:50:09 +0000851
852There are conflicts between entry points in the termcap and curses
853libraries and an entry point in the GL library. There's a hack of a
854fix for the termcap library if it's needed for the GNU readline
855library, but it doesn't work when you're using curses. Concluding,
856you can't build a Python binary containing both the curses and gl
857modules.
858
859
Guido van Rossuma7925f11994-01-26 10:20:16 +0000860
8614. Programming in Python
862========================
863
Guido van Rossum24349991994-02-02 14:12:45 +00008644.1. Q. Is there a source code level debugger with breakpoints, step,
865etc.?
866
867A. Yes. Check out module pdb; pdb.help() prints the documentation (or
868you can read it as Lib/pdb.doc). If you use the STDWIN option,
869there's also a windowing interface, wdb. You can write your own
870debugger by using the code for pdb or wdb as an example.
871
8724.2. Q. Can I create an object class with some methods implemented in
Guido van Rossuma7925f11994-01-26 10:20:16 +0000873C and others in Python (e.g. through inheritance)? (Also phrased as:
874Can I use a built-in type as base class?)
875
876A. No, but you can easily create a Python class which serves as a
877wrapper around a built-in object, e.g. (for dictionaries):
878
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000879 # A user-defined class behaving almost identical
880 # to a built-in dictionary.
881 class UserDict:
882 def __init__(self): self.data = {}
883 def __repr__(self): return repr(self.data)
884 def __cmp__(self, dict):
885 if type(dict) == type(self.data):
886 return cmp(self.data, dict)
887 else:
888 return cmp(self.data, dict.data)
889 def __len__(self): return len(self.data)
890 def __getitem__(self, key): return self.data[key]
891 def __setitem__(self, key, item): self.data[key] = item
892 def __delitem__(self, key): del self.data[key]
893 def keys(self): return self.data.keys()
894 def items(self): return self.data.items()
895 def values(self): return self.data.values()
896 def has_key(self, key): return self.data.has_key(key)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000897
Guido van Rossum24349991994-02-02 14:12:45 +00008984.3. Q. Is there a curses/termcap package for Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000899
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000900A. Yes -- Lance Ellinghaus has written a module that interfaces to
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000901System V's "ncurses". If you know a little curses and some Python,
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000902it's straightforward to use. It is part of the standard Python
903distribution, but not configured by default -- you must enable it by
904editing Modules/Setup. It requires a System V curses implementation.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000905
906You could also consider using the "alfa" (== character cell) version
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000907of STDWIN. (Standard Window System Interface, a portable windowing
908system interface by myself <URL:ftp://ftp.cwi.nl/pub/stdwin/>.) This
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000909will also prepare your program for porting to windowing environments
910such as X11 or the Macintosh.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000911
Guido van Rossum24349991994-02-02 14:12:45 +00009124.4. Q. Is there an equivalent to C's onexit() in Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000913
914A. Yes, if you import sys and assign a function to sys.exitfunc, it
915will be called when your program exits, is killed by an unhandled
916exception, or (on UNIX) receives a SIGHUP or SIGTERM signal.
917
Guido van Rossum24349991994-02-02 14:12:45 +00009184.5. Q. When I define a function nested inside another function, the
Guido van Rossuma7925f11994-01-26 10:20:16 +0000919nested function seemingly can't access the local variables of the
920outer function. What is going on? How do I pass local data to a
921nested function?
922
923A. Python does not have arbitrarily nested scopes. When you need to
924create a function that needs to access some data which you have
925available locally, create a new class to hold the data and return a
926method of an instance of that class, e.g.:
927
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000928 class MultiplierClass:
929 def __init__(self, factor):
930 self.factor = factor
931 def multiplier(self, argument):
932 return argument * self.factor
Guido van Rossuma7925f11994-01-26 10:20:16 +0000933
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000934 def generate_multiplier(factor):
935 return MultiplierClass(factor).multiplier
Guido van Rossuma7925f11994-01-26 10:20:16 +0000936
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000937 twice = generate_multiplier(2)
938 print twice(10)
939 # Output: 20
940
941An alternative solution uses default arguments, e.g.:
942
943 def generate_multiplier(factor):
944 def multiplier(arg, fact = factor):
945 return arg*fact
946 return multiplier
947
948 twice = generate_multiplier(2)
949 print twice(10)
950 # Output: 20
Guido van Rossuma7925f11994-01-26 10:20:16 +0000951
Guido van Rossum24349991994-02-02 14:12:45 +00009524.6. Q. How do I iterate over a sequence in reverse order?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000953
954A. If it is a list, the fastest solution is
955
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000956 list.reverse()
957 try:
958 for x in list:
959 "do something with x"
960 finally:
961 list.reverse()
Guido van Rossuma7925f11994-01-26 10:20:16 +0000962
963This has the disadvantage that while you are in the loop, the list
964is temporarily reversed. If you don't like this, you can make a copy.
965This appears expensive but is actually faster than other solutions:
966
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000967 rev = list[:]
968 rev.reverse()
969 for x in rev:
970 <do something with x>
Guido van Rossuma7925f11994-01-26 10:20:16 +0000971
972If it isn't a list, a more general but slower solution is:
973
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000974 i = len(list)
975 while i > 0:
976 i = i-1
977 x = list[i]
978 <do something with x>
Guido van Rossuma7925f11994-01-26 10:20:16 +0000979
980A more elegant solution, is to define a class which acts as a sequence
981and yields the elements in reverse order (solution due to Steve
982Majewski):
983
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000984 class Rev:
985 def __init__(self, seq):
986 self.forw = seq
987 def __len__(self):
988 return len(self.forw)
989 def __getitem__(self, i):
990 return self.forw[-(i + 1)]
Guido van Rossuma7925f11994-01-26 10:20:16 +0000991
992You can now simply write:
993
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000994 for x in Rev(list):
995 <do something with x>
Guido van Rossuma7925f11994-01-26 10:20:16 +0000996
Guido van Rossum061f1821994-10-06 16:03:45 +0000997Unfortunately, this solution is slowest of all, due to the method
Guido van Rossuma7925f11994-01-26 10:20:16 +0000998call overhead...
999
Guido van Rossum24349991994-02-02 14:12:45 +000010004.7. Q. My program is too slow. How do I speed it up?
Guido van Rossuma7925f11994-01-26 10:20:16 +00001001
1002A. That's a tough one, in general. There are many tricks to speed up
1003Python code; I would consider rewriting parts in C only as a last
1004resort. One thing to notice is that function and (especially) method
1005calls are rather expensive; if you have designed a purely OO interface
1006with lots of tiny functions that don't do much more than get or set an
1007instance variable or call another method, you may consider using a
1008more direct way, e.g. directly accessing instance variables. Also see
1009the standard module "profile" (described in the file
1010"python/lib/profile.doc") which makes it possible to find out where
1011your program is spending most of its time (if you have some patience
1012-- the profiling itself can slow your program down by an order of
1013magnitude).
1014
Guido van Rossum24349991994-02-02 14:12:45 +000010154.8. Q. When I have imported a module, then edit it, and import it
Guido van Rossuma7925f11994-01-26 10:20:16 +00001016again (into the same Python process), the changes don't seem to take
1017place. What is going on?
1018
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001019A. For reasons of efficiency as well as consistency, Python only reads
Guido van Rossum796b2591995-01-20 23:05:52 +00001020the module file on the first time a module is imported. (Otherwise a
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001021program consisting of many modules, each of which imports the same
Guido van Rossum796b2591995-01-20 23:05:52 +00001022basic module, would read the basic module over and over again.) To
1023force rereading of a changed module, do this:
Guido van Rossuma7925f11994-01-26 10:20:16 +00001024
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001025 import modname
1026 reload(modname)
Guido van Rossuma7925f11994-01-26 10:20:16 +00001027
1028Warning: this technique is not 100% fool-proof. In particular,
1029modules containing statements like
1030
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001031 from modname import some_objects
Guido van Rossuma7925f11994-01-26 10:20:16 +00001032
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001033will continue to work with the old version of the imported objects.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001034
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000010354.9. Q. How do I find the current module name?
Guido van Rossum24349991994-02-02 14:12:45 +00001036
Guido van Rossum061f1821994-10-06 16:03:45 +00001037A. A module can find out its own module name by looking at the
Guido van Rossum24349991994-02-02 14:12:45 +00001038(predefined) global variable __name__. If this has the value
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001039'__main__' you are running as a script.
1040
Guido van Rossuma6c707c1995-01-02 17:32:28 +000010414.10. Q. I have a module in which I want to execute some extra code
1042when it is run as a script. How do I find out whether I am running as
1043a script?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001044
1045A. See the previous question. E.g. if you put the following on the
1046last line of your module, main() is called only when your module is
1047running as a script:
Guido van Rossum24349991994-02-02 14:12:45 +00001048
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001049 if __name__ == '__main__': main()
Guido van Rossum24349991994-02-02 14:12:45 +00001050
Guido van Rossuma6c707c1995-01-02 17:32:28 +000010514.11. Q. I try to run a program from the Demo directory but it fails
1052with ImportError: No module named ...; what gives?
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001053
Guido van Rossum061f1821994-10-06 16:03:45 +00001054A. This is probably an optional module (written in C!) which hasn't
1055been configured on your system. This especially happens with modules
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001056like "Tkinter", "stdwin", "gl", "Xt" or "Xm". For Tkinter, STDWIN and
1057many other modules, see Modules/Setup.in for info on how to add these
1058modules to your Python, if it is possible at all. Sometimes you will
1059have to ftp and build another package first (e.g. STDWIN). Sometimes
1060the module only works on specific platforms (e.g. gl only works on SGI
1061machines).
1062
1063NOTE: if the complaint is about "Tkinter" (upper case T) and you have
Guido van Rossumca318ec1996-06-26 19:50:09 +00001064already configured module "tkinter" (lower case t), the solution is
1065*not* to rename tkinter to Tkinter or vice versa. There is probably
1066something wrong with your module search path. Check out the value of
1067sys.path.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001068
Guido van Rossum061f1821994-10-06 16:03:45 +00001069For X-related modules (Xt and Xm) you will have to do more work: they
1070are currently not part of the standard Python distribution. You will
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001071have to ftp the Extensions tar file, e.g.
Guido van Rossum7be34a81995-05-31 15:17:12 +00001072<URL:ftp://ftp.python.org/pub/python/src/extensions.tar.gz> and follow
1073the instructions there.
Guido van Rossum061f1821994-10-06 16:03:45 +00001074
1075See also the next question.
1076
10774.12. Q. I have successfully built Python with STDWIN but it can't
1078find some modules (e.g. stdwinevents).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001079
1080A. There's a subdirectory of the library directory named 'stdwin'
1081which should be in the default module search path. There's a line in
1082Modules/Setup(.in) that you have to enable for this purpose --
Guido van Rossum061f1821994-10-06 16:03:45 +00001083unfortunately in the latest release it's not near the other
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001084STDWIN-related lines so it's easy to miss it.
1085
10864.13. Q. What GUI toolkits exist for Python?
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001087
1088A. Depending on what platform(s) you are aiming at, there are several.
1089
Guido van Rossumc458e941996-09-11 15:43:13 +00001090Currently supported solutions:
1091
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001092- There's a neat object-oriented interface to the Tcl/Tk widget set,
Guido van Rossumc458e941996-09-11 15:43:13 +00001093called Tkinter. It is part of the standard Python distribution and
1094well-supported -- all you need to do is build and install Tcl/Tk and
1095enable the _tkinter module and the TKPATH definition in Modules/Setup
1096when building Python. This is probably the easiest to install and
1097use, and the most complete widget set. It is also very likely that in
1098the future the standard Python GUI API will be based on or at least
1099look very much like the Tkinter interface. For more info about Tk,
1100including pointers to the source, see the Tcl/Tk home page
1101<URL:http://www.sunlabs.com/research/tcl/>. Tcl/Tk is now fully
1102portable to the Mac and Windows platforms (NT and 95 only); you need
1103Python 1.4beta3 or later and Tk 4.1patch1 or later.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001104
1105- There's an interface to X11, including the Athena and Motif widget
1106sets (and a few individual widgets, like Mosaic's HTML widget and
Guido van Rossumc458e941996-09-11 15:43:13 +00001107SGI's GL widget) available from
1108<URL:ftp://ftp.python.org/pub/python/src/X-extension.tar.gz>.
1109Support by Sjoerd Mullender <sjoerd@cwi.nl>.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001110
Guido van Rossum8a913021996-10-08 17:18:30 +00001111- On top of the X11 interface there's the (recently revived) vpApp
1112toolkit by Per Spilling, now also maintained by Sjoerd Mullender
1113<sjoerd@cwi.nl>. See <URL:ftp://ftp.cwi.nl/pub/sjoerd/vpApp.tar.gz>.
1114
Guido van Rossumc458e941996-09-11 15:43:13 +00001115- The Mac port has a rich and ever-growing set of modules that support
1116the native Mac toolbox calls. See the documentation that comes with
1117the Mac port. See <URL:ftp://ftp.python.org/pub/python/mac>. Support
1118by Jack Jansen <jack@cwi.nl>.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001119
Guido van Rossumc458e941996-09-11 15:43:13 +00001120- The NT port supported by Mark Hammond <MHammond@skippinet.com.au>
1121(see question 7.2) includes an interface to the Microsoft Foundation
1122Classes and a Python programming environment using it that's written
1123mostly in Python. See
1124<URL:ftp://ftp.python.org/pub/python/pythonwin/>.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001125
Guido van Rossumc458e941996-09-11 15:43:13 +00001126- There's an object-oriented GUI based on the Microsoft Foundation
1127Classes model called WPY, supported by Jim Ahlstrom <jim@interet.com>.
1128Programs written in WPY run unchanged and with native look and feel on
1129Windows NT/95, Windows 3.1 (using win32s), and on Unix (using Tk).
1130Source and binaries for Windows and Linux are available in
1131<URL:ftp://ftp.python.org/pub/python/wpy/>.
1132
1133Obsolete or minority solutions:
Guido van Rossumbf8e7d51995-08-28 03:09:13 +00001134
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00001135- There's an interface to wxWindows. wxWindows is a portable GUI
1136class library written in C++. It supports XView, Motif, MS-Windows as
Guido van Rossumc458e941996-09-11 15:43:13 +00001137targets. There is some support for Macs and CURSES as well.
1138wxWindows preserves the look and feel of the underlying graphics
1139toolkit. See the wxPython WWW page at
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00001140<URL:http://www.aiai.ed.ac.uk/~jacs/wx/wxpython/wxpython.html>.
Guido van Rossumc458e941996-09-11 15:43:13 +00001141Support for wxPython (by Harri Pasanen <pa@tekla.fi>) appears
1142to have a low priority.
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00001143
Guido van Rossumc458e941996-09-11 15:43:13 +00001144- For SGI IRIX only, there are unsupported interfaces to the complete
1145GL (Graphics Library -- low level but very good 3D capabilities) as
1146well as to FORMS (a buttons-and-sliders-etc package built on top of GL
1147by Mark Overmars -- ftp'able from
1148<URL:ftp://ftp.cs.ruu.nl/pub/SGI/FORMS/>). This is probably also
1149becoming obsolete, as OpenGL takes over.
1150
1151- There's an interface to STDWIN, a platform-independent low-level
1152windowing interface for Mac and X11. This is totally unsupported and
1153rapidly becoming obsolete. The STDWIN sources are at
1154<URL:ftp://ftp.cwi.nl/pub/stdwin/>. (For info about STDWIN 2.0,
1155please refer to Steven Pemberton <steven@cwi.nl> -- I believe it is
1156also dead.)
1157
1158- There once was an interface to WAFE, a Tcl interface to the X11
1159Motif and Athena widget sets. WAFE is at
1160<URL:ftp://ftp.wu-wien.ac.at/pub/src/X11/wafe/>. It's not clear what
1161the status of the Python support is.
Guido van Rossum3fc9d731995-07-25 15:10:56 +00001162
Guido van Rossum0d20cfa1996-07-30 18:53:05 +00001163- (The Fresco port that was mentioned in earlier versions of this FAQ
1164no longer seems to exist. Inquire with Mark Linton.)
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00001165
Guido van Rossum061f1821994-10-06 16:03:45 +000011664.14. Q. Are there any interfaces to database packages in Python?
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001167
Guido van Rossum8727df41996-09-09 15:16:39 +00001168A. There's a whole collection of them in the contrib area of the ftp
1169server, see <URL:http://www.python.org/ftp/python/contrib/Database/>.
Guido van Rossum796b2591995-01-20 23:05:52 +00001170
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000011714.15. Q. Is it possible to write obfuscated one-liners in Python?
Guido van Rossumc24da7c1994-09-23 14:08:41 +00001172
1173A. Yes. See the following three examples, due to Ulf Bartelt:
1174
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001175 # Primes < 1000
1176 print filter(None,map(lambda y:y*reduce(lambda x,y:x*y!=0,
1177 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 +00001178
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001179 # First 10 Fibonacci numbers
1180 print map(lambda x,f=lambda x,f:(x<=1) or (f(x-1,f)+f(x-2,f)): f(x,f),
1181 range(10))
Guido van Rossumc24da7c1994-09-23 14:08:41 +00001182
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001183 # Mandelbrot set
1184 print (lambda Ru,Ro,Iu,Io,IM,Sx,Sy:reduce(lambda x,y:x+y,map(lambda y,
1185 Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,Sy=Sy,L=lambda yc,Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,i=IM,
1186 Sx=Sx,Sy=Sy:reduce(lambda x,y:x+y,map(lambda x,xc=Ru,yc=yc,Ru=Ru,Ro=Ro,
1187 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
1188 >=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(
1189 64+F(Ru+x*(Ro-Ru)/Sx,yc,0,0,i)),range(Sx))):L(Iu+y*(Io-Iu)/Sy),range(Sy
1190 ))))(-2.1, 0.7, -1.2, 1.2, 30, 80, 24)
1191 # \___ ___/ \___ ___/ | | |__ lines on screen
1192 # V V | |______ columns on screen
1193 # | | |__________ maximum of "iterations"
1194 # | |_________________ range on y axis
1195 # |____________________________ range on x axis
Guido van Rossumc24da7c1994-09-23 14:08:41 +00001196
1197Don't try this at home, kids!
1198
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000011994.16. Q. Is there an equivalent of C's "?:" ternary operator?
Guido van Rossumc24da7c1994-09-23 14:08:41 +00001200
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001201A. Not directly. In many cases you can mimic a?b:c with "a and b or
1202c", but there's a flaw: if b is zero (or empty, or None -- anything
1203that tests false) then c will be selected instead. In many cases you
1204can prove by looking at the code that this can't happen (e.g. because
1205b is a constant or has a type that can never be false), but in general
1206this can be a problem.
1207
Guido van Rossumc458e941996-09-11 15:43:13 +00001208Tim Peters (who wishes it was Steve Majewski) suggested the following
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001209solution: (a and [b] or [c])[0]. Because [b] is a singleton list it
1210is never false, so the wrong path is never taken; then applying [0] to
1211the whole thing gets the b or c that you really wanted. Ugly, but it
1212gets you there in the rare cases where it is really inconvenient to
1213rewrite your code using 'if'.
1214
12154.17. Q. My class defines __del__ but it is not called when I delete the
1216object.
1217
1218A. There are several possible reasons for this.
1219
1220- The del statement does not necessarily call __del__ -- it simply
1221decrements the object's reference count, and if this reaches zero
1222__del__ is called.
1223
1224- If your data structures contain circular links (e.g. a tree where
1225each child has a parent pointer and each parent has a list of
1226children) the reference counts will never go back to zero. You'll
Guido van Rossum061f1821994-10-06 16:03:45 +00001227have to define an explicit close() method which removes those
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001228pointers. Please don't ever call __del__ directly -- __del__ should
Guido van Rossum061f1821994-10-06 16:03:45 +00001229call close() and close() should make sure that it can be called more
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001230than once for the same object.
1231
1232- If the object has ever been a local variable (or argument, which is
1233really the same thing) to a function that caught an expression in an
1234except clause, chances are that a reference to the object still exists
1235in that function's stack frame as contained in the stack trace.
1236Normally, deleting (better: assigning None to) sys.exc_traceback will
1237take care of this. If you a stack was printed for an unhandled
1238exception in an interactive interpreter, delete sys.last_traceback
1239instead.
1240
1241- There is code that deletes all objects when the interpreter exits,
1242but if your Python has been configured to support threads, it is not
1243called (because other threads may still be active). You can define
Guido van Rossum061f1821994-10-06 16:03:45 +00001244your own cleanup function using sys.exitfunc (see question 4.4).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001245
Guido van Rossum8727df41996-09-09 15:16:39 +00001246- Finally, if your __del__ method raises an exception, this will be
1247ignored. Starting with Python 1.4beta3, a warning message is printed
1248to sys.stderr when this happens.
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001249
12504.18. Q. How do I change the shell environment for programs called
1251using os.popen() or os.system()? Changing os.environ doesn't work.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001252
1253A. Modifying the environment passed to subshells was left out of the
1254interpreter because there seemed to be no well-established portable
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001255way to do it (in particular, some systems, have putenv(), others have
1256setenv(), and some have none at all).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001257
1258However if all you want is to pass environment variables to the
1259commands run by os.system() or os.popen(), there's a simple solution:
1260prefix the command string with a couple of variable assignments and
Guido van Rossum796b2591995-01-20 23:05:52 +00001261export statements. The following would be universal for popen:
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001262
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001263 import os
1264 from commands import mkarg # nifty routine to add shell quoting
1265 def epopen(cmd, mode, env = {}):
1266 # env is a dictionary of environment variables
1267 prefix = ''
Guido van Rossum796b2591995-01-20 23:05:52 +00001268 for key, value in env.items():
1269 prefix = prefix + '%s=%s\n' % (key, mkarg(value)[1:])
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001270 prefix = prefix + 'export %s\n' % key
1271 return os.popen(prefix + cmd, mode)
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001272
12734.19. Q. What is a class?
1274
1275A. A class is the particular object type that is created by executing
Guido van Rossum796b2591995-01-20 23:05:52 +00001276a class statement. Class objects are used as templates, to create
1277class instance objects, which embody both the data structure and
1278program routines specific to a datatype.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001279
12804.20. Q. What is a method?
1281
1282A. A method is a function that you normally call as
Guido van Rossum796b2591995-01-20 23:05:52 +00001283x.name(arguments...) for some object x. The term is used for methods
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001284of classes and class instances as well as for methods of built-in
Guido van Rossum796b2591995-01-20 23:05:52 +00001285objects. (The latter have a completely different implementation and
1286only share the way their calls look in Python code.) Methods of
1287classes (and class instances) are defined as functions inside the
1288class definition.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001289
12904.21. Q. What is self?
1291
1292A. Self is merely a conventional name for the first argument of a
1293method -- i.e. a function defined inside a class definition. A method
1294defined as meth(self, a, b, c) should be called as x.meth(a, b, c) for
1295some instance x of the class in which the definition occurs;
1296the called method will think it is called as meth(x, a, b, c).
1297
12984.22. Q. What is a unbound method?
1299
1300A. An unbound method is a method defined in a class that is not yet
1301bound to an instance. You get an unbound method if you ask for a
1302class attribute that happens to be a function. You get a bound method
1303if you ask for an instance attribute. A bound method knows which
Guido van Rossum061f1821994-10-06 16:03:45 +00001304instance it belongs to and calling it supplies the instance automatically;
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001305an unbound method only knows which class it wants for its first
1306argument (a derived class is also OK). Calling an unbound method
1307doesn't "magically" derive the first argument from the context -- you
1308have to provide it explicitly.
1309
Guido van Rossuma6c707c1995-01-02 17:32:28 +000013104.23. Q. How do I call a method defined in a base class from a derived
1311class that overrides it?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001312
1313A. If your class definition starts with "class Derived(Base): ..."
1314then you can call method meth defined in Base (or one of Base's base
1315classes) as Base.meth(self, arguments...). Here, Base.meth is an
1316unbound method (see previous question).
1317
Guido van Rossuma6c707c1995-01-02 17:32:28 +000013184.24. Q. How do I call a method from a base class without using the
1319name of the base class?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001320
1321A. DON'T DO THIS. REALLY. I MEAN IT. It appears that you could call
1322self.__class__.__bases__[0].meth(self, arguments...) but this fails when
Guido van Rossum061f1821994-10-06 16:03:45 +00001323a doubly-derived method is derived from your class: for its instances,
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001324self.__class__.__bases__[0] is your class, not its base class -- so
1325(assuming you are doing this from within Derived.meth) you would start
1326a recursive call.
1327
13284.25. Q. How can I organize my code to make it easier to change the base
1329class?
1330
1331A. You could define an alias for the base class, assign the real base
1332class to it before your class definition, and use the alias throughout
1333your class. Then all you have to change is the value assigned to the
1334alias. Incidentally, this trick is also handy if you want to decide
1335dynamically (e.g. depending on availability of resources) which base
1336class to use. Example:
1337
Guido van Rossumc458e941996-09-11 15:43:13 +00001338 BaseAlias = <real base class>
1339 class Derived(BaseAlias):
1340 def meth(self):
1341 BaseAlias.meth(self)
1342 ...
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001343
13444.26. Q. How can I find the methods or attributes of an object?
1345
1346A. This depends on the object type.
1347
1348For an instance x of a user-defined class, instance attributes are
1349found in the dictionary x.__dict__, and methods and attributes defined
1350by its class are found in x.__class__.__bases__[i].__dict__ (for i in
1351range(len(x.__class__.__bases__))). You'll have to walk the tree of
1352base classes to find *all* class methods and attributes.
1353
1354Many, but not all built-in types define a list of their method names
1355in x.__methods__, and if they have data attributes, their names may be
1356found in x.__members__. However this is only a convention.
1357
1358For more information, read the source of the standard (but
1359undocumented) module newdir.
1360
13614.27. Q. I can't seem to use os.read() on a pipe created with os.popen().
1362
1363A. os.read() is a low-level function which takes a file descriptor (a
1364small integer). os.popen() creates a high-level file object -- the
1365same type used for sys.std{in,out,err} and returned by the builtin
1366open() function. Thus, to read n bytes from a pipe p created with
1367os.popen(), you need to use p.read(n).
Guido van Rossuma7925f11994-01-26 10:20:16 +00001368
Guido van Rossum061f1821994-10-06 16:03:45 +000013694.28. Q. How can I create a stand-alone binary from a Python script?
1370
1371The demo script "Demo/scripts/freeze.py" does what you want. (It's
1372actually not a demo but a support tool -- there is some extra code in
1373the interpreter to accommodate it.) It requires that you have the
1374Python build tree handy, complete with all the lib*.a files.
1375
1376This works by scanning your source recursively for import statements
1377(both forms) and looking for the modules on the standard Python path
1378as well as in the source directory (for built-in modules). It then
1379"compiles" the modules written in Python to C code (array initializers
1380that can be turned into code objects using the marshal module) and
1381creates a custom-made config file that only contains those built-in
1382modules which are actually used in the program. It then compiles the
1383generated C code and links it with the rest of the Python interpreter
1384to form a self-contained binary which acts exactly like your script.
1385
Guido van Rossum7be34a81995-05-31 15:17:12 +00001386Hint: the freeze program only works if your script's filename ends in
1387".py".
Guido van Rossum061f1821994-10-06 16:03:45 +00001388
Guido van Rossum7be34a81995-05-31 15:17:12 +000013894.29. Q. What WWW tools are there for Python?
Guido van Rossum061f1821994-10-06 16:03:45 +00001390
Guido van Rossum8727df41996-09-09 15:16:39 +00001391A. See the chapter titled "Internet and WWW" in the Library Reference
1392Manual. There's also a web browser written in Python, called Grail --
1393see <URL:http://monty.cnri.reston.va.us/grail/>.
Guido van Rossum061f1821994-10-06 16:03:45 +00001394
1395Steve Miale <smiale@cs.indiana.edu> has written a modular WWW browser
1396called Dancer. An alpha version can be FTP'ed from
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001397<URL:ftp://ftp.cs.indiana.edu/pub/smiale/dancer.tar.gz>. (There are a
1398few articles about Dancer in the (hyper)mail archive
1399<URL:http://www.cwi.nl/~guido/hypermail/python-1994q3/index.html>.)
Guido van Rossum061f1821994-10-06 16:03:45 +00001400
Guido van Rossum7be34a81995-05-31 15:17:12 +000014014.30. Q. How do I run a subprocess with pipes connected to both input
Guido van Rossum796b2591995-01-20 23:05:52 +00001402and output?
1403
1404A. This is really a UNIX question. Also, in general, it is unwise to
1405do so, because you can easily cause a deadlock where the parent
1406process is blocked waiting for output from the child, while the child
1407is blocked waiting for input from the child. This can be caused
1408because the parent expects the child to output more text than it does,
1409or it can be caused by data being stuck in stdio buffers due to lack
1410of flushing. The Python parent can of course explicitly flush the data
1411it sends to the child before it reads any output, but if the child is
1412a naive C program it can easily have been written to never explicitly
1413flush its output, even if it is interactive, since flushing is
1414normally automatic.
1415
1416In many cases, all you really need is to run some data through a
1417command and get the result back. Unless the data is infinite in size,
1418the easiest (and often the most efficient!) way to do this is to write
1419it to a temporary file and run the command with that temporary file as
1420input. The standard module tempfile exports a function mktemp() which
1421generates unique temporary file names.
1422
1423If after reading all of the above you still want to connect two pipes
1424to a subprocess's standard input and output, here's a simple solution,
1425due to Jack Jansen:
1426
Guido van Rossume7d92d51995-10-11 18:06:54 +00001427 import os
1428 import sys
1429 import string
Guido van Rossum796b2591995-01-20 23:05:52 +00001430
Guido van Rossume7d92d51995-10-11 18:06:54 +00001431 MAXFD = 100 # Max number of file descriptors in this system
Guido van Rossum796b2591995-01-20 23:05:52 +00001432
Guido van Rossume7d92d51995-10-11 18:06:54 +00001433 def popen2(cmd):
1434 cmd = string.split(cmd)
1435 p2cread, p2cwrite = os.pipe()
1436 c2pread, c2pwrite = os.pipe()
1437 pid = os.fork()
1438 if pid == 0:
1439 # Child
1440 os.close(0)
1441 os.close(1)
Guido van Rossumca318ec1996-06-26 19:50:09 +00001442 if os.dup(p2cread) != 0:
Guido van Rossume7d92d51995-10-11 18:06:54 +00001443 sys.stderr.write('popen2: bad read dup\n')
Guido van Rossumca318ec1996-06-26 19:50:09 +00001444 if os.dup(c2pwrite) != 1:
Guido van Rossume7d92d51995-10-11 18:06:54 +00001445 sys.stderr.write('popen2: bad write dup\n')
1446 for i in range(3, MAXFD):
1447 try:
1448 os.close(i)
1449 except:
1450 pass
1451 try:
1452 os.execv(cmd[0], cmd)
1453 finally:
1454 os._exit(1)
1455 os.close(p2cread)
1456 tochild = os.fdopen(p2cwrite, 'w')
1457 os.close(c2pwrite)
1458 fromchild = os.fdopen(c2pread, 'r')
1459 return fromchild, tochild
Guido van Rossum796b2591995-01-20 23:05:52 +00001460
1461Note that many interactive programs (e.g. vi) don't work well with
1462pipes substituted for standard input and output. You will have to use
1463pseudo ttys ("ptys") instead of pipes. There is some undocumented
1464code to use these in the library module pty.py -- I'm afraid you're on
Guido van Rossum3fc9d731995-07-25 15:10:56 +00001465your own here.
1466
1467A different answer is a Python interface to Don Libes' "expect"
1468library. A prerelease of this is available on the Python ftp mirror
1469sites in the contrib subdirectory as expy-0.3.tar.gz, e.g.
1470<URL:ftp://ftp.python.org/pub/python/contrib/expy-0.3.tar.gz>.
Guido van Rossum796b2591995-01-20 23:05:52 +00001471
Guido van Rossum7be34a81995-05-31 15:17:12 +000014724.31. Q. How do I call a function if I have the arguments in a tuple?
Guido van Rossumac3f2121995-04-10 11:53:42 +00001473
1474A. Use the built-in function apply(). For instance,
1475
1476 func(1, 2, 3)
1477
1478is equivalent to
1479
1480 args = (1, 2, 3)
1481 apply(func, args)
1482
1483Note that func(args) is not the same -- it calls func() with exactly
1484one argument, the tuple args, instead of three arguments, the integers
14851, 2 and 3.
1486
Guido van Rossum7be34a81995-05-31 15:17:12 +000014874.32. Q. How do I enable font-lock-mode for Python in Emacs?
1488
1489A. Assuming you're already using python-mode and font-lock-mode
1490separately, all you need to do is put this in your .emacs file:
1491
Guido van Rossume7d92d51995-10-11 18:06:54 +00001492 (defun my-python-mode-hook ()
1493 (setq font-lock-keywords python-font-lock-keywords)
1494 (font-lock-mode 1))
1495 (add-hook 'python-mode-hook 'my-python-mode-hook)
Guido van Rossum7be34a81995-05-31 15:17:12 +00001496
14974.33. Q. Is there an inverse to the format operator (a la C's scanf())?
1498
1499A. Not as such.
1500
1501For simple input parsing, the easiest approach is usually to split
1502the line into whitespace-delimited words using string.split(), and to
1503convert decimal strings to numeric values using string.atoi(),
1504string.atol() or string.atof(). (Python's atoi() is 32-bit and its
1505atol() is arbitrary precision.) If you want to use another delimiter
1506than whitespace, use string.splitfield() (possibly combining it with
1507string.strip() which removes surrounding whitespace from a string).
1508
1509For more complicated input parsing, regular expressions (see module
1510regex) are better suited and more powerful than C's scanf().
1511
Guido van Rossumca318ec1996-06-26 19:50:09 +000015124.34. Q. Can I have Tk events handled while waiting for I/O?
1513
1514A. Yes, and you don't even need threads! But you'll have to
1515restructure your I/O code a bit. Tk has the equivalent of Xt's
1516XtAddInput() call, which allows you to register a callback function
1517which will be called from the Tk mainloop when I/O is possible on a
1518file descriptor. Here's what you need:
1519
Guido van Rossumc458e941996-09-11 15:43:13 +00001520 from Tkinter import tkinter
1521 tkinter.createfilehandler(file, mask, callback)
Guido van Rossumca318ec1996-06-26 19:50:09 +00001522
1523The file may be a Python file or socket object (actually, anything
1524with a fileno() method), or an integer file descriptor. The mask is
1525one of the constants tkinter.READABLE or tkinter.WRITABLE. The
1526callback is called as follows:
1527
Guido van Rossumc458e941996-09-11 15:43:13 +00001528 callback(file, mask)
Guido van Rossumca318ec1996-06-26 19:50:09 +00001529
1530You must unregister the callback when you're done, using
1531
Guido van Rossumc458e941996-09-11 15:43:13 +00001532 tkinter.deletefilehandler(file)
Guido van Rossumca318ec1996-06-26 19:50:09 +00001533
1534Note: since you don't know *how many bytes* are available for reading,
1535you can't use the Python file object's read or readline methods, since
1536these will insist on reading a predefined number of bytes. For
1537sockets, the recv() or recvfrom() methods will work fine; for other
1538files, use os.read(file.fileno(), maxbytecount).
1539
15404.35. Q. How do I write a function with output parameters (call by reference)?
1541
1542A. [Mark Lutz] The thing to remember is that arguments are passed by
1543assignment in Python. Since assignment just creates references to
1544objects, there's no alias between an argument name in the caller and
1545callee, and so no call-by-reference per se. But you can simulate it
1546in a number of ways:
1547
15481) By using global variables; but you probably shouldn't :-)
1549
15502) By passing a mutable (changeable in-place) object:
1551
1552 def func1(a):
1553 a[0] = 'new-value' # 'a' references a mutable list
1554 a[1] = a[1] + 1 # changes a shared object
1555
1556 args = ['old-value', 99]
1557 func1(args)
1558 print args[0], args[1] # output: new-value 100
1559
15603) By return a tuple, holding the final values of arguments:
1561
1562 def func2(a, b):
1563 a = 'new-value' # a and b are local names
1564 b = b + 1 # assigned to new objects
1565 return a, b # return new values
1566
1567 x, y = 'old-value', 99
1568 x, y = func2(x, y)
1569 print x, y # output: new-value 100
1570
15714) And other ideas that fall-out from Python's object model. For
1572 instance, it might be clearer to pass in a mutable dictionary:
1573
1574 def func3(args):
1575 args['a'] = 'new-value' # args is a mutable dictionary
1576 args['b'] = args['b'] + 1 # change it in-place
1577
1578 args = {'a':' old-value', 'b': 99}
1579 func3(args)
1580 print args['a'], args['b']
1581
15825) Or bundle-up values in a class instance:
1583
1584 class callByRef:
1585 def __init__(self, **args):
1586 for (key, value) in args.items():
1587 setattr(self, key, value)
1588
1589 def func4(args):
1590 args.a = 'new-value' # args is a mutable callByRef
1591 args.b = args.b + 1 # change object in-place
1592
1593 args = callByRef(a='old-value', b=99)
1594 func4(args)
1595 print args.a, args.b
1596
1597 But there's probably no good reason to get this complicated :-).
1598
1599[Python' author favors solution 3 in most cases.]
1600
Guido van Rossum0d20cfa1996-07-30 18:53:05 +000016014.36. Q. Please explain the rules for local and global variables in Python.
1602
1603A. [Ken Manheimer] In Python, procedure variables are implicitly
1604global, unless they assigned anywhere within the block. In that case
1605they are implicitly local, and you need to explicitly declare them as
1606'global'.
1607
1608Though a bit surprising at first, a moments consideration explains
1609this. On one hand, requirement of 'global' for assigned vars provides
1610a bar against unintended side-effects. On the other hand, if global
1611were required for all global references, you'd be using global all the
1612time. Eg, you'd have to declare as global every reference to a
1613builtin function, or to a component of an imported module. This
1614clutter would defeat the usefulness of the 'global' declaration for
1615identifying side-effects.
1616
16174.37. Q. How can I have modules that mutually import each other?
1618
1619A. Jim Roskind recommends the following order in each module:
1620
1621First: all exports (like globals, functions, and classes that don't
1622need imported bases classes).
1623
1624Then: all import statements.
1625
1626Finally: all active code (including globals that are initialized from
1627imported values).
1628
1629Python's author doesn't like this approach much because the imports
1630appear in a strange place, but has to admit that it works. His
1631recommended strategy is to avoid all uses of "from <module> import *"
1632(so everything from an imported module is referenced as
1633<module>.<name>) and to place all code inside functions.
1634Initializations of global variables and class variables should use
1635constants or built-in functions only.
1636
Guido van Rossum8727df41996-09-09 15:16:39 +000016374.38. Q. How do I copy an object in Python?
1638
1639A. There is no generic copying operation built into Python, however
1640most object types have some way to create a clone. Here's how for the
1641most common objects:
1642
1643- For immutable objects (numbers, strings, tuples), cloning is
1644unnecessary since their value can't change.
1645
1646- For lists (and generally for mutable sequence types), a clone is
1647created by the expression l[:].
1648
1649- For dictionaries, the following function returns a clone:
1650
Guido van Rossumc458e941996-09-11 15:43:13 +00001651 def dictclone(o):
1652 n = {}
1653 for k in o.keys(): n[k] = o[k]
1654 return n
Guido van Rossum8727df41996-09-09 15:16:39 +00001655
1656- Finally, for generic objects, the "copy" module defines two
1657functions for copying objects. copy.copy(x) returns a copy as shown
1658by the above rules. copy.deepcopy(x) also copies the elements of
1659composite objects. See the section on this module in the Library
1660Reference Manual.
1661
16624.39. Q. How to implement persistent objects in Python? (Persistent ==
1663automatically saved to and restored from disk.)
1664
1665A. The library module "pickle" now solves this in a very general way
1666(though you still can't store things like open files, sockests or
1667windows), and the library module "shelve" uses pickle and (g)dbm to
1668create presistent mappings containing arbitrary Python objects.
1669
Guido van Rossum061f1821994-10-06 16:03:45 +00001670
Guido van Rossuma7925f11994-01-26 10:20:16 +000016715. Extending Python
1672===================
1673
16745.1. Q. Can I create my own functions in C?
1675
1676A. Yes, you can create built-in modules containing functions,
Guido van Rossum24349991994-02-02 14:12:45 +00001677variables, exceptions and even new types in C. This is explained in
1678the document "Extending and Embedding the Python Interpreter" (the
1679LaTeX file Doc/ext.tex). Also read the chapter on dynamic loading.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001680
16815.2. Q. Can I create my own functions in C++?
1682
1683A. Yes, using the C-compatibility features found in C++. Basically
1684you place extern "C" { ... } around the Python include files and put
1685extern "C" before each function that is going to be called by the
1686Python interpreter. Global or static C++ objects with constructors
1687are probably not a good idea.
1688
Guido van Rossum7ce61c11994-06-13 15:13:56 +000016895.3. Q. How can I execute arbitrary Python statements from C?
1690
1691A. The highest-level function to do this is run_command() which takes
1692a single string argument which is executed in the context of module
1693__main__ and returns 0 for success and -1 when an exception occurred
1694(including SyntaxError). If you want more control, use run_string();
1695see the source for run_command() in Python/pythonrun.c.
1696
16975.4. Q. How can I evaluate an arbitrary Python expression from C?
1698
1699A. Call the function run_string() from the previous question with the
1700start symbol eval_input; it then parses an expression, evaluates it
1701and returns its value. See exec_eval() in Python/bltinmodule.c.
1702
17035.5. Q. How do I extract C values from a Python object?
1704
1705A. That depends on the object's type. If it's a tuple,
1706gettuplesize(o) returns its length and gettupleitem(o, i) returns its
1707i'th item; similar for lists with getlistsize(o) and getlistitem(o,
1708i). For strings, getstringsize(o) returns its length and
1709getstringvalue(o) a pointer to its value (note that Python strings may
1710contain null bytes so strlen() is not safe). To test which type an
1711object is, first make sure it isn't NULL, and then use
1712is_stringobject(o), is_tupleobject(o), is_listobject(o) etc.
1713
17145.6. Q. How do I use mkvalue() to create a tuple of arbitrary length?
1715
1716A. You can't. Use t = newtupleobject(n) instead, and fill it with
1717objects using settupleitem(t, i, o) -- note that this "eats" a
1718reference count of o. Similar for lists with newlistobject(n) and
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001719setlistitem(l, i, o). Note that you *must* set all the tuple items to
1720some value before you pass the tuple to Python code --
1721newtupleobject(n) initializes them to NULL, which isn't a valid Python
1722value.
1723
Guido van Rossum8727df41996-09-09 15:16:39 +000017245.7. Q. How do I call an object's method from C?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001725
1726A. Here's a function (untested) that might become part of the next
1727release in some form. It uses <stdarg.h> to allow passing the
1728argument list on to vmkvalue():
1729
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001730 object *call_method(object *inst, char *methodname, char *format, ...)
1731 {
1732 object *method;
1733 object *args;
1734 object *result;
1735 va_list va;
1736 method = getattr(inst, methodname);
1737 if (method == NULL) return NULL;
1738 va_start(va, format);
1739 args = vmkvalue(format, va);
1740 va_end(va);
1741 if (args == NULL) {
1742 DECREF(method);
1743 return NULL;
1744 }
1745 result = call_object(method, args);
1746 DECREF(method);
1747 DECREF(args);
1748 return result;
1749 }
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001750
1751This works for any instance that has methods -- whether built-in or
1752user-defined. You are responsible for eventually DECREF'ing the
1753return value.
1754
1755To call, e.g., a file object's "seek" method with arguments 10, 0
1756(assuming the file object pointer is "f"):
1757
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001758 res = call_method(f, "seek", "(OO)", 10, 0);
1759 if (res == NULL) {
1760 ... an exception occurred ...
1761 }
1762 else {
1763 DECREF(res);
1764 }
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001765
1766Note that since call_object() *always* wants a tuple for the argument
1767list, to call a function without arguments, pass "()" for the format,
1768and to call a function with one argument, surround the argument in
1769parentheses, e.g. "(i)".
1770
Guido van Rossum8727df41996-09-09 15:16:39 +000017715.8. Q. How do I catch the output from print_error()?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001772
1773A. (Due to Mark Hammond):
1774
1775* in Python code, define an object that supports the "write()" method.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001776
1777* redirect sys.stdout and sys.stderr to this object.
1778
Guido van Rossum061f1821994-10-06 16:03:45 +00001779* call print_error, or just allow the standard traceback mechanism to
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001780work.
1781
Guido van Rossum061f1821994-10-06 16:03:45 +00001782Then, the output will go wherever your write() method sends it.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001783
Guido van Rossum8727df41996-09-09 15:16:39 +000017845.9. Q. How do I access a module written in Python from C?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001785
1786A. You can get a pointer to the module object as follows:
1787
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001788 module = import_module("<modulename>");
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001789
1790If the module hasn't been imported yet (i.e. it is not yet present in
1791sys.modules), this initializes the module; otherwise it simply returns
1792the value of sys.modules["<modulename>"]. Note that it doesn't enter
1793the module into any namespace -- it only ensures it has been
1794initialized and is stored in sys.modules.
1795
1796You can then access the module's attributes (i.e. any name defined in
1797the module) as follows:
1798
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001799 attr = getattr(module, "<attrname>");
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001800
1801Calling setattr(), to assign to variables in the module, also works.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001802
Guido van Rossum8727df41996-09-09 15:16:39 +000018035.10. Q. How do I interface to C++ objects from Python?
Guido van Rossumb0a2ce51995-08-28 19:41:35 +00001804
1805A. Depending on your requirements, there are many approaches. Begin
1806by reading the "Extending and Embedding" document (Doc/ext.tex, see
1807also <URL:http://www.python.org/doc/>). Realize that for the Python
1808run-time system, there isn't a whole lot of difference between C and
1809C++ -- so the strategy to build a new Python type around a C structure
1810(pointer) type will also work for C++ objects.
1811
1812Automatic generation of interfaces between Python and C++ is still at
1813the horizon -- parsing C++ header files requires an almost complete
1814C++ parser, and many features aren't easily translated from C++ to
1815Python: certain forms of operator oveloading, function overloading
1816(best approached by a varargs function which explicitly type-checks
1817its arguments), and reference arguments are just a number of features
1818that are hard to translate correctly if at all.
1819
1820The hardest problem is to transparently translate the C++ class
1821hierarchy to Python, so that Python programs derive classes from C++
1822classes. Given suitable constraints, this may be possible, but it
1823would require more space than I have in this FAQ to explain how.
1824In any case, you can get quite a bit done without this, using just the
1825existing classes from Python.
1826
1827If this all seems rather daunting, that may be because it is -- C++
1828isn't exactly a baby to handle without gloves! However, people have
1829accomplished amazing feats of interfacing between Python and C++, and
1830a detailed question posted to the Python list is likely to elicit some
1831interesting and useful responses.
1832
Guido van Rossuma7925f11994-01-26 10:20:16 +00001833
18346. Python's design
1835==================
1836
Guido van Rossum8727df41996-09-09 15:16:39 +000018376.1. Q. Why isn't there a switch or case statement in Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +00001838
1839A. You can do this easily enough with a sequence of
1840if... elif... elif... else. There have been some proposals for switch
Guido van Rossum061f1821994-10-06 16:03:45 +00001841statement syntax, but there is no consensus (yet) on whether and how
Guido van Rossuma7925f11994-01-26 10:20:16 +00001842to do range tests.
1843
Guido van Rossum8727df41996-09-09 15:16:39 +000018446.2. Q. Why does Python use indentation for grouping of statements?
Guido van Rossumc50158e1994-05-31 09:18:50 +00001845
1846A. Basically I believe that using indentation for grouping is
1847extremely elegant and contributes a lot to the clarity of the average
1848Python program. Most people learn to love this feature after a while.
1849Some arguments for it:
1850
1851- Since there are no begin/end brackets there cannot be a disagreement
1852between grouping perceived by the parser and the human reader. I
1853remember long ago seeing a C fragment like this:
1854
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001855 if (x <= y)
1856 x++;
1857 y--;
1858 z++;
Guido van Rossumc50158e1994-05-31 09:18:50 +00001859
1860and staring a long time at it wondering why y was being decremented
1861even for x > y... (And I wasn't a C newbie then either.)
1862
Guido van Rossum7be34a81995-05-31 15:17:12 +00001863- Since there are no begin/end brackets, Python is much less prone to
1864coding-style conflicts. In C there are loads of different ways to
1865place the braces (including the choice whether to place braces around
1866single statements in certain cases, for consistency). If you're used
1867to reading (and writing) code that uses one style, you will feel at
1868least slightly uneasy when reading (or being required to write)
1869another style.
Guido van Rossumc50158e1994-05-31 09:18:50 +00001870
1871- Many coding styles place begin/end brackets on a line by themself.
1872This makes programs considerably longer and wastes valuable screen
1873space, making it harder to get a good overview over a program.
1874Ideally, a function should fit on one basic tty screen (say, 20
1875lines). 20 lines of Python are worth a LOT more than 20 lines of C.
1876This is not solely due to the lack of begin/end brackets (the lack of
1877declarations also helps, and the powerful operations of course), but
1878it certainly helps!
1879
Guido van Rossum8727df41996-09-09 15:16:39 +000018806.3. Q. Why are Python strings immutable?
Guido van Rossum3de27361994-07-25 14:19:33 +00001881
1882A. There are two advantages. One is performance: knowing that a
1883string is immutable makes it easy to lay it out at construction time
1884-- fixed and unchanging storage requirements. (This is also one of
Guido van Rossum061f1821994-10-06 16:03:45 +00001885the reasons for the distinction between tuples and lists.) The
Guido van Rossum3de27361994-07-25 14:19:33 +00001886other is that strings in Python are considered as "elemental" as
1887numbers. No amount of activity will change the value 8 to anything
1888else, and in Python, no amount of activity will change the string
1889"eight" to anything else. (Adapted from Jim Roskind)
1890
Guido van Rossum8727df41996-09-09 15:16:39 +000018916.4. Q. Why don't strings have methods like index() or sort(), like
Guido van Rossum3de27361994-07-25 14:19:33 +00001892lists?
1893
1894A. Good question. Strings currently don't have methods at all
1895(likewise tuples and numbers). Long ago, it seemed unnecessary to
1896implement any of these functions in C, so a standard library module
1897"string" written in Python was created that performs string related
1898operations. Since then, the cry for performance has moved most of
1899them into the built-in module strop (this is imported by module
Guido van Rossumf8c76d01994-08-17 12:19:53 +00001900string, which is still the preferred interface, without loss of
Guido van Rossum3de27361994-07-25 14:19:33 +00001901performance except during initialization). Some of these functions
1902(e.g. index()) could easily be implemented as string methods instead,
1903but others (e.g. sort()) can't, since their interface prescribes that
1904they modify the object, while strings are immutable (see the previous
1905question).
1906
Guido van Rossum8727df41996-09-09 15:16:39 +000019076.5. Q. Why does Python use methods for some functionality
Guido van Rossum3de27361994-07-25 14:19:33 +00001908(e.g. list.index()) but functions for other (e.g. len(list))?
1909
1910A. Functions are used for those operations that are generic for a
1911group of types and which should work even for objects that don't have
1912methods at all (e.g. numbers, strings, tuples). Also, implementing
1913len(), max(), min() as a built-in function is actually less code than
1914implementing them as methods for each type. One can quibble about
1915individual cases but it's really too late to change such things
1916fundamentally now.
1917
Guido van Rossum8727df41996-09-09 15:16:39 +000019186.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 +00001919files)?
1920
1921A. This is caused by the relatively late addition of (user-defined)
1922classes to the language -- the implementation framework doesn't easily
1923allow it. See the answer to question 4.2 for a work-around. This
1924*may* be fixed in the (distant) future.
1925
Guido van Rossum8727df41996-09-09 15:16:39 +000019266.7. Q. Why must 'self' be declared and used explicitly in method
Guido van Rossum3de27361994-07-25 14:19:33 +00001927definitions and calls?
1928
1929A. By asking this question you reveal your C++ background. :-)
1930When I added classes, this was (again) the simplest way of
1931implementing methods without too many changes to the interpreter. I
1932borrowed the idea from Modula-3. It turns out to be very useful, for
1933a variety of reasons.
1934
1935First, it makes it more obvious that you are using a method or
1936instance attribute instead of a local variable. Reading "self.x" or
1937"self.meth()" makes it absolutely clear that an instance variable or
1938method is used even if you don't know the class definition by heart.
1939In C++, you can sort of tell by the lack of a local variable
Guido van Rossum061f1821994-10-06 16:03:45 +00001940declaration (assuming globals are rare or easily recognizable) -- but
Guido van Rossum3de27361994-07-25 14:19:33 +00001941in Python, there are no local variable declarations, so you'd have to
1942look up the class definition to be sure.
1943
1944Second, it means that no special syntax is necessary if you want to
1945explicitly reference or call the method from a particular class. In
1946C++, if you want to use a method from base class that is overridden in
1947a derived class, you have to use the :: operator -- in Python you can
1948write baseclass.methodname(self, <argument list>). This is
1949particularly useful for __init__() methods, and in general in cases
1950where a derived class method wants to extend the base class method of
1951the same name and thus has to call the base class method somehow.
1952
1953Lastly, for instance variables, it solves a syntactic problem with
1954assignment: since local variables in Python are (by definition!) those
1955variables to which a value assigned in a function body (and that
1956aren't explicitly declared global), there has to be some way to tell
1957the interpreter that an assignment was meant to assign to an instance
1958variable instead of to a local variable, and it should preferably be
1959syntactic (for efficiency reasons). C++ does this through
1960declarations, but Python doesn't have declarations and it would be a
1961pity having to introduce them just for this purpose. Using the
1962explicit "self.var" solves this nicely. Similarly, for using instance
1963variables, having to write "self.var" means that references to
1964unqualified names inside a method don't have to search the instance's
1965directories.
1966
Guido van Rossum8727df41996-09-09 15:16:39 +000019676.8. Q. Can't you emulate threads in the interpreter instead of
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001968relying on an OS-specific thread implementation?
1969
1970A. Unfortunately, the interpreter pushes at least one C stack frame
1971for each Python stack frame. Also, extensions can call back into
1972Python at almost random moments. Therefore a complete threads
1973implementation requires thread support for C.
1974
Guido van Rossum8727df41996-09-09 15:16:39 +000019756.9. Q. Why can't lambda forms contain statements?
Guido van Rossum061f1821994-10-06 16:03:45 +00001976
1977A. Python lambda forms cannot contain statements because Python's
Guido van Rossum796b2591995-01-20 23:05:52 +00001978syntactic framework can't handle statements nested inside expressions.
Guido van Rossum061f1821994-10-06 16:03:45 +00001979
1980However, in Python, this is not a serious problem. Unlike lambda
1981forms in other languages, where they add functionality, Python lambdas
1982are only a shorthand notation if you're too lazy to define a function.
1983
1984Functions are already first class objects in Python, and can be
1985declared in a local scope. Therefore the only advantage of using a
1986lambda form instead of a locally-defined function is that you'll have
1987to invent a name for the function -- but that's just a local variable
1988to which the function object (which is exactly the same type of object
1989that a lambda form yields) is assigned!
1990
Guido van Rossum8727df41996-09-09 15:16:39 +000019916.10. Q. Why don't lambdas have access to variables defined in the
1992containing scope?
1993
Guido van Rossumc458e941996-09-11 15:43:13 +00001994A. Because they are implemented as ordinary functions.
1995See question 4.5 above.
Guido van Rossum8727df41996-09-09 15:16:39 +00001996
19976.11. Q. Why can't recursive functions be defined inside other functions?
1998
1999A. See question 4.5 above.
2000
Guido van Rossum061f1821994-10-06 16:03:45 +000020016.12. Q. Why is there no more efficient way of iterating over a dictionary
2002than first constructing the list of keys()?
2003
2004A. Have you tried it? I bet it's fast enough for your purposes! In
2005most cases such a list takes only a few percent of the space occupied
2006by the dictionary -- it needs only 4 bytes (the size of a pointer) per
2007key -- a dictionary costs 8 bytes per key plus between 30 and 70
2008percent hash table overhead, plus the space for the keys and values --
2009by necessity all keys are unique objects and a string object (the most
2010common key type) costs at least 18 bytes plus the length of the
2011string. Add to that the values contained in the dictionary, and you
2012see that 4 bytes more per item really isn't that much more memory...
2013
2014A call to dict.keys() makes one fast scan over the dictionary
2015(internally, the iteration function does exist) copying the pointers
2016to the key objects into a pre-allocated list object of the right size.
2017The iteration time isn't lost (since you'll have to iterate anyway --
2018unless in the majority of cases your loop terminates very prematurely
2019(which I doubt since you're getting the keys in random order).
2020
2021I don't expose the dictionary iteration operation to Python
2022programmers because the dictionary shouldn't be modified during the
2023entire iteration -- if it is, there's a very small chance that the
2024dictionary is reorganized because the hash table becomes too full, and
2025then the iteration may miss some items and see others twice. Exactly
2026because this only occurs rarely, it would lead to hidden bugs in
2027programs: it's easy never to have it happen during test runs if you
2028only insert or delete a few items per iteration -- but your users will
2029surely hit upon it sooner or later.
2030
Guido van Rossumb0a2ce51995-08-28 19:41:35 +000020316.13. Q. Can Python be compiled to machine code, C or some other language?
2032
2033A. Not easily. Python's high level data types, dynamic typing of
2034objects and run-time invocation of the interpreter (using eval() or
2035exec) together mean that a "compiled" Python program would probably
2036consist mostly of calls into the Python run-time system, even for
2037seemingly simple operations like "x+1". Thus, the performance gain
2038would probably be minimal.
2039
2040Internally, Python source code is always translated into a "virtual
2041machine code" or "byte code" representation before it is interpreted
2042(by the "Python virtual machine" or "bytecode interpreter"). In order
2043to avoid the overhead of parsing and translating modules that rarely
2044change over and over again, this byte code is written on a file whose
2045name ends in ".pyc" whenever a module is parsed (from a file whose
2046name ends in ".py"). When the corresponding .py file is changed, it
2047is parsed and translated again and the .pyc file is rewritten. There
2048is no performance difference once the .pyc file has been loaded (the
2049bytecode read from the .pyc file is exactly the same as the bytecode
2050created by direct translation). The only difference is that loading
2051code from a .pyc file is faster than parsing and translating a .py
2052file, so the presence of precompiled .pyc files will generally improve
2053start-up time of Python scripts. If desired, the Lib/compileall.py
2054module/script can be used to force creation of valid .pyc files for a
2055given set of modules.
2056
2057If you are looking for a way to translate Python programs in order to
2058distribute them in binary form, without the need to distribute the
2059interpreter and library as well, have a look at the freeze.py script
2060in the Tools/freeze directory. This creates a single binary file
2061incorporating your program, the Python interpreter, and those parts of
2062the Python library that are needed by your program. Of course, the
2063resulting binary will only run on the same type of platform as that
2064used to create it.
2065
2066Hints for proper usage of freeze.py:
2067
2068- the script must be in a file whose name ends in .py
2069
2070- you must have installed Python fully:
2071
Guido van Rossume7d92d51995-10-11 18:06:54 +00002072 make install
2073 make libinstall
2074 make inclinstall
2075 make libainstall
Guido van Rossumb0a2ce51995-08-28 19:41:35 +00002076
Guido van Rossum05151e01995-09-28 13:24:46 +000020776.14. Q. Why doesn't Python use proper garbage collection?
2078
2079A. It's looking less and less likely that Python will ever get
2080"automatic" garbage collection (GC). For one thing, unless this were
2081added to C as a standard feature, it's a portability pain in the ass.
2082And yes, I know about the Xerox library. It has bits of assembler
2083code for *most* *common* platforms. Not for all. And although it is
2084mostly transparent, it isn't completely transparent (when I once
2085linked Python with it, it dumped core).
2086
2087"Proper" GC also becomes a problem when Python gets embedded into
2088other applications. While in a stand-alone Python it may be fine to
2089replace the standard malloc() and free() with versions provided by the
2090GC library, an application embedding Python may want to have its *own*
2091substitute for malloc() and free(), and may not want Python's. Right
2092now, Python works with anything that implements malloc() and free()
2093properly.
2094
2095Besides, the predictability of destructor calls in Python is kind of
2096attractive. With GC, the following code (which is fine in current
2097Python) will run out of file descriptors long before it runs out of
2098memory:
2099
Guido van Rossume7d92d51995-10-11 18:06:54 +00002100 for file in <very long list of files>:
2101 f = open(file)
2102 c = file.read(1)
Guido van Rossum05151e01995-09-28 13:24:46 +00002103
2104Using the current reference counting and destructor scheme, each new
2105assignment to f closes the previous file. Using GC, this is not
2106guaranteed. Sure, you can think of ways to fix this. But it's not
2107off-the-shelf technology.
2108
Guido van Rossuma7925f11994-01-26 10:20:16 +00002109
21107. Using Python on non-UNIX platforms
2111=====================================
2112
Guido van Rossum91f60831994-02-15 15:52:27 +000021137.1. Q. Is there a Mac version of Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +00002114
Guido van Rossume530c581995-04-10 12:32:16 +00002115A. Yes, see the "mac" subdirectory of the distribution sites,
Guido van Rossum7be34a81995-05-31 15:17:12 +00002116e.g. <URL:ftp://ftp.python.org/pub/python/mac/>.
Guido van Rossuma7925f11994-01-26 10:20:16 +00002117
Guido van Rossumc458e941996-09-11 15:43:13 +000021187.2. Q. Are there DOS and Windows versions of Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +00002119
Guido van Rossumc458e941996-09-11 15:43:13 +00002120A. Yes. There is a plethora of not-always-compatible versions. See
2121the "pythonwin", "wpy", "nt" and "pc" subdirectories of the
2122distribution sites. A quick comparison:
Guido van Rossuma7925f11994-01-26 10:20:16 +00002123
Guido van Rossumc458e941996-09-11 15:43:13 +00002124PythonWin: Extensive support for the 32-bit native Windows API and GUI
2125building using MFC. Windows NT and Windows 95 only (and Windows
21263.1(1) using win32s, until Microsoft stops supporting it :-( ).
2127<URL:ftp://ftp.python.org/pub/python/pythonwin/>.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00002128
Guido van Rossumc458e941996-09-11 15:43:13 +00002129WPY: Ports to DOS, Windows 3.1(1), Windows 95, Windows NT and OS/2.
2130Also contains a GUI package that offers portability between Windows
2131(not DOS) and Unix, and native look and feel on both.
2132<URL:ftp://ftp.python.org/pub/python/wpy/>.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00002133
Guido van Rossumc458e941996-09-11 15:43:13 +00002134NT: Basic ports built straight from the 1.4 distribution for Windows
213595 and Windows NT. This will eventually provide core support for
2136both PythonWin and WPY on all 32-bit Microsoft platforms.
Guido van Rossum3fc9d731995-07-25 15:10:56 +00002137<URL:ftp://ftp.python.org/pub/python/nt/>.
2138
Guido van Rossumc458e941996-09-11 15:43:13 +00002139PC: Old, unsupported ports to DOS, Windows 3.1(1) and OS/2.
2140<URL:ftp://ftp.python.org/pub/python/pc/>.
Guido van Rossum91f60831994-02-15 15:52:27 +00002141
Guido van Rossumc458e941996-09-11 15:43:13 +000021427.3. Q. Is there an OS/2 version of Python?
Guido van Rossum061f1821994-10-06 16:03:45 +00002143
Guido van Rossumc458e941996-09-11 15:43:13 +00002144A. Yes, see the "pc" and "wpy" subdirectory of the distribution sites
2145(see above).
Guido van Rossum061f1821994-10-06 16:03:45 +00002146
Guido van Rossumc458e941996-09-11 15:43:13 +000021477.4. Q. Is there a VMS version of Python?
Guido van Rossume530c581995-04-10 12:32:16 +00002148
Guido van Rossum3fc9d731995-07-25 15:10:56 +00002149A. Donn Cave <donn@cac.washington.edu> did a partial port. The
2150results of his efforts are on public display in
Guido van Rossumbf8e7d51995-08-28 03:09:13 +00002151<<URL:ftp://ftp.python.org/pub/python/contrib/vms.tar.gz/>. Someone
2152else is working on a more complete port, for details watch the list.
Guido van Rossume530c581995-04-10 12:32:16 +00002153
Guido van Rossumc458e941996-09-11 15:43:13 +000021547.5. Q. What about IBM mainframes, or other non-UNIX platforms?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00002155
Guido van Rossumb0a2ce51995-08-28 19:41:35 +00002156A. I haven't heard about these, except I remember hearing about an
2157OS/9 port and a port to Vxworks (both operating systems for embedded
2158systems). If you're interested in any of this, go directly to the
2159newsgroup and ask there, you may find exactly what you need. For
2160example, a port to MPE/iX 5.0 on HP3000 computers was just announced,
2161see <URL:http://www.allegro.com/software/>.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00002162
Guido van Rossumc458e941996-09-11 15:43:13 +000021637.6. Q. Where are the source or Makefiles for the non-UNIX versions?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00002164
Guido van Rossume530c581995-04-10 12:32:16 +00002165A. The standard sources can (almost) be used. Additional sources can
2166be found in the platform-specific subdirectories of the distribution.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00002167
Guido van Rossumc458e941996-09-11 15:43:13 +000021687.7. Q. What is the status and support for the non-UNIX versions?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00002169
2170A. I don't have access to most of these platforms, so in general I am
2171dependent on material submitted by volunteers(*). However I strive to
2172integrate all changes needed to get it to compile on a particular
2173platform back into the standard sources, so porting of the next
2174version to the various non-UNIX platforms should be easy.
2175
Guido van Rossuma6c707c1995-01-02 17:32:28 +00002176(*) For the Macintosh, that volunteer is me, with help from Jack
2177Jansen <jack@cwi.nl>.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00002178
Guido van Rossumc458e941996-09-11 15:43:13 +000021797.8. Q. I have a PC version but it appears to be only a binary.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00002180Where's the library?
Guido van Rossuma7925f11994-01-26 10:20:16 +00002181
2182A. You still need to copy the files from the distribution directory
Guido van Rossum91f60831994-02-15 15:52:27 +00002183"python/Lib" to your system. If you don't have the full distribution,
Guido van Rossume530c581995-04-10 12:32:16 +00002184you can get the file lib<version>.tar.gz from most ftp sites carrying
2185Python; this is a subset of the distribution containing just those
Guido van Rossum7be34a81995-05-31 15:17:12 +00002186files, e.g. <URL:ftp://ftp.python.org/pub/python/src/lib1.1.tar.gz>.
Guido van Rossuma7925f11994-01-26 10:20:16 +00002187
Guido van Rossum5333c5d1994-04-11 11:06:22 +00002188Once you have installed the library, you need to point sys.path to it.
2189Assuming the library is in C:\misc\python\lib, the following commands
2190will point your Python interpreter to it (note the doubled backslashes
2191-- you can also use single forward slashes instead):
2192
Guido van Rossuma6c707c1995-01-02 17:32:28 +00002193 >>> import sys
2194 >>> sys.path.insert(0, 'C:\\misc\\python\\lib')
2195 >>>
Guido van Rossum5333c5d1994-04-11 11:06:22 +00002196
2197For a more permanent effect, set the environment variable PYTHONPATH,
2198as follows (talking to a DOS prompt):
2199
Guido van Rossuma6c707c1995-01-02 17:32:28 +00002200 C> SET PYTHONPATH=C:\misc\python\lib
Guido van Rossum5333c5d1994-04-11 11:06:22 +00002201
Guido van Rossumc458e941996-09-11 15:43:13 +000022027.9. Q. Where's the documentation for the Mac or PC version?
Guido van Rossuma7925f11994-01-26 10:20:16 +00002203
Guido van Rossume530c581995-04-10 12:32:16 +00002204A. The documentation for the Unix version also applies to the Mac and
2205PC versions. Where applicable, differences are indicated in the text.
Guido van Rossuma7925f11994-01-26 10:20:16 +00002206
Guido van Rossumc458e941996-09-11 15:43:13 +000022077.10. Q. The Mac (PC) version doesn't seem to have any facilities for
Guido van Rossum91f60831994-02-15 15:52:27 +00002208creating or editing programs apart from entering it interactively, and
2209there seems to be no way to save code that was entered interactively.
2210How do I create a Python program on the Mac (PC)?
Guido van Rossuma7925f11994-01-26 10:20:16 +00002211
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00002212A. Use an external editor. On the Mac, BBEdit seems to be a popular
2213no-frills text editor. I work like this: start the interpreter; edit
2214a module file using BBedit; import and test it in the interpreter;
2215edit again in BBedit; then use the built-in function reload() to
2216re-read the imported module; etc.
Guido van Rossum5333c5d1994-04-11 11:06:22 +00002217
2218Regarding the same question for the PC, Kurt Wm. Hemr writes: "While
2219anyone with a pulse could certainly figure out how to do the same on
2220MS-Windows, I would recommend the NotGNU Emacs clone for MS-Windows.
2221Not only can you easily resave and "reload()" from Python after making
2222changes, but since WinNot auto-copies to the clipboard any text you
2223select, you can simply select the entire procedure (function) which
2224you changed in WinNot, switch to QWPython, and shift-ins to reenter
2225the changed program unit."