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 | 95f61a7 | 1994-01-26 17:23:37 +0000 | [diff] [blame] | 9 | Version: 1.3 |
| 10 | Last-modified: 26 Jan 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 |
| 78 | 3.1. Q. I have trouble building the md5 module and/or finding the file |
| 79 | md5.c. |
| 80 | 3.2. Q. Is there a test set? |
| 81 | 3.3. Q. When running the test set, I get complaints about floating point |
| 82 | operations, but when playing with floating point operations I cannot |
| 83 | find anything wrong with them. |
| 84 | 3.4. Q. I get an OverflowError on evaluating 2*2. What is going on? |
Guido van Rossum | 95f61a7 | 1994-01-26 17:23:37 +0000 | [diff] [blame] | 85 | 3.5. Q. Trouble building Python 0.9.9 or earlier on platform X. |
| 86 | 3.6. Q. Trouble building Python 1.0.0 on platform X. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 87 | |
| 88 | 4. Programming in Python |
| 89 | 4.1. Q. Can I create an object class with some methods implemented in |
| 90 | C and others in Python (e.g. through inheritance)? (Also phrased as: |
| 91 | Can I use a built-in type as base class?) |
| 92 | 4.2. Q. I assign to a variable in a call to exec() but when I try to |
| 93 | use it on the next line I get an error. What is going on? |
| 94 | 4.3. Q. Why does that work? |
| 95 | 4.4. Q. Is there a curses/termcap package for Python? |
| 96 | 4.5. Q. Is there an equivalent to C's onexit() in Python? |
| 97 | 4.6. Q. When I define a function nested inside another function, the |
| 98 | nested function seemingly can't access the local variables of the |
| 99 | outer function. What is going on? How do I pass local data to a |
| 100 | nested function? |
| 101 | 4.7. Q. How do I iterate over a sequence in reverse order? |
| 102 | 4.8. Q. My program is too slow. How do I speed it up? |
| 103 | 4.9. Q. When I have imported a module, then edit it, and import it |
| 104 | again (into the same Python process), the changes don't seem to take |
| 105 | place. What is going on? |
| 106 | |
| 107 | 5. Extending Python |
| 108 | 5.1. Q. Can I create my own functions in C? |
| 109 | 5.2. Q. Can I create my own functions in C++? |
| 110 | |
| 111 | 6. Python's design |
| 112 | 6.1. Q. Why isn't there a generic copying operation for objects in |
| 113 | Python? |
| 114 | 6.2. Q. Why isn't there a generic way to implement persistent objects |
| 115 | in Python? (Persistent == automatically saved to and restored from |
| 116 | disk.) |
| 117 | 6.3. Q. Why isn't there a switch or case statement in Python? |
| 118 | |
| 119 | 7. Using Python on non-UNIX platforms |
Guido van Rossum | 95f61a7 | 1994-01-26 17:23:37 +0000 | [diff] [blame] | 120 | 7.1. Q. Where's the DOS version of 1.0.0? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 121 | 7.2. Q. Is there a Windows version of Python? |
| 122 | 7.3. Q. I have the Mac or DOS version but it appears to be only a binary. |
| 123 | Where's the library? |
| 124 | 7.4. Q. Where's the documentation for the Mac or DOS version? |
| 125 | 7.5. Q. The Mac version doesn't seem to have any facilities for creating or |
| 126 | editing programs apart from entering it interactively, and there seems |
| 127 | to be no way to save code that was entered interactively. How do I |
| 128 | create a Python program on the Mac? |
| 129 | |
| 130 | To find a particular question, search for the question number followed |
| 131 | by a dot, a space, and a Q at the beginning of a line (e.g. to find |
| 132 | question 4.2 in vi, type /^4\.2\. Q/). |
| 133 | |
| 134 | |
| 135 | 1. General information and availability |
| 136 | ======================================= |
| 137 | |
| 138 | 1.1. Q. What is Python? |
| 139 | |
| 140 | A. Python is an interpreted, interactive, object-oriented programming |
| 141 | language. It incorporates modules, exceptions, dynamic typing, very |
| 142 | high level dynamic data types, and classes. Python combines |
| 143 | remarkable power with very clear syntax. It has interfaces to many |
| 144 | system calls and libraries, as well as to various window systems, and |
| 145 | is extensible in C or C++. It is also usable as an extension language |
| 146 | for applications that need a programmable interface. Finally, Python |
| 147 | is portable: it runs on many brands of UNIX, on the Mac, and on |
| 148 | MS-DOS. |
| 149 | |
| 150 | To find out more, the best thing to do is to start reading the |
| 151 | tutorial from the documentation set (see a few questions further |
| 152 | down). |
| 153 | |
| 154 | 1.2. Q. Why is it called Python? |
| 155 | |
| 156 | A. Apart from being a computer wizard, I'm also a fan of "Monty |
| 157 | Python's Flying Circus" (a BBC comedy series from the seventies, in |
| 158 | case you didn't know). It occurred to me one day that I needed a name |
| 159 | that was short, unique, and slightly mysterious. And I happened to be |
| 160 | reading some scripts from the series at the time... So then I decided |
| 161 | to call my language Python. But Python is not a joke. And don't you |
| 162 | associate it with dangerous reptiles either! |
| 163 | |
| 164 | 1.3. Q. How do I obtain a copy of the Python source? |
| 165 | |
| 166 | A. The latest Python source distribution is always available by |
| 167 | anonymous ftp from ftp.cwi.nl [192.16.184.180] in the directory |
| 168 | /pub/python, with filename python<version>.tar.Z. It is a compressed |
| 169 | tar file containing the complete C source, LaTeX documentation, Python |
| 170 | library modules, example programs, and several useful pieces of freely |
| 171 | distributable software. This will compile and run out of the box on |
Guido van Rossum | 95f61a7 | 1994-01-26 17:23:37 +0000 | [diff] [blame] | 172 | most UNIX platforms. Currently <version> is 1.0.0. (See section 7 |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 173 | for non-UNIX information.) |
| 174 | |
| 175 | 1.4. Q. How do I get documentation on Python? |
| 176 | |
| 177 | A. The latest Python documentation set is always available by |
| 178 | anonymous ftp from ftp.cwi.nl [192.16.184.180] in the directory |
| 179 | /pub/python, with filename pythondoc-ps<version>.tar.Z. It is a |
| 180 | compressed tar file containing PostScript files of the reference |
| 181 | manual, the library manual, and the tutorial. Currently <version> is |
Guido van Rossum | 95f61a7 | 1994-01-26 17:23:37 +0000 | [diff] [blame] | 182 | 1.0.0. (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] | 183 | set, as much of Python's power stems from the standard or built-in |
| 184 | types, functions and modules, all of which are described here.) |
| 185 | PostScript for a high-level description of Python is in the file |
| 186 | nluug-paper.ps. |
| 187 | |
| 188 | The following sites keep mirrors of the Python distribution: |
| 189 | |
| 190 | Site IP address Directory |
| 191 | |
| 192 | gatekeeper.dec.com 16.1.0.2 /pub/plan/python/cwi |
| 193 | ftp.uu.net 192.48.96.9 /languages/python |
| 194 | ftp.wustl.edu 128.252.135.4 /graphics/graphics/sgi-stuff/python |
| 195 | ftp.funet.fi 128.214.6.100 /pub/languages/python (old?) |
| 196 | ftp.fu-berlin.de 130.133.4.50 /pub/unix/languages/python (python* only) |
| 197 | |
Guido van Rossum | 95f61a7 | 1994-01-26 17:23:37 +0000 | [diff] [blame] | 198 | Or try archie on e.g. python1.0.0.tar.Z to locate the nearest copy of |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 199 | that version... |
| 200 | |
| 201 | 1.5. Q. Is there a newsgroup or mailing list devoted to Python? |
| 202 | |
| 203 | A. There is no Python newsgroup yet; if you want to post to the net |
| 204 | about Python, use comp.lang.misc. There is a mailing list devoted to |
| 205 | Python; send e-mail to python-list-request@cwi.nl to (un)subscribe. |
| 206 | There are plans to start the discussion about creation of |
| 207 | comp.lang.python as soon as version 1.0.0 has been released. |
| 208 | |
| 209 | 1.6. Q. Is there a book on Python, or will there be one out soon? |
| 210 | |
| 211 | A. Unfortunately, not yet. I would like to write one but my |
| 212 | obligations at CWI include too much other work to make much progress |
| 213 | on it. Several parties have expressed interest in sponsoring or |
| 214 | helping the production of a book or reference manual, but so far there |
| 215 | are no firm plans. If you volunteer help, by all means drop me a |
| 216 | note! |
| 217 | |
| 218 | 1.7. Q. Are there any published articles about Python that I can quote? |
| 219 | |
| 220 | A. So far the only refereed and published article that describes |
| 221 | Python in some detail is: |
| 222 | |
| 223 | Guido van Rossum and Jelke de Boer, "Interactively Testing Remote |
| 224 | Servers Using the Python Programming Language", CWI Quarterly, Volume |
| 225 | 4, Issue 4 (December 1991), Amsterdam, pp 283-303. |
| 226 | |
| 227 | LaTeX source for this paper is available as part of the Python source |
| 228 | distribution. |
| 229 | |
| 230 | A more recent high-level description of Python is: |
| 231 | |
| 232 | Guido van Rossum, "An Introduction to Python for UNIX/C |
| 233 | Programmers", in the proceedings of the NLUUG najaarsconferentie |
| 234 | 1993 (dutch UNIX users group meeting november 1993). |
| 235 | |
| 236 | PostScript for this paper and for the slides used for the accompanying |
| 237 | presentation can be found in the ftp directory mentioned a few |
| 238 | questions earlier, with filenames nluug-paper.ps and nluug-slides.ps, |
| 239 | respectively. |
| 240 | |
Guido van Rossum | 95f61a7 | 1994-01-26 17:23:37 +0000 | [diff] [blame] | 241 | 1.8. Q. How does the Python version numbering scheme work? |
| 242 | |
| 243 | A. Python versions are numbered A.B.C. A is the major version number |
| 244 | -- it is only incremented for major changes in functionality or source |
| 245 | structure. B is the minor version number, incremented for less |
| 246 | earth-shattering changes to a release. C is the patchlevel -- it is |
| 247 | incremented for each new release. Note that in the past, patches have |
| 248 | added significant changes; in fact the changeover from 0.9.9 to 1.0.0 |
| 249 | was the first time that either A or B changed! |
| 250 | |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 251 | |
| 252 | 2. Python in the real world |
| 253 | =========================== |
| 254 | |
| 255 | 2.1. Q. How many people are using Python? |
| 256 | |
| 257 | A. I don't know, but at the last count there were at least 130 |
| 258 | addresses on the Python mailing list (several of which are local |
| 259 | redistribution lists). I suspect that many users don't bother |
| 260 | to subscribe to the list. |
| 261 | |
| 262 | 2.2. Q. Have any significant projects been done in Python? |
| 263 | |
| 264 | A. Here at CWI (the home of Python), we have written a 20,000 line |
| 265 | authoring environment for transportable hypermedia presentations, a |
| 266 | multimedia teleconferencing tool, as well as many smaller programs. |
| 267 | |
| 268 | The University of Virginia uses Python to control a virtual reality |
| 269 | engine. Contact: Matt Conway <conway@virginia.edu>. |
| 270 | |
| 271 | See also the next question. |
| 272 | |
| 273 | 2.3. Q. Are there any commercial projects going on using Python? |
| 274 | |
| 275 | A. Several companies have revealed to me that they are planning or |
| 276 | considering to use Python in a future product. The furthest is |
| 277 | Sunrise Software, who already have a product out using Python -- they |
| 278 | use Python for a GUI management application and an SNMP network |
| 279 | manangement application. Contact: <info@sunrise.com>. |
| 280 | |
| 281 | Individuals at many other companies are using Python for |
| 282 | internal development (witness their contributions to the Python |
| 283 | mailing list). |
| 284 | |
| 285 | Python has also been elected as an extension language by MADE, a |
| 286 | consortium supported by the European Committee's ESPRIT program and |
| 287 | consisting of Bull, CWI and some other European companies. Contact: |
| 288 | Ivan Herman <ivan@cwi.nl>. |
| 289 | |
Guido van Rossum | 95f61a7 | 1994-01-26 17:23:37 +0000 | [diff] [blame] | 290 | 2.4. Q. How stable is Python? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 291 | |
Guido van Rossum | 95f61a7 | 1994-01-26 17:23:37 +0000 | [diff] [blame] | 292 | A. Very stable. While the current version number (1.0.0) would |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 293 | 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] | 294 | releases (numbered 0.9.x) have been coming out roughly every 3 to 6 |
| 295 | months for the past four years. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 296 | |
Guido van Rossum | 95f61a7 | 1994-01-26 17:23:37 +0000 | [diff] [blame] | 297 | 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] | 298 | |
Guido van Rossum | 95f61a7 | 1994-01-26 17:23:37 +0000 | [diff] [blame] | 299 | 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] | 300 | 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^] | 301 | switch/case statement. There have been some discussions on |
| 302 | hierarchical module names which might solve the problem of the |
| 303 | development of large packages. A pthreads interface has been |
| 304 | contributed which I would like to merge into the latest release. The |
| 305 | X interface needs improving. There are also some people |
| 306 | (independently) working on a windowing interface based on STDWIN but |
| 307 | with the power and ease of use of the average modern widget set. I |
| 308 | still hope to get some help in producing a Windows version. It would |
| 309 | be nice if there were a window-based class browser (Someone at CWI has |
| 310 | contributed one using Motif but it needs some work). Also: improved |
| 311 | support for embedding Python in other applications, e.g. by renaming |
| 312 | most global symbols to have a "Py" prefix and providing more |
| 313 | documentation and threading support. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 314 | |
| 315 | |
| 316 | 3. Building Python |
| 317 | ================== |
| 318 | |
| 319 | 3.1. Q. I have trouble building the md5 module and/or finding the file |
| 320 | md5.c. |
| 321 | |
| 322 | A. Apparently the md5 module was based on an older version of RSA's |
| 323 | md5 implementation. The ftp site rsa.com mentioned in the Makefile |
| 324 | where this version was found is no longer accessible, and the version |
| 325 | from RFC 1321 (md5c.c) is slightly different. This will be fixed in |
| 326 | the 1.0 release; write me if you need the fixes now. |
| 327 | |
| 328 | 3.2. Q. Is there a test set? |
| 329 | |
| 330 | A. Yes, simply do "import testall" (or "import autotest" if you aren't |
| 331 | interested in the output). The standard modules whose name begins |
| 332 | with "test" together comprise the test. The test set doesn't test |
| 333 | *all* features of Python but it goes a long way to confirm that a new |
| 334 | port is actually working. The Makefile contains an entry "make test" |
| 335 | which runs the autotest module. |
| 336 | |
| 337 | 3.3. Q. When running the test set, I get complaints about floating point |
| 338 | operations, but when playing with floating point operations I cannot |
| 339 | find anything wrong with them. |
| 340 | |
| 341 | A. The test set makes occasional unwarranted assumptions about the |
| 342 | semantics of C floating point operations. Until someone donates a |
| 343 | better floating point test set, you will have to comment out the |
| 344 | offending floating point tests and execute similar tests manually. |
| 345 | |
| 346 | 3.4. Q. I get an OverflowError on evaluating 2*2. What is going on? |
| 347 | |
| 348 | A. Your machine probably has 64 bit long integers (e.g. DEC alpha or |
| 349 | HP snake architectures). There are some dependencies on word length |
| 350 | in file intobject.c. This will be corrected in the 1.0 release; until |
| 351 | then, on a 64 bit machine, just comment out the check for overflow |
| 352 | from int_mul: |
| 353 | |
| 354 | #if 0 |
| 355 | if (x > 0x7fffffff || x < (double) (long) 0x80000000) |
| 356 | return err_ovf("integer multiplication"); |
| 357 | #endif |
| 358 | |
| 359 | You should also include <limits.h> and replace the constant 32 by |
| 360 | LONG_BIT in int_[lr]shift. |
| 361 | |
Guido van Rossum | 95f61a7 | 1994-01-26 17:23:37 +0000 | [diff] [blame] | 362 | 3.5. Q. Trouble building Python 0.9.9 or earlier on platform X. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 363 | |
Guido van Rossum | 95f61a7 | 1994-01-26 17:23:37 +0000 | [diff] [blame] | 364 | Please convert to Python 1.0.0 -- it is much more portable. |
| 365 | |
| 366 | 3.6. Q. Trouble building Python 1.0.0 on platform X. |
| 367 | |
| 368 | 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] | 369 | |
| 370 | |
| 371 | 4. Programming in Python |
| 372 | ======================== |
| 373 | |
| 374 | 4.1. Q. Can I create an object class with some methods implemented in |
| 375 | C and others in Python (e.g. through inheritance)? (Also phrased as: |
| 376 | Can I use a built-in type as base class?) |
| 377 | |
| 378 | A. No, but you can easily create a Python class which serves as a |
| 379 | wrapper around a built-in object, e.g. (for dictionaries): |
| 380 | |
| 381 | # A user-defined class behaving almost identical |
| 382 | # to a built-in dictionary. |
| 383 | class UserDict: |
| 384 | def __init__(self): self.data = {} |
| 385 | def __repr__(self): return repr(self.data) |
| 386 | def __cmp__(self, dict): |
| 387 | if type(dict) == type(self.data): |
| 388 | return cmp(self.data, dict) |
| 389 | else: |
| 390 | return cmp(self.data, dict.data) |
| 391 | def __len__(self): return len(self.data) |
| 392 | def __getitem__(self, key): return self.data[key] |
| 393 | def __setitem__(self, key, item): self.data[key] = item |
| 394 | def __delitem__(self, key): del self.data[key] |
| 395 | def keys(self): return self.data.keys() |
| 396 | def items(self): return self.data.items() |
| 397 | def values(self): return self.data.values() |
| 398 | def has_key(self, key): return self.data.has_key(key) |
| 399 | |
| 400 | 4.2. Q. I assign to a variable in a call to exec() but when I try to |
| 401 | use it on the next line I get an error. What is going on? |
| 402 | |
| 403 | A. The reason why this occurs is too complicated to explain (but see |
| 404 | the next question). To fix it is easy, however: simply assign None to |
| 405 | the variable *before* calling exec(). This will be fixed in the 1.0 |
| 406 | release. |
| 407 | |
| 408 | 4.3. Q. Why does that work? |
| 409 | |
| 410 | A. When parsing your program and converting it into internal pseudo |
| 411 | code, the interpreter does some optimizations to speed up function |
| 412 | execution: it figures out the names of all the local variables and |
| 413 | treats them specially. Because your assignment is done by exec(), it |
| 414 | is not seen initially by the parser and the variable is not recognized |
| 415 | as a local variable. The default treatment is as a global variable, |
| 416 | but the exec() statement places it in the local scope, where it is not |
| 417 | found. This will be fixed in release 1.0 by making exec into a |
| 418 | statement; the parser will then be able to switch off the |
| 419 | optimizations for local variables if it encounters an exec statement |
| 420 | (recognizing calls to built-in functions is not possible for the |
| 421 | parser, hence the syntax change to a statement). |
| 422 | |
| 423 | 4.4. Q. Is there a curses/termcap package for Python? |
| 424 | |
| 425 | A. No, but you can use the "alfa" (== character cell) version of |
| 426 | STDWIN. (STDWIN == Standard Windows, a portable windowing system |
| 427 | interface by the same author, URL ftp://ftp.cwi.nl/pub/stdwin.) |
| 428 | This will also prepare your program for porting to windowing |
| 429 | environments such as X11 or the Macintosh. |
| 430 | |
| 431 | 4.5. Q. Is there an equivalent to C's onexit() in Python? |
| 432 | |
| 433 | A. Yes, if you import sys and assign a function to sys.exitfunc, it |
| 434 | will be called when your program exits, is killed by an unhandled |
| 435 | exception, or (on UNIX) receives a SIGHUP or SIGTERM signal. |
| 436 | |
| 437 | 4.6. Q. When I define a function nested inside another function, the |
| 438 | nested function seemingly can't access the local variables of the |
| 439 | outer function. What is going on? How do I pass local data to a |
| 440 | nested function? |
| 441 | |
| 442 | A. Python does not have arbitrarily nested scopes. When you need to |
| 443 | create a function that needs to access some data which you have |
| 444 | available locally, create a new class to hold the data and return a |
| 445 | method of an instance of that class, e.g.: |
| 446 | |
| 447 | class MultiplierClass: |
| 448 | def __init__(self, factor): |
| 449 | self.factor = factor |
| 450 | def multiplier(self, argument): |
| 451 | return argument * self.factor |
| 452 | |
| 453 | def generate_multiplier(factor): |
| 454 | return MultiplierClass(factor).multiplier |
| 455 | |
| 456 | twice = generate_multiplier(2) |
| 457 | print twice(10) |
| 458 | # Output: 20 |
| 459 | |
| 460 | 4.7. Q. How do I iterate over a sequence in reverse order? |
| 461 | |
| 462 | A. If it is a list, the fastest solution is |
| 463 | |
| 464 | list.reverse() |
| 465 | try: |
| 466 | for x in list: |
| 467 | "do something with x" |
| 468 | finally: |
| 469 | list.reverse() |
| 470 | |
| 471 | This has the disadvantage that while you are in the loop, the list |
| 472 | is temporarily reversed. If you don't like this, you can make a copy. |
| 473 | This appears expensive but is actually faster than other solutions: |
| 474 | |
| 475 | rev = list[:] |
| 476 | rev.reverse() |
| 477 | for x in rev: |
| 478 | <do something with x> |
| 479 | |
| 480 | If it isn't a list, a more general but slower solution is: |
| 481 | |
| 482 | i = len(list) |
| 483 | while i > 0: |
| 484 | i = i-1 |
| 485 | x = list[i] |
| 486 | <do something with x> |
| 487 | |
| 488 | A more elegant solution, is to define a class which acts as a sequence |
| 489 | and yields the elements in reverse order (solution due to Steve |
| 490 | Majewski): |
| 491 | |
| 492 | class Rev: |
| 493 | def __init__(self, seq): |
| 494 | self.forw = seq |
| 495 | def __len__(self): |
| 496 | return len(self.forw) |
| 497 | def __getitem__(self, i): |
| 498 | return self.forw[-(i + 1)] |
| 499 | |
| 500 | You can now simply write: |
| 501 | |
| 502 | for x in Rev(list): |
| 503 | <do something with x> |
| 504 | |
| 505 | Unfortunately, this solution is slowest of all, due the the method |
| 506 | call overhead... |
| 507 | |
| 508 | 4.8. Q. My program is too slow. How do I speed it up? |
| 509 | |
| 510 | A. That's a tough one, in general. There are many tricks to speed up |
| 511 | Python code; I would consider rewriting parts in C only as a last |
| 512 | resort. One thing to notice is that function and (especially) method |
| 513 | calls are rather expensive; if you have designed a purely OO interface |
| 514 | with lots of tiny functions that don't do much more than get or set an |
| 515 | instance variable or call another method, you may consider using a |
| 516 | more direct way, e.g. directly accessing instance variables. Also see |
| 517 | the standard module "profile" (described in the file |
| 518 | "python/lib/profile.doc") which makes it possible to find out where |
| 519 | your program is spending most of its time (if you have some patience |
| 520 | -- the profiling itself can slow your program down by an order of |
| 521 | magnitude). |
| 522 | |
| 523 | 4.9. Q. When I have imported a module, then edit it, and import it |
| 524 | again (into the same Python process), the changes don't seem to take |
| 525 | place. What is going on? |
| 526 | |
| 527 | A. For efficiency reasons, Python only reads the module file on the |
| 528 | first time a module is imported (otherwise a program consisting of |
| 529 | many modules, each of which imports the same basic module, would read |
| 530 | the basic module over and over again). To force a changed module |
| 531 | being read again, do this: |
| 532 | |
| 533 | import modname |
| 534 | reload(modname) |
| 535 | |
| 536 | Warning: this technique is not 100% fool-proof. In particular, |
| 537 | modules containing statements like |
| 538 | |
| 539 | from modname import some_objects |
| 540 | |
| 541 | will continue to work with the old version of the objects imported |
| 542 | thus. |
| 543 | |
| 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, |
| 551 | variables, exceptions and even new types in C. This is all explained |
| 552 | in the file "python/misc/EXTENDING". Also read the file "DYNLOAD" |
| 553 | there for hints on how to load such extension modules |
| 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 | 95f61a7 | 1994-01-26 17:23:37 +0000 | [diff] [blame] | 597 | 7.1. Q. Where's the DOS version of 1.0.0? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 598 | |
Guido van Rossum | 95f61a7 | 1994-01-26 17:23:37 +0000 | [diff] [blame] | 599 | A. I hope it will be coming soon. Our institute has finally acquired |
| 600 | a 486 PC with a decent C compiler, now all I have to do is to learn |
| 601 | how to use it :-) Until then, you will have to make do with the 0.9.8 |
| 602 | version (which isn't so bad, actually). |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 603 | |
| 604 | 7.2. Q. Is there a Windows version of Python? |
| 605 | |
| 606 | A. Not yet. Several Windows hackers with C compilers are working on a |
| 607 | port though, so maybe we'll have one soon. |
| 608 | |
| 609 | 7.3. Q. I have the Mac or DOS version but it appears to be only a binary. |
| 610 | Where's the library? |
| 611 | |
| 612 | A. You still need to copy the files from the distribution directory |
| 613 | "python/lib" to your system. If you don't have the full distribution, |
Guido van Rossum | 95f61a7 | 1994-01-26 17:23:37 +0000 | [diff] [blame] | 614 | you can ftp the file pythonlib1.0.0.tar.Z from site ftp.cwi.nl, |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 615 | directory /pub/python; this is a subset of the distribution containing |
| 616 | just those file. |
| 617 | |
| 618 | 7.4. Q. Where's the documentation for the Mac or DOS version? |
| 619 | |
| 620 | A. There isn't any. The documentation for the Unix version also |
| 621 | applies to the Mac and DOS versions. Where applicable, differences |
| 622 | are indicated in the text. |
| 623 | |
| 624 | 7.5. Q. The Mac version doesn't seem to have any facilities for creating or |
| 625 | editing programs apart from entering it interactively, and there seems |
| 626 | to be no way to save code that was entered interactively. How do I |
| 627 | create a Python program on the Mac? |
| 628 | |
| 629 | A. Use an external editor. I am quite happy with the Desk Accessory |
| 630 | called Sigma Edit; this doesn't require Multifinder or System 7. I |
| 631 | work like this: start the interpreter; edit a module file using Sigma |
| 632 | Edit; import and test it in the interpreter; edit again in Sigma Edit; |
| 633 | then use the built-in function reload() to re-read the imported |
| 634 | module; etc. |