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