Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 1 | Subject: FAQ: Python -- an object-oriented language |
Guido van Rossum | 5333c5d | 1994-04-11 11:06:22 +0000 | [diff] [blame] | 2 | Newsgroups: comp.lang.python,comp.answers,news.answers |
| 3 | Followup-to: comp.lang.python |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 4 | From: guido@cwi.nl (Guido van Rossum) |
| 5 | Reply-to: guido@cwi.nl (Guido van Rossum) |
| 6 | Approved: news-answers-request@MIT.Edu |
| 7 | |
| 8 | Archive-name: python-faq/part1 |
Guido van Rossum | 44b4cb1 | 1994-05-04 13:28:51 +0000 | [diff] [blame] | 9 | Version: 1.7 |
| 10 | Last-modified: 4 May 1994 |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 11 | |
| 12 | This article contains answers to Frequently Asked Questions about |
| 13 | Python (an object-oriented interpreted programming language -- see |
| 14 | the answer to question 1.1 for a short overview). |
| 15 | |
| 16 | Copyright 1993, 1994 Guido van Rossum. Unchanged electronic |
| 17 | redistribution of this FAQ is allowed. Printed redistribution only |
| 18 | with permission of the author. No warranties. |
| 19 | |
| 20 | Author's address: |
| 21 | Guido van Rossum |
| 22 | CWI, dept. CST |
| 23 | Kruislaan 413 |
| 24 | P.O. Box 94079 |
| 25 | 1090 GB Amsterdam |
| 26 | The Netherlands |
| 27 | Email: guido@cwi.nl |
| 28 | |
| 29 | The latest version of this FAQ is available by anonymous ftp from |
| 30 | ftp.cwi.nl [192.16.184.180] in the directory /pub/python, with |
| 31 | filename python-FAQ. It will also be posted regularly to the |
Guido van Rossum | 5333c5d | 1994-04-11 11:06:22 +0000 | [diff] [blame] | 32 | newsgroups comp.answers and comp.lang.python. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 33 | |
| 34 | Many FAQs, including this one, are available by anonymous ftp from |
| 35 | rtfm.mit.edu [18.70.0.209] in the directory pub/usenet/news.answers. |
| 36 | The name under which a FAQ is archived appears in the Archive-name line |
| 37 | at the top of the article. This FAQ is archived as python-faq/part1. |
| 38 | |
| 39 | There's a mail server on that machine which will send you files from |
| 40 | the archive by e-mail if you have no ftp access. You send a e-mail |
| 41 | message to mail-server@rtfm.mit.edu containing the single word help in |
| 42 | the message body to receive instructions. |
| 43 | |
| 44 | This FAQ is divided in the following chapters: |
| 45 | |
| 46 | 1. General information and availability |
| 47 | 2. Python in the real world |
| 48 | 3. Building Python |
| 49 | 4. Programming in Python |
| 50 | 5. Extending Python |
| 51 | 6. Python's design |
| 52 | 7. Using Python on non-UNIX platforms |
| 53 | |
| 54 | To find the start of a particular chapter, search for the chapter number |
| 55 | followed by a dot and a space at the beginning of a line (e.g. to |
| 56 | find chapter 4 in vi, type /^4\. /). |
| 57 | |
| 58 | Here's an overview of the questions per chapter: |
| 59 | |
| 60 | 1. General information and availability |
| 61 | 1.1. Q. What is Python? |
| 62 | 1.2. Q. Why is it called Python? |
| 63 | 1.3. Q. How do I obtain a copy of the Python source? |
| 64 | 1.4. Q. How do I get documentation on Python? |
| 65 | 1.5. Q. Is there a newsgroup or mailing list devoted to Python? |
| 66 | 1.6. Q. Is there a book on Python, or will there be one out soon? |
| 67 | 1.7. Q. Are there any published articles about Python that I can quote? |
Guido van Rossum | 95f61a7 | 1994-01-26 17:23:37 +0000 | [diff] [blame] | 68 | 1.8. Q. How does the Python version numbering scheme work? |
Guido van Rossum | 5333c5d | 1994-04-11 11:06:22 +0000 | [diff] [blame] | 69 | 1.9. Q. Are there other ftp sites that carry Python related material? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 70 | |
| 71 | 2. Python in the real world |
| 72 | 2.1. Q. How many people are using Python? |
| 73 | 2.2. Q. Have any significant projects been done in Python? |
| 74 | 2.3. Q. Are there any commercial projects going on using Python? |
Guido van Rossum | 95f61a7 | 1994-01-26 17:23:37 +0000 | [diff] [blame] | 75 | 2.4. Q. How stable is Python? |
| 76 | 2.5. Q. What new developments are expected for Python in the future? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 77 | |
| 78 | 3. Building Python |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 79 | 3.1. Q. Is there a test set? |
| 80 | 3.2. Q. When running the test set, I get complaints about floating point |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 81 | operations, but when playing with floating point operations I cannot |
| 82 | find anything wrong with them. |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 83 | 3.3. Q. Link errors building Python with STDWIN on SGI IRIX. |
| 84 | 3.4. Q. Link errors after rerunning the configure script. |
| 85 | 3.5. Q. The python interpreter complains about options passed to a |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 86 | script (after the script name). |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 87 | 3.6. Q. When building on the SGI, make tries to run python to create |
Guido van Rossum | 5333c5d | 1994-04-11 11:06:22 +0000 | [diff] [blame] | 88 | glmodule.c, but python hasn't been built or installed yet. |
Guido van Rossum | 44b4cb1 | 1994-05-04 13:28:51 +0000 | [diff] [blame] | 89 | 3.7. Q. Other trouble building Python 1.0.2 on platform X. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 90 | |
| 91 | 4. Programming in Python |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 92 | 4.1. Q. Is there a source code level debugger with breakpoints, step, |
| 93 | etc.? |
| 94 | 4.2. Q. Can I create an object class with some methods implemented in |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 95 | C and others in Python (e.g. through inheritance)? (Also phrased as: |
| 96 | Can I use a built-in type as base class?) |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 97 | 4.3. Q. Is there a curses/termcap package for Python? |
| 98 | 4.4. Q. Is there an equivalent to C's onexit() in Python? |
| 99 | 4.5. Q. When I define a function nested inside another function, the |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 100 | nested function seemingly can't access the local variables of the |
| 101 | outer function. What is going on? How do I pass local data to a |
| 102 | nested function? |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 103 | 4.6. Q. How do I iterate over a sequence in reverse order? |
| 104 | 4.7. Q. My program is too slow. How do I speed it up? |
| 105 | 4.8. Q. When I have imported a module, then edit it, and import it |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 106 | again (into the same Python process), the changes don't seem to take |
| 107 | place. What is going on? |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 108 | 4.9. Q. I have a module in which I want to execute some extra code when it |
| 109 | is run as a script. How do I find out whether I am running as a |
| 110 | script? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 111 | |
| 112 | 5. Extending Python |
| 113 | 5.1. Q. Can I create my own functions in C? |
| 114 | 5.2. Q. Can I create my own functions in C++? |
| 115 | |
| 116 | 6. Python's design |
| 117 | 6.1. Q. Why isn't there a generic copying operation for objects in |
| 118 | Python? |
| 119 | 6.2. Q. Why isn't there a generic way to implement persistent objects |
| 120 | in Python? (Persistent == automatically saved to and restored from |
| 121 | disk.) |
| 122 | 6.3. Q. Why isn't there a switch or case statement in Python? |
| 123 | |
| 124 | 7. Using Python on non-UNIX platforms |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 125 | 7.1. Q. Is there a Mac version of Python? |
| 126 | 7.2. Q. Is there a DOS version of Python? |
| 127 | 7.3. Q. Is there a Windows version of Python? |
| 128 | 7.4. Q. Is there a Windows NT version of Python? |
| 129 | 7.5. Q. I have the DOS or Windows version but it appears to be only a |
| 130 | binary. Where's the library? |
| 131 | 7.6. Q. Where's the documentation for the Mac or PC version? |
| 132 | 7.7. Q. The Mac (PC) version doesn't seem to have any facilities for |
| 133 | creating or editing programs apart from entering it interactively, and |
| 134 | there seems to be no way to save code that was entered interactively. |
| 135 | How do I create a Python program on the Mac (PC)? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 136 | |
| 137 | To find a particular question, search for the question number followed |
| 138 | by a dot, a space, and a Q at the beginning of a line (e.g. to find |
| 139 | question 4.2 in vi, type /^4\.2\. Q/). |
| 140 | |
| 141 | |
| 142 | 1. General information and availability |
| 143 | ======================================= |
| 144 | |
| 145 | 1.1. Q. What is Python? |
| 146 | |
| 147 | A. Python is an interpreted, interactive, object-oriented programming |
| 148 | language. It incorporates modules, exceptions, dynamic typing, very |
| 149 | high level dynamic data types, and classes. Python combines |
| 150 | remarkable power with very clear syntax. It has interfaces to many |
| 151 | system calls and libraries, as well as to various window systems, and |
| 152 | is extensible in C or C++. It is also usable as an extension language |
| 153 | for applications that need a programmable interface. Finally, Python |
| 154 | is portable: it runs on many brands of UNIX, on the Mac, and on |
| 155 | MS-DOS. |
| 156 | |
| 157 | To find out more, the best thing to do is to start reading the |
| 158 | tutorial from the documentation set (see a few questions further |
| 159 | down). |
| 160 | |
| 161 | 1.2. Q. Why is it called Python? |
| 162 | |
| 163 | A. Apart from being a computer wizard, I'm also a fan of "Monty |
| 164 | Python's Flying Circus" (a BBC comedy series from the seventies, in |
Guido van Rossum | 5333c5d | 1994-04-11 11:06:22 +0000 | [diff] [blame] | 165 | the -- unlikely -- case you didn't know). It occurred to me one day |
| 166 | that I needed a name that was short, unique, and slightly mysterious. |
| 167 | And I happened to be reading some scripts from the series at the |
| 168 | time... So then I decided to call my language Python. But Python is |
| 169 | not a joke. And don't you associate it with dangerous reptiles |
| 170 | either! |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 171 | |
| 172 | 1.3. Q. How do I obtain a copy of the Python source? |
| 173 | |
| 174 | A. The latest Python source distribution is always available by |
| 175 | anonymous ftp from ftp.cwi.nl [192.16.184.180] in the directory |
Guido van Rossum | 44b4cb1 | 1994-05-04 13:28:51 +0000 | [diff] [blame] | 176 | /pub/python, with filename python<version>.tar.gz. (Old versions may |
| 177 | have an extension of .Z, indicating use of "compress" compression.) |
| 178 | It is a gzip'ed tar file containing the complete C source, LaTeX |
| 179 | documentation, Python library modules, example programs, and several |
| 180 | useful pieces of freely distributable software. This will compile and |
| 181 | run out of the box on most UNIX platforms. At the time of writing, |
| 182 | <version> is 1.0.2. (See section 7 for non-UNIX information.) |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 183 | |
| 184 | 1.4. Q. How do I get documentation on Python? |
| 185 | |
| 186 | A. The latest Python documentation set is always available by |
| 187 | anonymous ftp from ftp.cwi.nl [192.16.184.180] in the directory |
Guido van Rossum | 44b4cb1 | 1994-05-04 13:28:51 +0000 | [diff] [blame] | 188 | /pub/python, with filename pythondoc-ps<version>.tar.gz. It is a |
| 189 | gzip'ed tar file containing PostScript files of the reference manual, |
| 190 | the library manual, and the tutorial. At the time of writing |
| 191 | <version> is 1.0.2. Note that the library manual is the most |
| 192 | important one of the set, as much of Python's power stems from the |
| 193 | standard or built-in types, functions and modules, all of which are |
| 194 | described here. PostScript for a high-level description of Python is |
| 195 | in the file nluug-paper.ps. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 196 | |
| 197 | The following sites keep mirrors of the Python distribution: |
| 198 | |
| 199 | Site IP address Directory |
| 200 | |
| 201 | gatekeeper.dec.com 16.1.0.2 /pub/plan/python/cwi |
| 202 | ftp.uu.net 192.48.96.9 /languages/python |
| 203 | ftp.wustl.edu 128.252.135.4 /graphics/graphics/sgi-stuff/python |
| 204 | ftp.funet.fi 128.214.6.100 /pub/languages/python (old?) |
Guido van Rossum | 5333c5d | 1994-04-11 11:06:22 +0000 | [diff] [blame] | 205 | ftp.fu-berlin.de 130.133.4.50 /pub/unix/languages/python (*python* only) |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 206 | |
Guido van Rossum | 5333c5d | 1994-04-11 11:06:22 +0000 | [diff] [blame] | 207 | Or try archie on e.g. python1.0 to locate the nearest copy of that |
| 208 | version... |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 209 | |
| 210 | 1.5. Q. Is there a newsgroup or mailing list devoted to Python? |
| 211 | |
Guido van Rossum | 5333c5d | 1994-04-11 11:06:22 +0000 | [diff] [blame] | 212 | A. There is a newsgroup, comp.lang.python, and a mailing list. The |
| 213 | newsgroup and mailing list are gatewayed into each other -- if you can |
| 214 | read news it is not necessary to subscribe to the mailing list. Send |
| 215 | e-mail to python-list-request@cwi.nl to (un)subscribe to the mailing |
| 216 | list. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 217 | |
| 218 | 1.6. Q. Is there a book on Python, or will there be one out soon? |
| 219 | |
| 220 | A. Unfortunately, not yet. I would like to write one but my |
| 221 | obligations at CWI include too much other work to make much progress |
| 222 | on it. Several parties have expressed interest in sponsoring or |
| 223 | helping the production of a book or reference manual, but so far there |
| 224 | are no firm plans. If you volunteer help, by all means drop me a |
| 225 | note! |
| 226 | |
| 227 | 1.7. Q. Are there any published articles about Python that I can quote? |
| 228 | |
| 229 | A. So far the only refereed and published article that describes |
| 230 | Python in some detail is: |
| 231 | |
| 232 | Guido van Rossum and Jelke de Boer, "Interactively Testing Remote |
| 233 | Servers Using the Python Programming Language", CWI Quarterly, Volume |
| 234 | 4, Issue 4 (December 1991), Amsterdam, pp 283-303. |
| 235 | |
| 236 | LaTeX source for this paper is available as part of the Python source |
| 237 | distribution. |
| 238 | |
| 239 | A more recent high-level description of Python is: |
| 240 | |
| 241 | Guido van Rossum, "An Introduction to Python for UNIX/C |
| 242 | Programmers", in the proceedings of the NLUUG najaarsconferentie |
| 243 | 1993 (dutch UNIX users group meeting november 1993). |
| 244 | |
| 245 | PostScript for this paper and for the slides used for the accompanying |
| 246 | presentation can be found in the ftp directory mentioned a few |
| 247 | questions earlier, with filenames nluug-paper.ps and nluug-slides.ps, |
| 248 | respectively. |
| 249 | |
Guido van Rossum | 95f61a7 | 1994-01-26 17:23:37 +0000 | [diff] [blame] | 250 | 1.8. Q. How does the Python version numbering scheme work? |
| 251 | |
| 252 | A. Python versions are numbered A.B.C. A is the major version number |
| 253 | -- it is only incremented for major changes in functionality or source |
| 254 | structure. B is the minor version number, incremented for less |
| 255 | earth-shattering changes to a release. C is the patchlevel -- it is |
| 256 | incremented for each new release. Note that in the past, patches have |
| 257 | added significant changes; in fact the changeover from 0.9.9 to 1.0.0 |
| 258 | was the first time that either A or B changed! |
| 259 | |
Guido van Rossum | 5333c5d | 1994-04-11 11:06:22 +0000 | [diff] [blame] | 260 | 1.9. Q. Are there other ftp sites that carry Python related material? |
| 261 | |
| 262 | A. An interesting ftp site for Python users is ftp.markv.com |
| 263 | (192.122.251.1); the directory pub/python contains a growing |
| 264 | collection of interesting Python scripts. To submit a script for |
| 265 | inclusion, place it together with a readme file (with extension |
| 266 | .readme) in the publicly writable directory /incoming/python. This |
| 267 | service is maintained by Lance Ellinghouse <lance@markv.com>. |
| 268 | |
| 269 | |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 270 | |
| 271 | 2. Python in the real world |
| 272 | =========================== |
| 273 | |
| 274 | 2.1. Q. How many people are using Python? |
| 275 | |
Guido van Rossum | 5333c5d | 1994-04-11 11:06:22 +0000 | [diff] [blame] | 276 | A. I don't know, but the maximum number of simultaneous subscriptions |
| 277 | to the Python mailing list before it was gatewayed into the newsgroup |
| 278 | was about 180 (several of which were local redistribution lists). I |
| 279 | believe that many active Python users don't bother to subscribe to the |
| 280 | list, and now that there's a newsgroup the mailing list subscription |
| 281 | is even less meaningful. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 282 | |
| 283 | 2.2. Q. Have any significant projects been done in Python? |
| 284 | |
| 285 | A. Here at CWI (the home of Python), we have written a 20,000 line |
| 286 | authoring environment for transportable hypermedia presentations, a |
Guido van Rossum | 5333c5d | 1994-04-11 11:06:22 +0000 | [diff] [blame] | 287 | 5,000 line multimedia teleconferencing tool, as well as many many |
| 288 | smaller programs. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 289 | |
| 290 | The University of Virginia uses Python to control a virtual reality |
| 291 | engine. Contact: Matt Conway <conway@virginia.edu>. |
| 292 | |
| 293 | See also the next question. |
| 294 | |
| 295 | 2.3. Q. Are there any commercial projects going on using Python? |
| 296 | |
| 297 | A. Several companies have revealed to me that they are planning or |
| 298 | considering to use Python in a future product. The furthest is |
| 299 | Sunrise Software, who already have a product out using Python -- they |
| 300 | use Python for a GUI management application and an SNMP network |
| 301 | manangement application. Contact: <info@sunrise.com>. |
| 302 | |
| 303 | Individuals at many other companies are using Python for |
| 304 | internal development (witness their contributions to the Python |
Guido van Rossum | 5333c5d | 1994-04-11 11:06:22 +0000 | [diff] [blame] | 305 | mailing list or newsgroup). |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 306 | |
| 307 | Python has also been elected as an extension language by MADE, a |
| 308 | consortium supported by the European Committee's ESPRIT program and |
| 309 | consisting of Bull, CWI and some other European companies. Contact: |
| 310 | Ivan Herman <ivan@cwi.nl>. |
| 311 | |
Guido van Rossum | 95f61a7 | 1994-01-26 17:23:37 +0000 | [diff] [blame] | 312 | 2.4. Q. How stable is Python? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 313 | |
Guido van Rossum | 44b4cb1 | 1994-05-04 13:28:51 +0000 | [diff] [blame] | 314 | A. Very stable. While the current version number (1.0.2) would |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 315 | suggest it is in the early stages of development, in fact new, stable |
Guido van Rossum | 95f61a7 | 1994-01-26 17:23:37 +0000 | [diff] [blame] | 316 | releases (numbered 0.9.x) have been coming out roughly every 3 to 6 |
| 317 | months for the past four years. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 318 | |
Guido van Rossum | 95f61a7 | 1994-01-26 17:23:37 +0000 | [diff] [blame] | 319 | 2.5. Q. What new developments are expected for Python in the future? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 320 | |
Guido van Rossum | 95f61a7 | 1994-01-26 17:23:37 +0000 | [diff] [blame] | 321 | A. Without warranty that any of this will actually be realized: I am |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 322 | currently thinking about mechanisms for built-in on-line help and a |
Guido van Rossum | 99bf250 | 1994-01-26 17:30:41 +0000 | [diff] [blame] | 323 | switch/case statement. There have been some discussions on |
| 324 | hierarchical module names which might solve the problem of the |
| 325 | development of large packages. A pthreads interface has been |
| 326 | contributed which I would like to merge into the latest release. The |
| 327 | X interface needs improving. There are also some people |
| 328 | (independently) working on a windowing interface based on STDWIN but |
| 329 | with the power and ease of use of the average modern widget set. I |
| 330 | still hope to get some help in producing a Windows version. It would |
| 331 | be nice if there were a window-based class browser (Someone at CWI has |
| 332 | contributed one using Motif but it needs some work). Also: improved |
| 333 | support for embedding Python in other applications, e.g. by renaming |
| 334 | most global symbols to have a "Py" prefix and providing more |
| 335 | documentation and threading support. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 336 | |
| 337 | |
| 338 | 3. Building Python |
| 339 | ================== |
| 340 | |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 341 | 3.1. Q. Is there a test set? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 342 | |
| 343 | A. Yes, simply do "import testall" (or "import autotest" if you aren't |
| 344 | interested in the output). The standard modules whose name begins |
| 345 | with "test" together comprise the test. The test set doesn't test |
| 346 | *all* features of Python but it goes a long way to confirm that a new |
| 347 | port is actually working. The Makefile contains an entry "make test" |
| 348 | which runs the autotest module. |
| 349 | |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 350 | 3.2. Q. When running the test set, I get complaints about floating point |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 351 | operations, but when playing with floating point operations I cannot |
| 352 | find anything wrong with them. |
| 353 | |
| 354 | A. The test set makes occasional unwarranted assumptions about the |
| 355 | semantics of C floating point operations. Until someone donates a |
| 356 | better floating point test set, you will have to comment out the |
| 357 | offending floating point tests and execute similar tests manually. |
| 358 | |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 359 | 3.3. Q. Link errors building Python with STDWIN on SGI IRIX. |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 360 | |
| 361 | A. Rebuild STDWIN, specifying "CC=cc -cckr" in the Makefile. |
| 362 | |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 363 | 3.4. Q. Link errors after rerunning the configure script. |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 364 | |
| 365 | A. It is generally necessary to run "make clean" after a configuration |
| 366 | change. |
| 367 | |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 368 | 3.5. Q. The python interpreter complains about options passed to a |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 369 | script (after the script name). |
| 370 | |
| 371 | A. You are probably linking with GNU getopt, e.g. through -liberty. |
| 372 | Don't. (If you are using this because you link with -lreadline, use |
| 373 | the readline distributed with Python instead.) |
| 374 | |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 375 | 3.6. Q. When building on the SGI, make tries to run python to create |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 376 | glmodule.c, but python hasn't been built or installed yet. |
| 377 | |
| 378 | A. Comment out the line mentioning glmodule.c in Setup and build a |
| 379 | python without gl first; install it or make sure it is in your $PATH, |
| 380 | then edit the Setup file again to turn on the gl module, and make |
| 381 | again. You don't need to do "make clean"; you do need to run "make |
| 382 | Makefile" in the Modules subdirectory (or just run "make" at the |
| 383 | toplevel). |
| 384 | |
Guido van Rossum | 44b4cb1 | 1994-05-04 13:28:51 +0000 | [diff] [blame] | 385 | 3.7. Q. Other trouble building Python 1.0.2 on platform X. |
Guido van Rossum | 95f61a7 | 1994-01-26 17:23:37 +0000 | [diff] [blame] | 386 | |
| 387 | A. Please email the details to <guido@cwi.nl> and I'll look into it. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 388 | |
| 389 | |
| 390 | 4. Programming in Python |
| 391 | ======================== |
| 392 | |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 393 | 4.1. Q. Is there a source code level debugger with breakpoints, step, |
| 394 | etc.? |
| 395 | |
| 396 | A. Yes. Check out module pdb; pdb.help() prints the documentation (or |
| 397 | you can read it as Lib/pdb.doc). If you use the STDWIN option, |
| 398 | there's also a windowing interface, wdb. You can write your own |
| 399 | debugger by using the code for pdb or wdb as an example. |
| 400 | |
| 401 | 4.2. Q. Can I create an object class with some methods implemented in |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 402 | C and others in Python (e.g. through inheritance)? (Also phrased as: |
| 403 | Can I use a built-in type as base class?) |
| 404 | |
| 405 | A. No, but you can easily create a Python class which serves as a |
| 406 | wrapper around a built-in object, e.g. (for dictionaries): |
| 407 | |
| 408 | # A user-defined class behaving almost identical |
| 409 | # to a built-in dictionary. |
| 410 | class UserDict: |
| 411 | def __init__(self): self.data = {} |
| 412 | def __repr__(self): return repr(self.data) |
| 413 | def __cmp__(self, dict): |
| 414 | if type(dict) == type(self.data): |
| 415 | return cmp(self.data, dict) |
| 416 | else: |
| 417 | return cmp(self.data, dict.data) |
| 418 | def __len__(self): return len(self.data) |
| 419 | def __getitem__(self, key): return self.data[key] |
| 420 | def __setitem__(self, key, item): self.data[key] = item |
| 421 | def __delitem__(self, key): del self.data[key] |
| 422 | def keys(self): return self.data.keys() |
| 423 | def items(self): return self.data.items() |
| 424 | def values(self): return self.data.values() |
| 425 | def has_key(self, key): return self.data.has_key(key) |
| 426 | |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 427 | 4.3. Q. Is there a curses/termcap package for Python? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 428 | |
| 429 | A. No, but you can use the "alfa" (== character cell) version of |
| 430 | STDWIN. (STDWIN == Standard Windows, a portable windowing system |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 431 | interface by the same author, URL: ftp://ftp.cwi.nl/pub/stdwin.) |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 432 | This will also prepare your program for porting to windowing |
| 433 | environments such as X11 or the Macintosh. |
| 434 | |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 435 | 4.4. Q. Is there an equivalent to C's onexit() in Python? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 436 | |
| 437 | A. Yes, if you import sys and assign a function to sys.exitfunc, it |
| 438 | will be called when your program exits, is killed by an unhandled |
| 439 | exception, or (on UNIX) receives a SIGHUP or SIGTERM signal. |
| 440 | |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 441 | 4.5. Q. When I define a function nested inside another function, the |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 442 | nested function seemingly can't access the local variables of the |
| 443 | outer function. What is going on? How do I pass local data to a |
| 444 | nested function? |
| 445 | |
| 446 | A. Python does not have arbitrarily nested scopes. When you need to |
| 447 | create a function that needs to access some data which you have |
| 448 | available locally, create a new class to hold the data and return a |
| 449 | method of an instance of that class, e.g.: |
| 450 | |
| 451 | class MultiplierClass: |
| 452 | def __init__(self, factor): |
| 453 | self.factor = factor |
| 454 | def multiplier(self, argument): |
| 455 | return argument * self.factor |
| 456 | |
| 457 | def generate_multiplier(factor): |
| 458 | return MultiplierClass(factor).multiplier |
| 459 | |
| 460 | twice = generate_multiplier(2) |
| 461 | print twice(10) |
| 462 | # Output: 20 |
| 463 | |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 464 | 4.6. Q. How do I iterate over a sequence in reverse order? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 465 | |
| 466 | A. If it is a list, the fastest solution is |
| 467 | |
| 468 | list.reverse() |
| 469 | try: |
| 470 | for x in list: |
| 471 | "do something with x" |
| 472 | finally: |
| 473 | list.reverse() |
| 474 | |
| 475 | This has the disadvantage that while you are in the loop, the list |
| 476 | is temporarily reversed. If you don't like this, you can make a copy. |
| 477 | This appears expensive but is actually faster than other solutions: |
| 478 | |
| 479 | rev = list[:] |
| 480 | rev.reverse() |
| 481 | for x in rev: |
| 482 | <do something with x> |
| 483 | |
| 484 | If it isn't a list, a more general but slower solution is: |
| 485 | |
| 486 | i = len(list) |
| 487 | while i > 0: |
| 488 | i = i-1 |
| 489 | x = list[i] |
| 490 | <do something with x> |
| 491 | |
| 492 | A more elegant solution, is to define a class which acts as a sequence |
| 493 | and yields the elements in reverse order (solution due to Steve |
| 494 | Majewski): |
| 495 | |
| 496 | class Rev: |
| 497 | def __init__(self, seq): |
| 498 | self.forw = seq |
| 499 | def __len__(self): |
| 500 | return len(self.forw) |
| 501 | def __getitem__(self, i): |
| 502 | return self.forw[-(i + 1)] |
| 503 | |
| 504 | You can now simply write: |
| 505 | |
| 506 | for x in Rev(list): |
| 507 | <do something with x> |
| 508 | |
| 509 | Unfortunately, this solution is slowest of all, due the the method |
| 510 | call overhead... |
| 511 | |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 512 | 4.7. Q. My program is too slow. How do I speed it up? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 513 | |
| 514 | A. That's a tough one, in general. There are many tricks to speed up |
| 515 | Python code; I would consider rewriting parts in C only as a last |
| 516 | resort. One thing to notice is that function and (especially) method |
| 517 | calls are rather expensive; if you have designed a purely OO interface |
| 518 | with lots of tiny functions that don't do much more than get or set an |
| 519 | instance variable or call another method, you may consider using a |
| 520 | more direct way, e.g. directly accessing instance variables. Also see |
| 521 | the standard module "profile" (described in the file |
| 522 | "python/lib/profile.doc") which makes it possible to find out where |
| 523 | your program is spending most of its time (if you have some patience |
| 524 | -- the profiling itself can slow your program down by an order of |
| 525 | magnitude). |
| 526 | |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 527 | 4.8. Q. When I have imported a module, then edit it, and import it |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 528 | again (into the same Python process), the changes don't seem to take |
| 529 | place. What is going on? |
| 530 | |
| 531 | A. For efficiency reasons, Python only reads the module file on the |
| 532 | first time a module is imported (otherwise a program consisting of |
| 533 | many modules, each of which imports the same basic module, would read |
| 534 | the basic module over and over again). To force a changed module |
| 535 | being read again, do this: |
| 536 | |
| 537 | import modname |
| 538 | reload(modname) |
| 539 | |
| 540 | Warning: this technique is not 100% fool-proof. In particular, |
| 541 | modules containing statements like |
| 542 | |
| 543 | from modname import some_objects |
| 544 | |
| 545 | will continue to work with the old version of the objects imported |
| 546 | thus. |
| 547 | |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 548 | 4.9. Q. I have a module in which I want to execute some extra code when it |
| 549 | is run as a script. How do I find out whether I am running as a |
| 550 | script? |
| 551 | |
| 552 | A. A module can find out its own module name by alooking at the |
| 553 | (predefined) global variable __name__. If this has the value |
| 554 | '__main__' you are running as a script. E.g. if you put the following |
| 555 | on the last line of your module, main() is called only when your |
| 556 | module is running as a script: |
| 557 | |
| 558 | if __name__ == '__main__': main() |
| 559 | |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 560 | |
| 561 | 5. Extending Python |
| 562 | =================== |
| 563 | |
| 564 | 5.1. Q. Can I create my own functions in C? |
| 565 | |
| 566 | A. Yes, you can create built-in modules containing functions, |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 567 | variables, exceptions and even new types in C. This is explained in |
| 568 | the document "Extending and Embedding the Python Interpreter" (the |
| 569 | LaTeX file Doc/ext.tex). Also read the chapter on dynamic loading. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 570 | |
| 571 | 5.2. Q. Can I create my own functions in C++? |
| 572 | |
| 573 | A. Yes, using the C-compatibility features found in C++. Basically |
| 574 | you place extern "C" { ... } around the Python include files and put |
| 575 | extern "C" before each function that is going to be called by the |
| 576 | Python interpreter. Global or static C++ objects with constructors |
| 577 | are probably not a good idea. |
| 578 | |
| 579 | |
| 580 | 6. Python's design |
| 581 | ================== |
| 582 | |
| 583 | 6.1. Q. Why isn't there a generic copying operation for objects in |
| 584 | Python? |
| 585 | |
| 586 | A. Hmm. Maybe there should be one, but it's difficult to assign a |
| 587 | useful meaning to copying of open files, sockets and windows, or |
| 588 | recursive data structures. As long as you design all your classes |
| 589 | yourself you are of course free to define a standard base class that |
| 590 | defines an overridable copying operation for all the objects you care |
| 591 | about. (One practical point: it would have to be a built-in function, |
| 592 | not a standard method name, since not all built-in object types have |
| 593 | methods; e.g. strings, integers and tuples don't.) |
| 594 | |
| 595 | 6.2. Q. Why isn't there a generic way to implement persistent objects |
| 596 | in Python? (Persistent == automatically saved to and restored from |
| 597 | disk.) |
| 598 | |
| 599 | A. Hmm, hmm. Basically for the same reasons as why there is no |
| 600 | generic copying operation. |
| 601 | |
| 602 | 6.3. Q. Why isn't there a switch or case statement in Python? |
| 603 | |
| 604 | A. You can do this easily enough with a sequence of |
| 605 | if... elif... elif... else. There have been some proposals for switch |
| 606 | statement syntax, but there is no concensus (yet) on whether and how |
| 607 | to do range tests. |
| 608 | |
| 609 | |
| 610 | 7. Using Python on non-UNIX platforms |
| 611 | ===================================== |
| 612 | |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 613 | 7.1. Q. Is there a Mac version of Python? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 614 | |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 615 | A. Yes. It is on most ftp sites carrying Python as python.sea.hqx -- |
| 616 | this is a self-extracting archive containing the application binary as |
| 617 | well as the Lib modules. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 618 | |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 619 | 7.2. Q. Is there a DOS version of Python? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 620 | |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 621 | A. Yes. More than one, actually: 16python.exe runs in standard DOS |
| 622 | mode on 186 CPUs or higher; 32python.exe uses a DOS extender and only |
| 623 | runs on a 386 or higher CPUs. Although 16python.exe does not pass the |
| 624 | test set because test_grammar is too big for the parser, it actually |
| 625 | has about 270 kbyte of allocatable heap space, which is sufficient for |
| 626 | fairly large programs. 32python.exe is distributed as a tar file |
| 627 | containing the required DOS extended and 387 emulator. Both are on |
| 628 | most ftp sites carrying Python. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 629 | |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 630 | 7.3. Q. Is there a Windows version of Python? |
| 631 | |
| 632 | A. Yes. Use qwpython.exe. The only problem with it: ^C |
| 633 | unconditionally kills the entire program -- it does not raise |
| 634 | KeyboardInterrupt. You can also run 16python.exe or 32python.exe in a |
| 635 | "DOS box", but qwpython.exe appears to be slightly faster. |
| 636 | |
| 637 | 7.4. Q. Is there a Windows NT version of Python? |
| 638 | |
| 639 | A. Yes. Use ntpython.exe. This is for Intel CPUs. If you want a |
| 640 | Windows user interface, use qwpython.exe. |
| 641 | |
| 642 | 7.5. Q. I have the DOS or Windows version but it appears to be only a |
| 643 | binary. Where's the library? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 644 | |
| 645 | A. You still need to copy the files from the distribution directory |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 646 | "python/Lib" to your system. If you don't have the full distribution, |
Guido van Rossum | 44b4cb1 | 1994-05-04 13:28:51 +0000 | [diff] [blame] | 647 | you can get the file pythonlib1.0.2.tar.gz from most ftp sites carrying |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 648 | Python; this is a subset of the distribution containing just those |
| 649 | file. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 650 | |
Guido van Rossum | 5333c5d | 1994-04-11 11:06:22 +0000 | [diff] [blame] | 651 | Once you have installed the library, you need to point sys.path to it. |
| 652 | Assuming the library is in C:\misc\python\lib, the following commands |
| 653 | will point your Python interpreter to it (note the doubled backslashes |
| 654 | -- you can also use single forward slashes instead): |
| 655 | |
| 656 | >>> import sys |
| 657 | >>> sys.path.insert(0, 'C:\\misc\\python\\lib') |
| 658 | >>> |
| 659 | |
| 660 | For a more permanent effect, set the environment variable PYTHONPATH, |
| 661 | as follows (talking to a DOS prompt): |
| 662 | |
| 663 | C> SET PYTHONPATH=C:\misc\python\lib |
| 664 | |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 665 | 7.6. Q. Where's the documentation for the Mac or PC version? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 666 | |
| 667 | A. There isn't any. The documentation for the Unix version also |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 668 | applies to the Mac and PC versions. Where applicable, differences |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 669 | are indicated in the text. |
| 670 | |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 671 | 7.7. Q. The Mac (PC) version doesn't seem to have any facilities for |
| 672 | creating or editing programs apart from entering it interactively, and |
| 673 | there seems to be no way to save code that was entered interactively. |
| 674 | How do I create a Python program on the Mac (PC)? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 675 | |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 676 | A. Use an external editor. On the Mac, I am quite happy with the Desk |
| 677 | Accessory called Sigma Edit; this doesn't require Multifinder or |
| 678 | System 7. I work like this: start the interpreter; edit a module file |
| 679 | using Sigma Edit; import and test it in the interpreter; edit again in |
| 680 | Sigma Edit; then use the built-in function reload() to re-read the |
| 681 | imported module; etc. |
Guido van Rossum | 5333c5d | 1994-04-11 11:06:22 +0000 | [diff] [blame] | 682 | |
| 683 | Regarding the same question for the PC, Kurt Wm. Hemr writes: "While |
| 684 | anyone with a pulse could certainly figure out how to do the same on |
| 685 | MS-Windows, I would recommend the NotGNU Emacs clone for MS-Windows. |
| 686 | Not only can you easily resave and "reload()" from Python after making |
| 687 | changes, but since WinNot auto-copies to the clipboard any text you |
| 688 | select, you can simply select the entire procedure (function) which |
| 689 | you changed in WinNot, switch to QWPython, and shift-ins to reenter |
| 690 | the changed program unit." |