blob: 27a885de558da12ef333821b39e02a6831361a79 [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)
5Reply-to: guido@cwi.nl (Guido van Rossum)
6Approved: news-answers-request@MIT.Edu
7
8Archive-name: python-faq/part1
Guido van Rossuma6c707c1995-01-02 17:32:28 +00009Version: 1.18
10Last-modified: 2 January 1995
Guido van Rossuma7925f11994-01-26 10:20:16 +000011
12This article contains answers to Frequently Asked Questions about
13Python (an object-oriented interpreted programming language -- see
14the answer to question 1.1 for a short overview).
15
Guido van Rossuma6c707c1995-01-02 17:32:28 +000016Copyright 1993-1995 Guido van Rossum. Unchanged electronic
Guido van Rossuma7925f11994-01-26 10:20:16 +000017redistribution of this FAQ is allowed. Printed redistribution only
18with permission of the author. No warranties.
19
20Author's address:
Guido van Rossuma6c707c1995-01-02 17:32:28 +000021 Guido van Rossum
22 CWI, dept. CST
23 Kruislaan 413
24 P.O. Box 94079
25 1090 GB Amsterdam
26 The Netherlands
27Email: guido@cwi.nl
Guido van Rossuma7925f11994-01-26 10:20:16 +000028
29The latest version of this FAQ is available by anonymous ftp from
Guido van Rossuma6c707c1995-01-02 17:32:28 +000030<URL:ftp://ftp.cwi.nl/pub/python/python-FAQ>. It will also be posted
31regularly to the newsgroups comp.answers <URL:news:comp.answers> and
32comp.lang.python <URL:news:comp.lang.python>.
Guido van Rossuma7925f11994-01-26 10:20:16 +000033
Guido van Rossuma6c707c1995-01-02 17:32:28 +000034Many FAQs, including this one, are available by anonymous ftp
35<URL:ftp://rtfm.mit.edu/pub/usenet/news.answers/>. The name under
36which a FAQ is archived appears in the Archive-name line at the top of
37the article. This FAQ is archived as python-faq/part1
38<URL:ftp://rtfm.mit.edu/pub/usenet/news.answers/python-faq/part1>.
Guido van Rossuma7925f11994-01-26 10:20:16 +000039
40There's a mail server on that machine which will send you files from
41the archive by e-mail if you have no ftp access. You send a e-mail
Guido van Rossuma6c707c1995-01-02 17:32:28 +000042message to <mail-server@rtfm.mit.edu> containing the single word help
43in the message body to receive instructions.
Guido van Rossuma7925f11994-01-26 10:20:16 +000044
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000045Skip Montanaro <skip@automatrix.com> maintains an HTML version of this
Guido van Rossuma6c707c1995-01-02 17:32:28 +000046FAQ, <URL:http://www.automatrix.com/~skip/python-faq.html>.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000047
Guido van Rossuma7925f11994-01-26 10:20:16 +000048This FAQ is divided in the following chapters:
49
50 1. General information and availability
51 2. Python in the real world
52 3. Building Python
53 4. Programming in Python
54 5. Extending Python
55 6. Python's design
56 7. Using Python on non-UNIX platforms
57
58To find the start of a particular chapter, search for the chapter number
59followed by a dot and a space at the beginning of a line (e.g. to
60find chapter 4 in vi, type /^4\. /).
61
62Here's an overview of the questions per chapter:
63
64 1. General information and availability
65 1.1. Q. What is Python?
66 1.2. Q. Why is it called Python?
67 1.3. Q. How do I obtain a copy of the Python source?
68 1.4. Q. How do I get documentation on Python?
Guido van Rossumc50158e1994-05-31 09:18:50 +000069 1.5. Q. Are there other ftp sites that mirror the Python distribution?
70 1.6. Q. Is there a newsgroup or mailing list devoted to Python?
Guido van Rossuma6c707c1995-01-02 17:32:28 +000071 1.7. Q. Is there a WWW page devoted to Python?
72 1.8. Q. Is there a book on Python, or will there be one out soon?
73 1.9. Q. Are there any published articles about Python that I can quote?
74 1.10. Q. Are there short introductory papers or talks on Python?
75 1.11. Q. How does the Python version numbering scheme work?
76 1.12. Q. Are there other ftp sites that carry Python related material?
77 1.13. Q. Are there copyright restrictions on the use of Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +000078
79 2. Python in the real world
80 2.1. Q. How many people are using Python?
81 2.2. Q. Have any significant projects been done in Python?
82 2.3. Q. Are there any commercial projects going on using Python?
Guido van Rossum95f61a71994-01-26 17:23:37 +000083 2.4. Q. How stable is Python?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000084 2.5. Q. When will the next version be released?
85 2.6. Q. What new developments are expected for Python in the future?
86 2.7. Q. Is it reasonable to propose incompatible changes to Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +000087
88 3. Building Python
Guido van Rossum91f60831994-02-15 15:52:27 +000089 3.1. Q. Is there a test set?
90 3.2. Q. When running the test set, I get complaints about floating point
Guido van Rossuma7925f11994-01-26 10:20:16 +000091 operations, but when playing with floating point operations I cannot
92 find anything wrong with them.
Guido van Rossum9351fdb1994-11-10 23:03:51 +000093 3.3. Q. Link errors building Python with STDWIN 0.9.8. on SGI IRIX.
94 3.4. Q. Link errors building Python with STDWIN 0.9.9.
95 3.5. Q. Link errors after rerunning the configure script.
96 3.6. Q. The python interpreter complains about options passed to a
Guido van Rossum24349991994-02-02 14:12:45 +000097 script (after the script name).
Guido van Rossum9351fdb1994-11-10 23:03:51 +000098 3.7. Q. When building on the SGI, make tries to run python to create
Guido van Rossum5333c5d1994-04-11 11:06:22 +000099 glmodule.c, but python hasn't been built or installed yet.
Guido van Rossum9351fdb1994-11-10 23:03:51 +0000100 3.8. Q. Python built with gcc for the DEC Alpha doesn't work.
101 3.9. Q. I use VPATH but some targets are built in the source directory.
102 3.10. Q. Trouble building or linking with the GNU readline library.
103 3.11. Q. Trouble building Python on Linux.
104 3.12. Q. Trouble with prototypes on Ultrix.
105 3.13. Q. Trouble with posix.listdir on NeXTSTEP 3.2.
106 3.14. Q. Other trouble building Python on platform X.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000107
108 4. Programming in Python
Guido van Rossum24349991994-02-02 14:12:45 +0000109 4.1. Q. Is there a source code level debugger with breakpoints, step,
110 etc.?
111 4.2. Q. Can I create an object class with some methods implemented in
Guido van Rossuma7925f11994-01-26 10:20:16 +0000112 C and others in Python (e.g. through inheritance)? (Also phrased as:
113 Can I use a built-in type as base class?)
Guido van Rossum24349991994-02-02 14:12:45 +0000114 4.3. Q. Is there a curses/termcap package for Python?
115 4.4. Q. Is there an equivalent to C's onexit() in Python?
116 4.5. Q. When I define a function nested inside another function, the
Guido van Rossuma7925f11994-01-26 10:20:16 +0000117 nested function seemingly can't access the local variables of the
118 outer function. What is going on? How do I pass local data to a
119 nested function?
Guido van Rossum24349991994-02-02 14:12:45 +0000120 4.6. Q. How do I iterate over a sequence in reverse order?
121 4.7. Q. My program is too slow. How do I speed it up?
122 4.8. Q. When I have imported a module, then edit it, and import it
Guido van Rossuma7925f11994-01-26 10:20:16 +0000123 again (into the same Python process), the changes don't seem to take
124 place. What is going on?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000125 4.9. Q. How do I find the current module name?
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000126 4.10. Q. I have a module in which I want to execute some extra code
127 when it is run as a script. How do I find out whether I am running as
128 a script?
129 4.11. Q. I try to run a program from the Demo directory but it fails
130 with ImportError: No module named ...; what gives?
Guido van Rossum061f1821994-10-06 16:03:45 +0000131 4.12. Q. I have successfully built Python with STDWIN but it can't
132 find some modules (e.g. stdwinevents).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000133 4.13. Q. What GUI toolkits exist for Python?
Guido van Rossum061f1821994-10-06 16:03:45 +0000134 4.14. Q. Are there any interfaces to database packages in Python?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000135 4.15. Q. Is it possible to write obfuscated one-liners in Python?
136 4.16. Q. Is there an equivalent of C's "?:" ternary operator?
137 4.17. Q. My class defines __del__ but it is not called when I delete the
138 object.
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000139 4.18. Q. How do I change the shell environment for programs called
140 using os.popen() or os.system()? Changing os.environ doesn't work.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000141 4.19. Q. What is a class?
142 4.20. Q. What is a method?
143 4.21. Q. What is self?
144 4.22. Q. What is a unbound method?
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000145 4.23. Q. How do I call a method defined in a base class from a derived
146 class that overrides it?
147 4.24. Q. How do I call a method from a base class without using the
148 name of the base class?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000149 4.25. Q. How can I organize my code to make it easier to change the base
150 class?
151 4.26. Q. How can I find the methods or attributes of an object?
Guido van Rossum061f1821994-10-06 16:03:45 +0000152 4.27. Q. I can't seem to use os.read() on a pipe created with os.popen().
153 4.28. Q. How can I create a stand-alone binary from a Python script?
154 4.29. Q. Is there a special lib for writing CGI scripts in Python?
155 4.30. Q. What other WWW tools are there for Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000156
157 5. Extending Python
158 5.1. Q. Can I create my own functions in C?
159 5.2. Q. Can I create my own functions in C++?
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000160 5.3. Q. How can I execute arbitrary Python statements from C?
161 5.4. Q. How can I evaluate an arbitrary Python expression from C?
162 5.5. Q. How do I extract C values from a Python object?
163 5.6. Q. How do I use mkvalue() to create a tuple of arbitrary length?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000164 5.7. Q. What happened to mktuple(), featuring in an example in the
165 Extensions manual?
166 5.8. Q. How do I call an object's method from C?
167 5.9. Q. How do I catch the output from print_error()?
168 5.10. Q. How do I access a module written in Python from C?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000169
170 6. Python's design
171 6.1. Q. Why isn't there a generic copying operation for objects in
172 Python?
173 6.2. Q. Why isn't there a generic way to implement persistent objects
174 in Python? (Persistent == automatically saved to and restored from
175 disk.)
176 6.3. Q. Why isn't there a switch or case statement in Python?
Guido van Rossumc50158e1994-05-31 09:18:50 +0000177 6.4. Q. Why does Python use indentation for grouping of statements?
Guido van Rossum3de27361994-07-25 14:19:33 +0000178 6.5. Q. Why are Python strings immutable?
179 6.6. Q. Why don't strings have methods like index() or sort(), like
180 lists?
181 6.7. Q. Why does Python use methods for some functionality
182 (e.g. list.index()) but functions for other (e.g. len(list))?
183 6.8. Q. Why can't I derive a class from built-in types (e.g. lists or
184 files)?
185 6.9. Q. Why must 'self' be declared and used explicitly in method
186 definitions and calls?
Guido van Rossum061f1821994-10-06 16:03:45 +0000187 6.10. Q. Can't you emulate threads in the interpreter instead of
188 relying on an OS-specific thread implementation?
189 6.11. Q. Why can't lambda forms contain statements?
190 6.12. Q. Why is there no more efficient way of iterating over a dictionary
191 than first constructing the list of keys()?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000192
193 7. Using Python on non-UNIX platforms
Guido van Rossum91f60831994-02-15 15:52:27 +0000194 7.1. Q. Is there a Mac version of Python?
195 7.2. Q. Is there a DOS version of Python?
196 7.3. Q. Is there a Windows version of Python?
197 7.4. Q. Is there a Windows NT version of Python?
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000198 7.5. Q. Is there an OS/2 version of Python?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000199 7.6. Q. Is there a VMS version of Python?
200 7.7. Q. What about IBM mainframes, or other esoteric non-UNIX
201 platforms?
202 7.8. Q. Where are the source or Makefiles for the non-UNIX versions?
203 7.9. Q. What is the status and support for the non-UNIX versions?
204 7.10. Q. I have the PC version but it appears to be only a binary.
205 Where's the library?
206 7.11. Q. Where's the documentation for the Mac or PC version?
207 7.12. Q. The Mac (PC) version doesn't seem to have any facilities for
208 creating or editing programs apart from entering it interactively, and
209 there seems to be no way to save code that was entered interactively.
210 How do I create a Python program on the Mac (PC)?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000211
212To find a particular question, search for the question number followed
213by a dot, a space, and a Q at the beginning of a line (e.g. to find
214question 4.2 in vi, type /^4\.2\. Q/).
215
216
2171. General information and availability
218=======================================
219
2201.1. Q. What is Python?
221
222A. Python is an interpreted, interactive, object-oriented programming
223language. It incorporates modules, exceptions, dynamic typing, very
224high level dynamic data types, and classes. Python combines
225remarkable power with very clear syntax. It has interfaces to many
226system calls and libraries, as well as to various window systems, and
227is extensible in C or C++. It is also usable as an extension language
228for applications that need a programmable interface. Finally, Python
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000229is portable: it runs on many brands of UNIX, on the Mac, and on PCs
230under MS-DOS, Windows, Windows NT, and OS/2.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000231
232To find out more, the best thing to do is to start reading the
233tutorial from the documentation set (see a few questions further
234down).
235
2361.2. Q. Why is it called Python?
237
238A. Apart from being a computer wizard, I'm also a fan of "Monty
239Python's Flying Circus" (a BBC comedy series from the seventies, in
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000240the -- unlikely -- case you didn't know). It occurred to me one day
241that I needed a name that was short, unique, and slightly mysterious.
242And I happened to be reading some scripts from the series at the
243time... So then I decided to call my language Python. But Python is
244not a joke. And don't you associate it with dangerous reptiles
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000245either! (If you need an icon, use an image of the 16-ton weight from
246the TV series or of a can of SPAM :-)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000247
2481.3. Q. How do I obtain a copy of the Python source?
249
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000250A. The latest complete Python source distribution is always available
251by anonymous ftp, e.g.
252<URL:ftp://ftp.cwi.nl/pub/python/python1.1.tar.gz>. It is a gzipped
253tar file containing the complete C source, LaTeX documentation, Python
254library modules, example programs, and several useful pieces of freely
255distributable software. This will compile and run out of the box on
256most UNIX platforms. (See section 7 for non-UNIX information.)
257
258Occasionally a set of patches is issued which has to be applied using
259the patch program. These patches are placed in the same directory,
260e.g. <URL:ftp://ftp.cwi.nl/pub/python/patch1.1.1>.
261
262An index of said ftp directory can be found in the file INDEX. An
263HTML version of the index can be found in the file index.html,
264<URL:ftp://ftp.cwi.nl/pub/python/index.html>.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000265
2661.4. Q. How do I get documentation on Python?
267
268A. The latest Python documentation set is always available by
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000269anonymous ftp, e.g.
270<URL:ftp://ftp.cwi.nl/pub/python/pythondoc-ps1.1.tar.gz>. It is a
271gzipped tar file containing PostScript files of the reference manual,
Guido van Rossum3de27361994-07-25 14:19:33 +0000272the library manual, and the tutorial. Note that the library manual is
273the most important one of the set, as much of Python's power stems
274from the standard or built-in types, functions and modules, all of
275which are described here. PostScript for a high-level description of
276Python is in the file nluug-paper.ps.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000277
Guido van Rossumc50158e1994-05-31 09:18:50 +00002781.5. Q. Are there other ftp sites that mirror the Python distribution?
279
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000280A. The following anonymous ftp sites keep mirrors of the Python
281distribution:
Guido van Rossuma7925f11994-01-26 10:20:16 +0000282
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000283 <URL:ftp://gatekeeper.dec.com/pub/plan/python/>
284 <URL:ftp://ftp.uu.net/languages/python/>
285 <URL:ftp://ftp.wustl.edu/graphics/graphics/sgi-stuff/python/>
286 <URL:ftp://ftp.funet.fi/pub/languages/python/>
287 <URL:ftp://ftp.sunet.se/pub/lang/python/>
288 <URL:ftp://unix.hensa.ac.uk/uunet/languages/python/>
289 <URL:ftp://ftp.sterlng.com/programming/languages/python/>
290 <URL:ftp://ftp.ibp.fr/pub/python/>
Guido van Rossuma7925f11994-01-26 10:20:16 +0000291
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000292Or try archie on the string "python".
Guido van Rossuma7925f11994-01-26 10:20:16 +0000293
Guido van Rossumc50158e1994-05-31 09:18:50 +00002941.6. Q. Is there a newsgroup or mailing list devoted to Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000295
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000296A. There is a newsgroup, comp.lang.python <URL:news:comp.lang.python>,
297and a mailing list. The newsgroup and mailing list are gatewayed into
298each other -- if you can read news it's unnecessary to subscribe to
299the mailing list. Send e-mail to <python-list-request@cwi.nl> to
300(un)subscribe to the mailing list
301<URL:mailto:python-list-request@cwi.nl>. Hypermail archives of
Guido van Rossum061f1821994-10-06 16:03:45 +0000302(nearly) everything posted to the mailing list (and thus the
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000303newsgroup) are available on our WWW server,
304<URL:http://www.cwi.nl/~guido/hypermail/index.html>. The raw archives
305are also available by ftp, e.g.
306<URL:ftp://ftp.cwi.nl/pub/python/mailinglist.gz>. The uncompressed
307versions of these files can be read with the standard UNIX Mail
308program ("Mail -f file") or with nn ("nn file"). To read them using
309MH, you could use "inc -file file".
Guido van Rossuma7925f11994-01-26 10:20:16 +0000310
Guido van Rossuma6c707c1995-01-02 17:32:28 +00003111.7. Q. Is there a WWW page devoted to Python?
312
313A. The official Python home page is
314<URL:http://www.cwi.nl/~guido/Python.html>. Michael McLay at NIST
315maintains a Python page at <URL:http://www.eeel.nist.gov/python/>.
316
3171.8. Q. Is there a book on Python, or will there be one out soon?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000318
Guido van Rossum061f1821994-10-06 16:03:45 +0000319A. I am writing one. Addison-Wesley is interested. Optimistically,
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000320it will be ready for the press by mid-1995. Mark Lutz is working on
321one too -- more details as they emerge.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000322
Guido van Rossuma6c707c1995-01-02 17:32:28 +00003231.9. Q. Are there any published articles about Python that I can quote?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000324
325A. So far the only refereed and published article that describes
326Python in some detail is:
327
328 Guido van Rossum and Jelke de Boer, "Interactively Testing Remote
329 Servers Using the Python Programming Language", CWI Quarterly, Volume
330 4, Issue 4 (December 1991), Amsterdam, pp 283-303.
331
332LaTeX source for this paper is available as part of the Python source
333distribution.
334
Guido van Rossuma6c707c1995-01-02 17:32:28 +00003351.10. Q. Are there short introductory papers or talks on Python?
336
337A. A recent high-level description of Python is:
Guido van Rossuma7925f11994-01-26 10:20:16 +0000338
339 Guido van Rossum, "An Introduction to Python for UNIX/C
340 Programmers", in the proceedings of the NLUUG najaarsconferentie
Guido van Rossum061f1821994-10-06 16:03:45 +0000341 1993 (dutch UNIX users group meeting November 1993).
Guido van Rossuma7925f11994-01-26 10:20:16 +0000342
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000343PostScript for this paper and for the slides used for the accompanying
344presentation is available by ftp as
345<URL:ftp://ftp.cwi.nl/pub/python/nluug-paper.ps> and
346<URL:ftp://ftp.cwi.nl/pub/python/nluug-slides.ps>, respectively.
347
348Slides for a talk on Python that I gave at the Usenix Symposium on
349Very High Level Languages in Santa Fe, NM, USA in October 1995 are
350available as <URL:ftp://ftp.cwi.nl/pub/python/vhll-slides.ps>.
351
3521.11. Q. How does the Python version numbering scheme work?
Guido van Rossum95f61a71994-01-26 17:23:37 +0000353
354A. Python versions are numbered A.B.C. A is the major version number
355-- it is only incremented for major changes in functionality or source
356structure. B is the minor version number, incremented for less
357earth-shattering changes to a release. C is the patchlevel -- it is
358incremented for each new release. Note that in the past, patches have
359added significant changes; in fact the changeover from 0.9.9 to 1.0.0
360was the first time that either A or B changed!
361
Guido van Rossuma6c707c1995-01-02 17:32:28 +00003621.12. Q. Are there other ftp sites that carry Python related material?
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000363
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000364A. An interesting ftp site for Python users is ftp.markv.com; the
365directory pub/python contains a growing collection of interesting
366Python scripts <URL:ftp://ftp.markv.com/pub/python/>. To submit a
367script for inclusion, place it together with a readme file (with
368extension .readme) in the publicly writable directory
369/incoming/python. This service is maintained by Lance Ellinghaus
370<lance@markv.com>. (I've heard complaints about this service not
371being very responsive -- try at your own risk.)
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000372
Guido van Rossuma6c707c1995-01-02 17:32:28 +00003731.13. Q. Are there copyright restrictions on the use of Python?
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000374
375A. Hardly. You can do anything you want with the source, as long as
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000376you leave the copyrights in, and display those copyrights in any
377documentation about Python that you produce. Also, don't use the
378author's institute's name in publicity without prior written
379permission, and don't hold them responsible for anything (read the
380actual copyright for a precise legal wording).
381
382In particular, if you honor the copyright rules, it's OK to use Python
383for commercial use, to sell copies of Python in source or binary form,
384or to sell products that enhance Python or incorporate Python (or part
385of it) in some form. I would still like to know about all commercial
386use of Python!
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000387
Guido van Rossuma7925f11994-01-26 10:20:16 +0000388
3892. Python in the real world
390===========================
391
3922.1. Q. How many people are using Python?
393
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000394A. I don't know, but the maximum number of simultaneous subscriptions
395to the Python mailing list before it was gatewayed into the newsgroup
396was about 180 (several of which were local redistribution lists). I
397believe that many active Python users don't bother to subscribe to the
398list, and now that there's a newsgroup the mailing list subscription
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000399is even less meaningful. I see new names on the newsgroup all the
400time and my best guess is that there are currently at least several
401thousands of users.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000402
4032.2. Q. Have any significant projects been done in Python?
404
405A. Here at CWI (the home of Python), we have written a 20,000 line
406authoring environment for transportable hypermedia presentations, a
Guido van Rossum5333c5d1994-04-11 11:06:22 +00004075,000 line multimedia teleconferencing tool, as well as many many
408smaller programs.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000409
410The University of Virginia uses Python to control a virtual reality
411engine. Contact: Matt Conway <conway@virginia.edu>.
412
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000413The ILU project at Xerox PARC can generate Python glue for ILU
414interfaces. See <URL:ftp://ftp.parc.xerox.com/pub/ilu/ilu.html>.
415
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000416If you have done a significant project in Python that you'd like to be
417included in the list above, send me email!
Guido van Rossuma7925f11994-01-26 10:20:16 +0000418
4192.3. Q. Are there any commercial projects going on using Python?
420
421A. Several companies have revealed to me that they are planning or
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000422considering to use Python in a future product.
423
424The furthest is Sunrise Software, who already have a product out using
425Python -- they use Python for a GUI management application and an SNMP
Guido van Rossum061f1821994-10-06 16:03:45 +0000426network management application. Contact: <info@sunrise.com>.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000427
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000428Infoseek uses Python to implement their commercial WWW information
429retrieval service <URL:http://www.infoseek.com/>. Contact:
430<info@infoseek.com>.
Guido van Rossumf8c76d01994-08-17 12:19:53 +0000431
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000432Michael Powers of daVinci Time & Space is "writing tons-o-python for
433interactive television entertainment." Contact: <powers@dvts.com>.
434
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000435Paul Everitt of Connecting Minds is planning a Lotus Notes gateway.
436Contact: <Paul.Everitt@cminds.com>. Or see their WWW server
437<URL:http://www.cminds.com/>.
438
Guido van Rossuma7925f11994-01-26 10:20:16 +0000439Individuals at many other companies are using Python for
440internal development (witness their contributions to the Python
Guido van Rossum5333c5d1994-04-11 11:06:22 +0000441mailing list or newsgroup).
Guido van Rossuma7925f11994-01-26 10:20:16 +0000442
443Python has also been elected as an extension language by MADE, a
444consortium supported by the European Committee's ESPRIT program and
445consisting of Bull, CWI and some other European companies. Contact:
446Ivan Herman <ivan@cwi.nl>.
447
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000448If you'd like to be included in the list above, send me email!
449
Guido van Rossum95f61a71994-01-26 17:23:37 +00004502.4. Q. How stable is Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000451
Guido van Rossum3de27361994-07-25 14:19:33 +0000452A. Very stable. While the current version number would suggest it is
453in the early stages of development, in fact new, stable releases
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000454(numbered 0.9.x through 1.1.x) have been coming out roughly every 3 to
4556 months for the past four years.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000456
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00004572.5. Q. When will the next version be released?
458
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000459A. I am planning to release 1.2 in January 1995. It will contain
460hooks into the implementation of the import command, a (still limited)
461form of persistent objects, and the usual complement of bug fixes
462(including many fixed memory leaks and thread problems).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000463
4642.6. Q. What new developments are expected for Python in the future?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000465
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000466A. There will be better ports to the Mac, DOS, Windows, Windows NT, and
Guido van Rossumf8c76d01994-08-17 12:19:53 +0000467OS/2. The Windows ports will support dynamically loaded modules using
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000468DLLs.
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000469
470Also planned is improved support for embedding Python in other
471applications, e.g. by renaming most global symbols to have a "Py"
472prefix and providing more documentation and threading support.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000473
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000474Some proposals were discussed at the recent Python workshop:
475
476 - persistent objects
477
478 - safe execution of untrusted Python code
479
480 - extensions to the import statement for managing packages
481 (groups of related modules)
482
483 - automatic generation of C/C++ interface glue
484
485 - interfaces to OMG IDL (== Interface Definition Language by
486 the Object Management Group)
487
488 - a portable GUI API (Graphical User Interface Application
489 Programmers Interface)
490
491 - module customization tools
492
493 - standardized documentation strings on module, class and
494 function objects
495
496 - the formation of a Python Steering Committee
497
498 - another Python Workshop
499
500For more info, have a look at the WWW page for the last Python
501Workshop <URL:http://www.eeel.nist.gov/python/workshop11-94/>.
502
503
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00005042.7. Q. Is it reasonable to propose incompatible changes to Python?
Guido van Rossum3de27361994-07-25 14:19:33 +0000505
506A. In general, no. There are already millions of lines of Python code
507around the world, so any changes in the language that invalidates more
508than a very small fraction of existing programs has to be frowned
509upon. Even if you can provide a conversion program, there still is
510the problem of updating all documentation. Providing a gradual
511upgrade path is the only way if a feature has to be changed.
512
Guido van Rossuma7925f11994-01-26 10:20:16 +0000513
5143. Building Python
515==================
516
Guido van Rossum91f60831994-02-15 15:52:27 +00005173.1. Q. Is there a test set?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000518
519A. Yes, simply do "import testall" (or "import autotest" if you aren't
520interested in the output). The standard modules whose name begins
521with "test" together comprise the test. The test set doesn't test
522*all* features of Python but it goes a long way to confirm that a new
523port is actually working. The Makefile contains an entry "make test"
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000524which runs the autotest module. NOTE: if "make test" fails, run the
525tests manually ("import testall") to see what goes wrong before
526reporting the error.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000527
Guido van Rossum91f60831994-02-15 15:52:27 +00005283.2. Q. When running the test set, I get complaints about floating point
Guido van Rossuma7925f11994-01-26 10:20:16 +0000529operations, but when playing with floating point operations I cannot
530find anything wrong with them.
531
532A. The test set makes occasional unwarranted assumptions about the
533semantics of C floating point operations. Until someone donates a
534better floating point test set, you will have to comment out the
535offending floating point tests and execute similar tests manually.
536
Guido van Rossum061f1821994-10-06 16:03:45 +00005373.3. Q. Link errors building Python with STDWIN 0.9.8. on SGI IRIX.
Guido van Rossum24349991994-02-02 14:12:45 +0000538
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000539A. Get STDWIN 0.9.9 <URL:ftp://ftp.cwi.nl/pub/stdwin/stdwin0.9.9.tar.gz>.
Guido van Rossum061f1821994-10-06 16:03:45 +0000540
Guido van Rossum9351fdb1994-11-10 23:03:51 +00005413.4. Q. Link errors building Python with STDWIN 0.9.9.
Guido van Rossum061f1821994-10-06 16:03:45 +0000542
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000543A. Probably routines like 'tereate', 'tenew' etc. The STDWIN 0.9.9
Guido van Rossum061f1821994-10-06 16:03:45 +0000544distribution requires that you add TWO libraries from stdwin to the
545line for stdwin in the Setupfile. Use something like this (all on one
546line!):
547
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000548 stdwin stdwinmodule.c -I$(STDWIN)/H $(STDWIN)/Packs/textedit/libtextedit.a $(STDWIN)/Ports/x11/libstdwin.a -lX11
Guido van Rossum24349991994-02-02 14:12:45 +0000549
Guido van Rossum9351fdb1994-11-10 23:03:51 +00005503.5. Q. Link errors after rerunning the configure script.
Guido van Rossum24349991994-02-02 14:12:45 +0000551
552A. It is generally necessary to run "make clean" after a configuration
553change.
554
Guido van Rossum9351fdb1994-11-10 23:03:51 +00005553.6. Q. The python interpreter complains about options passed to a
Guido van Rossum24349991994-02-02 14:12:45 +0000556script (after the script name).
557
558A. You are probably linking with GNU getopt, e.g. through -liberty.
Guido van Rossumf8c76d01994-08-17 12:19:53 +0000559Don't. The reason for the complaint is that GNU getopt, unlike System
560V getopt and other getopt implementations, doesn't consider a
561non-option to be the end of the option list. A quick (and compatible)
562fix for scripts is to add "--" to the interpreter, like this:
563
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000564 #! /usr/local/bin/python --
Guido van Rossumf8c76d01994-08-17 12:19:53 +0000565
566You can also use this interactively:
567
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000568 python -- script.py [options]
569
570Note that a working getopt implementation is provided in the Python
571distribution (in Python/getopt.c) but not automatically used.
Guido van Rossum24349991994-02-02 14:12:45 +0000572
Guido van Rossum9351fdb1994-11-10 23:03:51 +00005733.7. Q. When building on the SGI, make tries to run python to create
Guido van Rossum24349991994-02-02 14:12:45 +0000574glmodule.c, but python hasn't been built or installed yet.
575
576A. Comment out the line mentioning glmodule.c in Setup and build a
577python without gl first; install it or make sure it is in your $PATH,
578then edit the Setup file again to turn on the gl module, and make
579again. You don't need to do "make clean"; you do need to run "make
580Makefile" in the Modules subdirectory (or just run "make" at the
581toplevel).
582
Guido van Rossum9351fdb1994-11-10 23:03:51 +00005833.8. Q. Python built with gcc for the DEC Alpha doesn't work.
Guido van Rossum3de27361994-07-25 14:19:33 +0000584
585People have reported problems with both gcc 2.5.8 and 2.6.0. The DEC
586OSF/1 cc compiler does not have these problems so it's probably gcc's
587fault. One person reported that the problem went away when using -g
588instead of -O so this may be an option if you insist on using gcc. If
589someone tracks it down more completely I'd like to hear about it!
590
Guido van Rossum9351fdb1994-11-10 23:03:51 +00005913.9. Q. I use VPATH but some targets are built in the source directory.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000592
593A. On some systems (e.g. Sun), if the target already exists in the
594source directory, it is created there instead of in the build
595directory. This is usually because you have previously built without
596VPATH. Try running "make clobber" in the source directory.
597
Guido van Rossum9351fdb1994-11-10 23:03:51 +00005983.10. Q. Trouble building or linking with the GNU readline library.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000599
600A. Consider using readline 2.0. From the Python 1.1 README:
601
602- You can use the GNU readline library to improve the interactive user
603interface: this gives you line editing and command history when
604calling python interactively. You need to configure build the GNU
605readline library before running the configure script. Its sources are
606no longer distributed with Python; you can ftp them from any GNU
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000607mirror site, or from its home site
608<URL:ftp://slc2.ins.cwru.edu/pub/dist/readline-2.0.tar.gz> (or a
609higher version number -- using version 1.x is not recommended). Pass
610the Python configure script the option --with-readline=DIRECTORY where
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000611DIRECTORY is the absolute pathname of the directory where you've built
612the readline library. Some hints on building and using the readline
613library:
614
615- On SGI IRIX 5, you may have to add the following
616to rldefs.h:
617
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000618 #ifndef sigmask
619 #define sigmask(sig) (1L << ((sig)-1))
620 #endif
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000621
622- On most systems, you will have to add #include "rldefs.h" to the
623top of several source files, and if you use the VPATH feature, you
624will have to add dependencies of the form foo.o: foo.c to the
625Makefile for several values of foo.
626
627- The readline library requires use of the termcap library. A
628known problem with this is that it contains entry points which
Guido van Rossum061f1821994-10-06 16:03:45 +0000629cause conflicts with the STDWIN and SGI GL libraries. The STDWIN
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000630conflict can be solved (and will be, in the next release of
Guido van Rossum061f1821994-10-06 16:03:45 +0000631STDWIN) by adding a line saying '#define werase w_erase' to the
632stdwin.h file (in the STDWIN distribution, subdirectory H). The
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000633GL conflict has been solved in the Python configure script by a
634hack that forces use of the static version of the termcap library.
635
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000636- Check the newsgroup gnu.bash.bugs <URL:news:gnu.bash.bugs> for
637specific problems with the readline library (I don't get this group
638here but I've been told that it is the place for readline bugs.)
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000639
Guido van Rossum9351fdb1994-11-10 23:03:51 +00006403.11. Q. Trouble building Python on Linux.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000641
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000642A. (Adapted from Bennet Todd:)
643
644It seems to work for some people but not for others.
645
646This might depend on which Linux release you're using. Older Linux
647releases had (understandably) fewer gracious hack to improve Unix
648compatibility; really old Linux was Posix 1003.1 compatible, without
649nearly as much Unix compatibility as one might have wished. Current
650releases of Linux build most current free software, either BSD or
651System V, with little or no trouble.
652
653Besides that, there are quite a few different releases currently,
654including MCC, Slackware, SLS, and Debian, and probably more. The
655differences between their compilation environments are far smaller
656than they used to be, but they're still updated with different
657frequencies. It's likely that the current Slackware works fine, as it
658currently seems to be the most popular. But that's just a guess.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000659
Guido van Rossum9351fdb1994-11-10 23:03:51 +00006603.12. Q. Trouble with prototypes on Ultrix.
Guido van Rossum72eb83c1994-10-07 11:33:28 +0000661
662A. Ultrix cc seems broken -- use gcc, or edit config.h to #undef
663HAVE_PROTOTYPES.
664
Guido van Rossum9351fdb1994-11-10 23:03:51 +00006653.13. Q. Trouble with posix.listdir on NeXTSTEP 3.2.
666
667A. The problem seems to be that that the NeXT posix library and the
668NeXT dynamic loading library are incompatible. Mike Carlton reports
669that the following worked for him (from a clean 1.1 distribution):
670
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000671 1) ./configure
672 2) edited config.status and changed
673 OPT='-O'
674 to
675 OPT='-posix -O'
676 3) edited Python/import.c and commented out the section
677 #if defined(NeXT) || defined(WITH_RLD)
678 #define DYNAMIC_LINK
679 #define USE_RLD
680 #endif
681 this disables dynamic loading
682 4) make
Guido van Rossum9351fdb1994-11-10 23:03:51 +0000683
6843.14. Q. Other trouble building Python on platform X.
Guido van Rossum95f61a71994-01-26 17:23:37 +0000685
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000686A. Please email me the details <URL:mailto:guido@cwi.nl> and I'll look
687into it. Please provide as many details as possible. In particular,
688if you don't tell me what type of computer and what operating system
689(and version) you are using it will be difficult for me to figure out
690what is the matter. If you get a specific error message, please email
691it to me too.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000692
693
6944. Programming in Python
695========================
696
Guido van Rossum24349991994-02-02 14:12:45 +00006974.1. Q. Is there a source code level debugger with breakpoints, step,
698etc.?
699
700A. Yes. Check out module pdb; pdb.help() prints the documentation (or
701you can read it as Lib/pdb.doc). If you use the STDWIN option,
702there's also a windowing interface, wdb. You can write your own
703debugger by using the code for pdb or wdb as an example.
704
7054.2. Q. Can I create an object class with some methods implemented in
Guido van Rossuma7925f11994-01-26 10:20:16 +0000706C and others in Python (e.g. through inheritance)? (Also phrased as:
707Can I use a built-in type as base class?)
708
709A. No, but you can easily create a Python class which serves as a
710wrapper around a built-in object, e.g. (for dictionaries):
711
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000712 # A user-defined class behaving almost identical
713 # to a built-in dictionary.
714 class UserDict:
715 def __init__(self): self.data = {}
716 def __repr__(self): return repr(self.data)
717 def __cmp__(self, dict):
718 if type(dict) == type(self.data):
719 return cmp(self.data, dict)
720 else:
721 return cmp(self.data, dict.data)
722 def __len__(self): return len(self.data)
723 def __getitem__(self, key): return self.data[key]
724 def __setitem__(self, key, item): self.data[key] = item
725 def __delitem__(self, key): del self.data[key]
726 def keys(self): return self.data.keys()
727 def items(self): return self.data.items()
728 def values(self): return self.data.values()
729 def has_key(self, key): return self.data.has_key(key)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000730
Guido van Rossum24349991994-02-02 14:12:45 +00007314.3. Q. Is there a curses/termcap package for Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000732
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000733A. Yes -- Lance Ellinghaus has written a module that interfaces to
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000734System V's "ncurses". If you know a little curses and some Python,
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000735it's straightforward to use. It is part of the standard Python
736distribution, but not configured by default -- you must enable it by
737editing Modules/Setup. It requires a System V curses implementation.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000738
739You could also consider using the "alfa" (== character cell) version
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000740of STDWIN. (Standard Window System Interface, a portable windowing
741system interface by myself <URL:ftp://ftp.cwi.nl/pub/stdwin/>.) This
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000742will also prepare your program for porting to windowing environments
743such as X11 or the Macintosh.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000744
Guido van Rossum24349991994-02-02 14:12:45 +00007454.4. Q. Is there an equivalent to C's onexit() in Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000746
747A. Yes, if you import sys and assign a function to sys.exitfunc, it
748will be called when your program exits, is killed by an unhandled
749exception, or (on UNIX) receives a SIGHUP or SIGTERM signal.
750
Guido van Rossum24349991994-02-02 14:12:45 +00007514.5. Q. When I define a function nested inside another function, the
Guido van Rossuma7925f11994-01-26 10:20:16 +0000752nested function seemingly can't access the local variables of the
753outer function. What is going on? How do I pass local data to a
754nested function?
755
756A. Python does not have arbitrarily nested scopes. When you need to
757create a function that needs to access some data which you have
758available locally, create a new class to hold the data and return a
759method of an instance of that class, e.g.:
760
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000761 class MultiplierClass:
762 def __init__(self, factor):
763 self.factor = factor
764 def multiplier(self, argument):
765 return argument * self.factor
Guido van Rossuma7925f11994-01-26 10:20:16 +0000766
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000767 def generate_multiplier(factor):
768 return MultiplierClass(factor).multiplier
Guido van Rossuma7925f11994-01-26 10:20:16 +0000769
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000770 twice = generate_multiplier(2)
771 print twice(10)
772 # Output: 20
773
774An alternative solution uses default arguments, e.g.:
775
776 def generate_multiplier(factor):
777 def multiplier(arg, fact = factor):
778 return arg*fact
779 return multiplier
780
781 twice = generate_multiplier(2)
782 print twice(10)
783 # Output: 20
Guido van Rossuma7925f11994-01-26 10:20:16 +0000784
Guido van Rossum24349991994-02-02 14:12:45 +00007854.6. Q. How do I iterate over a sequence in reverse order?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000786
787A. If it is a list, the fastest solution is
788
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000789 list.reverse()
790 try:
791 for x in list:
792 "do something with x"
793 finally:
794 list.reverse()
Guido van Rossuma7925f11994-01-26 10:20:16 +0000795
796This has the disadvantage that while you are in the loop, the list
797is temporarily reversed. If you don't like this, you can make a copy.
798This appears expensive but is actually faster than other solutions:
799
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000800 rev = list[:]
801 rev.reverse()
802 for x in rev:
803 <do something with x>
Guido van Rossuma7925f11994-01-26 10:20:16 +0000804
805If it isn't a list, a more general but slower solution is:
806
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000807 i = len(list)
808 while i > 0:
809 i = i-1
810 x = list[i]
811 <do something with x>
Guido van Rossuma7925f11994-01-26 10:20:16 +0000812
813A more elegant solution, is to define a class which acts as a sequence
814and yields the elements in reverse order (solution due to Steve
815Majewski):
816
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000817 class Rev:
818 def __init__(self, seq):
819 self.forw = seq
820 def __len__(self):
821 return len(self.forw)
822 def __getitem__(self, i):
823 return self.forw[-(i + 1)]
Guido van Rossuma7925f11994-01-26 10:20:16 +0000824
825You can now simply write:
826
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000827 for x in Rev(list):
828 <do something with x>
Guido van Rossuma7925f11994-01-26 10:20:16 +0000829
Guido van Rossum061f1821994-10-06 16:03:45 +0000830Unfortunately, this solution is slowest of all, due to the method
Guido van Rossuma7925f11994-01-26 10:20:16 +0000831call overhead...
832
Guido van Rossum24349991994-02-02 14:12:45 +00008334.7. Q. My program is too slow. How do I speed it up?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000834
835A. That's a tough one, in general. There are many tricks to speed up
836Python code; I would consider rewriting parts in C only as a last
837resort. One thing to notice is that function and (especially) method
838calls are rather expensive; if you have designed a purely OO interface
839with lots of tiny functions that don't do much more than get or set an
840instance variable or call another method, you may consider using a
841more direct way, e.g. directly accessing instance variables. Also see
842the standard module "profile" (described in the file
843"python/lib/profile.doc") which makes it possible to find out where
844your program is spending most of its time (if you have some patience
845-- the profiling itself can slow your program down by an order of
846magnitude).
847
Guido van Rossum24349991994-02-02 14:12:45 +00008484.8. Q. When I have imported a module, then edit it, and import it
Guido van Rossuma7925f11994-01-26 10:20:16 +0000849again (into the same Python process), the changes don't seem to take
850place. What is going on?
851
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000852A. For reasons of efficiency as well as consistency, Python only reads
853the module file on the first time a module is imported (otherwise a
854program consisting of many modules, each of which imports the same
855basic module, would read the basic module over and over again). To
856force a changed module being read again, do this:
Guido van Rossuma7925f11994-01-26 10:20:16 +0000857
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000858 import modname
859 reload(modname)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000860
861Warning: this technique is not 100% fool-proof. In particular,
862modules containing statements like
863
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000864 from modname import some_objects
Guido van Rossuma7925f11994-01-26 10:20:16 +0000865
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000866will continue to work with the old version of the imported objects.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000867
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00008684.9. Q. How do I find the current module name?
Guido van Rossum24349991994-02-02 14:12:45 +0000869
Guido van Rossum061f1821994-10-06 16:03:45 +0000870A. A module can find out its own module name by looking at the
Guido van Rossum24349991994-02-02 14:12:45 +0000871(predefined) global variable __name__. If this has the value
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000872'__main__' you are running as a script.
873
Guido van Rossuma6c707c1995-01-02 17:32:28 +00008744.10. Q. I have a module in which I want to execute some extra code
875when it is run as a script. How do I find out whether I am running as
876a script?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000877
878A. See the previous question. E.g. if you put the following on the
879last line of your module, main() is called only when your module is
880running as a script:
Guido van Rossum24349991994-02-02 14:12:45 +0000881
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000882 if __name__ == '__main__': main()
Guido van Rossum24349991994-02-02 14:12:45 +0000883
Guido van Rossuma6c707c1995-01-02 17:32:28 +00008844.11. Q. I try to run a program from the Demo directory but it fails
885with ImportError: No module named ...; what gives?
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000886
Guido van Rossum061f1821994-10-06 16:03:45 +0000887A. This is probably an optional module (written in C!) which hasn't
888been configured on your system. This especially happens with modules
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000889like "Tkinter", "stdwin", "gl", "Xt" or "Xm". For Tkinter, STDWIN and
890many other modules, see Modules/Setup.in for info on how to add these
891modules to your Python, if it is possible at all. Sometimes you will
892have to ftp and build another package first (e.g. STDWIN). Sometimes
893the module only works on specific platforms (e.g. gl only works on SGI
894machines).
895
896NOTE: if the complaint is about "Tkinter" (upper case T) and you have
897already configured module "tkinter" (lower case t), the solution is
898*not* to rename tkinter to Tkinter or vice versa. There is probably
899something wring with your module search path. Check out the value of
900sys.path.
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000901
Guido van Rossum061f1821994-10-06 16:03:45 +0000902For X-related modules (Xt and Xm) you will have to do more work: they
903are currently not part of the standard Python distribution. You will
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000904have to ftp the Extensions tar file, e.g.
905<URL:ftp://ftp.cwi.nl/pub/python/extensions.tar.gz> and follow the
906instructions there. Note: the X related modules are still somewhat
907flakey, so don't try this unless you alread know a bit or two about
908building X applications on your platform.
Guido van Rossum061f1821994-10-06 16:03:45 +0000909
910See also the next question.
911
9124.12. Q. I have successfully built Python with STDWIN but it can't
913find some modules (e.g. stdwinevents).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000914
915A. There's a subdirectory of the library directory named 'stdwin'
916which should be in the default module search path. There's a line in
917Modules/Setup(.in) that you have to enable for this purpose --
Guido van Rossum061f1821994-10-06 16:03:45 +0000918unfortunately in the latest release it's not near the other
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000919STDWIN-related lines so it's easy to miss it.
920
9214.13. Q. What GUI toolkits exist for Python?
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000922
923A. Depending on what platform(s) you are aiming at, there are several.
924
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000925- There's a neat object-oriented interface to the Tcl/Tk widget set,
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000926called Tkinter. As of python 1.1, it is part of the standard Python
927distribution -- all you need to do is enable it in Modules/Setup
928(provided you have already installed Tk and Tcl). This is probably
929the easiest to install and use, and the most complete widget set. It
930is also very likely that in the future the standard Python GUI API
931will be based on or at least look very much like the Tkinter
932interface. For more info about Tk, including pointers to the source,
933see John Ousterhout's home page
934<URL:http://playground.Sun.COM:80/~ouster/>.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000935
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000936- The standard Python distribution comes with an interface to STDWIN,
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000937a platform-independent low-level windowing interface. You have to ftp
938the source for STDWIN separately,
939e.g. <URL:ftp://ftp.cwi.nl/pub/stdwin/> or gatekeeper.dec.com in
940pub/misc/stdwin <URL:ftp://gatekeeper.dec.com/pub/misc/stdwin/>.
941STDWIN runs under X11 or the Mac; a Windows port has been attempted
942but I can't seem to get it working. Note that STDWIN is really not
943powerful enough to implement a modern GUI (no widgets, etc.) and that
944I don't have the time to maintain or extend it, so you may be better
945off using Tkinter or the Motif interface, unless you require
946portability to the Mac (which is also offered by SUIT, by the way --
947see below).
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000948
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000949- For SGI IRIX only, there's an interface to the complete GL (Graphics
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000950Library -- low level but very good 3D capabilities) as well as to
951FORMS (a buttons-and-sliders-etc package built on top of GL by Mark
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000952Overmars -- ftp'able from <URL:ftp://ftp.cs.ruu.nl/pub/SGI/FORMS/>).
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000953
954- There's an interface to X11, including the Athena and Motif widget
955sets (and a few individual widgets, like Mosaic's HTML widget and
956SGI's GL widget) in the Extensions set, which is separately ftp'able
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000957<URL:ftp://ftp.cwi.nl/pub/python/extensions.tar.gz>.
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000958
959- There's an interface to SUIT, the U of Virginia's Simple User
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000960Interface Toolkit; it can be ftp'ed from
961<URL:ftp://uvacs.cs.virginia.edu/pub/suit/python/SUIT_python.tar.Z>.
962A PC binary of Python 1.0.2 compiled with DJGPP and with SUIT support
963built-in has been made available by Antonio Costa
964<URL:ftp://asterix.inescn.pt/pub/PC/python/pyt102su.exe> (a
965self-extracting archive). Note that the UVa people themselves have
966expressed doubts about SUIT, and are planning to build a Python GUI
967API based upon Tk (though not necessarily on Tkinter); see
968<URL:http://server.cs.virginia.edu/~tnb2d/IT/IT.html>.
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000969
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000970- There's an interface to WAFE, a Tcl interface to the X11 Motif and
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000971Athena widget sets. Last I heard about it it was included in the WAFE
9721.0 prerelease
973<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 +0000974
Guido van Rossum061f1821994-10-06 16:03:45 +00009754.14. Q. Are there any interfaces to database packages in Python?
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000976
977A. There's an interface to SYBASE by John Redford
978<jredford@lehman.com>.
979
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000980There's also an interface to metalbase by Lance Ellinghaus
981<lance@markv.com>; it is part of the separate Extensions distribution
982<URL:ftp://ftp.cwi.nl/pub/python/extensions.tar.gz>.
Guido van Rossum7ce61c11994-06-13 15:13:56 +0000983
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000984Anthony Baxter <anthony.baxter@aaii.oz.au> has written an interface to
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000985mSQL (mini-SQL). <URL:ftp://ftp.cwi.nl/pub/python/PymSQL.tar.gz>.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +0000986
9874.15. Q. Is it possible to write obfuscated one-liners in Python?
Guido van Rossumc24da7c1994-09-23 14:08:41 +0000988
989A. Yes. See the following three examples, due to Ulf Bartelt:
990
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000991 # Primes < 1000
992 print filter(None,map(lambda y:y*reduce(lambda x,y:x*y!=0,
993 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 +0000994
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000995 # First 10 Fibonacci numbers
996 print map(lambda x,f=lambda x,f:(x<=1) or (f(x-1,f)+f(x-2,f)): f(x,f),
997 range(10))
Guido van Rossumc24da7c1994-09-23 14:08:41 +0000998
Guido van Rossuma6c707c1995-01-02 17:32:28 +0000999 # Mandelbrot set
1000 print (lambda Ru,Ro,Iu,Io,IM,Sx,Sy:reduce(lambda x,y:x+y,map(lambda y,
1001 Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,Sy=Sy,L=lambda yc,Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,i=IM,
1002 Sx=Sx,Sy=Sy:reduce(lambda x,y:x+y,map(lambda x,xc=Ru,yc=yc,Ru=Ru,Ro=Ro,
1003 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
1004 >=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(
1005 64+F(Ru+x*(Ro-Ru)/Sx,yc,0,0,i)),range(Sx))):L(Iu+y*(Io-Iu)/Sy),range(Sy
1006 ))))(-2.1, 0.7, -1.2, 1.2, 30, 80, 24)
1007 # \___ ___/ \___ ___/ | | |__ lines on screen
1008 # V V | |______ columns on screen
1009 # | | |__________ maximum of "iterations"
1010 # | |_________________ range on y axis
1011 # |____________________________ range on x axis
Guido van Rossumc24da7c1994-09-23 14:08:41 +00001012
1013Don't try this at home, kids!
1014
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000010154.16. Q. Is there an equivalent of C's "?:" ternary operator?
Guido van Rossumc24da7c1994-09-23 14:08:41 +00001016
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001017A. Not directly. In many cases you can mimic a?b:c with "a and b or
1018c", but there's a flaw: if b is zero (or empty, or None -- anything
1019that tests false) then c will be selected instead. In many cases you
1020can prove by looking at the code that this can't happen (e.g. because
1021b is a constant or has a type that can never be false), but in general
1022this can be a problem.
1023
1024Steve Majewski (or was it Tim Peters?) suggested the following
1025solution: (a and [b] or [c])[0]. Because [b] is a singleton list it
1026is never false, so the wrong path is never taken; then applying [0] to
1027the whole thing gets the b or c that you really wanted. Ugly, but it
1028gets you there in the rare cases where it is really inconvenient to
1029rewrite your code using 'if'.
1030
10314.17. Q. My class defines __del__ but it is not called when I delete the
1032object.
1033
1034A. There are several possible reasons for this.
1035
1036- The del statement does not necessarily call __del__ -- it simply
1037decrements the object's reference count, and if this reaches zero
1038__del__ is called.
1039
1040- If your data structures contain circular links (e.g. a tree where
1041each child has a parent pointer and each parent has a list of
1042children) the reference counts will never go back to zero. You'll
Guido van Rossum061f1821994-10-06 16:03:45 +00001043have to define an explicit close() method which removes those
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001044pointers. Please don't ever call __del__ directly -- __del__ should
Guido van Rossum061f1821994-10-06 16:03:45 +00001045call close() and close() should make sure that it can be called more
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001046than once for the same object.
1047
1048- If the object has ever been a local variable (or argument, which is
1049really the same thing) to a function that caught an expression in an
1050except clause, chances are that a reference to the object still exists
1051in that function's stack frame as contained in the stack trace.
1052Normally, deleting (better: assigning None to) sys.exc_traceback will
1053take care of this. If you a stack was printed for an unhandled
1054exception in an interactive interpreter, delete sys.last_traceback
1055instead.
1056
1057- There is code that deletes all objects when the interpreter exits,
1058but if your Python has been configured to support threads, it is not
1059called (because other threads may still be active). You can define
Guido van Rossum061f1821994-10-06 16:03:45 +00001060your own cleanup function using sys.exitfunc (see question 4.4).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001061
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001062- Finally, there are some obscure bugs if your __del__ method does
1063complicated things such as updating dictionaries or lists or
1064references globals. I hope to have fixed these in release 1.2.
1065
10664.18. Q. How do I change the shell environment for programs called
1067using os.popen() or os.system()? Changing os.environ doesn't work.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001068
1069A. Modifying the environment passed to subshells was left out of the
1070interpreter because there seemed to be no well-established portable
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001071way to do it (in particular, some systems, have putenv(), others have
1072setenv(), and some have none at all).
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001073
1074However if all you want is to pass environment variables to the
1075commands run by os.system() or os.popen(), there's a simple solution:
1076prefix the command string with a couple of variable assignments and
1077export statements. I guess the following would be universal for popen
1078(untested):
1079
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001080 import os
1081 from commands import mkarg # nifty routine to add shell quoting
1082 def epopen(cmd, mode, env = {}):
1083 # env is a dictionary of environment variables
1084 prefix = ''
1085 for key, value in env.values():
1086 prefix = prefix + '%s=%s\n' % (key, mkarg(value))
1087 prefix = prefix + 'export %s\n' % key
1088 return os.popen(prefix + cmd, mode)
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001089
10904.19. Q. What is a class?
1091
1092A. A class is the particular object type that is created by executing
1093a class statement.
1094
10954.20. Q. What is a method?
1096
1097A. A method is a function that you normally call as
1098x.name(arguments...) for some object x. The word is used for methods
1099of classes and class instances as well as for methods of built-in
1100objects. The latter have a completely different implementation and
1101only share the way their calls look in Python code.
1102
11034.21. Q. What is self?
1104
1105A. Self is merely a conventional name for the first argument of a
1106method -- i.e. a function defined inside a class definition. A method
1107defined as meth(self, a, b, c) should be called as x.meth(a, b, c) for
1108some instance x of the class in which the definition occurs;
1109the called method will think it is called as meth(x, a, b, c).
1110
11114.22. Q. What is a unbound method?
1112
1113A. An unbound method is a method defined in a class that is not yet
1114bound to an instance. You get an unbound method if you ask for a
1115class attribute that happens to be a function. You get a bound method
1116if you ask for an instance attribute. A bound method knows which
Guido van Rossum061f1821994-10-06 16:03:45 +00001117instance it belongs to and calling it supplies the instance automatically;
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001118an unbound method only knows which class it wants for its first
1119argument (a derived class is also OK). Calling an unbound method
1120doesn't "magically" derive the first argument from the context -- you
1121have to provide it explicitly.
1122
Guido van Rossuma6c707c1995-01-02 17:32:28 +000011234.23. Q. How do I call a method defined in a base class from a derived
1124class that overrides it?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001125
1126A. If your class definition starts with "class Derived(Base): ..."
1127then you can call method meth defined in Base (or one of Base's base
1128classes) as Base.meth(self, arguments...). Here, Base.meth is an
1129unbound method (see previous question).
1130
Guido van Rossuma6c707c1995-01-02 17:32:28 +000011314.24. Q. How do I call a method from a base class without using the
1132name of the base class?
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001133
1134A. DON'T DO THIS. REALLY. I MEAN IT. It appears that you could call
1135self.__class__.__bases__[0].meth(self, arguments...) but this fails when
Guido van Rossum061f1821994-10-06 16:03:45 +00001136a doubly-derived method is derived from your class: for its instances,
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001137self.__class__.__bases__[0] is your class, not its base class -- so
1138(assuming you are doing this from within Derived.meth) you would start
1139a recursive call.
1140
11414.25. Q. How can I organize my code to make it easier to change the base
1142class?
1143
1144A. You could define an alias for the base class, assign the real base
1145class to it before your class definition, and use the alias throughout
1146your class. Then all you have to change is the value assigned to the
1147alias. Incidentally, this trick is also handy if you want to decide
1148dynamically (e.g. depending on availability of resources) which base
1149class to use. Example:
1150
1151BaseAlias = <real base class>
1152class Derived(BaseAlias):
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001153 def meth(self):
1154 BaseAlias.meth(self)
1155 ...
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001156
11574.26. Q. How can I find the methods or attributes of an object?
1158
1159A. This depends on the object type.
1160
1161For an instance x of a user-defined class, instance attributes are
1162found in the dictionary x.__dict__, and methods and attributes defined
1163by its class are found in x.__class__.__bases__[i].__dict__ (for i in
1164range(len(x.__class__.__bases__))). You'll have to walk the tree of
1165base classes to find *all* class methods and attributes.
1166
1167Many, but not all built-in types define a list of their method names
1168in x.__methods__, and if they have data attributes, their names may be
1169found in x.__members__. However this is only a convention.
1170
1171For more information, read the source of the standard (but
1172undocumented) module newdir.
1173
11744.27. Q. I can't seem to use os.read() on a pipe created with os.popen().
1175
1176A. os.read() is a low-level function which takes a file descriptor (a
1177small integer). os.popen() creates a high-level file object -- the
1178same type used for sys.std{in,out,err} and returned by the builtin
1179open() function. Thus, to read n bytes from a pipe p created with
1180os.popen(), you need to use p.read(n).
Guido van Rossuma7925f11994-01-26 10:20:16 +00001181
Guido van Rossum061f1821994-10-06 16:03:45 +000011824.28. Q. How can I create a stand-alone binary from a Python script?
1183
1184The demo script "Demo/scripts/freeze.py" does what you want. (It's
1185actually not a demo but a support tool -- there is some extra code in
1186the interpreter to accommodate it.) It requires that you have the
1187Python build tree handy, complete with all the lib*.a files.
1188
1189This works by scanning your source recursively for import statements
1190(both forms) and looking for the modules on the standard Python path
1191as well as in the source directory (for built-in modules). It then
1192"compiles" the modules written in Python to C code (array initializers
1193that can be turned into code objects using the marshal module) and
1194creates a custom-made config file that only contains those built-in
1195modules which are actually used in the program. It then compiles the
1196generated C code and links it with the rest of the Python interpreter
1197to form a self-contained binary which acts exactly like your script.
1198
1199Unfortunately, the current version is very platform-specific, because
1200each platform has its own compilation flags and libraries to link
1201with. You will probably have to edit the freeze.py file to point it
1202to the right directories and tell it about the compilation and link
1203flags for your platform. A new version will be released with Python
12041.1 -- if you want a peek write to my colleague <Jack.Jansen@cwi.nl>.
1205
12064.29. Q. Is there a special lib for writing CGI scripts in Python?
1207
1208A. There's documentation and code for a cgi.py module by Michael McLay
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001209<mclay@eeel.nist.gov> available from
1210<URL:http://www.eeel.nist.gov/python/>
Guido van Rossum061f1821994-10-06 16:03:45 +00001211
1212(For the curious: CGI or Common Gateway Interface is the protocol
1213between HTTP servers (WWW servers) and programs/scripts they run to
1214perform queries and other tasks that require returning a dynamically
1215generated document.)
1216
12174.30. Q. What other WWW tools are there for Python?
1218
1219A. The standard library has a module urllib, which can retrieve most
1220commonly used URL types (file, ftp, http, gopher).
1221
1222The Demo2/www directory (Demo2 has to be retrieved separately from the
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001223Python ftp sites <URL:ftp://ftp.cwi.nl/pub/python/demo2.tar.gz>)
1224contains some (really old) code to parse HTML and to display it.
Guido van Rossum061f1821994-10-06 16:03:45 +00001225
1226Steve Miale <smiale@cs.indiana.edu> has written a modular WWW browser
1227called Dancer. An alpha version can be FTP'ed from
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001228<URL:ftp://ftp.cs.indiana.edu/pub/smiale/dancer.tar.gz>. (There are a
1229few articles about Dancer in the (hyper)mail archive
1230<URL:http://www.cwi.nl/~guido/hypermail/python-1994q3/index.html>.)
Guido van Rossum061f1821994-10-06 16:03:45 +00001231
1232
Guido van Rossuma7925f11994-01-26 10:20:16 +000012335. Extending Python
1234===================
1235
12365.1. Q. Can I create my own functions in C?
1237
1238A. Yes, you can create built-in modules containing functions,
Guido van Rossum24349991994-02-02 14:12:45 +00001239variables, exceptions and even new types in C. This is explained in
1240the document "Extending and Embedding the Python Interpreter" (the
1241LaTeX file Doc/ext.tex). Also read the chapter on dynamic loading.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001242
12435.2. Q. Can I create my own functions in C++?
1244
1245A. Yes, using the C-compatibility features found in C++. Basically
1246you place extern "C" { ... } around the Python include files and put
1247extern "C" before each function that is going to be called by the
1248Python interpreter. Global or static C++ objects with constructors
1249are probably not a good idea.
1250
Guido van Rossum7ce61c11994-06-13 15:13:56 +000012515.3. Q. How can I execute arbitrary Python statements from C?
1252
1253A. The highest-level function to do this is run_command() which takes
1254a single string argument which is executed in the context of module
1255__main__ and returns 0 for success and -1 when an exception occurred
1256(including SyntaxError). If you want more control, use run_string();
1257see the source for run_command() in Python/pythonrun.c.
1258
12595.4. Q. How can I evaluate an arbitrary Python expression from C?
1260
1261A. Call the function run_string() from the previous question with the
1262start symbol eval_input; it then parses an expression, evaluates it
1263and returns its value. See exec_eval() in Python/bltinmodule.c.
1264
12655.5. Q. How do I extract C values from a Python object?
1266
1267A. That depends on the object's type. If it's a tuple,
1268gettuplesize(o) returns its length and gettupleitem(o, i) returns its
1269i'th item; similar for lists with getlistsize(o) and getlistitem(o,
1270i). For strings, getstringsize(o) returns its length and
1271getstringvalue(o) a pointer to its value (note that Python strings may
1272contain null bytes so strlen() is not safe). To test which type an
1273object is, first make sure it isn't NULL, and then use
1274is_stringobject(o), is_tupleobject(o), is_listobject(o) etc.
1275
12765.6. Q. How do I use mkvalue() to create a tuple of arbitrary length?
1277
1278A. You can't. Use t = newtupleobject(n) instead, and fill it with
1279objects using settupleitem(t, i, o) -- note that this "eats" a
1280reference count of o. Similar for lists with newlistobject(n) and
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001281setlistitem(l, i, o). Note that you *must* set all the tuple items to
1282some value before you pass the tuple to Python code --
1283newtupleobject(n) initializes them to NULL, which isn't a valid Python
1284value.
1285
12865.7. Q. What happened to mktuple(), featuring in an example in the
1287Extensions manual?
1288
1289A. It's a typo, I meant newtupleobject() (see previous question).
1290
12915.8. Q. How do I call an object's method from C?
1292
1293A. Here's a function (untested) that might become part of the next
1294release in some form. It uses <stdarg.h> to allow passing the
1295argument list on to vmkvalue():
1296
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001297 object *call_method(object *inst, char *methodname, char *format, ...)
1298 {
1299 object *method;
1300 object *args;
1301 object *result;
1302 va_list va;
1303 method = getattr(inst, methodname);
1304 if (method == NULL) return NULL;
1305 va_start(va, format);
1306 args = vmkvalue(format, va);
1307 va_end(va);
1308 if (args == NULL) {
1309 DECREF(method);
1310 return NULL;
1311 }
1312 result = call_object(method, args);
1313 DECREF(method);
1314 DECREF(args);
1315 return result;
1316 }
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001317
1318This works for any instance that has methods -- whether built-in or
1319user-defined. You are responsible for eventually DECREF'ing the
1320return value.
1321
1322To call, e.g., a file object's "seek" method with arguments 10, 0
1323(assuming the file object pointer is "f"):
1324
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001325 res = call_method(f, "seek", "(OO)", 10, 0);
1326 if (res == NULL) {
1327 ... an exception occurred ...
1328 }
1329 else {
1330 DECREF(res);
1331 }
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001332
1333Note that since call_object() *always* wants a tuple for the argument
1334list, to call a function without arguments, pass "()" for the format,
1335and to call a function with one argument, surround the argument in
1336parentheses, e.g. "(i)".
1337
13385.9. Q. How do I catch the output from print_error()?
1339
1340A. (Due to Mark Hammond):
1341
1342* in Python code, define an object that supports the "write()" method.
1343FWIW, there seems to be a small problem that requires the 'softspace'
Guido van Rossum061f1821994-10-06 16:03:45 +00001344attribute to be defined too (I can't remember exact details of the
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001345problem).
1346
1347* redirect sys.stdout and sys.stderr to this object.
1348
Guido van Rossum061f1821994-10-06 16:03:45 +00001349* call print_error, or just allow the standard traceback mechanism to
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001350work.
1351
Guido van Rossum061f1821994-10-06 16:03:45 +00001352Then, the output will go wherever your write() method sends it.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001353
13545.10. Q. How do I access a module written in Python from C?
1355
1356A. You can get a pointer to the module object as follows:
1357
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001358 module = import_module("<modulename>");
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001359
1360If the module hasn't been imported yet (i.e. it is not yet present in
1361sys.modules), this initializes the module; otherwise it simply returns
1362the value of sys.modules["<modulename>"]. Note that it doesn't enter
1363the module into any namespace -- it only ensures it has been
1364initialized and is stored in sys.modules.
1365
1366You can then access the module's attributes (i.e. any name defined in
1367the module) as follows:
1368
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001369 attr = getattr(module, "<attrname>");
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001370
1371Calling setattr(), to assign to variables in the module, also works.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001372
Guido van Rossuma7925f11994-01-26 10:20:16 +00001373
13746. Python's design
1375==================
1376
13776.1. Q. Why isn't there a generic copying operation for objects in
1378Python?
1379
1380A. Hmm. Maybe there should be one, but it's difficult to assign a
1381useful meaning to copying of open files, sockets and windows, or
1382recursive data structures. As long as you design all your classes
1383yourself you are of course free to define a standard base class that
1384defines an overridable copying operation for all the objects you care
1385about. (One practical point: it would have to be a built-in function,
1386not a standard method name, since not all built-in object types have
1387methods; e.g. strings, integers and tuples don't.)
1388
13896.2. Q. Why isn't there a generic way to implement persistent objects
1390in Python? (Persistent == automatically saved to and restored from
1391disk.)
1392
1393A. Hmm, hmm. Basically for the same reasons as why there is no
1394generic copying operation.
1395
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001396A partial solution will appear in release 1.2. This will also provide
1397a partial solution to the problem of a generic copying operation.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001398
Guido van Rossuma7925f11994-01-26 10:20:16 +000013996.3. Q. Why isn't there a switch or case statement in Python?
1400
1401A. You can do this easily enough with a sequence of
1402if... elif... elif... else. There have been some proposals for switch
Guido van Rossum061f1821994-10-06 16:03:45 +00001403statement syntax, but there is no consensus (yet) on whether and how
Guido van Rossuma7925f11994-01-26 10:20:16 +00001404to do range tests.
1405
Guido van Rossumc50158e1994-05-31 09:18:50 +000014066.4. Q. Why does Python use indentation for grouping of statements?
1407
1408A. Basically I believe that using indentation for grouping is
1409extremely elegant and contributes a lot to the clarity of the average
1410Python program. Most people learn to love this feature after a while.
1411Some arguments for it:
1412
1413- Since there are no begin/end brackets there cannot be a disagreement
1414between grouping perceived by the parser and the human reader. I
1415remember long ago seeing a C fragment like this:
1416
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001417 if (x <= y)
1418 x++;
1419 y--;
1420 z++;
Guido van Rossumc50158e1994-05-31 09:18:50 +00001421
1422and staring a long time at it wondering why y was being decremented
1423even for x > y... (And I wasn't a C newbie then either.)
1424
1425- Since there are no begin/end brackets there can be no conflicting
1426coding styles. In C there are loads of different ways to place the
1427braces (including the choice whether to place braces around single
1428statements in certain cases, for consistency). If you're used to
1429reading (and writing) code that uses one style, you will feel at least
1430slightly uneasy when reading (or being required to write) another
1431style.
1432
1433- Many coding styles place begin/end brackets on a line by themself.
1434This makes programs considerably longer and wastes valuable screen
1435space, making it harder to get a good overview over a program.
1436Ideally, a function should fit on one basic tty screen (say, 20
1437lines). 20 lines of Python are worth a LOT more than 20 lines of C.
1438This is not solely due to the lack of begin/end brackets (the lack of
1439declarations also helps, and the powerful operations of course), but
1440it certainly helps!
1441
Guido van Rossum3de27361994-07-25 14:19:33 +000014426.5. Q. Why are Python strings immutable?
1443
1444A. There are two advantages. One is performance: knowing that a
1445string is immutable makes it easy to lay it out at construction time
1446-- fixed and unchanging storage requirements. (This is also one of
Guido van Rossum061f1821994-10-06 16:03:45 +00001447the reasons for the distinction between tuples and lists.) The
Guido van Rossum3de27361994-07-25 14:19:33 +00001448other is that strings in Python are considered as "elemental" as
1449numbers. No amount of activity will change the value 8 to anything
1450else, and in Python, no amount of activity will change the string
1451"eight" to anything else. (Adapted from Jim Roskind)
1452
14536.6. Q. Why don't strings have methods like index() or sort(), like
1454lists?
1455
1456A. Good question. Strings currently don't have methods at all
1457(likewise tuples and numbers). Long ago, it seemed unnecessary to
1458implement any of these functions in C, so a standard library module
1459"string" written in Python was created that performs string related
1460operations. Since then, the cry for performance has moved most of
1461them into the built-in module strop (this is imported by module
Guido van Rossumf8c76d01994-08-17 12:19:53 +00001462string, which is still the preferred interface, without loss of
Guido van Rossum3de27361994-07-25 14:19:33 +00001463performance except during initialization). Some of these functions
1464(e.g. index()) could easily be implemented as string methods instead,
1465but others (e.g. sort()) can't, since their interface prescribes that
1466they modify the object, while strings are immutable (see the previous
1467question).
1468
14696.7. Q. Why does Python use methods for some functionality
1470(e.g. list.index()) but functions for other (e.g. len(list))?
1471
1472A. Functions are used for those operations that are generic for a
1473group of types and which should work even for objects that don't have
1474methods at all (e.g. numbers, strings, tuples). Also, implementing
1475len(), max(), min() as a built-in function is actually less code than
1476implementing them as methods for each type. One can quibble about
1477individual cases but it's really too late to change such things
1478fundamentally now.
1479
14806.8. Q. Why can't I derive a class from built-in types (e.g. lists or
1481files)?
1482
1483A. This is caused by the relatively late addition of (user-defined)
1484classes to the language -- the implementation framework doesn't easily
1485allow it. See the answer to question 4.2 for a work-around. This
1486*may* be fixed in the (distant) future.
1487
14886.9. Q. Why must 'self' be declared and used explicitly in method
1489definitions and calls?
1490
1491A. By asking this question you reveal your C++ background. :-)
1492When I added classes, this was (again) the simplest way of
1493implementing methods without too many changes to the interpreter. I
1494borrowed the idea from Modula-3. It turns out to be very useful, for
1495a variety of reasons.
1496
1497First, it makes it more obvious that you are using a method or
1498instance attribute instead of a local variable. Reading "self.x" or
1499"self.meth()" makes it absolutely clear that an instance variable or
1500method is used even if you don't know the class definition by heart.
1501In C++, you can sort of tell by the lack of a local variable
Guido van Rossum061f1821994-10-06 16:03:45 +00001502declaration (assuming globals are rare or easily recognizable) -- but
Guido van Rossum3de27361994-07-25 14:19:33 +00001503in Python, there are no local variable declarations, so you'd have to
1504look up the class definition to be sure.
1505
1506Second, it means that no special syntax is necessary if you want to
1507explicitly reference or call the method from a particular class. In
1508C++, if you want to use a method from base class that is overridden in
1509a derived class, you have to use the :: operator -- in Python you can
1510write baseclass.methodname(self, <argument list>). This is
1511particularly useful for __init__() methods, and in general in cases
1512where a derived class method wants to extend the base class method of
1513the same name and thus has to call the base class method somehow.
1514
1515Lastly, for instance variables, it solves a syntactic problem with
1516assignment: since local variables in Python are (by definition!) those
1517variables to which a value assigned in a function body (and that
1518aren't explicitly declared global), there has to be some way to tell
1519the interpreter that an assignment was meant to assign to an instance
1520variable instead of to a local variable, and it should preferably be
1521syntactic (for efficiency reasons). C++ does this through
1522declarations, but Python doesn't have declarations and it would be a
1523pity having to introduce them just for this purpose. Using the
1524explicit "self.var" solves this nicely. Similarly, for using instance
1525variables, having to write "self.var" means that references to
1526unqualified names inside a method don't have to search the instance's
1527directories.
1528
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000015296.10. Q. Can't you emulate threads in the interpreter instead of
1530relying on an OS-specific thread implementation?
1531
1532A. Unfortunately, the interpreter pushes at least one C stack frame
1533for each Python stack frame. Also, extensions can call back into
1534Python at almost random moments. Therefore a complete threads
1535implementation requires thread support for C.
1536
Guido van Rossum061f1821994-10-06 16:03:45 +000015376.11. Q. Why can't lambda forms contain statements?
1538
1539A. Python lambda forms cannot contain statements because Python's
1540syntactic framework can't handle statements nested inside functions.
1541
1542However, in Python, this is not a serious problem. Unlike lambda
1543forms in other languages, where they add functionality, Python lambdas
1544are only a shorthand notation if you're too lazy to define a function.
1545
1546Functions are already first class objects in Python, and can be
1547declared in a local scope. Therefore the only advantage of using a
1548lambda form instead of a locally-defined function is that you'll have
1549to invent a name for the function -- but that's just a local variable
1550to which the function object (which is exactly the same type of object
1551that a lambda form yields) is assigned!
1552
15536.12. Q. Why is there no more efficient way of iterating over a dictionary
1554than first constructing the list of keys()?
1555
1556A. Have you tried it? I bet it's fast enough for your purposes! In
1557most cases such a list takes only a few percent of the space occupied
1558by the dictionary -- it needs only 4 bytes (the size of a pointer) per
1559key -- a dictionary costs 8 bytes per key plus between 30 and 70
1560percent hash table overhead, plus the space for the keys and values --
1561by necessity all keys are unique objects and a string object (the most
1562common key type) costs at least 18 bytes plus the length of the
1563string. Add to that the values contained in the dictionary, and you
1564see that 4 bytes more per item really isn't that much more memory...
1565
1566A call to dict.keys() makes one fast scan over the dictionary
1567(internally, the iteration function does exist) copying the pointers
1568to the key objects into a pre-allocated list object of the right size.
1569The iteration time isn't lost (since you'll have to iterate anyway --
1570unless in the majority of cases your loop terminates very prematurely
1571(which I doubt since you're getting the keys in random order).
1572
1573I don't expose the dictionary iteration operation to Python
1574programmers because the dictionary shouldn't be modified during the
1575entire iteration -- if it is, there's a very small chance that the
1576dictionary is reorganized because the hash table becomes too full, and
1577then the iteration may miss some items and see others twice. Exactly
1578because this only occurs rarely, it would lead to hidden bugs in
1579programs: it's easy never to have it happen during test runs if you
1580only insert or delete a few items per iteration -- but your users will
1581surely hit upon it sooner or later.
1582
Guido van Rossuma7925f11994-01-26 10:20:16 +00001583
15847. Using Python on non-UNIX platforms
1585=====================================
1586
Guido van Rossum91f60831994-02-15 15:52:27 +000015877.1. Q. Is there a Mac version of Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +00001588
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001589A. Yes. It is on most ftp sites carrying Python as python.sea.hqx,
1590e.g. <URL:ftp://ftp.cwi.nl/pub/python/python_1.1.sea.hqx> -- this is a
1591self-extracting archive containing the application binary as well as
1592the Lib modules.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001593
Guido van Rossum91f60831994-02-15 15:52:27 +000015947.2. Q. Is there a DOS version of Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +00001595
Guido van Rossum91f60831994-02-15 15:52:27 +00001596A. Yes. More than one, actually: 16python.exe runs in standard DOS
1597mode on 186 CPUs or higher; 32python.exe uses a DOS extender and only
1598runs on a 386 or higher CPUs. Although 16python.exe does not pass the
1599test set because test_grammar is too big for the parser, it actually
1600has about 270 kbyte of allocatable heap space, which is sufficient for
1601fairly large programs. 32python.exe is distributed as a tar file
1602containing the required DOS extended and 387 emulator. Both are on
1603most ftp sites carrying Python.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001604
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001605The file dosbuild.tar.gz on the standard ftp sites contains
1606rudimentary Makefiles and instructions
1607<URL:ftp://ftp.cwi.nl/pub/python/dosbuild.tar.gz>.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001608
Guido van Rossum91f60831994-02-15 15:52:27 +000016097.3. Q. Is there a Windows version of Python?
1610
1611A. Yes. Use qwpython.exe. The only problem with it: ^C
1612unconditionally kills the entire program -- it does not raise
1613KeyboardInterrupt. You can also run 16python.exe or 32python.exe in a
1614"DOS box", but qwpython.exe appears to be slightly faster.
1615
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001616There aren't any Makefiles at this moment. Sorry.
1617
1618Using Win32s (a free NT compatibility package by Microsoft) you can
1619also use the NT version by Mark Hammond -- the Win32s package is also
1620in that directory (you'll need several MB of disk space to install
1621it). See the next question.
1622
Guido van Rossum91f60831994-02-15 15:52:27 +000016237.4. Q. Is there a Windows NT version of Python?
1624
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001625A. Yes. Mark Hammond <MHammond@cmutual.com.au> has built a full NT
1626port. This supports using DLLs for dynamic loading of Python modules,
1627and includes an interface to the Microsoft Foundation Classes and a
1628Python programming environment using it that's written mostly in
1629Python. See <URL:ftp://ftp.cwi.nl/pub/python/nt/> -- most mirrors
1630will also have this.
Guido van Rossum91f60831994-02-15 15:52:27 +00001631
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001632Sam Rushing <rushing@squirl.oau.org> once announced he knows how to
1633build Python for the Windows NT on the DEC Alpha AXP.
Guido van Rossum061f1821994-10-06 16:03:45 +00001634
1635Note that currently there is no unified compilation environment for
1636all NT platforms -- hopefully Microsoft will fix this with the release
1637of Visual C++ 2.0.
1638
Guido van Rossum7ce61c11994-06-13 15:13:56 +000016397.5. Q. Is there an OS/2 version of Python?
1640
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001641A. Yes. You can ftp it from the usual places as pyth_os2.zip, e.g.
1642<URL:ftp://ftp.cwi.nl/pub/python/pyth_os2.zip>. This contains both an
1643executable and Makefiles for those fortunate enough to have a C
1644compiler.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001645
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000016467.6. Q. Is there a VMS version of Python?
1647
1648A. I think not (yet). This question has been asked on the list
1649several times and I've never seen an answer. Maybe someone with a VMS
1650C compiler could attempt a port? Probably coming up with proper
1651Makefiles, config.h and config.c should be sufficient. If you succeed
1652(or even if you get stuck halfway), please let me know! (Info as of
165323 September 1994.)
1654
16557.7. Q. What about IBM mainframes, or other esoteric non-UNIX
1656platforms?
1657
1658A. Basically, the same story as for VMS... (Info as of 23 September
16591994.)
1660
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000016617.8. Q. Where are the source or Makefiles for the non-UNIX versions?
1662
1663A. The standard sources can (almost) be used. See the previous
1664questions for availability of Makefiles/projects or patches. If you
1665find things in the standard sources that don't work on your particular
1666platform, please let me know and I'll integrate a solution into the
1667next release of the standard sources. If you submit a fix, please use
1668some kind of #ifdef so as to keep the source working for other
1669platforms. In particular, if the patch works around the availability
1670of a particular function of header file, you should mimic the
1671HAVE_... style used by the configure script -- you can then submit a
1672config.h file for a particular platform so there are no absolutely
1673platform-specific #ifdefs in the rest of the sources.
1674
16757.9. Q. What is the status and support for the non-UNIX versions?
1676
1677A. I don't have access to most of these platforms, so in general I am
1678dependent on material submitted by volunteers(*). However I strive to
1679integrate all changes needed to get it to compile on a particular
1680platform back into the standard sources, so porting of the next
1681version to the various non-UNIX platforms should be easy.
1682
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001683(*) For the Macintosh, that volunteer is me, with help from Jack
1684Jansen <jack@cwi.nl>.
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001685
16867.10. Q. I have the PC version but it appears to be only a binary.
Guido van Rossum7ce61c11994-06-13 15:13:56 +00001687Where's the library?
Guido van Rossuma7925f11994-01-26 10:20:16 +00001688
1689A. You still need to copy the files from the distribution directory
Guido van Rossum91f60831994-02-15 15:52:27 +00001690"python/Lib" to your system. If you don't have the full distribution,
Guido van Rossum3de27361994-07-25 14:19:33 +00001691you can get the file pythonlib<version>.tar.gz from most ftp sites
1692carrying Python; this is a subset of the distribution containing just
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001693those file, e.g.
1694<URL:ftp://ftp.cwi.nl/pub/python/pythonlib1.1.tar.gz>.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001695
Guido van Rossum5333c5d1994-04-11 11:06:22 +00001696Once you have installed the library, you need to point sys.path to it.
1697Assuming the library is in C:\misc\python\lib, the following commands
1698will point your Python interpreter to it (note the doubled backslashes
1699-- you can also use single forward slashes instead):
1700
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001701 >>> import sys
1702 >>> sys.path.insert(0, 'C:\\misc\\python\\lib')
1703 >>>
Guido van Rossum5333c5d1994-04-11 11:06:22 +00001704
1705For a more permanent effect, set the environment variable PYTHONPATH,
1706as follows (talking to a DOS prompt):
1707
Guido van Rossuma6c707c1995-01-02 17:32:28 +00001708 C> SET PYTHONPATH=C:\misc\python\lib
Guido van Rossum5333c5d1994-04-11 11:06:22 +00001709
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000017107.11. Q. Where's the documentation for the Mac or PC version?
Guido van Rossuma7925f11994-01-26 10:20:16 +00001711
1712A. There isn't any. The documentation for the Unix version also
Guido van Rossum91f60831994-02-15 15:52:27 +00001713applies to the Mac and PC versions. Where applicable, differences
Guido van Rossuma7925f11994-01-26 10:20:16 +00001714are indicated in the text.
1715
Guido van Rossumb34e8aa1994-09-23 14:20:38 +000017167.12. Q. The Mac (PC) version doesn't seem to have any facilities for
Guido van Rossum91f60831994-02-15 15:52:27 +00001717creating or editing programs apart from entering it interactively, and
1718there seems to be no way to save code that was entered interactively.
1719How do I create a Python program on the Mac (PC)?
Guido van Rossuma7925f11994-01-26 10:20:16 +00001720
Guido van Rossumb34e8aa1994-09-23 14:20:38 +00001721A. Use an external editor. On the Mac, BBEdit seems to be a popular
1722no-frills text editor. I work like this: start the interpreter; edit
1723a module file using BBedit; import and test it in the interpreter;
1724edit again in BBedit; then use the built-in function reload() to
1725re-read the imported module; etc.
Guido van Rossum5333c5d1994-04-11 11:06:22 +00001726
1727Regarding the same question for the PC, Kurt Wm. Hemr writes: "While
1728anyone with a pulse could certainly figure out how to do the same on
1729MS-Windows, I would recommend the NotGNU Emacs clone for MS-Windows.
1730Not only can you easily resave and "reload()" from Python after making
1731changes, but since WinNot auto-copies to the clipboard any text you
1732select, you can simply select the entire procedure (function) which
1733you changed in WinNot, switch to QWPython, and shift-ins to reenter
1734the changed program unit."