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