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