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 | ea2c6f7 | 1994-07-14 12:35:14 +0000 | [diff] [blame] | 9 | Version: 1.10 |
| 10 | Last-modified: 14 July 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 |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 30 | ftp.cwi.nl [192.16.191.128] in the directory /pub/python, with |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 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 |
Guido van Rossum | ea2c6f7 | 1994-07-14 12:35:14 +0000 | [diff] [blame] | 35 | rtfm.mit.edu [18.181.0.24] in the directory pub/usenet/news.answers. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 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? |
Guido van Rossum | c50158e | 1994-05-31 09:18:50 +0000 | [diff] [blame] | 65 | 1.5. Q. Are there other ftp sites that mirror the Python distribution? |
| 66 | 1.6. Q. Is there a newsgroup or mailing list devoted to Python? |
| 67 | 1.7. Q. Is there a book on Python, or will there be one out soon? |
| 68 | 1.8. Q. Are there any published articles about Python that I can quote? |
| 69 | 1.9. Q. How does the Python version numbering scheme work? |
| 70 | 1.10. Q. Are there other ftp sites that carry Python related material? |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 71 | 1.11. Q. Are there copyright restrictions on the use of Python? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 72 | |
| 73 | 2. Python in the real world |
| 74 | 2.1. Q. How many people are using Python? |
| 75 | 2.2. Q. Have any significant projects been done in Python? |
| 76 | 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] | 77 | 2.4. Q. How stable is Python? |
| 78 | 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] | 79 | |
| 80 | 3. Building Python |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 81 | 3.1. Q. Is there a test set? |
| 82 | 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] | 83 | operations, but when playing with floating point operations I cannot |
| 84 | find anything wrong with them. |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 85 | 3.3. Q. Link errors building Python with STDWIN on SGI IRIX. |
| 86 | 3.4. Q. Link errors after rerunning the configure script. |
| 87 | 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] | 88 | script (after the script name). |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 89 | 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] | 90 | glmodule.c, but python hasn't been built or installed yet. |
Guido van Rossum | 44b4cb1 | 1994-05-04 13:28:51 +0000 | [diff] [blame] | 91 | 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] | 92 | |
| 93 | 4. Programming in Python |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 94 | 4.1. Q. Is there a source code level debugger with breakpoints, step, |
| 95 | etc.? |
| 96 | 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] | 97 | C and others in Python (e.g. through inheritance)? (Also phrased as: |
| 98 | Can I use a built-in type as base class?) |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 99 | 4.3. Q. Is there a curses/termcap package for Python? |
| 100 | 4.4. Q. Is there an equivalent to C's onexit() in Python? |
| 101 | 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] | 102 | nested function seemingly can't access the local variables of the |
| 103 | outer function. What is going on? How do I pass local data to a |
| 104 | nested function? |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 105 | 4.6. Q. How do I iterate over a sequence in reverse order? |
| 106 | 4.7. Q. My program is too slow. How do I speed it up? |
| 107 | 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] | 108 | again (into the same Python process), the changes don't seem to take |
| 109 | place. What is going on? |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 110 | 4.9. Q. I have a module in which I want to execute some extra code when it |
| 111 | is run as a script. How do I find out whether I am running as a |
| 112 | script? |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 113 | 4.10. Q. I try to run a program from the Demo directory but it fails with |
| 114 | ImportError: No module named ...; what gives? |
| 115 | 4.11. Q. What GUI toolkits exist for Python? |
| 116 | 4.12. Q. Are there any interfaces to commercial database in Python? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 117 | |
| 118 | 5. Extending Python |
| 119 | 5.1. Q. Can I create my own functions in C? |
| 120 | 5.2. Q. Can I create my own functions in C++? |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 121 | 5.3. Q. How can I execute arbitrary Python statements from C? |
| 122 | 5.4. Q. How can I evaluate an arbitrary Python expression from C? |
| 123 | 5.5. Q. How do I extract C values from a Python object? |
| 124 | 5.6. Q. How do I use mkvalue() to create a tuple of arbitrary length? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 125 | |
| 126 | 6. Python's design |
| 127 | 6.1. Q. Why isn't there a generic copying operation for objects in |
| 128 | Python? |
| 129 | 6.2. Q. Why isn't there a generic way to implement persistent objects |
| 130 | in Python? (Persistent == automatically saved to and restored from |
| 131 | disk.) |
| 132 | 6.3. Q. Why isn't there a switch or case statement in Python? |
Guido van Rossum | c50158e | 1994-05-31 09:18:50 +0000 | [diff] [blame] | 133 | 6.4. Q. Why does Python use indentation for grouping of statements? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 134 | |
| 135 | 7. Using Python on non-UNIX platforms |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 136 | 7.1. Q. Is there a Mac version of Python? |
| 137 | 7.2. Q. Is there a DOS version of Python? |
| 138 | 7.3. Q. Is there a Windows version of Python? |
| 139 | 7.4. Q. Is there a Windows NT version of Python? |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 140 | 7.5. Q. Is there an OS/2 version of Python? |
| 141 | 7.6. Q. I have the PC version but it appears to be only a binary. |
| 142 | Where's the library? |
| 143 | 7.7. Q. Where's the documentation for the Mac or PC version? |
| 144 | 7.8. Q. The Mac (PC) version doesn't seem to have any facilities for |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 145 | creating or editing programs apart from entering it interactively, and |
| 146 | there seems to be no way to save code that was entered interactively. |
| 147 | How do I create a Python program on the Mac (PC)? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 148 | |
| 149 | To find a particular question, search for the question number followed |
| 150 | by a dot, a space, and a Q at the beginning of a line (e.g. to find |
| 151 | question 4.2 in vi, type /^4\.2\. Q/). |
| 152 | |
| 153 | |
| 154 | 1. General information and availability |
| 155 | ======================================= |
| 156 | |
| 157 | 1.1. Q. What is Python? |
| 158 | |
| 159 | A. Python is an interpreted, interactive, object-oriented programming |
| 160 | language. It incorporates modules, exceptions, dynamic typing, very |
| 161 | high level dynamic data types, and classes. Python combines |
| 162 | remarkable power with very clear syntax. It has interfaces to many |
| 163 | system calls and libraries, as well as to various window systems, and |
| 164 | is extensible in C or C++. It is also usable as an extension language |
| 165 | for applications that need a programmable interface. Finally, Python |
| 166 | is portable: it runs on many brands of UNIX, on the Mac, and on |
| 167 | MS-DOS. |
| 168 | |
| 169 | To find out more, the best thing to do is to start reading the |
| 170 | tutorial from the documentation set (see a few questions further |
| 171 | down). |
| 172 | |
| 173 | 1.2. Q. Why is it called Python? |
| 174 | |
| 175 | A. Apart from being a computer wizard, I'm also a fan of "Monty |
| 176 | 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] | 177 | the -- unlikely -- case you didn't know). It occurred to me one day |
| 178 | that I needed a name that was short, unique, and slightly mysterious. |
| 179 | And I happened to be reading some scripts from the series at the |
| 180 | time... So then I decided to call my language Python. But Python is |
| 181 | not a joke. And don't you associate it with dangerous reptiles |
| 182 | either! |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 183 | |
| 184 | 1.3. Q. How do I obtain a copy of the Python source? |
| 185 | |
| 186 | A. The latest Python source distribution is always available by |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 187 | anonymous ftp from ftp.cwi.nl [192.16.191.128] in the directory |
Guido van Rossum | 44b4cb1 | 1994-05-04 13:28:51 +0000 | [diff] [blame] | 188 | /pub/python, with filename python<version>.tar.gz. (Old versions may |
| 189 | have an extension of .Z, indicating use of "compress" compression.) |
| 190 | It is a gzip'ed tar file containing the complete C source, LaTeX |
| 191 | documentation, Python library modules, example programs, and several |
| 192 | useful pieces of freely distributable software. This will compile and |
| 193 | run out of the box on most UNIX platforms. At the time of writing, |
| 194 | <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] | 195 | |
| 196 | 1.4. Q. How do I get documentation on Python? |
| 197 | |
| 198 | A. The latest Python documentation set is always available by |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 199 | anonymous ftp from ftp.cwi.nl [192.16.191.128] in the directory |
Guido van Rossum | 44b4cb1 | 1994-05-04 13:28:51 +0000 | [diff] [blame] | 200 | /pub/python, with filename pythondoc-ps<version>.tar.gz. It is a |
| 201 | gzip'ed tar file containing PostScript files of the reference manual, |
| 202 | the library manual, and the tutorial. At the time of writing |
| 203 | <version> is 1.0.2. Note that the library manual is the most |
| 204 | important one of the set, as much of Python's power stems from the |
| 205 | standard or built-in types, functions and modules, all of which are |
| 206 | described here. PostScript for a high-level description of Python is |
| 207 | in the file nluug-paper.ps. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 208 | |
Guido van Rossum | c50158e | 1994-05-31 09:18:50 +0000 | [diff] [blame] | 209 | 1.5. Q. Are there other ftp sites that mirror the Python distribution? |
| 210 | |
| 211 | A. The following sites keep mirrors of the Python distribution: |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 212 | |
| 213 | Site IP address Directory |
| 214 | |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 215 | gatekeeper.dec.com 16.1.0.2 /pub/plan/python |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 216 | ftp.uu.net 192.48.96.9 /languages/python |
| 217 | ftp.wustl.edu 128.252.135.4 /graphics/graphics/sgi-stuff/python |
Guido van Rossum | c50158e | 1994-05-31 09:18:50 +0000 | [diff] [blame] | 218 | ftp.funet.fi 128.214.6.100 /pub/languages/python |
Guido van Rossum | 5333c5d | 1994-04-11 11:06:22 +0000 | [diff] [blame] | 219 | ftp.fu-berlin.de 130.133.4.50 /pub/unix/languages/python (*python* only) |
Guido van Rossum | c50158e | 1994-05-31 09:18:50 +0000 | [diff] [blame] | 220 | ftp.sunet.se 130.238.127.3 /pub/lang/python |
Guido van Rossum | ea2c6f7 | 1994-07-14 12:35:14 +0000 | [diff] [blame] | 221 | unix.hensa.ac.uk 129.12.43.16 /uunet/languages/python |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 222 | |
Guido van Rossum | 5333c5d | 1994-04-11 11:06:22 +0000 | [diff] [blame] | 223 | Or try archie on e.g. python1.0 to locate the nearest copy of that |
| 224 | version... |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 225 | |
Guido van Rossum | c50158e | 1994-05-31 09:18:50 +0000 | [diff] [blame] | 226 | 1.6. Q. Is there a newsgroup or mailing list devoted to Python? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 227 | |
Guido van Rossum | 5333c5d | 1994-04-11 11:06:22 +0000 | [diff] [blame] | 228 | A. There is a newsgroup, comp.lang.python, and a mailing list. The |
| 229 | newsgroup and mailing list are gatewayed into each other -- if you can |
| 230 | read news it is not necessary to subscribe to the mailing list. Send |
| 231 | e-mail to python-list-request@cwi.nl to (un)subscribe to the mailing |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 232 | list. Once you're on, send e-mail tp python-list@cwi.nl to send mail |
| 233 | to the entire mailing list and newsgroup. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 234 | |
Guido van Rossum | c50158e | 1994-05-31 09:18:50 +0000 | [diff] [blame] | 235 | 1.7. Q. Is there a book on Python, or will there be one out soon? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 236 | |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 237 | A. Unfortunately, not yet. Mark Lutz and I are planning to write one, |
| 238 | but we are still a in very preliminary stage. If you would like to |
| 239 | beat us at it and get rich from book royalties, go ahead! |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 240 | |
Guido van Rossum | c50158e | 1994-05-31 09:18:50 +0000 | [diff] [blame] | 241 | 1.8. Q. Are there any published articles about Python that I can quote? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 242 | |
| 243 | A. So far the only refereed and published article that describes |
| 244 | Python in some detail is: |
| 245 | |
| 246 | Guido van Rossum and Jelke de Boer, "Interactively Testing Remote |
| 247 | Servers Using the Python Programming Language", CWI Quarterly, Volume |
| 248 | 4, Issue 4 (December 1991), Amsterdam, pp 283-303. |
| 249 | |
| 250 | LaTeX source for this paper is available as part of the Python source |
| 251 | distribution. |
| 252 | |
| 253 | A more recent high-level description of Python is: |
| 254 | |
| 255 | Guido van Rossum, "An Introduction to Python for UNIX/C |
| 256 | Programmers", in the proceedings of the NLUUG najaarsconferentie |
| 257 | 1993 (dutch UNIX users group meeting november 1993). |
| 258 | |
| 259 | PostScript for this paper and for the slides used for the accompanying |
| 260 | presentation can be found in the ftp directory mentioned a few |
| 261 | questions earlier, with filenames nluug-paper.ps and nluug-slides.ps, |
| 262 | respectively. |
| 263 | |
Guido van Rossum | c50158e | 1994-05-31 09:18:50 +0000 | [diff] [blame] | 264 | 1.9. Q. How does the Python version numbering scheme work? |
Guido van Rossum | 95f61a7 | 1994-01-26 17:23:37 +0000 | [diff] [blame] | 265 | |
| 266 | A. Python versions are numbered A.B.C. A is the major version number |
| 267 | -- it is only incremented for major changes in functionality or source |
| 268 | structure. B is the minor version number, incremented for less |
| 269 | earth-shattering changes to a release. C is the patchlevel -- it is |
| 270 | incremented for each new release. Note that in the past, patches have |
| 271 | added significant changes; in fact the changeover from 0.9.9 to 1.0.0 |
| 272 | was the first time that either A or B changed! |
| 273 | |
Guido van Rossum | c50158e | 1994-05-31 09:18:50 +0000 | [diff] [blame] | 274 | 1.10. Q. Are there other ftp sites that carry Python related material? |
Guido van Rossum | 5333c5d | 1994-04-11 11:06:22 +0000 | [diff] [blame] | 275 | |
| 276 | A. An interesting ftp site for Python users is ftp.markv.com |
| 277 | (192.122.251.1); the directory pub/python contains a growing |
| 278 | collection of interesting Python scripts. To submit a script for |
| 279 | inclusion, place it together with a readme file (with extension |
| 280 | .readme) in the publicly writable directory /incoming/python. This |
| 281 | service is maintained by Lance Ellinghouse <lance@markv.com>. |
| 282 | |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 283 | 1.11. Q. Are there copyright restrictions on the use of Python? |
| 284 | |
| 285 | A. Hardly. You can do anything you want with the source, as long as |
| 286 | you leave the copyrights in, display those copyrights in any |
| 287 | documentation about Python that you produce, don't use the author's |
| 288 | institute's name in publicity without prior written permission, and |
| 289 | don't hold them responsible for anything (read the actual copyright |
| 290 | for a precise legal wording). |
Guido van Rossum | 5333c5d | 1994-04-11 11:06:22 +0000 | [diff] [blame] | 291 | |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 292 | |
| 293 | 2. Python in the real world |
| 294 | =========================== |
| 295 | |
| 296 | 2.1. Q. How many people are using Python? |
| 297 | |
Guido van Rossum | 5333c5d | 1994-04-11 11:06:22 +0000 | [diff] [blame] | 298 | A. I don't know, but the maximum number of simultaneous subscriptions |
| 299 | to the Python mailing list before it was gatewayed into the newsgroup |
| 300 | was about 180 (several of which were local redistribution lists). I |
| 301 | believe that many active Python users don't bother to subscribe to the |
| 302 | list, and now that there's a newsgroup the mailing list subscription |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 303 | is even less meaningful. I see new names on the newsgroup all the |
| 304 | time and my best guess is that there are currently at least several |
| 305 | thousands of users. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 306 | |
| 307 | 2.2. Q. Have any significant projects been done in Python? |
| 308 | |
| 309 | A. Here at CWI (the home of Python), we have written a 20,000 line |
| 310 | authoring environment for transportable hypermedia presentations, a |
Guido van Rossum | 5333c5d | 1994-04-11 11:06:22 +0000 | [diff] [blame] | 311 | 5,000 line multimedia teleconferencing tool, as well as many many |
| 312 | smaller programs. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 313 | |
| 314 | The University of Virginia uses Python to control a virtual reality |
| 315 | engine. Contact: Matt Conway <conway@virginia.edu>. |
| 316 | |
| 317 | See also the next question. |
| 318 | |
| 319 | 2.3. Q. Are there any commercial projects going on using Python? |
| 320 | |
| 321 | A. Several companies have revealed to me that they are planning or |
| 322 | considering to use Python in a future product. The furthest is |
| 323 | Sunrise Software, who already have a product out using Python -- they |
| 324 | use Python for a GUI management application and an SNMP network |
| 325 | manangement application. Contact: <info@sunrise.com>. |
| 326 | |
| 327 | Individuals at many other companies are using Python for |
| 328 | internal development (witness their contributions to the Python |
Guido van Rossum | 5333c5d | 1994-04-11 11:06:22 +0000 | [diff] [blame] | 329 | mailing list or newsgroup). |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 330 | |
| 331 | Python has also been elected as an extension language by MADE, a |
| 332 | consortium supported by the European Committee's ESPRIT program and |
| 333 | consisting of Bull, CWI and some other European companies. Contact: |
| 334 | Ivan Herman <ivan@cwi.nl>. |
| 335 | |
Guido van Rossum | 95f61a7 | 1994-01-26 17:23:37 +0000 | [diff] [blame] | 336 | 2.4. Q. How stable is Python? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 337 | |
Guido van Rossum | 44b4cb1 | 1994-05-04 13:28:51 +0000 | [diff] [blame] | 338 | 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] | 339 | 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] | 340 | releases (numbered 0.9.x) have been coming out roughly every 3 to 6 |
| 341 | months for the past four years. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 342 | |
Guido van Rossum | 95f61a7 | 1994-01-26 17:23:37 +0000 | [diff] [blame] | 343 | 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] | 344 | |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 345 | A. A proposal is being worked out to change the semantics of operator |
| 346 | overloading (__add__, __mul__ etc.) to make them more useful for |
| 347 | implementing types that don't resemble numbers. Additions will be |
| 348 | __call__ (to call an instance as if it were a function) and __eq__, |
| 349 | _lt__ etc. (to override individual comparisons). A (new) pthreads |
| 350 | interface has been submitted which will be integrated in the next |
| 351 | release. The X11/Motif interface will be improved. There are ideas |
| 352 | about built-in help using strings placed into function objects, and |
| 353 | possibly a switch statement. |
| 354 | |
| 355 | There will be better ports to the Mac, DOS, Windows, Windows NT, and |
| 356 | OS/2. The Windows ports will support dynalically loaded modules using |
| 357 | DLLs. The Mac port may also support dynamic loading of modules. |
| 358 | There may be a Windows version of STDWIN. |
| 359 | |
| 360 | Also planned is improved support for embedding Python in other |
| 361 | applications, e.g. by renaming most global symbols to have a "Py" |
| 362 | prefix and providing more documentation and threading support. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 363 | |
| 364 | |
| 365 | 3. Building Python |
| 366 | ================== |
| 367 | |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 368 | 3.1. Q. Is there a test set? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 369 | |
| 370 | A. Yes, simply do "import testall" (or "import autotest" if you aren't |
| 371 | interested in the output). The standard modules whose name begins |
| 372 | with "test" together comprise the test. The test set doesn't test |
| 373 | *all* features of Python but it goes a long way to confirm that a new |
| 374 | port is actually working. The Makefile contains an entry "make test" |
| 375 | which runs the autotest module. |
| 376 | |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 377 | 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] | 378 | operations, but when playing with floating point operations I cannot |
| 379 | find anything wrong with them. |
| 380 | |
| 381 | A. The test set makes occasional unwarranted assumptions about the |
| 382 | semantics of C floating point operations. Until someone donates a |
| 383 | better floating point test set, you will have to comment out the |
| 384 | offending floating point tests and execute similar tests manually. |
| 385 | |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 386 | 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] | 387 | |
| 388 | A. Rebuild STDWIN, specifying "CC=cc -cckr" in the Makefile. |
| 389 | |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 390 | 3.4. Q. Link errors after rerunning the configure script. |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 391 | |
| 392 | A. It is generally necessary to run "make clean" after a configuration |
| 393 | change. |
| 394 | |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 395 | 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] | 396 | script (after the script name). |
| 397 | |
| 398 | A. You are probably linking with GNU getopt, e.g. through -liberty. |
| 399 | Don't. (If you are using this because you link with -lreadline, use |
| 400 | the readline distributed with Python instead.) |
| 401 | |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 402 | 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] | 403 | glmodule.c, but python hasn't been built or installed yet. |
| 404 | |
| 405 | A. Comment out the line mentioning glmodule.c in Setup and build a |
| 406 | python without gl first; install it or make sure it is in your $PATH, |
| 407 | then edit the Setup file again to turn on the gl module, and make |
| 408 | again. You don't need to do "make clean"; you do need to run "make |
| 409 | Makefile" in the Modules subdirectory (or just run "make" at the |
| 410 | toplevel). |
| 411 | |
Guido van Rossum | 44b4cb1 | 1994-05-04 13:28:51 +0000 | [diff] [blame] | 412 | 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] | 413 | |
| 414 | 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] | 415 | |
| 416 | |
| 417 | 4. Programming in Python |
| 418 | ======================== |
| 419 | |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 420 | 4.1. Q. Is there a source code level debugger with breakpoints, step, |
| 421 | etc.? |
| 422 | |
| 423 | A. Yes. Check out module pdb; pdb.help() prints the documentation (or |
| 424 | you can read it as Lib/pdb.doc). If you use the STDWIN option, |
| 425 | there's also a windowing interface, wdb. You can write your own |
| 426 | debugger by using the code for pdb or wdb as an example. |
| 427 | |
| 428 | 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] | 429 | C and others in Python (e.g. through inheritance)? (Also phrased as: |
| 430 | Can I use a built-in type as base class?) |
| 431 | |
| 432 | A. No, but you can easily create a Python class which serves as a |
| 433 | wrapper around a built-in object, e.g. (for dictionaries): |
| 434 | |
| 435 | # A user-defined class behaving almost identical |
| 436 | # to a built-in dictionary. |
| 437 | class UserDict: |
| 438 | def __init__(self): self.data = {} |
| 439 | def __repr__(self): return repr(self.data) |
| 440 | def __cmp__(self, dict): |
| 441 | if type(dict) == type(self.data): |
| 442 | return cmp(self.data, dict) |
| 443 | else: |
| 444 | return cmp(self.data, dict.data) |
| 445 | def __len__(self): return len(self.data) |
| 446 | def __getitem__(self, key): return self.data[key] |
| 447 | def __setitem__(self, key, item): self.data[key] = item |
| 448 | def __delitem__(self, key): del self.data[key] |
| 449 | def keys(self): return self.data.keys() |
| 450 | def items(self): return self.data.items() |
| 451 | def values(self): return self.data.values() |
| 452 | def has_key(self, key): return self.data.has_key(key) |
| 453 | |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 454 | 4.3. Q. Is there a curses/termcap package for Python? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 455 | |
| 456 | A. No, but you can use the "alfa" (== character cell) version of |
| 457 | STDWIN. (STDWIN == Standard Windows, a portable windowing system |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 458 | 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] | 459 | This will also prepare your program for porting to windowing |
| 460 | environments such as X11 or the Macintosh. |
| 461 | |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 462 | 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] | 463 | |
| 464 | A. Yes, if you import sys and assign a function to sys.exitfunc, it |
| 465 | will be called when your program exits, is killed by an unhandled |
| 466 | exception, or (on UNIX) receives a SIGHUP or SIGTERM signal. |
| 467 | |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 468 | 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] | 469 | nested function seemingly can't access the local variables of the |
| 470 | outer function. What is going on? How do I pass local data to a |
| 471 | nested function? |
| 472 | |
| 473 | A. Python does not have arbitrarily nested scopes. When you need to |
| 474 | create a function that needs to access some data which you have |
| 475 | available locally, create a new class to hold the data and return a |
| 476 | method of an instance of that class, e.g.: |
| 477 | |
| 478 | class MultiplierClass: |
| 479 | def __init__(self, factor): |
| 480 | self.factor = factor |
| 481 | def multiplier(self, argument): |
| 482 | return argument * self.factor |
| 483 | |
| 484 | def generate_multiplier(factor): |
| 485 | return MultiplierClass(factor).multiplier |
| 486 | |
| 487 | twice = generate_multiplier(2) |
| 488 | print twice(10) |
| 489 | # Output: 20 |
| 490 | |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 491 | 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] | 492 | |
| 493 | A. If it is a list, the fastest solution is |
| 494 | |
| 495 | list.reverse() |
| 496 | try: |
| 497 | for x in list: |
| 498 | "do something with x" |
| 499 | finally: |
| 500 | list.reverse() |
| 501 | |
| 502 | This has the disadvantage that while you are in the loop, the list |
| 503 | is temporarily reversed. If you don't like this, you can make a copy. |
| 504 | This appears expensive but is actually faster than other solutions: |
| 505 | |
| 506 | rev = list[:] |
| 507 | rev.reverse() |
| 508 | for x in rev: |
| 509 | <do something with x> |
| 510 | |
| 511 | If it isn't a list, a more general but slower solution is: |
| 512 | |
| 513 | i = len(list) |
| 514 | while i > 0: |
| 515 | i = i-1 |
| 516 | x = list[i] |
| 517 | <do something with x> |
| 518 | |
| 519 | A more elegant solution, is to define a class which acts as a sequence |
| 520 | and yields the elements in reverse order (solution due to Steve |
| 521 | Majewski): |
| 522 | |
| 523 | class Rev: |
| 524 | def __init__(self, seq): |
| 525 | self.forw = seq |
| 526 | def __len__(self): |
| 527 | return len(self.forw) |
| 528 | def __getitem__(self, i): |
| 529 | return self.forw[-(i + 1)] |
| 530 | |
| 531 | You can now simply write: |
| 532 | |
| 533 | for x in Rev(list): |
| 534 | <do something with x> |
| 535 | |
| 536 | Unfortunately, this solution is slowest of all, due the the method |
| 537 | call overhead... |
| 538 | |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 539 | 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] | 540 | |
| 541 | A. That's a tough one, in general. There are many tricks to speed up |
| 542 | Python code; I would consider rewriting parts in C only as a last |
| 543 | resort. One thing to notice is that function and (especially) method |
| 544 | calls are rather expensive; if you have designed a purely OO interface |
| 545 | with lots of tiny functions that don't do much more than get or set an |
| 546 | instance variable or call another method, you may consider using a |
| 547 | more direct way, e.g. directly accessing instance variables. Also see |
| 548 | the standard module "profile" (described in the file |
| 549 | "python/lib/profile.doc") which makes it possible to find out where |
| 550 | your program is spending most of its time (if you have some patience |
| 551 | -- the profiling itself can slow your program down by an order of |
| 552 | magnitude). |
| 553 | |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 554 | 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] | 555 | again (into the same Python process), the changes don't seem to take |
| 556 | place. What is going on? |
| 557 | |
| 558 | A. For efficiency reasons, Python only reads the module file on the |
| 559 | first time a module is imported (otherwise a program consisting of |
| 560 | many modules, each of which imports the same basic module, would read |
| 561 | the basic module over and over again). To force a changed module |
| 562 | being read again, do this: |
| 563 | |
| 564 | import modname |
| 565 | reload(modname) |
| 566 | |
| 567 | Warning: this technique is not 100% fool-proof. In particular, |
| 568 | modules containing statements like |
| 569 | |
| 570 | from modname import some_objects |
| 571 | |
| 572 | will continue to work with the old version of the objects imported |
| 573 | thus. |
| 574 | |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 575 | 4.9. Q. I have a module in which I want to execute some extra code when it |
| 576 | is run as a script. How do I find out whether I am running as a |
| 577 | script? |
| 578 | |
| 579 | A. A module can find out its own module name by alooking at the |
| 580 | (predefined) global variable __name__. If this has the value |
| 581 | '__main__' you are running as a script. E.g. if you put the following |
| 582 | on the last line of your module, main() is called only when your |
| 583 | module is running as a script: |
| 584 | |
| 585 | if __name__ == '__main__': main() |
| 586 | |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 587 | 4.10. Q. I try to run a program from the Demo directory but it fails with |
| 588 | ImportError: No module named ...; what gives? |
| 589 | |
| 590 | A. This is probably an optional module which hasn't been configured on |
| 591 | your system. This especially happens with modules like "stdwin", |
| 592 | "gl", "Xt" or "Xm". For stdwin and many other modules, see |
| 593 | Modules/Setup.in for info on how to add these modules to your Python, |
| 594 | if it is possible at all. Sometimes you will have to ftp and build |
| 595 | another package first (e.g. stdwin). Sometimes the module only works |
| 596 | on specific platforms (e.g. gl only works on SGI machines). For |
| 597 | X-related modules (Xt and Xm) you will have to do more work: they are |
| 598 | currently not part of the standard Python distribution. You will have |
| 599 | to ftp the file "extensions.tar.gz" file from a Python ftp repository |
| 600 | (e.g. ftp.cwi.nl) and follow the instructions there. Note: the X |
| 601 | related modules are still somewhat flakey, so don't try this unless |
| 602 | you know a bit or two about building X applications on your platform. |
| 603 | |
| 604 | 4.11. Q. What GUI toolkits exist for Python? |
| 605 | |
| 606 | A. Depending on what platform(s) you are aiming at, there are several. |
| 607 | |
| 608 | - The standard Python distribution comes with an interface to STDWIN, |
| 609 | a platform-independent low-level windowing interface (you have to ftp |
| 610 | the source for stdwin separately, e.g. from ftp.cwi.nl in pub/stdwin |
| 611 | or gatekeeper.dec.com in pub/misc/stdwin). STDWIN runs under X11 or |
| 612 | the Mac; a Windows port is about to be released. |
| 613 | |
| 614 | - For SGI only, there's an interface to the complete GL (Graphics |
| 615 | Library -- low level but very good 3D capabilities) as well as to |
| 616 | FORMS (a buttons-and-sliders-etc package built on top of GL by Mark |
| 617 | Overmars -- ftp'able from ftp.cs.ruu.nl in pub/SGI/FORMS). |
| 618 | |
| 619 | - There's an interface to X11, including the Athena and Motif widget |
| 620 | sets (and a few individual widgets, like Mosaic's HTML widget and |
| 621 | SGI's GL widget) in the Extensions set, which is separately ftp'able |
| 622 | from ftp.cwi.nl as pub/python/extensions.tar.gz. |
| 623 | |
| 624 | - There's an interface to SUIT, the U of Virginia's Simple User |
| 625 | Interface Toolkit; it can be ftp'ed from uvacs.cs.virginia.edu as |
| 626 | pub/suit/python/SUIT_python.tar.Z. A PC binary of Python 1.0.2 |
| 627 | compiled with DJGPP and with SUIT support built-in has been made |
| 628 | available by Antonio Costa on ftp site asterix.inescn.pt, directory |
| 629 | pub/PC/python, file pyt102su.exe (a self-extracting archive). |
| 630 | |
| 631 | - There's a neat interface to the Tcl/Tk widget set; ftp it from |
| 632 | ftp.cwi.nl as pub/python/tkinter.tar.gz. |
| 633 | |
| 634 | - There's an interface to WAFE, a Tcl interface to the X11 Motif and |
| 635 | Athena widget sets. Last I heard about it it was included in the |
| 636 | WAFE 1.0 prerelease, ftp'able from ftp.wu-wien.ac.at as |
| 637 | pub/src/X11/wafe/wafe-1.0.tar.gz-prerelease. |
| 638 | |
| 639 | 4.12. Q. Are there any interfaces to commercial database in Python? |
| 640 | |
| 641 | A. There's an interface to SYBASE by John Redford |
| 642 | <jredford@lehman.com>. |
| 643 | |
| 644 | There's also an interface to metalbase by Lance Ellinghouse |
| 645 | <lance@markv.com>. |
| 646 | |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 647 | |
| 648 | 5. Extending Python |
| 649 | =================== |
| 650 | |
| 651 | 5.1. Q. Can I create my own functions in C? |
| 652 | |
| 653 | A. Yes, you can create built-in modules containing functions, |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 654 | variables, exceptions and even new types in C. This is explained in |
| 655 | the document "Extending and Embedding the Python Interpreter" (the |
| 656 | 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] | 657 | |
| 658 | 5.2. Q. Can I create my own functions in C++? |
| 659 | |
| 660 | A. Yes, using the C-compatibility features found in C++. Basically |
| 661 | you place extern "C" { ... } around the Python include files and put |
| 662 | extern "C" before each function that is going to be called by the |
| 663 | Python interpreter. Global or static C++ objects with constructors |
| 664 | are probably not a good idea. |
| 665 | |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 666 | 5.3. Q. How can I execute arbitrary Python statements from C? |
| 667 | |
| 668 | A. The highest-level function to do this is run_command() which takes |
| 669 | a single string argument which is executed in the context of module |
| 670 | __main__ and returns 0 for success and -1 when an exception occurred |
| 671 | (including SyntaxError). If you want more control, use run_string(); |
| 672 | see the source for run_command() in Python/pythonrun.c. |
| 673 | |
| 674 | 5.4. Q. How can I evaluate an arbitrary Python expression from C? |
| 675 | |
| 676 | A. Call the function run_string() from the previous question with the |
| 677 | start symbol eval_input; it then parses an expression, evaluates it |
| 678 | and returns its value. See exec_eval() in Python/bltinmodule.c. |
| 679 | |
| 680 | 5.5. Q. How do I extract C values from a Python object? |
| 681 | |
| 682 | A. That depends on the object's type. If it's a tuple, |
| 683 | gettuplesize(o) returns its length and gettupleitem(o, i) returns its |
| 684 | i'th item; similar for lists with getlistsize(o) and getlistitem(o, |
| 685 | i). For strings, getstringsize(o) returns its length and |
| 686 | getstringvalue(o) a pointer to its value (note that Python strings may |
| 687 | contain null bytes so strlen() is not safe). To test which type an |
| 688 | object is, first make sure it isn't NULL, and then use |
| 689 | is_stringobject(o), is_tupleobject(o), is_listobject(o) etc. |
| 690 | |
| 691 | 5.6. Q. How do I use mkvalue() to create a tuple of arbitrary length? |
| 692 | |
| 693 | A. You can't. Use t = newtupleobject(n) instead, and fill it with |
| 694 | objects using settupleitem(t, i, o) -- note that this "eats" a |
| 695 | reference count of o. Similar for lists with newlistobject(n) and |
| 696 | setlistitem(l, i, o). |
| 697 | |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 698 | |
| 699 | 6. Python's design |
| 700 | ================== |
| 701 | |
| 702 | 6.1. Q. Why isn't there a generic copying operation for objects in |
| 703 | Python? |
| 704 | |
| 705 | A. Hmm. Maybe there should be one, but it's difficult to assign a |
| 706 | useful meaning to copying of open files, sockets and windows, or |
| 707 | recursive data structures. As long as you design all your classes |
| 708 | yourself you are of course free to define a standard base class that |
| 709 | defines an overridable copying operation for all the objects you care |
| 710 | about. (One practical point: it would have to be a built-in function, |
| 711 | not a standard method name, since not all built-in object types have |
| 712 | methods; e.g. strings, integers and tuples don't.) |
| 713 | |
| 714 | 6.2. Q. Why isn't there a generic way to implement persistent objects |
| 715 | in Python? (Persistent == automatically saved to and restored from |
| 716 | disk.) |
| 717 | |
| 718 | A. Hmm, hmm. Basically for the same reasons as why there is no |
| 719 | generic copying operation. |
| 720 | |
| 721 | 6.3. Q. Why isn't there a switch or case statement in Python? |
| 722 | |
| 723 | A. You can do this easily enough with a sequence of |
| 724 | if... elif... elif... else. There have been some proposals for switch |
| 725 | statement syntax, but there is no concensus (yet) on whether and how |
| 726 | to do range tests. |
| 727 | |
Guido van Rossum | c50158e | 1994-05-31 09:18:50 +0000 | [diff] [blame] | 728 | 6.4. Q. Why does Python use indentation for grouping of statements? |
| 729 | |
| 730 | A. Basically I believe that using indentation for grouping is |
| 731 | extremely elegant and contributes a lot to the clarity of the average |
| 732 | Python program. Most people learn to love this feature after a while. |
| 733 | Some arguments for it: |
| 734 | |
| 735 | - Since there are no begin/end brackets there cannot be a disagreement |
| 736 | between grouping perceived by the parser and the human reader. I |
| 737 | remember long ago seeing a C fragment like this: |
| 738 | |
| 739 | if (x <= y) |
| 740 | x++; |
| 741 | y--; |
| 742 | z++; |
| 743 | |
| 744 | and staring a long time at it wondering why y was being decremented |
| 745 | even for x > y... (And I wasn't a C newbie then either.) |
| 746 | |
| 747 | - Since there are no begin/end brackets there can be no conflicting |
| 748 | coding styles. In C there are loads of different ways to place the |
| 749 | braces (including the choice whether to place braces around single |
| 750 | statements in certain cases, for consistency). If you're used to |
| 751 | reading (and writing) code that uses one style, you will feel at least |
| 752 | slightly uneasy when reading (or being required to write) another |
| 753 | style. |
| 754 | |
| 755 | - Many coding styles place begin/end brackets on a line by themself. |
| 756 | This makes programs considerably longer and wastes valuable screen |
| 757 | space, making it harder to get a good overview over a program. |
| 758 | Ideally, a function should fit on one basic tty screen (say, 20 |
| 759 | lines). 20 lines of Python are worth a LOT more than 20 lines of C. |
| 760 | This is not solely due to the lack of begin/end brackets (the lack of |
| 761 | declarations also helps, and the powerful operations of course), but |
| 762 | it certainly helps! |
| 763 | |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 764 | |
| 765 | 7. Using Python on non-UNIX platforms |
| 766 | ===================================== |
| 767 | |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 768 | 7.1. Q. Is there a Mac version of Python? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 769 | |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 770 | A. Yes. It is on most ftp sites carrying Python as python.sea.hqx -- |
| 771 | this is a self-extracting archive containing the application binary as |
| 772 | well as the Lib modules. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 773 | |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 774 | 7.2. Q. Is there a DOS version of Python? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 775 | |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 776 | A. Yes. More than one, actually: 16python.exe runs in standard DOS |
| 777 | mode on 186 CPUs or higher; 32python.exe uses a DOS extender and only |
| 778 | runs on a 386 or higher CPUs. Although 16python.exe does not pass the |
| 779 | test set because test_grammar is too big for the parser, it actually |
| 780 | has about 270 kbyte of allocatable heap space, which is sufficient for |
| 781 | fairly large programs. 32python.exe is distributed as a tar file |
| 782 | containing the required DOS extended and 387 emulator. Both are on |
| 783 | most ftp sites carrying Python. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 784 | |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 785 | 7.3. Q. Is there a Windows version of Python? |
| 786 | |
| 787 | A. Yes. Use qwpython.exe. The only problem with it: ^C |
| 788 | unconditionally kills the entire program -- it does not raise |
| 789 | KeyboardInterrupt. You can also run 16python.exe or 32python.exe in a |
| 790 | "DOS box", but qwpython.exe appears to be slightly faster. |
| 791 | |
| 792 | 7.4. Q. Is there a Windows NT version of Python? |
| 793 | |
| 794 | A. Yes. Use ntpython.exe. This is for Intel CPUs. If you want a |
| 795 | Windows user interface, use qwpython.exe. |
| 796 | |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 797 | 7.5. Q. Is there an OS/2 version of Python? |
| 798 | |
| 799 | A. Yes. You can ftp it (from ftp.cwi.nl in pub/python, or from the |
| 800 | mirror sites) as pyth_os2.zip. This contains both an executable and |
| 801 | Makefiles for those fortunate enough to have a C compiler. |
| 802 | |
| 803 | 7.6. Q. I have the PC version but it appears to be only a binary. |
| 804 | Where's the library? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 805 | |
| 806 | 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] | 807 | "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] | 808 | 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] | 809 | Python; this is a subset of the distribution containing just those |
| 810 | file. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 811 | |
Guido van Rossum | 5333c5d | 1994-04-11 11:06:22 +0000 | [diff] [blame] | 812 | Once you have installed the library, you need to point sys.path to it. |
| 813 | Assuming the library is in C:\misc\python\lib, the following commands |
| 814 | will point your Python interpreter to it (note the doubled backslashes |
| 815 | -- you can also use single forward slashes instead): |
| 816 | |
| 817 | >>> import sys |
| 818 | >>> sys.path.insert(0, 'C:\\misc\\python\\lib') |
| 819 | >>> |
| 820 | |
| 821 | For a more permanent effect, set the environment variable PYTHONPATH, |
| 822 | as follows (talking to a DOS prompt): |
| 823 | |
| 824 | C> SET PYTHONPATH=C:\misc\python\lib |
| 825 | |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 826 | 7.7. Q. Where's the documentation for the Mac or PC version? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 827 | |
| 828 | 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] | 829 | applies to the Mac and PC versions. Where applicable, differences |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 830 | are indicated in the text. |
| 831 | |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 832 | 7.8. Q. The Mac (PC) version doesn't seem to have any facilities for |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 833 | creating or editing programs apart from entering it interactively, and |
| 834 | there seems to be no way to save code that was entered interactively. |
| 835 | How do I create a Python program on the Mac (PC)? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 836 | |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 837 | A. Use an external editor. On the Mac, I am quite happy with the Desk |
| 838 | Accessory called Sigma Edit; this doesn't require Multifinder or |
| 839 | System 7. I work like this: start the interpreter; edit a module file |
| 840 | using Sigma Edit; import and test it in the interpreter; edit again in |
| 841 | Sigma Edit; then use the built-in function reload() to re-read the |
| 842 | imported module; etc. |
Guido van Rossum | 5333c5d | 1994-04-11 11:06:22 +0000 | [diff] [blame] | 843 | |
| 844 | Regarding the same question for the PC, Kurt Wm. Hemr writes: "While |
| 845 | anyone with a pulse could certainly figure out how to do the same on |
| 846 | MS-Windows, I would recommend the NotGNU Emacs clone for MS-Windows. |
| 847 | Not only can you easily resave and "reload()" from Python after making |
| 848 | changes, but since WinNot auto-copies to the clipboard any text you |
| 849 | select, you can simply select the entire procedure (function) which |
| 850 | you changed in WinNot, switch to QWPython, and shift-ins to reenter |
| 851 | the changed program unit." |