blob: ca25b796f752805b2a7c55d68799147d41d693b5 [file] [log] [blame]
Guido van Rossuma7925f11994-01-26 10:20:16 +00001Subject: FAQ: Python -- an object-oriented language
Guido van Rossum5333c5d1994-04-11 11:06:22 +00002Newsgroups: comp.lang.python,comp.answers,news.answers
3Followup-to: comp.lang.python
Guido van Rossuma7925f11994-01-26 10:20:16 +00004From: guido@cwi.nl (Guido van Rossum)
Guido van Rossum7be34a81995-05-31 15:17:12 +00005Reply-to: guido@cnri.reston.va.us (Guido van Rossum)
Guido van Rossumb0a2ce51995-08-28 19:41:35 +00006Expires: Sun, 1 Oct 1995 00:00:00 GMT
Guido van Rossumbf8e7d51995-08-28 03:09:13 +00007Supersedes: <DCA29p.A9A@cwi.nl>
Guido van Rossuma7925f11994-01-26 10:20:16 +00008Approved: news-answers-request@MIT.Edu
9
10Archive-name: python-faq/part1
Guido van Rossum07779351995-02-07 16:59:56 +000011Submitted-by: Guido van Rossum <guido@cwi.nl>
Guido van Rossumb0a2ce51995-08-28 19:41:35 +000012Version: 1.27
13Last-modified: 28 August 1995
Guido van Rossuma7925f11994-01-26 10:20:16 +000014
15This article contains answers to Frequently Asked Questions about
16Python (an object-oriented interpreted programming language -- see
17the answer to question 1.1 for a short overview).
18
Guido van Rossuma6c707c1995-01-02 17:32:28 +000019Copyright 1993-1995 Guido van Rossum. Unchanged electronic
Guido van Rossuma7925f11994-01-26 10:20:16 +000020redistribution of this FAQ is allowed. Printed redistribution only
21with permission of the author. No warranties.
22
23Author's address:
Guido van Rossumb0a2ce51995-08-28 19:41:35 +000024 Guido van Rossum
25 C.N.R.I.
26 1895 Preston White Drive
27 Reston, VA 22091
28 U.S.A.
29Email: <guido@python.org>, <guido@cnri.reston.va.us>, <guido@cwi.nl>
Guido van Rossuma7925f11994-01-26 10:20:16 +000030
31The latest version of this FAQ is available by anonymous ftp from
Guido van Rossum7be34a81995-05-31 15:17:12 +000032<URL:ftp://ftp.python.org/pub/python/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?
77 1.14. Q. Are there other ftp sites that carry Python related material?
78 1.15. Q. Are there copyright restrictions on the use of Python?
Guido van Rossum7be34a81995-05-31 15:17:12 +000079 1.16. Q. Why was Python created in the first place?
80 1.17. Q. What happened to Tim Peters (the author of python-mode.el, and many
81 entertaining and enlightening pieces of Python email)?
Guido van Rossuma7925f11994-01-26 10:20:16 +000082
83 2. Python in the real world
84 2.1. Q. How many people are using Python?
85 2.2. Q. Have any significant projects been done in Python?
86 2.3. Q. Are there any commercial projects going on using Python?
Guido van Rossum95f61a71994-01-26 17:23:37 +000087 2.4. Q. How stable is Python?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000088 2.5. Q. When will the next version be released?
89 2.6. Q. What new developments are expected for Python in the future?
90 2.7. Q. Is it reasonable to propose incompatible changes to Python?
Guido van Rossumb0a2ce51995-08-28 19:41:35 +000091 2.8. Q. What is the future of Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +000092
Guido van Rossumb0a2ce51995-08-28 19:41:35 +000093 3. Building Python and Other Known Bugs
Guido van Rossum91f60831994-02-15 15:52:27 +000094 3.1. Q. Is there a test set?
95 3.2. Q. When running the test set, I get complaints about floating point
Guido van Rossuma7925f11994-01-26 10:20:16 +000096 operations, but when playing with floating point operations I cannot
97 find anything wrong with them.
Guido van Rossum7be34a81995-05-31 15:17:12 +000098 3.3. Q. Link errors after rerunning the configure script.
99 3.4. Q. The python interpreter complains about options passed to a
Guido van Rossum24349991994-02-02 14:12:45 +0000100 script (after the script name).
Guido van Rossum7be34a81995-05-31 15:17:12 +0000101 3.5. Q. When building on the SGI, make tries to run python to create
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000102 glmodule.c, but python hasn't been built or installed yet.
Guido van Rossum7be34a81995-05-31 15:17:12 +0000103 3.6. Q. Python built with gcc for the DEC Alpha doesn't work.
104 3.7. Q. I use VPATH but some targets are built in the source directory.
105 3.8. Q. Trouble building or linking with the GNU readline library.
106 3.9. Q. Trouble building Python on Linux.
107 3.10. Q. Trouble with prototypes on Ultrix.
108 3.11. Q. Trouble with posix.listdir on NeXTSTEP 3.2.
109 3.12. Q. Other trouble building Python on platform X.
110 3.13. Q. How to configure dynamic loading on Lixux.
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000111 3.14. Q. Under Solaris 2.x, using GCC, how do I use shared libraries?
112 3.15. Q. Errors when linking with a shared library containing C++ code.
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000113 3.16. Q. Tk menus don't work properly in Python 1.2
114 3.17. Q. I built with tkintermodule.c enabled but get "Tkinter not found"
115 3.18. Q. I built with Tk 4.0 but Tkinter complains about the Tk version
116 3.19. Q. Tk doesn't work right on DEC Alpha
Guido van Rossuma7925f11994-01-26 10:20:16 +0000117
118 4. Programming in Python
Guido van Rossum24349991994-02-02 14:12:45 +0000119 4.1. Q. Is there a source code level debugger with breakpoints, step,
120 etc.?
121 4.2. Q. Can I create an object class with some methods implemented in
Guido van Rossuma7925f11994-01-26 10:20:16 +0000122 C and others in Python (e.g. through inheritance)? (Also phrased as:
123 Can I use a built-in type as base class?)
Guido van Rossum24349991994-02-02 14:12:45 +0000124 4.3. Q. Is there a curses/termcap package for Python?
125 4.4. Q. Is there an equivalent to C's onexit() in Python?
126 4.5. Q. When I define a function nested inside another function, the
Guido van Rossuma7925f11994-01-26 10:20:16 +0000127 nested function seemingly can't access the local variables of the
128 outer function. What is going on? How do I pass local data to a
129 nested function?
Guido van Rossum24349991994-02-02 14:12:45 +0000130 4.6. Q. How do I iterate over a sequence in reverse order?
131 4.7. Q. My program is too slow. How do I speed it up?
132 4.8. Q. When I have imported a module, then edit it, and import it
Guido van Rossuma7925f11994-01-26 10:20:16 +0000133 again (into the same Python process), the changes don't seem to take
134 place. What is going on?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000135 4.9. Q. How do I find the current module name?
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000136 4.10. Q. I have a module in which I want to execute some extra code
137 when it is run as a script. How do I find out whether I am running as
138 a script?
139 4.11. Q. I try to run a program from the Demo directory but it fails
140 with ImportError: No module named ...; what gives?
Guido van Rossum061f1821994-10-06 16:03:45 +0000141 4.12. Q. I have successfully built Python with STDWIN but it can't
142 find some modules (e.g. stdwinevents).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000143 4.13. Q. What GUI toolkits exist for Python?
Guido van Rossum061f1821994-10-06 16:03:45 +0000144 4.14. Q. Are there any interfaces to database packages in Python?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000145 4.15. Q. Is it possible to write obfuscated one-liners in Python?
146 4.16. Q. Is there an equivalent of C's "?:" ternary operator?
147 4.17. Q. My class defines __del__ but it is not called when I delete the
148 object.
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000149 4.18. Q. How do I change the shell environment for programs called
150 using os.popen() or os.system()? Changing os.environ doesn't work.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000151 4.19. Q. What is a class?
152 4.20. Q. What is a method?
153 4.21. Q. What is self?
154 4.22. Q. What is a unbound method?
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000155 4.23. Q. How do I call a method defined in a base class from a derived
156 class that overrides it?
157 4.24. Q. How do I call a method from a base class without using the
158 name of the base class?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000159 4.25. Q. How can I organize my code to make it easier to change the base
160 class?
161 4.26. Q. How can I find the methods or attributes of an object?
Guido van Rossum061f1821994-10-06 16:03:45 +0000162 4.27. Q. I can't seem to use os.read() on a pipe created with os.popen().
163 4.28. Q. How can I create a stand-alone binary from a Python script?
Guido van Rossum7be34a81995-05-31 15:17:12 +0000164 4.29. Q. What WWW tools are there for Python?
165 4.30. Q. How do I run a subprocess with pipes connected to both input
Guido van Rossum796b2591995-01-20 23:05:52 +0000166 and output?
Guido van Rossum7be34a81995-05-31 15:17:12 +0000167 4.31. Q. How do I call a function if I have the arguments in a tuple?
168 4.32. Q. How do I enable font-lock-mode for Python in Emacs?
169 4.33. Q. Is there an inverse to the format operator (a la C's scanf())?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000170
171 5. Extending Python
172 5.1. Q. Can I create my own functions in C?
173 5.2. Q. Can I create my own functions in C++?
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000174 5.3. Q. How can I execute arbitrary Python statements from C?
175 5.4. Q. How can I evaluate an arbitrary Python expression from C?
176 5.5. Q. How do I extract C values from a Python object?
177 5.6. Q. How do I use mkvalue() to create a tuple of arbitrary length?
Guido van Rossum796b2591995-01-20 23:05:52 +0000178 5.7. Q. What happened to mktuple(), featured in an example in the
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000179 Extensions manual?
180 5.8. Q. How do I call an object's method from C?
181 5.9. Q. How do I catch the output from print_error()?
182 5.10. Q. How do I access a module written in Python from C?
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000183 5.11. Q. How do I interface to C++ objects from Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000184
185 6. Python's design
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000186 6.1. Q. Why isn't there a generic copying operation for objects in Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000187 6.2. Q. Why isn't there a generic way to implement persistent objects
188 in Python? (Persistent == automatically saved to and restored from
189 disk.)
190 6.3. Q. Why isn't there a switch or case statement in Python?
Guido van Rossumc50158e1994-05-31 09:18:50 +0000191 6.4. Q. Why does Python use indentation for grouping of statements?
Guido van Rossum3de27361994-07-25 14:19:33 +0000192 6.5. Q. Why are Python strings immutable?
193 6.6. Q. Why don't strings have methods like index() or sort(), like
194 lists?
195 6.7. Q. Why does Python use methods for some functionality
196 (e.g. list.index()) but functions for other (e.g. len(list))?
197 6.8. Q. Why can't I derive a class from built-in types (e.g. lists or
198 files)?
199 6.9. Q. Why must 'self' be declared and used explicitly in method
200 definitions and calls?
Guido van Rossum061f1821994-10-06 16:03:45 +0000201 6.10. Q. Can't you emulate threads in the interpreter instead of
202 relying on an OS-specific thread implementation?
203 6.11. Q. Why can't lambda forms contain statements?
204 6.12. Q. Why is there no more efficient way of iterating over a dictionary
205 than first constructing the list of keys()?
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000206 6.13. Q. Can Python be compiled to machine code, C or some other language?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000207
208 7. Using Python on non-UNIX platforms
Guido van Rossum91f60831994-02-15 15:52:27 +0000209 7.1. Q. Is there a Mac version of Python?
210 7.2. Q. Is there a DOS version of Python?
Guido van Rossume530c581995-04-10 12:32:16 +0000211 7.3. Q. Is there a Windows 3.1(1) version of Python?
Guido van Rossum91f60831994-02-15 15:52:27 +0000212 7.4. Q. Is there a Windows NT version of Python?
Guido van Rossume530c581995-04-10 12:32:16 +0000213 7.5. Q. Is there a Windows 95 version of Python?
214 7.6. Q. Is there an OS/2 version of Python?
215 7.7. Q. Is there a VMS version of Python?
Guido van Rossumbf8e7d51995-08-28 03:09:13 +0000216 7.8. Q. What about IBM mainframes, or other non-UNIX platforms?
Guido van Rossume530c581995-04-10 12:32:16 +0000217 7.9. Q. Where are the source or Makefiles for the non-UNIX versions?
218 7.10. Q. What is the status and support for the non-UNIX versions?
219 7.11. Q. I have a PC version but it appears to be only a binary.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000220 Where's the library?
Guido van Rossume530c581995-04-10 12:32:16 +0000221 7.12. Q. Where's the documentation for the Mac or PC version?
222 7.13. Q. The Mac (PC) version doesn't seem to have any facilities for
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000223 creating or editing programs apart from entering it interactively, and
224 there seems to be no way to save code that was entered interactively.
225 How do I create a Python program on the Mac (PC)?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000226
227To find a particular question, search for the question number followed
228by a dot, a space, and a Q at the beginning of a line (e.g. to find
229question 4.2 in vi, type /^4\.2\. Q/).
230
231
2321. General information and availability
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000233 =======================================
Guido van Rossuma7925f11994-01-26 10:20:16 +0000234
2351.1. Q. What is Python?
236
237A. Python is an interpreted, interactive, object-oriented programming
238language. It incorporates modules, exceptions, dynamic typing, very
239high level dynamic data types, and classes. Python combines
240remarkable power with very clear syntax. It has interfaces to many
241system calls and libraries, as well as to various window systems, and
242is extensible in C or C++. It is also usable as an extension language
243for applications that need a programmable interface. Finally, Python
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000244is portable: it runs on many brands of UNIX, on the Mac, and on PCs
245under MS-DOS, Windows, Windows NT, and OS/2.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000246
247To find out more, the best thing to do is to start reading the
248tutorial from the documentation set (see a few questions further
249down).
250
2511.2. Q. Why is it called Python?
252
Guido van Rossum796b2591995-01-20 23:05:52 +0000253A. Apart from being a computer scientist, I'm also a fan of "Monty
Guido van Rossuma7925f11994-01-26 10:20:16 +0000254Python's Flying Circus" (a BBC comedy series from the seventies, in
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000255the -- unlikely -- case you didn't know). It occurred to me one day
256that I needed a name that was short, unique, and slightly mysterious.
257And I happened to be reading some scripts from the series at the
258time... So then I decided to call my language Python. But Python is
259not a joke. And don't you associate it with dangerous reptiles
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000260either! (If you need an icon, use an image of the 16-ton weight from
261the TV series or of a can of SPAM :-)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000262
2631.3. Q. How do I obtain a copy of the Python source?
264
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000265A. The latest complete Python source distribution is always available
266by anonymous ftp, e.g.
Guido van Rossum7be34a81995-05-31 15:17:12 +0000267<URL:ftp://ftp.python.org/pub/python/src/python1.2.tar.gz>. It is a
Guido van Rossume530c581995-04-10 12:32:16 +0000268gzipped tar file containing the complete C source, LaTeX
269documentation, Python library modules, example programs, and several
270useful pieces of freely distributable software. This will compile and
271run out of the box on most UNIX platforms. (See section 7 for
272non-UNIX information.)
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000273
274Occasionally a set of patches is issued which has to be applied using
275the patch program. These patches are placed in the same directory,
Guido van Rossum7be34a81995-05-31 15:17:12 +0000276e.g. <URL:ftp://ftp.python.org/pub/python/src/patch1.1.1>. (At the time
Guido van Rossume530c581995-04-10 12:32:16 +0000277of writing, no patches exist for 1.2.)
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000278
279An index of said ftp directory can be found in the file INDEX. An
280HTML version of the index can be found in the file index.html,
Guido van Rossum7be34a81995-05-31 15:17:12 +0000281<URL:ftp://ftp.python.org/pub/python/index.html>.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000282
2831.4. Q. How do I get documentation on Python?
284
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000285A. The LaTeX source for the documentation is part of the source
286distribution. If you don't have LaTeX, the latest Python
287documentation set is always available by anonymous ftp, e.g.
Guido van Rossum7be34a81995-05-31 15:17:12 +0000288<URL:ftp://ftp.python.org/pub/python/doc/postscript.tar.gz>. It is a
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000289gzipped tar file containing PostScript files of the reference manual,
Guido van Rossum3de27361994-07-25 14:19:33 +0000290the library manual, and the tutorial. Note that the library manual is
291the most important one of the set, as much of Python's power stems
292from the standard or built-in types, functions and modules, all of
293which are described here. PostScript for a high-level description of
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000294Python is in the file nluug-paper.ps (a separate file on the ftp
295site).
Guido van Rossuma7925f11994-01-26 10:20:16 +0000296
Guido van Rossumc50158e1994-05-31 09:18:50 +00002971.5. Q. Are there other ftp sites that mirror the Python distribution?
298
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000299A. The following anonymous ftp sites keep mirrors of the Python
300distribution:
Guido van Rossuma7925f11994-01-26 10:20:16 +0000301
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000302 <URL:ftp://ftp.cwi.nl/pub/python/>
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000303 <URL:ftp://gatekeeper.dec.com/pub/plan/python/>
304 <URL:ftp://ftp.uu.net/languages/python/>
305 <URL:ftp://ftp.wustl.edu/graphics/graphics/sgi-stuff/python/>
306 <URL:ftp://ftp.funet.fi/pub/languages/python/>
307 <URL:ftp://ftp.sunet.se/pub/lang/python/>
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000308 <URL:ftp://unix.hensa.ac.uk/mirrors/uunet/languages/python/>
309 <URL:ftp://ftp.sterling.com/programming/languages/python/>
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000310 <URL:ftp://ftp.ibp.fr/pub/python/>
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000311 <URL:ftp://ftp.switch.ch/software/sources/python/>
312 <URL:ftp://ftp.informatik.tu-muenchen.de/pub/comp/programming/languages/python/>
Guido van Rossuma7925f11994-01-26 10:20:16 +0000313
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000314Or try archie on the string "python".
Guido van Rossuma7925f11994-01-26 10:20:16 +0000315
Guido van Rossumc50158e1994-05-31 09:18:50 +00003161.6. Q. Is there a newsgroup or mailing list devoted to Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000317
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000318A. There is a newsgroup, comp.lang.python <URL:news:comp.lang.python>,
319and a mailing list. The newsgroup and mailing list are gatewayed into
320each other -- if you can read news it's unnecessary to subscribe to
321the mailing list. Send e-mail to <python-list-request@cwi.nl> to
Guido van Rossume530c581995-04-10 12:32:16 +0000322(un)subscribe to the mailing list. Hypermail archives of (nearly)
323everything posted to the mailing list (and thus the newsgroup) are
324available on our WWW server,
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000325<URL:http://www.cwi.nl/~guido/hypermail/index.html>. The raw archives
326are also available by ftp, e.g.
Guido van Rossum7be34a81995-05-31 15:17:12 +0000327<URL:ftp://ftp.python.org/pub/python/mail/mailinglist.gz>. The
Guido van Rossume530c581995-04-10 12:32:16 +0000328uncompressed versions of these files can be read with the standard
329UNIX Mail program ("Mail -f file") or with nn ("nn file"). To read
Guido van Rossumbf8e7d51995-08-28 03:09:13 +0000330them using MH, you could use "inc -file file". (The archival service
331has stopped archiving new articles around the end of April 1995. I
332hope to revive it on the PSA server www.python.org sometime in the
333future.)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000334
Guido van Rossuma6c707c1995-01-02 17:32:28 +00003351.7. Q. Is there a WWW page devoted to Python?
336
Guido van Rossum7be34a81995-05-31 15:17:12 +0000337A. Yes, <URL:http://www.python.org/> is the official Python home page.
338At the time of writing, this page is not yet completely operational;
339you may have a look at the old Python home page:
340<URL:http://www.cwi.nl/~guido/Python.html> or at the U.S. copy:
341<URL:http://www.python.org/~guido/Python.html>.
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000342
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00003431.8. Q. Is the Python documentation available on the WWW?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000344
Guido van Rossum7be34a81995-05-31 15:17:12 +0000345A. Yes, see <URL:http://www.python.org/> (Python's home page). It
346contains pointers to hypertext versions of the whole documentation set
347(as hypertext, not just PostScript).
Guido van Rossuma7925f11994-01-26 10:20:16 +0000348
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000349If you wish to browse this collection of HTML files on your own
350machine, it is available bundled up by anonymous ftp,
Guido van Rossum7be34a81995-05-31 15:17:12 +0000351e.g. <URL:ftp://ftp.python.org/pub/python/doc/html.tar.gz>.
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000352
353An Emacs-INFO set containing the library manual is also available by
Guido van Rossum7be34a81995-05-31 15:17:12 +0000354ftp, e.g. <URL:ftp://ftp.python.org/pub/python/doc/lib-info.tar.gz>.
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000355
3561.9. Q. Is there a book on Python, or will there be one out soon?
357
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000358A. Mark Lutz is writing a Python book for O'Reilly and Associates, to
359be published early 1996. See the outline (in PostScript):
360<URL:http://www.python.org/workshops/1995-05/outlinep.eps>.
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000361
3621.10. Q. Are there any published articles about Python that I can quote?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000363
364A. So far the only refereed and published article that describes
365Python in some detail is:
366
367 Guido van Rossum and Jelke de Boer, "Interactively Testing Remote
368 Servers Using the Python Programming Language", CWI Quarterly, Volume
369 4, Issue 4 (December 1991), Amsterdam, pp 283-303.
370
371LaTeX source for this paper is available as part of the Python source
372distribution.
373
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00003741.11. Q. Are there short introductory papers or talks on Python?
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000375
376A. A recent high-level description of Python is:
Guido van Rossuma7925f11994-01-26 10:20:16 +0000377
378 Guido van Rossum, "An Introduction to Python for UNIX/C
379 Programmers", in the proceedings of the NLUUG najaarsconferentie
Guido van Rossum061f1821994-10-06 16:03:45 +0000380 1993 (dutch UNIX users group meeting November 1993).
Guido van Rossuma7925f11994-01-26 10:20:16 +0000381
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000382PostScript for this paper and for the slides used for the accompanying
383presentation is available by ftp as
Guido van Rossum7be34a81995-05-31 15:17:12 +0000384<URL:ftp://ftp.python.org/pub/python/doc/nluug-paper.ps> and
385<URL:ftp://ftp.python.org/pub/python/doc/nluug-slides.ps>, respectively.
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000386
387Slides for a talk on Python that I gave at the Usenix Symposium on
388Very High Level Languages in Santa Fe, NM, USA in October 1995 are
Guido van Rossum7be34a81995-05-31 15:17:12 +0000389available as <URL:ftp://ftp.python.org/pub/python/doc/vhll-slides.ps>.
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000390
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00003911.12. Q. How does the Python version numbering scheme work?
Guido van Rossum95f61a71994-01-26 17:23:37 +0000392
393A. Python versions are numbered A.B.C. A is the major version number
394-- it is only incremented for major changes in functionality or source
395structure. B is the minor version number, incremented for less
396earth-shattering changes to a release. C is the patchlevel -- it is
397incremented for each new release. Note that in the past, patches have
398added significant changes; in fact the changeover from 0.9.9 to 1.0.0
399was the first time that either A or B changed!
400
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000401Beta versions have an additional suffix of "-beta-N" for some small
402number N. Note that (for instance) all versions labeled 1.2-beta-N
403*precede* the actual release of 1.2.
404
4051.13. Q. How do I get a beta test version of Python?
406
407A. Write me. I might like you :-) Seriously, anybody who has
408previously used and installed Python can become a beta tester, but I
409expect feedback, so you have to write me first before I divulge the
410secret location of the latest beta release.
411
4121.14. Q. Are there other ftp sites that carry Python related material?
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000413
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000414A. An interesting ftp site for Python users is ftp.markv.com; the
415directory pub/python contains a growing collection of interesting
416Python scripts <URL:ftp://ftp.markv.com/pub/python/>. To submit a
417script for inclusion, place it together with a readme file (with
418extension .readme) in the publicly writable directory
419/incoming/python. This service is maintained by Lance Ellinghaus
420<lance@markv.com>. (I've heard complaints about this service not
421being very responsive -- try at your own risk.)
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000422
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00004231.15. Q. Are there copyright restrictions on the use of Python?
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000424
425A. Hardly. You can do anything you want with the source, as long as
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000426you leave the copyrights in, and display those copyrights in any
427documentation about Python that you produce. Also, don't use the
428author's institute's name in publicity without prior written
429permission, and don't hold them responsible for anything (read the
430actual copyright for a precise legal wording).
431
432In particular, if you honor the copyright rules, it's OK to use Python
433for commercial use, to sell copies of Python in source or binary form,
434or to sell products that enhance Python or incorporate Python (or part
435of it) in some form. I would still like to know about all commercial
436use of Python!
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000437
Guido van Rossum7be34a81995-05-31 15:17:12 +00004381.16. Q. Why was Python created in the first place?
439
440A. Here's a *very* brief summary of what got me started:
441
442- I had extensive experience with implementing an interpreted language
443in the ABC group at CWI, and from working with this group I had
444learned a lot about language design. This is the origin of many
445Python features, including the use of indentation for statement
446grouping and the inclusion of very-high-level data types (although the
447details are all different in Python).
448
449- I had a number of gripes about the ABC language, but also liked many
450of its features. It was impossible to extend the ABC language (or its
451implementation) to remedy my complaints -- in fact its lack of
452extensibility was one of its biggest problems.
453
454- I had some experience with using Modula-2+ and talked with the
455designers of Modula-3 (and read the M3 report). M3 is the origin of
456the syntax and semantics used for exceptions, and some other Python
457features.
458
459- I was working in the Amoeba distributed operating system group at
460CWI. We needed a better way to do system administration than by
461writing either C programs or Bourne shell scripts, since Amoeba had
462its own system call interface which wasn't easily accessible from the
Guido van Rossumbf8e7d51995-08-28 03:09:13 +0000463Bourne shell. My experience with error handling in Amoeba made me
464acutely aware of the importance of exceptions as a programming
465language feature.
Guido van Rossum7be34a81995-05-31 15:17:12 +0000466
467- It occurred to me that a scripting language with a syntax like ABC
468but with access to the Amoeba system calls would fill the need. I
469realized that it would be foolish to write an Amoeba-specific
470language, so I decided that I needed a language that was generally
471extensible.
472
473- During the 1989 Christmas holidays, I had a lot of time on my hand,
474so I decided to give it a try. During the next year, while still
475mostly working on it in my own time, Python was used in the Amoeba
476project with increasing success, and the feedback from colleagues made
477me add many early improvements.
478
479- In February 1991, after just over a year of development, I decided
480to post to USENET. The rest is in the Misc/HISTORY file.
481
4821.17. Q. What happened to Tim Peters (the author of python-mode.el, and many
483entertaining and enlightening pieces of Python email)?
484
485A. He worked at KSR (Kendall Square Research, a start-up building a
486new kind of massively parallel processor). When KSR folded down a
487couple of years ago, Tim lost his email access. He hasn't surfaced
488on the net since then.
489
490 Missing-him-too-ly yours...
491
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000492PS: support for Python's Emacs mode (Misc/python-mode.el in the
493distribution) has been taken up by Barry Warsaw. Questions about it
494should be mailed to <python-mode@python.org>.
495
Guido van Rossuma7925f11994-01-26 10:20:16 +0000496
4972. Python in the real world
498===========================
499
5002.1. Q. How many people are using Python?
501
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000502A. I don't know, but the maximum number of simultaneous subscriptions
503to the Python mailing list before it was gatewayed into the newsgroup
504was about 180 (several of which were local redistribution lists). I
505believe that many active Python users don't bother to subscribe to the
506list, and now that there's a newsgroup the mailing list subscription
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000507is even less meaningful. I see new names on the newsgroup all the
508time and my best guess is that there are currently at least several
509thousands of users.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000510
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000511Another statistic is the number of accesses to the Python WWW server.
512Have a look at <URL:http://www.python.org/stats/>.
513
Guido van Rossuma7925f11994-01-26 10:20:16 +00005142.2. Q. Have any significant projects been done in Python?
515
516A. Here at CWI (the home of Python), we have written a 20,000 line
517authoring environment for transportable hypermedia presentations, a
Guido van Rossum5333c5d1994-04-11 11:06:22 +00005185,000 line multimedia teleconferencing tool, as well as many many
519smaller programs.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000520
521The University of Virginia uses Python to control a virtual reality
522engine. Contact: Matt Conway <conway@virginia.edu>.
523
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000524The ILU project at Xerox PARC can generate Python glue for ILU
525interfaces. See <URL:ftp://ftp.parc.xerox.com/pub/ilu/ilu.html>.
526
Guido van Rossumac3f2121995-04-10 11:53:42 +0000527The University of California, Irvine uses a student administration
528system called TELE-Vision written entirely in Python. Contact: Ray
529Price <rlprice@uci.edu>.
530
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000531See also the next question.
532
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000533If you have done a significant project in Python that you'd like to be
534included in the list above, send me email!
Guido van Rossuma7925f11994-01-26 10:20:16 +0000535
5362.3. Q. Are there any commercial projects going on using Python?
537
538A. Several companies have revealed to me that they are planning or
Guido van Rossum796b2591995-01-20 23:05:52 +0000539considering use of Python in a future product.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000540
Guido van Rossumbf8e7d51995-08-28 03:09:13 +0000541Sunrise Software has a product out using Python -- they use Python
Guido van Rossumac3f2121995-04-10 11:53:42 +0000542for a GUI management application and an SNMP network management
543application. Contact: <info@sunrise.com>.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000544
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000545Infoseek uses Python to implement their commercial WWW information
546retrieval service <URL:http://www.infoseek.com/>. Contact:
547<info@infoseek.com>.
Guido van Rossumf8c76d01994-08-17 12:19:53 +0000548
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000549Michael Powers of daVinci Time & Space is "writing tons-o-python for
550interactive television entertainment." Contact: <powers@dvts.com>.
551
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000552Paul Everitt of Connecting Minds is planning a Lotus Notes gateway.
553Contact: <Paul.Everitt@cminds.com>. Or see their WWW server
554<URL:http://www.cminds.com/>.
555
Guido van Rossumac3f2121995-04-10 11:53:42 +0000556KaPRE in Boulder, CO is using Python for on-site customization of C++
557applications, rapid-prototyping/development,
558language-based-components, and possibly more. This is pretty solid:
559Python's being shipped with their tool-set now, to beta sites.
560Contact: <lutz@KaPRE.COM> (Mark Lutz).
561
562Individuals at many other companies are using Python for internal
563development or for as yet unannounced products (witness their
564contributions to the Python mailing list or newsgroup).
Guido van Rossuma7925f11994-01-26 10:20:16 +0000565
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000566SGI has advertised in the Python list looking for Python programmers
567for a project involving interactive television.
568
569See also the workshop minutes at
570<URL:http://www.python.org/workshops/> -- in general the WWW server is
571more up to date than the FAQ for these issues.
572
Guido van Rossuma7925f11994-01-26 10:20:16 +0000573Python has also been elected as an extension language by MADE, a
574consortium supported by the European Committee's ESPRIT program and
575consisting of Bull, CWI and some other European companies. Contact:
576Ivan Herman <ivan@cwi.nl>.
577
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000578If you'd like to be included in the list above, send me email!
579
Guido van Rossum95f61a71994-01-26 17:23:37 +00005802.4. Q. How stable is Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000581
Guido van Rossum3de27361994-07-25 14:19:33 +0000582A. Very stable. While the current version number would suggest it is
583in the early stages of development, in fact new, stable releases
Guido van Rossumbf8e7d51995-08-28 03:09:13 +0000584(numbered 0.9.x through 1.2) have been coming out roughly every 3 to
Guido van Rossuma6c707c1995-01-02 17:32:28 +00005856 months for the past four years.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000586
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00005872.5. Q. When will the next version be released?
588
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000589A. I am planning to release 1.3 in early October 1995. It will
590contain keyword parameters as the most important new language feature.
591A beta version was made available in August, more beta versions may
592appear.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000593
5942.6. Q. What new developments are expected for Python in the future?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000595
Guido van Rossum7be34a81995-05-31 15:17:12 +0000596A. See my Work-In-Progress web page, currently at
597<URL:http://www.python.org:~guido/WIP.html>, and the pages for the
598Second Python Workshop (best reached via the Python home page,
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000599<URL:http://www.python.org/>). Also follow the newsgroup discussions!
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000600
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00006012.7. Q. Is it reasonable to propose incompatible changes to Python?
Guido van Rossum3de27361994-07-25 14:19:33 +0000602
603A. In general, no. There are already millions of lines of Python code
604around the world, so any changes in the language that invalidates more
605than a very small fraction of existing programs has to be frowned
606upon. Even if you can provide a conversion program, there still is
607the problem of updating all documentation. Providing a gradual
608upgrade path is the only way if a feature has to be changed.
609
Guido van Rossumb0a2ce51995-08-28 19:41:35 +00006102.8. Q. What is the future of Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000611
Guido van Rossumb0a2ce51995-08-28 19:41:35 +0000612A. If I knew, I'd be rich :-)
613
614Seriously, the formation of the PSA (Pyton Software Activity, see
615<URL:http://www.python.org/psa/>) ensures some kind of support even in
616the (unlikely! event that I'd be hit by a bus (actually, here in the
617US, a car accident would be more likely :-), were to join a nunnery,
618or would be head-hunted. A large number of Python users have become
619experts at Python programming as well as maintenance of the
620implementation, and would easily fill the vacuum created by my
621disappearance.
622
623In the mean time, I have no plans to disappear -- rather, I am
624committed to improving Python, and my current benefactor, CNRI (see
625<URL:http://www.cnri.reston.va.us>) is just as committed to continue
626its support of Python and the PSA. In fact, we have great plans for
627Python -- we just can't tell yet!
628
629
6303. Building Python and Other Known Bugs
631=======================================
Guido van Rossuma7925f11994-01-26 10:20:16 +0000632
Guido van Rossum91f60831994-02-15 15:52:27 +00006333.1. Q. Is there a test set?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000634
635A. Yes, simply do "import testall" (or "import autotest" if you aren't
636interested in the output). The standard modules whose name begins
637with "test" together comprise the test. The test set doesn't test
638*all* features of Python but it goes a long way to confirm that a new
639port is actually working. The Makefile contains an entry "make test"
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000640which runs the autotest module. NOTE: if "make test" fails, run the
641tests manually ("import testall") to see what goes wrong before
642reporting the error.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000643
Guido van Rossum91f60831994-02-15 15:52:27 +00006443.2. Q. When running the test set, I get complaints about floating point
Guido van Rossuma7925f11994-01-26 10:20:16 +0000645operations, but when playing with floating point operations I cannot
646find anything wrong with them.
647
648A. The test set makes occasional unwarranted assumptions about the
649semantics of C floating point operations. Until someone donates a
650better floating point test set, you will have to comment out the
651offending floating point tests and execute similar tests manually.
652
Guido van Rossum7be34a81995-05-31 15:17:12 +00006533.3. Q. Link errors after rerunning the configure script.
Guido van Rossum24349991994-02-02 14:12:45 +0000654
655A. It is generally necessary to run "make clean" after a configuration
656change.
657
Guido van Rossum7be34a81995-05-31 15:17:12 +00006583.4. Q. The python interpreter complains about options passed to a
Guido van Rossum24349991994-02-02 14:12:45 +0000659script (after the script name).
660
661A. You are probably linking with GNU getopt, e.g. through -liberty.
Guido van Rossumf8c76d01994-08-17 12:19:53 +0000662Don't. The reason for the complaint is that GNU getopt, unlike System
663V getopt and other getopt implementations, doesn't consider a
664non-option to be the end of the option list. A quick (and compatible)
665fix for scripts is to add "--" to the interpreter, like this:
666
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000667 #! /usr/local/bin/python --
Guido van Rossumf8c76d01994-08-17 12:19:53 +0000668
669You can also use this interactively:
670
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000671 python -- script.py [options]
672
673Note that a working getopt implementation is provided in the Python
674distribution (in Python/getopt.c) but not automatically used.
Guido van Rossum24349991994-02-02 14:12:45 +0000675
Guido van Rossum7be34a81995-05-31 15:17:12 +00006763.5. Q. When building on the SGI, make tries to run python to create
Guido van Rossum24349991994-02-02 14:12:45 +0000677glmodule.c, but python hasn't been built or installed yet.
678
679A. Comment out the line mentioning glmodule.c in Setup and build a
680python without gl first; install it or make sure it is in your $PATH,
681then edit the Setup file again to turn on the gl module, and make
682again. You don't need to do "make clean"; you do need to run "make
683Makefile" in the Modules subdirectory (or just run "make" at the
684toplevel).
685
Guido van Rossum7be34a81995-05-31 15:17:12 +00006863.6. Q. Python built with gcc for the DEC Alpha doesn't work.
Guido van Rossum3de27361994-07-25 14:19:33 +0000687
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000688People have reported problems with gcc 2.5.8 up to 2.6.3. The DEC
689OSF/1 cc compiler does not have these problems so it's likely a gcc
Guido van Rossume530c581995-04-10 12:32:16 +0000690bug. The latest news is that this has been fixed in Python 1.2 by a
691source change (I gave up waiting for a fixed gcc).
Guido van Rossum3de27361994-07-25 14:19:33 +0000692
Guido van Rossum7be34a81995-05-31 15:17:12 +00006933.7. Q. I use VPATH but some targets are built in the source directory.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000694
695A. On some systems (e.g. Sun), if the target already exists in the
696source directory, it is created there instead of in the build
697directory. This is usually because you have previously built without
698VPATH. Try running "make clobber" in the source directory.
699
Guido van Rossum7be34a81995-05-31 15:17:12 +00007003.8. Q. Trouble building or linking with the GNU readline library.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000701
Guido van Rossumac3f2121995-04-10 11:53:42 +0000702A. Consider using readline 2.0. Some hints:
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000703
704- You can use the GNU readline library to improve the interactive user
705interface: this gives you line editing and command history when
Guido van Rossum796b2591995-01-20 23:05:52 +0000706calling python interactively. You need to configure and build the GNU
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000707readline library before running the configure script. Its sources are
708no longer distributed with Python; you can ftp them from any GNU
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000709mirror site, or from its home site
710<URL:ftp://slc2.ins.cwru.edu/pub/dist/readline-2.0.tar.gz> (or a
711higher version number -- using version 1.x is not recommended). Pass
712the Python configure script the option --with-readline=DIRECTORY where
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000713DIRECTORY is the absolute pathname of the directory where you've built
714the readline library. Some hints on building and using the readline
715library:
716
717- On SGI IRIX 5, you may have to add the following
718to rldefs.h:
719
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000720 #ifndef sigmask
721 #define sigmask(sig) (1L << ((sig)-1))
722 #endif
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000723
724- On most systems, you will have to add #include "rldefs.h" to the
725top of several source files, and if you use the VPATH feature, you
726will have to add dependencies of the form foo.o: foo.c to the
727Makefile for several values of foo.
728
729- The readline library requires use of the termcap library. A
730known problem with this is that it contains entry points which
Guido van Rossum061f1821994-10-06 16:03:45 +0000731cause conflicts with the STDWIN and SGI GL libraries. The STDWIN
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000732conflict can be solved (and will be, in the next release of
Guido van Rossum061f1821994-10-06 16:03:45 +0000733STDWIN) by adding a line saying '#define werase w_erase' to the
734stdwin.h file (in the STDWIN distribution, subdirectory H). The
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000735GL conflict has been solved in the Python configure script by a
736hack that forces use of the static version of the termcap library.
737
Guido van Rossumac3f2121995-04-10 11:53:42 +0000738- Check the newsgroup gnu.bash.bug <URL:news:gnu.bash.bug> for
739specific problems with the readline library (I don't read this group
740but I've been told that it is the place for readline bugs).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000741
Guido van Rossum7be34a81995-05-31 15:17:12 +00007423.9. Q. Trouble building Python on Linux.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000743
Guido van Rossumac3f2121995-04-10 11:53:42 +0000744A. If you're building Python 1.2, Slackware 2.2 has a buggy bash
745(version 1.14.3) which breaks a sed script that is used to build
746Modules/Makefile. Replace /bin/sh with /bin/ash in both makesetup and
747Makefile.pre.in.
748
749In 1.1 and 1.1.1, there's a bug in the reference counting logic of
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000750ternary pow() which is only tripped by very picky mallocs, like the
751GNU malloc on Linux. This has been fixed in 1.2. To continue the
Guido van Rossumac3f2121995-04-10 11:53:42 +0000752tests in 1.1(.1), just disable the tests of pow() with three arguments
753from Lib/test/test_b2.py.
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000754
Guido van Rossumac3f2121995-04-10 11:53:42 +0000755Apart from this, Python builds and runs fine on most Linux versions
756(if you run into trouble on an old Linux version, consider upgrading).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000757
Guido van Rossum7be34a81995-05-31 15:17:12 +00007583.10. Q. Trouble with prototypes on Ultrix.
Guido van Rossum72eb83c1994-10-07 11:33:28 +0000759
760A. Ultrix cc seems broken -- use gcc, or edit config.h to #undef
761HAVE_PROTOTYPES.
762
Guido van Rossum7be34a81995-05-31 15:17:12 +00007633.11. Q. Trouble with posix.listdir on NeXTSTEP 3.2.
Guido van Rossum9351fdb1994-11-10 23:03:51 +0000764
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000765A. (This often manifests itself as a weird error from the
766compileall.py script run by "make libinstall".)
Guido van Rossum9351fdb1994-11-10 23:03:51 +0000767
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000768Don't use gcc, use the Next C compiler (cc). Even though it is
769derived from (an old version of) gcc, its interpretation of the
770"-posix" switch is different; in this particular case, cc is right and
771gcc is wrong.
Guido van Rossum9351fdb1994-11-10 23:03:51 +0000772
Guido van Rossum7be34a81995-05-31 15:17:12 +00007733.12. Q. Other trouble building Python on platform X.
Guido van Rossum95f61a71994-01-26 17:23:37 +0000774
Guido van Rossum796b2591995-01-20 23:05:52 +0000775A. Please email the details to <guido@cwi.nl> and I'll look into it.
776Please provide as many details as possible. In particular, if you
777don't tell me what type of computer and what operating system (and
778version) you are using it will be difficult for me to figure out what
779is the matter. If you get a specific error message, please email it
780to me too.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000781
Guido van Rossum7be34a81995-05-31 15:17:12 +00007823.13. Q. How to configure dynamic loading on Lixux.
Guido van Rossum07779351995-02-07 16:59:56 +0000783
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000784A. There are two answers, depending on whether you are using the new
785ELF object format or not.
Guido van Rossum07779351995-02-07 16:59:56 +0000786
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000787For ELF, this seems to be the procedure (thanks to Martin von Loewis):
Guido van Rossum07779351995-02-07 16:59:56 +0000788
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000789Compile Python to an ELF binary.
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +0000790
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000791In addition, you have to use the following flags:
Guido van Rossum07779351995-02-07 16:59:56 +0000792
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000793 - when linking python: -rdynamic -ldl
794 - when compiling an object that goes into a shared module: -fPIC
795 - when linking a shared module: -shared -ldl
Guido van Rossum07779351995-02-07 16:59:56 +0000796
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000797Furthermore, it appears that some Python releases did not understand
798that Linux has dynamic linking. Python 1.2 did it right, but you
799should check wether the generated config.h indicates the use of -ldl
800(i.e. dlopen, dlsym). Finally, you can load a shared module by saying
801'use foo'. Make sure the module is in your PYTHONPATH.
Guido van Rossum07779351995-02-07 16:59:56 +0000802
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000803For pre-ELF systems (thanks to Andrew Kuchling):
Guido van Rossum07779351995-02-07 16:59:56 +0000804
Guido van Rossum3fc9d731995-07-25 15:10:56 +0000805Pre-ELF Linux requires that you use the GNU DLD library. The stages
806of using dynamic libraries on Linux are:
807
808 1) Get dld 3.2.5 from a Linux site. Be careful here; the most
809 recent GNU version is 3.2.3, and doesn't support Linux; be sure to
810 get it from a Linux mirror, not a GNU mirror (3.2.4 should also
811 work). Compile it and install the library libdld.a somewhere; I
812 used /usr/local/lib.
813
814 Suitable URLs for the dld distribution are currently:
815 <URL:ftp://sunsite.unc.edu/pub/Linux/libs/dld-3.2.5.src.tar.gz> and
816 <URL:ftp://tsx-11.mit.edu/pub/linux/sources/libs/dld-3.2.5.src.tar.gz>.
817 There's also a binary distribution of it:
818 <URL:ftp://sunsite.unc.edu/pub/Linux/libs/dld-3.2.5.bin.tar.gz>.
819
820 2) Get Jack Jansen's DL library; its location is given in the
821 _Extending Python_ manual as <URL:ftp://ftp.cwi.nl/pub/dynload/>.
822 Compile it and install libdl.a wherever you put libdld.a.
823
824 3) Run Python's configure script, giving it the --with-dl-dld option,
825 which requires a parameter giving the directory where you put the
826 libraries.
827
828 4) Recompile Python.
829
8303.14. Q. Under Solaris 2.x, using GCC, how do I use shared libraries?
831
832A. Use the linker in /usr/ucb/ld, not the GNU linker. The latter
833cannot create shared libraries.
834
8353.15. Q. Errors when linking with a shared library containing C++ code.
836
837A. Link the main Python binary with C++. Change the definition of
838LINKCC in Modules/Makefile to be your C++ compiler. You may have to
839edit config.c slightly to make it compilable with C++.
Guido van Rossum07779351995-02-07 16:59:56 +0000840
Guido van Rossumb0a2ce51995-08-28 19:41:35 +00008413.16. Q. Tk menus don't work properly in Python 1.2.
842
843A. There's a bug in Tkinter.py; remove ClassType and InstanceType from
844the definition of CallableTypes near the top of Lib/tkinter/Tkinter.py.
845
8463.17. Q. I built with tkintermodule.c enabled but get "Tkinter not found".
847
848A. Tkinter.py (note: upper case T) lives in a subdirectory of Lib,
849Lib/tkinter. If you are using the default module search path, you
850probably didn't enable the line in the Modules/Setup file defining
851TKPATH; if you use the environment variable PYTHONPATH, you'll have to
852add the proper tkinter subdirectory.
853
8543.18. Q. I built with Tk 4.0 but Tkinter complains about the Tk version.
855
856A. Several things could cause this. You most likely have a Tk 3.6
857installation that wasn't completely eradicated by the Tk 4.0
858installation (which tends to add "4.0" to its installed files). You
859may have the Tk 3.6 support library installed in the place where the
860Tk 4.0 support files should be (default /usr/local/lib/tk/); you may
861have compiled Python with the old tk.h header file (yes, this actually
862compiles!); you may actually have linked with Tk 3.6 even though Tk
8634.0 is also around. Similar for Tcl 7.4 vs. Tcl 7.3.
864
8653.19. Q. Tk doesn't work right on DEC Alpha.
866
867A. You probably compiled either Tcl, Tk or Python with gcc. Don't.
868For this platform, which has 64-bit integers, gcc is known to generate
869broken code. The standard cc (which comes bundled with the OS!)
870works. If you still prefer gcc, at least try recompiling with cc
871before reporting problems to the newsgroup or the author; if this
872fixes the problem, report the bug to the gcc developers instead. (As
873far as we know, there are no problem with gcc on other platforms --
874the instabilities seem to be restricted to the DEC Alpha.) See also
875question 3.6.
876
Guido van Rossuma7925f11994-01-26 10:20:16 +0000877
8784. Programming in Python
879========================
880
Guido van Rossum24349991994-02-02 14:12:45 +00008814.1. Q. Is there a source code level debugger with breakpoints, step,
882etc.?
883
884A. Yes. Check out module pdb; pdb.help() prints the documentation (or
885you can read it as Lib/pdb.doc). If you use the STDWIN option,
886there's also a windowing interface, wdb. You can write your own
887debugger by using the code for pdb or wdb as an example.
888
8894.2. Q. Can I create an object class with some methods implemented in
Guido van Rossuma7925f11994-01-26 10:20:16 +0000890C and others in Python (e.g. through inheritance)? (Also phrased as:
891Can I use a built-in type as base class?)
892
893A. No, but you can easily create a Python class which serves as a
894wrapper around a built-in object, e.g. (for dictionaries):
895
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000896 # A user-defined class behaving almost identical
897 # to a built-in dictionary.
898 class UserDict:
899 def __init__(self): self.data = {}
900 def __repr__(self): return repr(self.data)
901 def __cmp__(self, dict):
902 if type(dict) == type(self.data):
903 return cmp(self.data, dict)
904 else:
905 return cmp(self.data, dict.data)
906 def __len__(self): return len(self.data)
907 def __getitem__(self, key): return self.data[key]
908 def __setitem__(self, key, item): self.data[key] = item
909 def __delitem__(self, key): del self.data[key]
910 def keys(self): return self.data.keys()
911 def items(self): return self.data.items()
912 def values(self): return self.data.values()
913 def has_key(self, key): return self.data.has_key(key)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000914
Guido van Rossum24349991994-02-02 14:12:45 +00009154.3. Q. Is there a curses/termcap package for Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000916
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000917A. Yes -- Lance Ellinghaus has written a module that interfaces to
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000918System V's "ncurses". If you know a little curses and some Python,
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000919it's straightforward to use. It is part of the standard Python
920distribution, but not configured by default -- you must enable it by
921editing Modules/Setup. It requires a System V curses implementation.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000922
923You could also consider using the "alfa" (== character cell) version
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000924of STDWIN. (Standard Window System Interface, a portable windowing
925system interface by myself <URL:ftp://ftp.cwi.nl/pub/stdwin/>.) This
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000926will also prepare your program for porting to windowing environments
927such as X11 or the Macintosh.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000928
Guido van Rossum24349991994-02-02 14:12:45 +00009294.4. Q. Is there an equivalent to C's onexit() in Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000930
931A. Yes, if you import sys and assign a function to sys.exitfunc, it
932will be called when your program exits, is killed by an unhandled
933exception, or (on UNIX) receives a SIGHUP or SIGTERM signal.
934
Guido van Rossum24349991994-02-02 14:12:45 +00009354.5. Q. When I define a function nested inside another function, the
Guido van Rossuma7925f11994-01-26 10:20:16 +0000936nested function seemingly can't access the local variables of the
937outer function. What is going on? How do I pass local data to a
938nested function?
939
940A. Python does not have arbitrarily nested scopes. When you need to
941create a function that needs to access some data which you have
942available locally, create a new class to hold the data and return a
943method of an instance of that class, e.g.:
944
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000945 class MultiplierClass:
946 def __init__(self, factor):
947 self.factor = factor
948 def multiplier(self, argument):
949 return argument * self.factor
Guido van Rossuma7925f11994-01-26 10:20:16 +0000950
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000951 def generate_multiplier(factor):
952 return MultiplierClass(factor).multiplier
Guido van Rossuma7925f11994-01-26 10:20:16 +0000953
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000954 twice = generate_multiplier(2)
955 print twice(10)
956 # Output: 20
957
958An alternative solution uses default arguments, e.g.:
959
960 def generate_multiplier(factor):
961 def multiplier(arg, fact = factor):
962 return arg*fact
963 return multiplier
964
965 twice = generate_multiplier(2)
966 print twice(10)
967 # Output: 20
Guido van Rossuma7925f11994-01-26 10:20:16 +0000968
Guido van Rossum24349991994-02-02 14:12:45 +00009694.6. Q. How do I iterate over a sequence in reverse order?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000970
971A. If it is a list, the fastest solution is
972
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000973 list.reverse()
974 try:
975 for x in list:
976 "do something with x"
977 finally:
978 list.reverse()
Guido van Rossuma7925f11994-01-26 10:20:16 +0000979
980This has the disadvantage that while you are in the loop, the list
981is temporarily reversed. If you don't like this, you can make a copy.
982This appears expensive but is actually faster than other solutions:
983
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000984 rev = list[:]
985 rev.reverse()
986 for x in rev:
987 <do something with x>
Guido van Rossuma7925f11994-01-26 10:20:16 +0000988
989If it isn't a list, a more general but slower solution is:
990
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000991 i = len(list)
992 while i > 0:
993 i = i-1
994 x = list[i]
995 <do something with x>
Guido van Rossuma7925f11994-01-26 10:20:16 +0000996
997A more elegant solution, is to define a class which acts as a sequence
998and yields the elements in reverse order (solution due to Steve
999Majewski):
1000
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001001 class Rev:
1002 def __init__(self, seq):
1003 self.forw = seq
1004 def __len__(self):
1005 return len(self.forw)
1006 def __getitem__(self, i):
1007 return self.forw[-(i + 1)]
Guido van Rossuma7925f11994-01-26 10:20:16 +00001008
1009You can now simply write:
1010
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001011 for x in Rev(list):
1012 <do something with x>
Guido van Rossuma7925f11994-01-26 10:20:16 +00001013
Guido van Rossum061f1821994-10-06 16:03:45 +00001014Unfortunately, this solution is slowest of all, due to the method
Guido van Rossuma7925f11994-01-26 10:20:16 +00001015call overhead...
1016
Guido van Rossum24349991994-02-02 14:12:45 +000010174.7. Q. My program is too slow. How do I speed it up?
Guido van Rossuma7925f11994-01-26 10:20:16 +00001018
1019A. That's a tough one, in general. There are many tricks to speed up
1020Python code; I would consider rewriting parts in C only as a last
1021resort. One thing to notice is that function and (especially) method
1022calls are rather expensive; if you have designed a purely OO interface
1023with lots of tiny functions that don't do much more than get or set an
1024instance variable or call another method, you may consider using a
1025more direct way, e.g. directly accessing instance variables. Also see
1026the standard module "profile" (described in the file
1027"python/lib/profile.doc") which makes it possible to find out where
1028your program is spending most of its time (if you have some patience
1029-- the profiling itself can slow your program down by an order of
1030magnitude).
1031
Guido van Rossum24349991994-02-02 14:12:45 +000010324.8. Q. When I have imported a module, then edit it, and import it
Guido van Rossuma7925f11994-01-26 10:20:16 +00001033again (into the same Python process), the changes don't seem to take
1034place. What is going on?
1035
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001036A. For reasons of efficiency as well as consistency, Python only reads
Guido van Rossum796b2591995-01-20 23:05:52 +00001037the module file on the first time a module is imported. (Otherwise a
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001038program consisting of many modules, each of which imports the same
Guido van Rossum796b2591995-01-20 23:05:52 +00001039basic module, would read the basic module over and over again.) To
1040force rereading of a changed module, do this:
Guido van Rossuma7925f11994-01-26 10:20:16 +00001041
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001042 import modname
1043 reload(modname)
Guido van Rossuma7925f11994-01-26 10:20:16 +00001044
1045Warning: this technique is not 100% fool-proof. In particular,
1046modules containing statements like
1047
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001048 from modname import some_objects
Guido van Rossuma7925f11994-01-26 10:20:16 +00001049
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001050will continue to work with the old version of the imported objects.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001051
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000010524.9. Q. How do I find the current module name?
Guido van Rossum24349991994-02-02 14:12:45 +00001053
Guido van Rossum061f1821994-10-06 16:03:45 +00001054A. A module can find out its own module name by looking at the
Guido van Rossum24349991994-02-02 14:12:45 +00001055(predefined) global variable __name__. If this has the value
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001056'__main__' you are running as a script.
1057
Guido van Rossuma6c707c1995-01-02 17:32:28 +000010584.10. Q. I have a module in which I want to execute some extra code
1059when it is run as a script. How do I find out whether I am running as
1060a script?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001061
1062A. See the previous question. E.g. if you put the following on the
1063last line of your module, main() is called only when your module is
1064running as a script:
Guido van Rossum24349991994-02-02 14:12:45 +00001065
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001066 if __name__ == '__main__': main()
Guido van Rossum24349991994-02-02 14:12:45 +00001067
Guido van Rossuma6c707c1995-01-02 17:32:28 +000010684.11. Q. I try to run a program from the Demo directory but it fails
1069with ImportError: No module named ...; what gives?
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001070
Guido van Rossum061f1821994-10-06 16:03:45 +00001071A. This is probably an optional module (written in C!) which hasn't
1072been configured on your system. This especially happens with modules
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001073like "Tkinter", "stdwin", "gl", "Xt" or "Xm". For Tkinter, STDWIN and
1074many other modules, see Modules/Setup.in for info on how to add these
1075modules to your Python, if it is possible at all. Sometimes you will
1076have to ftp and build another package first (e.g. STDWIN). Sometimes
1077the module only works on specific platforms (e.g. gl only works on SGI
1078machines).
1079
1080NOTE: if the complaint is about "Tkinter" (upper case T) and you have
1081already configured module "tkinter" (lower case t), the solution is
1082*not* to rename tkinter to Tkinter or vice versa. There is probably
Guido van Rossum796b2591995-01-20 23:05:52 +00001083something wrong with your module search path. Check out the value of
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001084sys.path.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001085
Guido van Rossum061f1821994-10-06 16:03:45 +00001086For X-related modules (Xt and Xm) you will have to do more work: they
1087are currently not part of the standard Python distribution. You will
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001088have to ftp the Extensions tar file, e.g.
Guido van Rossum7be34a81995-05-31 15:17:12 +00001089<URL:ftp://ftp.python.org/pub/python/src/extensions.tar.gz> and follow
1090the instructions there.
Guido van Rossum061f1821994-10-06 16:03:45 +00001091
1092See also the next question.
1093
10944.12. Q. I have successfully built Python with STDWIN but it can't
1095find some modules (e.g. stdwinevents).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001096
1097A. There's a subdirectory of the library directory named 'stdwin'
1098which should be in the default module search path. There's a line in
1099Modules/Setup(.in) that you have to enable for this purpose --
Guido van Rossum061f1821994-10-06 16:03:45 +00001100unfortunately in the latest release it's not near the other
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001101STDWIN-related lines so it's easy to miss it.
1102
11034.13. Q. What GUI toolkits exist for Python?
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001104
1105A. Depending on what platform(s) you are aiming at, there are several.
1106
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001107- There's a neat object-oriented interface to the Tcl/Tk widget set,
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001108called Tkinter. As of python 1.1, it is part of the standard Python
1109distribution -- all you need to do is enable it in Modules/Setup
1110(provided you have already installed Tk and Tcl). This is probably
1111the easiest to install and use, and the most complete widget set. It
1112is also very likely that in the future the standard Python GUI API
1113will be based on or at least look very much like the Tkinter
1114interface. For more info about Tk, including pointers to the source,
Guido van Rossumbf8e7d51995-08-28 03:09:13 +00001115see the Tcl/Tk home page <URL:http://www.sunlabs.com/research/tcl/>.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001116
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001117- The standard Python distribution comes with an interface to STDWIN,
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001118a platform-independent low-level windowing interface. You have to ftp
1119the source for STDWIN separately,
1120e.g. <URL:ftp://ftp.cwi.nl/pub/stdwin/> or gatekeeper.dec.com in
1121pub/misc/stdwin <URL:ftp://gatekeeper.dec.com/pub/misc/stdwin/>.
1122STDWIN runs under X11 or the Mac; a Windows port has been attempted
1123but I can't seem to get it working. Note that STDWIN is really not
1124powerful enough to implement a modern GUI (no widgets, etc.) and that
1125I don't have the time to maintain or extend it, so you may be better
1126off using Tkinter or the Motif interface, unless you require
1127portability to the Mac (which is also offered by SUIT, by the way --
1128see below).
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001129
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001130- For SGI IRIX only, there's an interface to the complete GL (Graphics
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001131Library -- low level but very good 3D capabilities) as well as to
1132FORMS (a buttons-and-sliders-etc package built on top of GL by Mark
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001133Overmars -- ftp'able from <URL:ftp://ftp.cs.ruu.nl/pub/SGI/FORMS/>).
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001134
1135- There's an interface to X11, including the Athena and Motif widget
1136sets (and a few individual widgets, like Mosaic's HTML widget and
1137SGI's GL widget) in the Extensions set, which is separately ftp'able
Guido van Rossum7be34a81995-05-31 15:17:12 +00001138<URL:ftp://ftp.python.org/pub/python/src/extensions.tar.gz>.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001139
1140- There's an interface to SUIT, the U of Virginia's Simple User
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001141Interface Toolkit; it can be ftp'ed from
1142<URL:ftp://uvacs.cs.virginia.edu/pub/suit/python/SUIT_python.tar.Z>.
1143A PC binary of Python 1.0.2 compiled with DJGPP and with SUIT support
1144built-in has been made available by Antonio Costa
1145<URL:ftp://asterix.inescn.pt/pub/PC/python/pyt102su.exe> (a
1146self-extracting archive). Note that the UVa people themselves have
1147expressed doubts about SUIT, and are planning to build a Python GUI
1148API based upon Tk (though not necessarily on Tkinter); see
1149<URL:http://server.cs.virginia.edu/~tnb2d/IT/IT.html>.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001150
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001151- There's an interface to WAFE, a Tcl interface to the X11 Motif and
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001152Athena widget sets. Last I heard about it it was included in the WAFE
11531.0 prerelease
1154<URL:ftp://ftp.wu-wien.ac.at/pub/src/X11/wafe/wafe-1.0.tar.gz-prerelease>.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001155
Guido van Rossumbf8e7d51995-08-28 03:09:13 +00001156- The NT port by Mark Hammond (see question 7.4) includes an interface
1157to the Microsoft Foundation Classes and a Python programming
1158environment using it that's written mostly in Python. See
1159<URL:ftp://ftp.python.org/pub/python/nt/>.
1160
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00001161- There's an interface to wxWindows. wxWindows is a portable GUI
1162class library written in C++. It supports XView, Motif, MS-Windows as
1163targets. There is some support for Macs and CURSES as well. wxWindows
1164preserves the look and feel of the underlying graphics toolkit. See
1165the wxPython WWW page at
1166<URL:http://www.aiai.ed.ac.uk/~jacs/wx/wxpython/wxpython.html>.
1167
Guido van Rossum3fc9d731995-07-25 15:10:56 +00001168- There's an object-oriented GUI based on the Microsoft Foundation
1169Classes model called WPY. Programs written in WPY run unchanged and
1170with native look and feel on NT, Windows 3.1 (using win32s) and on
1171Unix (using Tk). Source and binaries for NT and Linux are available
1172in <URL:ftp://ftp.python.org/pub/python/wpy>.
1173
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00001174- Python has been mentioned on the "Futurism" subpage of the Fresco
1175home page <URL:http://www.faslab.com/fresco/HomePage.html>. "Pesto"
1176is a Python interface to the CORBA dynamic invocation interface, and
1177thus Fresco. A Pesto prototype is running and is currently being
Guido van Rossum7be34a81995-05-31 15:17:12 +00001178packaged up for inclusion in the Fresco snapshot. See also the Pesto
1179web pages: <URL:http://www.faslab.com/fresco/pesto/Index.html>.
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00001180
Guido van Rossum061f1821994-10-06 16:03:45 +000011814.14. Q. Are there any interfaces to database packages in Python?
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001182
1183A. There's an interface to SYBASE by John Redford
1184<jredford@lehman.com>.
1185
Guido van Rossum796b2591995-01-20 23:05:52 +00001186There's an interface to metalbase by Lance Ellinghaus
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001187<lance@markv.com>; it is part of the separate Extensions distribution
Guido van Rossum7be34a81995-05-31 15:17:12 +00001188<URL:ftp://ftp.python.org/pub/python/src/extensions.tar.gz>.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001189
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001190Anthony Baxter <anthony.baxter@aaii.oz.au> has written an interface to
Guido van Rossume530c581995-04-10 12:32:16 +00001191mSQL (mini-SQL).
Guido van Rossum7be34a81995-05-31 15:17:12 +00001192<URL:ftp://ftp.python.org/pub/python/contrib/PymSQL.tar.gz>.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001193
Guido van Rossum7be34a81995-05-31 15:17:12 +00001194Tom Culliton <culliton@clark.net> has written an Oracle module.
1195See <URL:ftp://ftp.clark.net/pub/culliton/oramod.tgz>.
Guido van Rossum796b2591995-01-20 23:05:52 +00001196
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000011974.15. Q. Is it possible to write obfuscated one-liners in Python?
Guido van Rossumc24da7c1994-09-23 14:08:41 +00001198
1199A. Yes. See the following three examples, due to Ulf Bartelt:
1200
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001201 # Primes < 1000
1202 print filter(None,map(lambda y:y*reduce(lambda x,y:x*y!=0,
1203 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 +00001204
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001205 # First 10 Fibonacci numbers
1206 print map(lambda x,f=lambda x,f:(x<=1) or (f(x-1,f)+f(x-2,f)): f(x,f),
1207 range(10))
Guido van Rossumc24da7c1994-09-23 14:08:41 +00001208
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001209 # Mandelbrot set
1210 print (lambda Ru,Ro,Iu,Io,IM,Sx,Sy:reduce(lambda x,y:x+y,map(lambda y,
1211 Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,Sy=Sy,L=lambda yc,Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,i=IM,
1212 Sx=Sx,Sy=Sy:reduce(lambda x,y:x+y,map(lambda x,xc=Ru,yc=yc,Ru=Ru,Ro=Ro,
1213 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
1214 >=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(
1215 64+F(Ru+x*(Ro-Ru)/Sx,yc,0,0,i)),range(Sx))):L(Iu+y*(Io-Iu)/Sy),range(Sy
1216 ))))(-2.1, 0.7, -1.2, 1.2, 30, 80, 24)
1217 # \___ ___/ \___ ___/ | | |__ lines on screen
1218 # V V | |______ columns on screen
1219 # | | |__________ maximum of "iterations"
1220 # | |_________________ range on y axis
1221 # |____________________________ range on x axis
Guido van Rossumc24da7c1994-09-23 14:08:41 +00001222
1223Don't try this at home, kids!
1224
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000012254.16. Q. Is there an equivalent of C's "?:" ternary operator?
Guido van Rossumc24da7c1994-09-23 14:08:41 +00001226
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001227A. Not directly. In many cases you can mimic a?b:c with "a and b or
1228c", but there's a flaw: if b is zero (or empty, or None -- anything
1229that tests false) then c will be selected instead. In many cases you
1230can prove by looking at the code that this can't happen (e.g. because
1231b is a constant or has a type that can never be false), but in general
1232this can be a problem.
1233
1234Steve Majewski (or was it Tim Peters?) suggested the following
1235solution: (a and [b] or [c])[0]. Because [b] is a singleton list it
1236is never false, so the wrong path is never taken; then applying [0] to
1237the whole thing gets the b or c that you really wanted. Ugly, but it
1238gets you there in the rare cases where it is really inconvenient to
1239rewrite your code using 'if'.
1240
12414.17. Q. My class defines __del__ but it is not called when I delete the
1242object.
1243
1244A. There are several possible reasons for this.
1245
1246- The del statement does not necessarily call __del__ -- it simply
1247decrements the object's reference count, and if this reaches zero
1248__del__ is called.
1249
1250- If your data structures contain circular links (e.g. a tree where
1251each child has a parent pointer and each parent has a list of
1252children) the reference counts will never go back to zero. You'll
Guido van Rossum061f1821994-10-06 16:03:45 +00001253have to define an explicit close() method which removes those
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001254pointers. Please don't ever call __del__ directly -- __del__ should
Guido van Rossum061f1821994-10-06 16:03:45 +00001255call close() and close() should make sure that it can be called more
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001256than once for the same object.
1257
1258- If the object has ever been a local variable (or argument, which is
1259really the same thing) to a function that caught an expression in an
1260except clause, chances are that a reference to the object still exists
1261in that function's stack frame as contained in the stack trace.
1262Normally, deleting (better: assigning None to) sys.exc_traceback will
1263take care of this. If you a stack was printed for an unhandled
1264exception in an interactive interpreter, delete sys.last_traceback
1265instead.
1266
1267- There is code that deletes all objects when the interpreter exits,
1268but if your Python has been configured to support threads, it is not
1269called (because other threads may still be active). You can define
Guido van Rossum061f1821994-10-06 16:03:45 +00001270your own cleanup function using sys.exitfunc (see question 4.4).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001271
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001272- Finally, there are some obscure bugs if your __del__ method does
1273complicated things such as updating dictionaries or lists or
1274references globals. I hope to have fixed these in release 1.2.
1275
12764.18. Q. How do I change the shell environment for programs called
1277using os.popen() or os.system()? Changing os.environ doesn't work.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001278
1279A. Modifying the environment passed to subshells was left out of the
1280interpreter because there seemed to be no well-established portable
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001281way to do it (in particular, some systems, have putenv(), others have
1282setenv(), and some have none at all).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001283
1284However if all you want is to pass environment variables to the
1285commands run by os.system() or os.popen(), there's a simple solution:
1286prefix the command string with a couple of variable assignments and
Guido van Rossum796b2591995-01-20 23:05:52 +00001287export statements. The following would be universal for popen:
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001288
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001289 import os
1290 from commands import mkarg # nifty routine to add shell quoting
1291 def epopen(cmd, mode, env = {}):
1292 # env is a dictionary of environment variables
1293 prefix = ''
Guido van Rossum796b2591995-01-20 23:05:52 +00001294 for key, value in env.items():
1295 prefix = prefix + '%s=%s\n' % (key, mkarg(value)[1:])
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001296 prefix = prefix + 'export %s\n' % key
1297 return os.popen(prefix + cmd, mode)
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001298
12994.19. Q. What is a class?
1300
1301A. A class is the particular object type that is created by executing
Guido van Rossum796b2591995-01-20 23:05:52 +00001302a class statement. Class objects are used as templates, to create
1303class instance objects, which embody both the data structure and
1304program routines specific to a datatype.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001305
13064.20. Q. What is a method?
1307
1308A. A method is a function that you normally call as
Guido van Rossum796b2591995-01-20 23:05:52 +00001309x.name(arguments...) for some object x. The term is used for methods
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001310of classes and class instances as well as for methods of built-in
Guido van Rossum796b2591995-01-20 23:05:52 +00001311objects. (The latter have a completely different implementation and
1312only share the way their calls look in Python code.) Methods of
1313classes (and class instances) are defined as functions inside the
1314class definition.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001315
13164.21. Q. What is self?
1317
1318A. Self is merely a conventional name for the first argument of a
1319method -- i.e. a function defined inside a class definition. A method
1320defined as meth(self, a, b, c) should be called as x.meth(a, b, c) for
1321some instance x of the class in which the definition occurs;
1322the called method will think it is called as meth(x, a, b, c).
1323
13244.22. Q. What is a unbound method?
1325
1326A. An unbound method is a method defined in a class that is not yet
1327bound to an instance. You get an unbound method if you ask for a
1328class attribute that happens to be a function. You get a bound method
1329if you ask for an instance attribute. A bound method knows which
Guido van Rossum061f1821994-10-06 16:03:45 +00001330instance it belongs to and calling it supplies the instance automatically;
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001331an unbound method only knows which class it wants for its first
1332argument (a derived class is also OK). Calling an unbound method
1333doesn't "magically" derive the first argument from the context -- you
1334have to provide it explicitly.
1335
Guido van Rossuma6c707c1995-01-02 17:32:28 +000013364.23. Q. How do I call a method defined in a base class from a derived
1337class that overrides it?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001338
1339A. If your class definition starts with "class Derived(Base): ..."
1340then you can call method meth defined in Base (or one of Base's base
1341classes) as Base.meth(self, arguments...). Here, Base.meth is an
1342unbound method (see previous question).
1343
Guido van Rossuma6c707c1995-01-02 17:32:28 +000013444.24. Q. How do I call a method from a base class without using the
1345name of the base class?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001346
1347A. DON'T DO THIS. REALLY. I MEAN IT. It appears that you could call
1348self.__class__.__bases__[0].meth(self, arguments...) but this fails when
Guido van Rossum061f1821994-10-06 16:03:45 +00001349a doubly-derived method is derived from your class: for its instances,
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001350self.__class__.__bases__[0] is your class, not its base class -- so
1351(assuming you are doing this from within Derived.meth) you would start
1352a recursive call.
1353
13544.25. Q. How can I organize my code to make it easier to change the base
1355class?
1356
1357A. You could define an alias for the base class, assign the real base
1358class to it before your class definition, and use the alias throughout
1359your class. Then all you have to change is the value assigned to the
1360alias. Incidentally, this trick is also handy if you want to decide
1361dynamically (e.g. depending on availability of resources) which base
1362class to use. Example:
1363
1364BaseAlias = <real base class>
1365class Derived(BaseAlias):
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001366 def meth(self):
1367 BaseAlias.meth(self)
1368 ...
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001369
13704.26. Q. How can I find the methods or attributes of an object?
1371
1372A. This depends on the object type.
1373
1374For an instance x of a user-defined class, instance attributes are
1375found in the dictionary x.__dict__, and methods and attributes defined
1376by its class are found in x.__class__.__bases__[i].__dict__ (for i in
1377range(len(x.__class__.__bases__))). You'll have to walk the tree of
1378base classes to find *all* class methods and attributes.
1379
1380Many, but not all built-in types define a list of their method names
1381in x.__methods__, and if they have data attributes, their names may be
1382found in x.__members__. However this is only a convention.
1383
1384For more information, read the source of the standard (but
1385undocumented) module newdir.
1386
13874.27. Q. I can't seem to use os.read() on a pipe created with os.popen().
1388
1389A. os.read() is a low-level function which takes a file descriptor (a
1390small integer). os.popen() creates a high-level file object -- the
1391same type used for sys.std{in,out,err} and returned by the builtin
1392open() function. Thus, to read n bytes from a pipe p created with
1393os.popen(), you need to use p.read(n).
Guido van Rossuma7925f11994-01-26 10:20:16 +00001394
Guido van Rossum061f1821994-10-06 16:03:45 +000013954.28. Q. How can I create a stand-alone binary from a Python script?
1396
1397The demo script "Demo/scripts/freeze.py" does what you want. (It's
1398actually not a demo but a support tool -- there is some extra code in
1399the interpreter to accommodate it.) It requires that you have the
1400Python build tree handy, complete with all the lib*.a files.
1401
1402This works by scanning your source recursively for import statements
1403(both forms) and looking for the modules on the standard Python path
1404as well as in the source directory (for built-in modules). It then
1405"compiles" the modules written in Python to C code (array initializers
1406that can be turned into code objects using the marshal module) and
1407creates a custom-made config file that only contains those built-in
1408modules which are actually used in the program. It then compiles the
1409generated C code and links it with the rest of the Python interpreter
1410to form a self-contained binary which acts exactly like your script.
1411
Guido van Rossum7be34a81995-05-31 15:17:12 +00001412Hint: the freeze program only works if your script's filename ends in
1413".py".
Guido van Rossum061f1821994-10-06 16:03:45 +00001414
Guido van Rossum7be34a81995-05-31 15:17:12 +000014154.29. Q. What WWW tools are there for Python?
Guido van Rossum061f1821994-10-06 16:03:45 +00001416
Guido van Rossum7be34a81995-05-31 15:17:12 +00001417A. There's a whole lot in the 1.2 release; see the section on
1418"Internet and WWW" in the Library Reference Manual. Highlights: a CGI
1419parser, a url retriever, an html parser.
Guido van Rossum061f1821994-10-06 16:03:45 +00001420
1421Steve Miale <smiale@cs.indiana.edu> has written a modular WWW browser
1422called Dancer. An alpha version can be FTP'ed from
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001423<URL:ftp://ftp.cs.indiana.edu/pub/smiale/dancer.tar.gz>. (There are a
1424few articles about Dancer in the (hyper)mail archive
1425<URL:http://www.cwi.nl/~guido/hypermail/python-1994q3/index.html>.)
Guido van Rossum061f1821994-10-06 16:03:45 +00001426
Guido van Rossum7be34a81995-05-31 15:17:12 +000014274.30. Q. How do I run a subprocess with pipes connected to both input
Guido van Rossum796b2591995-01-20 23:05:52 +00001428and output?
1429
1430A. This is really a UNIX question. Also, in general, it is unwise to
1431do so, because you can easily cause a deadlock where the parent
1432process is blocked waiting for output from the child, while the child
1433is blocked waiting for input from the child. This can be caused
1434because the parent expects the child to output more text than it does,
1435or it can be caused by data being stuck in stdio buffers due to lack
1436of flushing. The Python parent can of course explicitly flush the data
1437it sends to the child before it reads any output, but if the child is
1438a naive C program it can easily have been written to never explicitly
1439flush its output, even if it is interactive, since flushing is
1440normally automatic.
1441
1442In many cases, all you really need is to run some data through a
1443command and get the result back. Unless the data is infinite in size,
1444the easiest (and often the most efficient!) way to do this is to write
1445it to a temporary file and run the command with that temporary file as
1446input. The standard module tempfile exports a function mktemp() which
1447generates unique temporary file names.
1448
1449If after reading all of the above you still want to connect two pipes
1450to a subprocess's standard input and output, here's a simple solution,
1451due to Jack Jansen:
1452
1453 import os
1454 import sys
1455 import string
1456
1457 MAXFD = 100 # Max number of file descriptors in this system
1458
1459 def popen2(cmd):
1460 cmd = string.split(cmd)
1461 p2cread, p2cwrite = os.pipe()
1462 c2pread, c2pwrite = os.pipe()
1463 pid = os.fork()
1464 if pid == 0:
1465 # Child
1466 os.close(0)
1467 os.close(1)
1468 if os.dup(p2cread) <> 0:
1469 sys.stderr.write('popen2: bad read dup\n')
1470 if os.dup(c2pwrite) <> 1:
1471 sys.stderr.write('popen2: bad write dup\n')
1472 for i in range(3, MAXFD):
1473 try:
1474 os.close(i)
1475 except:
1476 pass
1477 try:
1478 os.execv(cmd[0], cmd)
1479 finally:
1480 os._exit(1)
1481 os.close(p2cread)
1482 tochild = os.fdopen(p2cwrite, 'w')
1483 os.close(c2pwrite)
1484 fromchild = os.fdopen(c2pread, 'r')
1485 return fromchild, tochild
1486
1487Note that many interactive programs (e.g. vi) don't work well with
1488pipes substituted for standard input and output. You will have to use
1489pseudo ttys ("ptys") instead of pipes. There is some undocumented
1490code to use these in the library module pty.py -- I'm afraid you're on
Guido van Rossum3fc9d731995-07-25 15:10:56 +00001491your own here.
1492
1493A different answer is a Python interface to Don Libes' "expect"
1494library. A prerelease of this is available on the Python ftp mirror
1495sites in the contrib subdirectory as expy-0.3.tar.gz, e.g.
1496<URL:ftp://ftp.python.org/pub/python/contrib/expy-0.3.tar.gz>.
Guido van Rossum796b2591995-01-20 23:05:52 +00001497
Guido van Rossum7be34a81995-05-31 15:17:12 +000014984.31. Q. How do I call a function if I have the arguments in a tuple?
Guido van Rossumac3f2121995-04-10 11:53:42 +00001499
1500A. Use the built-in function apply(). For instance,
1501
1502 func(1, 2, 3)
1503
1504is equivalent to
1505
1506 args = (1, 2, 3)
1507 apply(func, args)
1508
1509Note that func(args) is not the same -- it calls func() with exactly
1510one argument, the tuple args, instead of three arguments, the integers
15111, 2 and 3.
1512
Guido van Rossum7be34a81995-05-31 15:17:12 +000015134.32. Q. How do I enable font-lock-mode for Python in Emacs?
1514
1515A. Assuming you're already using python-mode and font-lock-mode
1516separately, all you need to do is put this in your .emacs file:
1517
1518 (defun my-python-mode-hook ()
1519 (setq font-lock-keywords python-font-lock-keywords)
1520 (font-lock-mode 1))
1521 (add-hook 'python-mode-hook 'my-python-mode-hook)
1522
15234.33. Q. Is there an inverse to the format operator (a la C's scanf())?
1524
1525A. Not as such.
1526
1527For simple input parsing, the easiest approach is usually to split
1528the line into whitespace-delimited words using string.split(), and to
1529convert decimal strings to numeric values using string.atoi(),
1530string.atol() or string.atof(). (Python's atoi() is 32-bit and its
1531atol() is arbitrary precision.) If you want to use another delimiter
1532than whitespace, use string.splitfield() (possibly combining it with
1533string.strip() which removes surrounding whitespace from a string).
1534
1535For more complicated input parsing, regular expressions (see module
1536regex) are better suited and more powerful than C's scanf().
1537
Guido van Rossum061f1821994-10-06 16:03:45 +00001538
Guido van Rossuma7925f11994-01-26 10:20:16 +000015395. Extending Python
1540===================
1541
15425.1. Q. Can I create my own functions in C?
1543
1544A. Yes, you can create built-in modules containing functions,
Guido van Rossum24349991994-02-02 14:12:45 +00001545variables, exceptions and even new types in C. This is explained in
1546the document "Extending and Embedding the Python Interpreter" (the
1547LaTeX file Doc/ext.tex). Also read the chapter on dynamic loading.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001548
15495.2. Q. Can I create my own functions in C++?
1550
1551A. Yes, using the C-compatibility features found in C++. Basically
1552you place extern "C" { ... } around the Python include files and put
1553extern "C" before each function that is going to be called by the
1554Python interpreter. Global or static C++ objects with constructors
1555are probably not a good idea.
1556
Guido van Rossum7ce61c11994-06-13 15:13:56 +000015575.3. Q. How can I execute arbitrary Python statements from C?
1558
1559A. The highest-level function to do this is run_command() which takes
1560a single string argument which is executed in the context of module
1561__main__ and returns 0 for success and -1 when an exception occurred
1562(including SyntaxError). If you want more control, use run_string();
1563see the source for run_command() in Python/pythonrun.c.
1564
15655.4. Q. How can I evaluate an arbitrary Python expression from C?
1566
1567A. Call the function run_string() from the previous question with the
1568start symbol eval_input; it then parses an expression, evaluates it
1569and returns its value. See exec_eval() in Python/bltinmodule.c.
1570
15715.5. Q. How do I extract C values from a Python object?
1572
1573A. That depends on the object's type. If it's a tuple,
1574gettuplesize(o) returns its length and gettupleitem(o, i) returns its
1575i'th item; similar for lists with getlistsize(o) and getlistitem(o,
1576i). For strings, getstringsize(o) returns its length and
1577getstringvalue(o) a pointer to its value (note that Python strings may
1578contain null bytes so strlen() is not safe). To test which type an
1579object is, first make sure it isn't NULL, and then use
1580is_stringobject(o), is_tupleobject(o), is_listobject(o) etc.
1581
15825.6. Q. How do I use mkvalue() to create a tuple of arbitrary length?
1583
1584A. You can't. Use t = newtupleobject(n) instead, and fill it with
1585objects using settupleitem(t, i, o) -- note that this "eats" a
1586reference count of o. Similar for lists with newlistobject(n) and
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001587setlistitem(l, i, o). Note that you *must* set all the tuple items to
1588some value before you pass the tuple to Python code --
1589newtupleobject(n) initializes them to NULL, which isn't a valid Python
1590value.
1591
Guido van Rossum796b2591995-01-20 23:05:52 +000015925.7. Q. What happened to mktuple(), featured in an example in the
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001593Extensions manual?
1594
1595A. It's a typo, I meant newtupleobject() (see previous question).
1596
15975.8. Q. How do I call an object's method from C?
1598
1599A. Here's a function (untested) that might become part of the next
1600release in some form. It uses <stdarg.h> to allow passing the
1601argument list on to vmkvalue():
1602
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001603 object *call_method(object *inst, char *methodname, char *format, ...)
1604 {
1605 object *method;
1606 object *args;
1607 object *result;
1608 va_list va;
1609 method = getattr(inst, methodname);
1610 if (method == NULL) return NULL;
1611 va_start(va, format);
1612 args = vmkvalue(format, va);
1613 va_end(va);
1614 if (args == NULL) {
1615 DECREF(method);
1616 return NULL;
1617 }
1618 result = call_object(method, args);
1619 DECREF(method);
1620 DECREF(args);
1621 return result;
1622 }
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001623
1624This works for any instance that has methods -- whether built-in or
1625user-defined. You are responsible for eventually DECREF'ing the
1626return value.
1627
1628To call, e.g., a file object's "seek" method with arguments 10, 0
1629(assuming the file object pointer is "f"):
1630
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001631 res = call_method(f, "seek", "(OO)", 10, 0);
1632 if (res == NULL) {
1633 ... an exception occurred ...
1634 }
1635 else {
1636 DECREF(res);
1637 }
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001638
1639Note that since call_object() *always* wants a tuple for the argument
1640list, to call a function without arguments, pass "()" for the format,
1641and to call a function with one argument, surround the argument in
1642parentheses, e.g. "(i)".
1643
16445.9. Q. How do I catch the output from print_error()?
1645
1646A. (Due to Mark Hammond):
1647
1648* in Python code, define an object that supports the "write()" method.
1649FWIW, there seems to be a small problem that requires the 'softspace'
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00001650attribute to be defined too (fixed in 1.2).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001651
1652* redirect sys.stdout and sys.stderr to this object.
1653
Guido van Rossum061f1821994-10-06 16:03:45 +00001654* call print_error, or just allow the standard traceback mechanism to
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001655work.
1656
Guido van Rossum061f1821994-10-06 16:03:45 +00001657Then, the output will go wherever your write() method sends it.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001658
16595.10. Q. How do I access a module written in Python from C?
1660
1661A. You can get a pointer to the module object as follows:
1662
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001663 module = import_module("<modulename>");
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001664
1665If the module hasn't been imported yet (i.e. it is not yet present in
1666sys.modules), this initializes the module; otherwise it simply returns
1667the value of sys.modules["<modulename>"]. Note that it doesn't enter
1668the module into any namespace -- it only ensures it has been
1669initialized and is stored in sys.modules.
1670
1671You can then access the module's attributes (i.e. any name defined in
1672the module) as follows:
1673
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001674 attr = getattr(module, "<attrname>");
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001675
1676Calling setattr(), to assign to variables in the module, also works.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001677
Guido van Rossumb0a2ce51995-08-28 19:41:35 +000016785.11. Q. How do I interface to C++ objects from Python?
1679
1680A. Depending on your requirements, there are many approaches. Begin
1681by reading the "Extending and Embedding" document (Doc/ext.tex, see
1682also <URL:http://www.python.org/doc/>). Realize that for the Python
1683run-time system, there isn't a whole lot of difference between C and
1684C++ -- so the strategy to build a new Python type around a C structure
1685(pointer) type will also work for C++ objects.
1686
1687Automatic generation of interfaces between Python and C++ is still at
1688the horizon -- parsing C++ header files requires an almost complete
1689C++ parser, and many features aren't easily translated from C++ to
1690Python: certain forms of operator oveloading, function overloading
1691(best approached by a varargs function which explicitly type-checks
1692its arguments), and reference arguments are just a number of features
1693that are hard to translate correctly if at all.
1694
1695The hardest problem is to transparently translate the C++ class
1696hierarchy to Python, so that Python programs derive classes from C++
1697classes. Given suitable constraints, this may be possible, but it
1698would require more space than I have in this FAQ to explain how.
1699In any case, you can get quite a bit done without this, using just the
1700existing classes from Python.
1701
1702If this all seems rather daunting, that may be because it is -- C++
1703isn't exactly a baby to handle without gloves! However, people have
1704accomplished amazing feats of interfacing between Python and C++, and
1705a detailed question posted to the Python list is likely to elicit some
1706interesting and useful responses.
1707
Guido van Rossuma7925f11994-01-26 10:20:16 +00001708
17096. Python's design
1710==================
1711
Guido van Rossumb0a2ce51995-08-28 19:41:35 +000017126.1. Q. Why isn't there a generic copying operation for objects in Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +00001713
Guido van Rossumb0a2ce51995-08-28 19:41:35 +00001714A. Historically, there wasn't. However, starting in Python 1.2,
1715there's a library module "copy" which supports both shallow and deep
1716copying of most common Python objects (though not things like open
1717files, sockets or windows), including an extensible mechanism to copy
1718class instances.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001719
17206.2. Q. Why isn't there a generic way to implement persistent objects
1721in Python? (Persistent == automatically saved to and restored from
1722disk.)
1723
Guido van Rossumb0a2ce51995-08-28 19:41:35 +00001724A. Like the previous question, historically, there wasn't. The
1725library module "pickle" now solves this in a very general way (though
1726you still can't store things like open files, sockests or windows),
1727and the library module "shelve" uses pickle and (g)dbm to create
1728presistent mappings containing arbitrary Python objects. There are
1729some problems with shelve when using gdbm which will be solved in
1730Python 1.3.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001731
Guido van Rossuma7925f11994-01-26 10:20:16 +000017326.3. Q. Why isn't there a switch or case statement in Python?
1733
1734A. You can do this easily enough with a sequence of
1735if... elif... elif... else. There have been some proposals for switch
Guido van Rossum061f1821994-10-06 16:03:45 +00001736statement syntax, but there is no consensus (yet) on whether and how
Guido van Rossuma7925f11994-01-26 10:20:16 +00001737to do range tests.
1738
Guido van Rossumc50158e1994-05-31 09:18:50 +000017396.4. Q. Why does Python use indentation for grouping of statements?
1740
1741A. Basically I believe that using indentation for grouping is
1742extremely elegant and contributes a lot to the clarity of the average
1743Python program. Most people learn to love this feature after a while.
1744Some arguments for it:
1745
1746- Since there are no begin/end brackets there cannot be a disagreement
1747between grouping perceived by the parser and the human reader. I
1748remember long ago seeing a C fragment like this:
1749
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001750 if (x <= y)
1751 x++;
1752 y--;
1753 z++;
Guido van Rossumc50158e1994-05-31 09:18:50 +00001754
1755and staring a long time at it wondering why y was being decremented
1756even for x > y... (And I wasn't a C newbie then either.)
1757
Guido van Rossum7be34a81995-05-31 15:17:12 +00001758- Since there are no begin/end brackets, Python is much less prone to
1759coding-style conflicts. In C there are loads of different ways to
1760place the braces (including the choice whether to place braces around
1761single statements in certain cases, for consistency). If you're used
1762to reading (and writing) code that uses one style, you will feel at
1763least slightly uneasy when reading (or being required to write)
1764another style.
Guido van Rossumc50158e1994-05-31 09:18:50 +00001765
1766- Many coding styles place begin/end brackets on a line by themself.
1767This makes programs considerably longer and wastes valuable screen
1768space, making it harder to get a good overview over a program.
1769Ideally, a function should fit on one basic tty screen (say, 20
1770lines). 20 lines of Python are worth a LOT more than 20 lines of C.
1771This is not solely due to the lack of begin/end brackets (the lack of
1772declarations also helps, and the powerful operations of course), but
1773it certainly helps!
1774
Guido van Rossum3de27361994-07-25 14:19:33 +000017756.5. Q. Why are Python strings immutable?
1776
1777A. There are two advantages. One is performance: knowing that a
1778string is immutable makes it easy to lay it out at construction time
1779-- fixed and unchanging storage requirements. (This is also one of
Guido van Rossum061f1821994-10-06 16:03:45 +00001780the reasons for the distinction between tuples and lists.) The
Guido van Rossum3de27361994-07-25 14:19:33 +00001781other is that strings in Python are considered as "elemental" as
1782numbers. No amount of activity will change the value 8 to anything
1783else, and in Python, no amount of activity will change the string
1784"eight" to anything else. (Adapted from Jim Roskind)
1785
17866.6. Q. Why don't strings have methods like index() or sort(), like
1787lists?
1788
1789A. Good question. Strings currently don't have methods at all
1790(likewise tuples and numbers). Long ago, it seemed unnecessary to
1791implement any of these functions in C, so a standard library module
1792"string" written in Python was created that performs string related
1793operations. Since then, the cry for performance has moved most of
1794them into the built-in module strop (this is imported by module
Guido van Rossumf8c76d01994-08-17 12:19:53 +00001795string, which is still the preferred interface, without loss of
Guido van Rossum3de27361994-07-25 14:19:33 +00001796performance except during initialization). Some of these functions
1797(e.g. index()) could easily be implemented as string methods instead,
1798but others (e.g. sort()) can't, since their interface prescribes that
1799they modify the object, while strings are immutable (see the previous
1800question).
1801
18026.7. Q. Why does Python use methods for some functionality
1803(e.g. list.index()) but functions for other (e.g. len(list))?
1804
1805A. Functions are used for those operations that are generic for a
1806group of types and which should work even for objects that don't have
1807methods at all (e.g. numbers, strings, tuples). Also, implementing
1808len(), max(), min() as a built-in function is actually less code than
1809implementing them as methods for each type. One can quibble about
1810individual cases but it's really too late to change such things
1811fundamentally now.
1812
18136.8. Q. Why can't I derive a class from built-in types (e.g. lists or
1814files)?
1815
1816A. This is caused by the relatively late addition of (user-defined)
1817classes to the language -- the implementation framework doesn't easily
1818allow it. See the answer to question 4.2 for a work-around. This
1819*may* be fixed in the (distant) future.
1820
18216.9. Q. Why must 'self' be declared and used explicitly in method
1822definitions and calls?
1823
1824A. By asking this question you reveal your C++ background. :-)
1825When I added classes, this was (again) the simplest way of
1826implementing methods without too many changes to the interpreter. I
1827borrowed the idea from Modula-3. It turns out to be very useful, for
1828a variety of reasons.
1829
1830First, it makes it more obvious that you are using a method or
1831instance attribute instead of a local variable. Reading "self.x" or
1832"self.meth()" makes it absolutely clear that an instance variable or
1833method is used even if you don't know the class definition by heart.
1834In C++, you can sort of tell by the lack of a local variable
Guido van Rossum061f1821994-10-06 16:03:45 +00001835declaration (assuming globals are rare or easily recognizable) -- but
Guido van Rossum3de27361994-07-25 14:19:33 +00001836in Python, there are no local variable declarations, so you'd have to
1837look up the class definition to be sure.
1838
1839Second, it means that no special syntax is necessary if you want to
1840explicitly reference or call the method from a particular class. In
1841C++, if you want to use a method from base class that is overridden in
1842a derived class, you have to use the :: operator -- in Python you can
1843write baseclass.methodname(self, <argument list>). This is
1844particularly useful for __init__() methods, and in general in cases
1845where a derived class method wants to extend the base class method of
1846the same name and thus has to call the base class method somehow.
1847
1848Lastly, for instance variables, it solves a syntactic problem with
1849assignment: since local variables in Python are (by definition!) those
1850variables to which a value assigned in a function body (and that
1851aren't explicitly declared global), there has to be some way to tell
1852the interpreter that an assignment was meant to assign to an instance
1853variable instead of to a local variable, and it should preferably be
1854syntactic (for efficiency reasons). C++ does this through
1855declarations, but Python doesn't have declarations and it would be a
1856pity having to introduce them just for this purpose. Using the
1857explicit "self.var" solves this nicely. Similarly, for using instance
1858variables, having to write "self.var" means that references to
1859unqualified names inside a method don't have to search the instance's
1860directories.
1861
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000018626.10. Q. Can't you emulate threads in the interpreter instead of
1863relying on an OS-specific thread implementation?
1864
1865A. Unfortunately, the interpreter pushes at least one C stack frame
1866for each Python stack frame. Also, extensions can call back into
1867Python at almost random moments. Therefore a complete threads
1868implementation requires thread support for C.
1869
Guido van Rossum061f1821994-10-06 16:03:45 +000018706.11. Q. Why can't lambda forms contain statements?
1871
1872A. Python lambda forms cannot contain statements because Python's
Guido van Rossum796b2591995-01-20 23:05:52 +00001873syntactic framework can't handle statements nested inside expressions.
Guido van Rossum061f1821994-10-06 16:03:45 +00001874
1875However, in Python, this is not a serious problem. Unlike lambda
1876forms in other languages, where they add functionality, Python lambdas
1877are only a shorthand notation if you're too lazy to define a function.
1878
1879Functions are already first class objects in Python, and can be
1880declared in a local scope. Therefore the only advantage of using a
1881lambda form instead of a locally-defined function is that you'll have
1882to invent a name for the function -- but that's just a local variable
1883to which the function object (which is exactly the same type of object
1884that a lambda form yields) is assigned!
1885
18866.12. Q. Why is there no more efficient way of iterating over a dictionary
1887than first constructing the list of keys()?
1888
1889A. Have you tried it? I bet it's fast enough for your purposes! In
1890most cases such a list takes only a few percent of the space occupied
1891by the dictionary -- it needs only 4 bytes (the size of a pointer) per
1892key -- a dictionary costs 8 bytes per key plus between 30 and 70
1893percent hash table overhead, plus the space for the keys and values --
1894by necessity all keys are unique objects and a string object (the most
1895common key type) costs at least 18 bytes plus the length of the
1896string. Add to that the values contained in the dictionary, and you
1897see that 4 bytes more per item really isn't that much more memory...
1898
1899A call to dict.keys() makes one fast scan over the dictionary
1900(internally, the iteration function does exist) copying the pointers
1901to the key objects into a pre-allocated list object of the right size.
1902The iteration time isn't lost (since you'll have to iterate anyway --
1903unless in the majority of cases your loop terminates very prematurely
1904(which I doubt since you're getting the keys in random order).
1905
1906I don't expose the dictionary iteration operation to Python
1907programmers because the dictionary shouldn't be modified during the
1908entire iteration -- if it is, there's a very small chance that the
1909dictionary is reorganized because the hash table becomes too full, and
1910then the iteration may miss some items and see others twice. Exactly
1911because this only occurs rarely, it would lead to hidden bugs in
1912programs: it's easy never to have it happen during test runs if you
1913only insert or delete a few items per iteration -- but your users will
1914surely hit upon it sooner or later.
1915
Guido van Rossumb0a2ce51995-08-28 19:41:35 +000019166.13. Q. Can Python be compiled to machine code, C or some other language?
1917
1918A. Not easily. Python's high level data types, dynamic typing of
1919objects and run-time invocation of the interpreter (using eval() or
1920exec) together mean that a "compiled" Python program would probably
1921consist mostly of calls into the Python run-time system, even for
1922seemingly simple operations like "x+1". Thus, the performance gain
1923would probably be minimal.
1924
1925Internally, Python source code is always translated into a "virtual
1926machine code" or "byte code" representation before it is interpreted
1927(by the "Python virtual machine" or "bytecode interpreter"). In order
1928to avoid the overhead of parsing and translating modules that rarely
1929change over and over again, this byte code is written on a file whose
1930name ends in ".pyc" whenever a module is parsed (from a file whose
1931name ends in ".py"). When the corresponding .py file is changed, it
1932is parsed and translated again and the .pyc file is rewritten. There
1933is no performance difference once the .pyc file has been loaded (the
1934bytecode read from the .pyc file is exactly the same as the bytecode
1935created by direct translation). The only difference is that loading
1936code from a .pyc file is faster than parsing and translating a .py
1937file, so the presence of precompiled .pyc files will generally improve
1938start-up time of Python scripts. If desired, the Lib/compileall.py
1939module/script can be used to force creation of valid .pyc files for a
1940given set of modules.
1941
1942If you are looking for a way to translate Python programs in order to
1943distribute them in binary form, without the need to distribute the
1944interpreter and library as well, have a look at the freeze.py script
1945in the Tools/freeze directory. This creates a single binary file
1946incorporating your program, the Python interpreter, and those parts of
1947the Python library that are needed by your program. Of course, the
1948resulting binary will only run on the same type of platform as that
1949used to create it.
1950
1951Hints for proper usage of freeze.py:
1952
1953- the script must be in a file whose name ends in .py
1954
1955- you must have installed Python fully:
1956
1957 make install
1958 make libinstall
1959 make inclinstall
1960 make libainstall
1961
Guido van Rossuma7925f11994-01-26 10:20:16 +00001962
19637. Using Python on non-UNIX platforms
1964=====================================
1965
Guido van Rossum91f60831994-02-15 15:52:27 +000019667.1. Q. Is there a Mac version of Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +00001967
Guido van Rossume530c581995-04-10 12:32:16 +00001968A. Yes, see the "mac" subdirectory of the distribution sites,
Guido van Rossum7be34a81995-05-31 15:17:12 +00001969e.g. <URL:ftp://ftp.python.org/pub/python/mac/>.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001970
Guido van Rossum91f60831994-02-15 15:52:27 +000019717.2. Q. Is there a DOS version of Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +00001972
Guido van Rossume530c581995-04-10 12:32:16 +00001973A. Yes, see the "pc" subdirectory of the distribution sites,
Guido van Rossum7be34a81995-05-31 15:17:12 +00001974e.g. <URL:ftp://ftp.python.org/pub/python/pc/>.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001975
Guido van Rossume530c581995-04-10 12:32:16 +000019767.3. Q. Is there a Windows 3.1(1) version of Python?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001977
Guido van Rossume530c581995-04-10 12:32:16 +00001978A. Yes, also see the "pc" subdirectory of the distribution sites,
Guido van Rossum3fc9d731995-07-25 15:10:56 +00001979e.g. <URL:ftp://ftp.python.org/pub/python/pc/>. You may also be able
1980to run either of the Windows NT versions (see next question) if you
1981have Microsoft's "win32s".
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001982
Guido van Rossum91f60831994-02-15 15:52:27 +000019837.4. Q. Is there a Windows NT version of Python?
1984
Guido van Rossum3fc9d731995-07-25 15:10:56 +00001985A. There are two, both sporting DLL support for dynamic loading of
1986Python modules, and extensions to access the Win32 GUI API.
1987
1988Mark Hammond <MHammond@cmutual.com.au> maintains an NT port which
1989includes an interface to the Microsoft Foundation Classes and a Python
1990programming environment using it that's written mostly in Python. See
1991<URL:ftp://ftp.python.org/pub/python/nt/>.
1992
1993Jim Ahlstrom's WPY portable GUI runs on Windows NT and is modeled
1994after the Microsoft Foundation Classes. Source and binaries are
1995available in <URL:ftp://ftp.python.org/pub/python/wpy>.
Guido van Rossum91f60831994-02-15 15:52:27 +00001996
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001997Sam Rushing <rushing@squirl.oau.org> once announced he knows how to
1998build Python for the Windows NT on the DEC Alpha AXP.
Guido van Rossum061f1821994-10-06 16:03:45 +00001999
2000Note that currently there is no unified compilation environment for
2001all NT platforms -- hopefully Microsoft will fix this with the release
2002of Visual C++ 2.0.
2003
Guido van Rossume530c581995-04-10 12:32:16 +000020047.5. Q. Is there a Windows 95 version of Python?
Guido van Rossum7ce61c11994-06-13 15:13:56 +00002005
Guido van Rossum3fc9d731995-07-25 15:10:56 +00002006A. The Windows NT versions might work, otherwise the Windows 3.1(1)
Guido van Rossume530c581995-04-10 12:32:16 +00002007version should work (isn't Windows 95 supposed to be backwards
2008compatible?).
Guido van Rossum7ce61c11994-06-13 15:13:56 +00002009
Guido van Rossume530c581995-04-10 12:32:16 +000020107.6. Q. Is there an OS/2 version of Python?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00002011
Guido van Rossume530c581995-04-10 12:32:16 +00002012A. Yes, also see the "pc" subdirectory of the distribution sites,
Guido van Rossum7be34a81995-05-31 15:17:12 +00002013e.g. <URL:ftp://ftp.python.org/pub/python/pc/>.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00002014
Guido van Rossume530c581995-04-10 12:32:16 +000020157.7. Q. Is there a VMS version of Python?
2016
Guido van Rossum3fc9d731995-07-25 15:10:56 +00002017A. Donn Cave <donn@cac.washington.edu> did a partial port. The
2018results of his efforts are on public display in
Guido van Rossumbf8e7d51995-08-28 03:09:13 +00002019<<URL:ftp://ftp.python.org/pub/python/contrib/vms.tar.gz/>. Someone
2020else is working on a more complete port, for details watch the list.
Guido van Rossume530c581995-04-10 12:32:16 +00002021
Guido van Rossum3fc9d731995-07-25 15:10:56 +000020227.8. Q. What about IBM mainframes, or other non-UNIX platforms?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00002023
Guido van Rossumb0a2ce51995-08-28 19:41:35 +00002024A. I haven't heard about these, except I remember hearing about an
2025OS/9 port and a port to Vxworks (both operating systems for embedded
2026systems). If you're interested in any of this, go directly to the
2027newsgroup and ask there, you may find exactly what you need. For
2028example, a port to MPE/iX 5.0 on HP3000 computers was just announced,
2029see <URL:http://www.allegro.com/software/>.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00002030
Guido van Rossume530c581995-04-10 12:32:16 +000020317.9. Q. Where are the source or Makefiles for the non-UNIX versions?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00002032
Guido van Rossume530c581995-04-10 12:32:16 +00002033A. The standard sources can (almost) be used. Additional sources can
2034be found in the platform-specific subdirectories of the distribution.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00002035
Guido van Rossume530c581995-04-10 12:32:16 +000020367.10. Q. What is the status and support for the non-UNIX versions?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00002037
2038A. I don't have access to most of these platforms, so in general I am
2039dependent on material submitted by volunteers(*). However I strive to
2040integrate all changes needed to get it to compile on a particular
2041platform back into the standard sources, so porting of the next
2042version to the various non-UNIX platforms should be easy.
2043
Guido van Rossuma6c707c1995-01-02 17:32:28 +00002044(*) For the Macintosh, that volunteer is me, with help from Jack
2045Jansen <jack@cwi.nl>.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00002046
Guido van Rossume530c581995-04-10 12:32:16 +000020477.11. Q. I have a PC version but it appears to be only a binary.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00002048Where's the library?
Guido van Rossuma7925f11994-01-26 10:20:16 +00002049
2050A. You still need to copy the files from the distribution directory
Guido van Rossum91f60831994-02-15 15:52:27 +00002051"python/Lib" to your system. If you don't have the full distribution,
Guido van Rossume530c581995-04-10 12:32:16 +00002052you can get the file lib<version>.tar.gz from most ftp sites carrying
2053Python; this is a subset of the distribution containing just those
Guido van Rossum7be34a81995-05-31 15:17:12 +00002054files, e.g. <URL:ftp://ftp.python.org/pub/python/src/lib1.1.tar.gz>.
Guido van Rossuma7925f11994-01-26 10:20:16 +00002055
Guido van Rossum5333c5d1994-04-11 11:06:22 +00002056Once you have installed the library, you need to point sys.path to it.
2057Assuming the library is in C:\misc\python\lib, the following commands
2058will point your Python interpreter to it (note the doubled backslashes
2059-- you can also use single forward slashes instead):
2060
Guido van Rossuma6c707c1995-01-02 17:32:28 +00002061 >>> import sys
2062 >>> sys.path.insert(0, 'C:\\misc\\python\\lib')
2063 >>>
Guido van Rossum5333c5d1994-04-11 11:06:22 +00002064
2065For a more permanent effect, set the environment variable PYTHONPATH,
2066as follows (talking to a DOS prompt):
2067
Guido van Rossuma6c707c1995-01-02 17:32:28 +00002068 C> SET PYTHONPATH=C:\misc\python\lib
Guido van Rossum5333c5d1994-04-11 11:06:22 +00002069
Guido van Rossume530c581995-04-10 12:32:16 +000020707.12. Q. Where's the documentation for the Mac or PC version?
Guido van Rossuma7925f11994-01-26 10:20:16 +00002071
Guido van Rossume530c581995-04-10 12:32:16 +00002072A. The documentation for the Unix version also applies to the Mac and
2073PC versions. Where applicable, differences are indicated in the text.
Guido van Rossuma7925f11994-01-26 10:20:16 +00002074
Guido van Rossume530c581995-04-10 12:32:16 +000020757.13. Q. The Mac (PC) version doesn't seem to have any facilities for
Guido van Rossum91f60831994-02-15 15:52:27 +00002076creating or editing programs apart from entering it interactively, and
2077there seems to be no way to save code that was entered interactively.
2078How do I create a Python program on the Mac (PC)?
Guido van Rossuma7925f11994-01-26 10:20:16 +00002079
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00002080A. Use an external editor. On the Mac, BBEdit seems to be a popular
2081no-frills text editor. I work like this: start the interpreter; edit
2082a module file using BBedit; import and test it in the interpreter;
2083edit again in BBedit; then use the built-in function reload() to
2084re-read the imported module; etc.
Guido van Rossum5333c5d1994-04-11 11:06:22 +00002085
2086Regarding the same question for the PC, Kurt Wm. Hemr writes: "While
2087anyone with a pulse could certainly figure out how to do the same on
2088MS-Windows, I would recommend the NotGNU Emacs clone for MS-Windows.
2089Not only can you easily resave and "reload()" from Python after making
2090changes, but since WinNot auto-copies to the clipboard any text you
2091select, you can simply select the entire procedure (function) which
2092you changed in WinNot, switch to QWPython, and shift-ins to reenter
2093the changed program unit."