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 | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame^] | 9 | Version: 1.14 |
| 10 | Last-modified: 23 September 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 | |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame^] | 44 | Skip Montanaro <skip@automatrix.com> maintains an HTML version of this |
| 45 | FAQ. The URL is "http://www.automatrix.com/~skip/python-faq.html". |
| 46 | |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 47 | This FAQ is divided in the following chapters: |
| 48 | |
| 49 | 1. General information and availability |
| 50 | 2. Python in the real world |
| 51 | 3. Building Python |
| 52 | 4. Programming in Python |
| 53 | 5. Extending Python |
| 54 | 6. Python's design |
| 55 | 7. Using Python on non-UNIX platforms |
| 56 | |
| 57 | To find the start of a particular chapter, search for the chapter number |
| 58 | followed by a dot and a space at the beginning of a line (e.g. to |
| 59 | find chapter 4 in vi, type /^4\. /). |
| 60 | |
| 61 | Here's an overview of the questions per chapter: |
| 62 | |
| 63 | 1. General information and availability |
| 64 | 1.1. Q. What is Python? |
| 65 | 1.2. Q. Why is it called Python? |
| 66 | 1.3. Q. How do I obtain a copy of the Python source? |
| 67 | 1.4. Q. How do I get documentation on Python? |
Guido van Rossum | c50158e | 1994-05-31 09:18:50 +0000 | [diff] [blame] | 68 | 1.5. Q. Are there other ftp sites that mirror the Python distribution? |
| 69 | 1.6. Q. Is there a newsgroup or mailing list devoted to Python? |
| 70 | 1.7. Q. Is there a book on Python, or will there be one out soon? |
| 71 | 1.8. Q. Are there any published articles about Python that I can quote? |
| 72 | 1.9. Q. How does the Python version numbering scheme work? |
| 73 | 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] | 74 | 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] | 75 | |
| 76 | 2. Python in the real world |
| 77 | 2.1. Q. How many people are using Python? |
| 78 | 2.2. Q. Have any significant projects been done in Python? |
| 79 | 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] | 80 | 2.4. Q. How stable is Python? |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame^] | 81 | 2.5. Q. When will the next version be released? |
| 82 | 2.6. Q. What new developments are expected for Python in the future? |
| 83 | 2.7. Q. Is it reasonable to propose incompatible changes to Python? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 84 | |
| 85 | 3. Building Python |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 86 | 3.1. Q. Is there a test set? |
| 87 | 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] | 88 | operations, but when playing with floating point operations I cannot |
| 89 | find anything wrong with them. |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 90 | 3.3. Q. Link errors building Python with STDWIN on SGI IRIX. |
| 91 | 3.4. Q. Link errors after rerunning the configure script. |
| 92 | 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] | 93 | script (after the script name). |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 94 | 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] | 95 | glmodule.c, but python hasn't been built or installed yet. |
Guido van Rossum | 3de2736 | 1994-07-25 14:19:33 +0000 | [diff] [blame] | 96 | 3.7. Q. Python built with gcc for the DEC Alpha doesn't work. |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame^] | 97 | 3.8. Q. I use VPATH but some targets are built in the source directory. |
| 98 | 3.9. Q. Trouble building or linking with the GNU readline library. |
| 99 | 3.10. Q. Trouble building Python on Linux. |
| 100 | 3.11. Q. Other trouble building Python on platform X. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 101 | |
| 102 | 4. Programming in Python |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 103 | 4.1. Q. Is there a source code level debugger with breakpoints, step, |
| 104 | etc.? |
| 105 | 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] | 106 | C and others in Python (e.g. through inheritance)? (Also phrased as: |
| 107 | Can I use a built-in type as base class?) |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 108 | 4.3. Q. Is there a curses/termcap package for Python? |
| 109 | 4.4. Q. Is there an equivalent to C's onexit() in Python? |
| 110 | 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] | 111 | nested function seemingly can't access the local variables of the |
| 112 | outer function. What is going on? How do I pass local data to a |
| 113 | nested function? |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 114 | 4.6. Q. How do I iterate over a sequence in reverse order? |
| 115 | 4.7. Q. My program is too slow. How do I speed it up? |
| 116 | 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] | 117 | again (into the same Python process), the changes don't seem to take |
| 118 | place. What is going on? |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame^] | 119 | 4.9. Q. How do I find the current module name? |
| 120 | 4.10. Q. I have a module in which I want to execute some extra code when it |
| 121 | is run as a script. How do I find out whether I am running as a |
| 122 | script? |
| 123 | 4.11. Q. I try to run a program from the Demo directory but it fails with |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 124 | ImportError: No module named ...; what gives? |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame^] | 125 | 4.12. Q. I have successfully built Python with STDWIN but it can't find some |
| 126 | modules (e.g. stdwinevents). |
| 127 | 4.13. Q. What GUI toolkits exist for Python? |
| 128 | 4.14. Q. Are there any interfaces to commercial database in Python? |
| 129 | 4.15. Q. Is it possible to write obfuscated one-liners in Python? |
| 130 | 4.16. Q. Is there an equivalent of C's "?:" ternary operator? |
| 131 | 4.17. Q. My class defines __del__ but it is not called when I delete the |
| 132 | object. |
| 133 | 4.18. Q. How do I change the shell environment for programs called using |
| 134 | os.popen() or os.system()? Changing os.environ doesn't work. |
| 135 | 4.19. Q. What is a class? |
| 136 | 4.20. Q. What is a method? |
| 137 | 4.21. Q. What is self? |
| 138 | 4.22. Q. What is a unbound method? |
| 139 | 4.23. Q. How do I call a method defined in a base class from a derived class |
| 140 | that overrides it? |
| 141 | 4.24. Q. How do I call a method from a base class without using the name of |
| 142 | the base class? |
| 143 | 4.25. Q. How can I organize my code to make it easier to change the base |
| 144 | class? |
| 145 | 4.26. Q. How can I find the methods or attributes of an object? |
| 146 | 4.27. an't seem to use os.read() on a pipe created with os.popen(). |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 147 | |
| 148 | 5. Extending Python |
| 149 | 5.1. Q. Can I create my own functions in C? |
| 150 | 5.2. Q. Can I create my own functions in C++? |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 151 | 5.3. Q. How can I execute arbitrary Python statements from C? |
| 152 | 5.4. Q. How can I evaluate an arbitrary Python expression from C? |
| 153 | 5.5. Q. How do I extract C values from a Python object? |
| 154 | 5.6. Q. How do I use mkvalue() to create a tuple of arbitrary length? |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame^] | 155 | 5.7. Q. What happened to mktuple(), featuring in an example in the |
| 156 | Extensions manual? |
| 157 | 5.8. Q. How do I call an object's method from C? |
| 158 | 5.9. Q. How do I catch the output from print_error()? |
| 159 | 5.10. Q. How do I access a module written in Python from C? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 160 | |
| 161 | 6. Python's design |
| 162 | 6.1. Q. Why isn't there a generic copying operation for objects in |
| 163 | Python? |
| 164 | 6.2. Q. Why isn't there a generic way to implement persistent objects |
| 165 | in Python? (Persistent == automatically saved to and restored from |
| 166 | disk.) |
| 167 | 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] | 168 | 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] | 169 | 6.5. Q. Why are Python strings immutable? |
| 170 | 6.6. Q. Why don't strings have methods like index() or sort(), like |
| 171 | lists? |
| 172 | 6.7. Q. Why does Python use methods for some functionality |
| 173 | (e.g. list.index()) but functions for other (e.g. len(list))? |
| 174 | 6.8. Q. Why can't I derive a class from built-in types (e.g. lists or |
| 175 | files)? |
| 176 | 6.9. Q. Why must 'self' be declared and used explicitly in method |
| 177 | definitions and calls? |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame^] | 178 | 6.10. 't you emulate threads in the interpreter instead of relying on |
| 179 | an OS-specific thread implementation? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 180 | |
| 181 | 7. Using Python on non-UNIX platforms |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 182 | 7.1. Q. Is there a Mac version of Python? |
| 183 | 7.2. Q. Is there a DOS version of Python? |
| 184 | 7.3. Q. Is there a Windows version of Python? |
| 185 | 7.4. Q. Is there a Windows NT version of Python? |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 186 | 7.5. Q. Is there an OS/2 version of Python? |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame^] | 187 | 7.6. Q. Is there a VMS version of Python? |
| 188 | 7.7. Q. What about IBM mainframes, or other esoteric non-UNIX |
| 189 | platforms? |
| 190 | 7.8. Q. Where are the source or Makefiles for the non-UNIX versions? |
| 191 | 7.9. Q. What is the status and support for the non-UNIX versions? |
| 192 | 7.10. Q. I have the PC version but it appears to be only a binary. |
| 193 | Where's the library? |
| 194 | 7.11. Q. Where's the documentation for the Mac or PC version? |
| 195 | 7.12. Q. The Mac (PC) version doesn't seem to have any facilities for |
| 196 | creating or editing programs apart from entering it interactively, and |
| 197 | there seems to be no way to save code that was entered interactively. |
| 198 | How do I create a Python program on the Mac (PC)? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 199 | |
| 200 | To find a particular question, search for the question number followed |
| 201 | by a dot, a space, and a Q at the beginning of a line (e.g. to find |
| 202 | question 4.2 in vi, type /^4\.2\. Q/). |
| 203 | |
| 204 | |
| 205 | 1. General information and availability |
| 206 | ======================================= |
| 207 | |
| 208 | 1.1. Q. What is Python? |
| 209 | |
| 210 | A. Python is an interpreted, interactive, object-oriented programming |
| 211 | language. It incorporates modules, exceptions, dynamic typing, very |
| 212 | high level dynamic data types, and classes. Python combines |
| 213 | remarkable power with very clear syntax. It has interfaces to many |
| 214 | system calls and libraries, as well as to various window systems, and |
| 215 | is extensible in C or C++. It is also usable as an extension language |
| 216 | for applications that need a programmable interface. Finally, Python |
| 217 | is portable: it runs on many brands of UNIX, on the Mac, and on |
| 218 | MS-DOS. |
| 219 | |
| 220 | To find out more, the best thing to do is to start reading the |
| 221 | tutorial from the documentation set (see a few questions further |
| 222 | down). |
| 223 | |
| 224 | 1.2. Q. Why is it called Python? |
| 225 | |
| 226 | A. Apart from being a computer wizard, I'm also a fan of "Monty |
| 227 | 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] | 228 | the -- unlikely -- case you didn't know). It occurred to me one day |
| 229 | that I needed a name that was short, unique, and slightly mysterious. |
| 230 | And I happened to be reading some scripts from the series at the |
| 231 | time... So then I decided to call my language Python. But Python is |
| 232 | not a joke. And don't you associate it with dangerous reptiles |
| 233 | either! |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 234 | |
| 235 | 1.3. Q. How do I obtain a copy of the Python source? |
| 236 | |
| 237 | A. The latest Python source distribution is always available by |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 238 | 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] | 239 | /pub/python, with filename python<version>.tar.gz. (Old versions may |
| 240 | have an extension of .Z, indicating use of "compress" compression.) |
| 241 | It is a gzip'ed tar file containing the complete C source, LaTeX |
| 242 | documentation, Python library modules, example programs, and several |
| 243 | useful pieces of freely distributable software. This will compile and |
Guido van Rossum | 3de2736 | 1994-07-25 14:19:33 +0000 | [diff] [blame] | 244 | run out of the box on most UNIX platforms. (See section 7 for |
| 245 | non-UNIX information.) |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 246 | |
| 247 | 1.4. Q. How do I get documentation on Python? |
| 248 | |
| 249 | A. The latest Python documentation set is always available by |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 250 | 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] | 251 | /pub/python, with filename pythondoc-ps<version>.tar.gz. It is a |
| 252 | gzip'ed tar file containing PostScript files of the reference manual, |
Guido van Rossum | 3de2736 | 1994-07-25 14:19:33 +0000 | [diff] [blame] | 253 | the library manual, and the tutorial. Note that the library manual is |
| 254 | the most important one of the set, as much of Python's power stems |
| 255 | from the standard or built-in types, functions and modules, all of |
| 256 | which are described here. PostScript for a high-level description of |
| 257 | Python is in the file nluug-paper.ps. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 258 | |
Guido van Rossum | c50158e | 1994-05-31 09:18:50 +0000 | [diff] [blame] | 259 | 1.5. Q. Are there other ftp sites that mirror the Python distribution? |
| 260 | |
| 261 | A. The following sites keep mirrors of the Python distribution: |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 262 | |
| 263 | Site IP address Directory |
| 264 | |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 265 | gatekeeper.dec.com 16.1.0.2 /pub/plan/python |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 266 | ftp.uu.net 192.48.96.9 /languages/python |
| 267 | 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] | 268 | ftp.funet.fi 128.214.6.100 /pub/languages/python |
Guido van Rossum | f8c76d0 | 1994-08-17 12:19:53 +0000 | [diff] [blame] | 269 | 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] | 270 | ftp.sunet.se 130.238.127.3 /pub/lang/python |
Guido van Rossum | ea2c6f7 | 1994-07-14 12:35:14 +0000 | [diff] [blame] | 271 | unix.hensa.ac.uk 129.12.43.16 /uunet/languages/python |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 272 | |
Guido van Rossum | 5333c5d | 1994-04-11 11:06:22 +0000 | [diff] [blame] | 273 | Or try archie on e.g. python1.0 to locate the nearest copy of that |
| 274 | version... |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 275 | |
Guido van Rossum | c50158e | 1994-05-31 09:18:50 +0000 | [diff] [blame] | 276 | 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] | 277 | |
Guido van Rossum | 5333c5d | 1994-04-11 11:06:22 +0000 | [diff] [blame] | 278 | A. There is a newsgroup, comp.lang.python, and a mailing list. The |
| 279 | newsgroup and mailing list are gatewayed into each other -- if you can |
| 280 | read news it is not necessary to subscribe to the mailing list. Send |
| 281 | 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] | 282 | list. Once you're on, send e-mail tp python-list@cwi.nl to send mail |
| 283 | to the entire mailing list and newsgroup. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 284 | |
Guido van Rossum | c50158e | 1994-05-31 09:18:50 +0000 | [diff] [blame] | 285 | 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] | 286 | |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 287 | A. Unfortunately, not yet. Mark Lutz and I are planning to write one, |
| 288 | but we are still a in very preliminary stage. If you would like to |
| 289 | 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] | 290 | |
Guido van Rossum | c50158e | 1994-05-31 09:18:50 +0000 | [diff] [blame] | 291 | 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] | 292 | |
| 293 | A. So far the only refereed and published article that describes |
| 294 | Python in some detail is: |
| 295 | |
| 296 | Guido van Rossum and Jelke de Boer, "Interactively Testing Remote |
| 297 | Servers Using the Python Programming Language", CWI Quarterly, Volume |
| 298 | 4, Issue 4 (December 1991), Amsterdam, pp 283-303. |
| 299 | |
| 300 | LaTeX source for this paper is available as part of the Python source |
| 301 | distribution. |
| 302 | |
| 303 | A more recent high-level description of Python is: |
| 304 | |
| 305 | Guido van Rossum, "An Introduction to Python for UNIX/C |
| 306 | Programmers", in the proceedings of the NLUUG najaarsconferentie |
| 307 | 1993 (dutch UNIX users group meeting november 1993). |
| 308 | |
| 309 | PostScript for this paper and for the slides used for the accompanying |
| 310 | presentation can be found in the ftp directory mentioned a few |
| 311 | questions earlier, with filenames nluug-paper.ps and nluug-slides.ps, |
| 312 | respectively. |
| 313 | |
Guido van Rossum | c50158e | 1994-05-31 09:18:50 +0000 | [diff] [blame] | 314 | 1.9. Q. How does the Python version numbering scheme work? |
Guido van Rossum | 95f61a7 | 1994-01-26 17:23:37 +0000 | [diff] [blame] | 315 | |
| 316 | A. Python versions are numbered A.B.C. A is the major version number |
| 317 | -- it is only incremented for major changes in functionality or source |
| 318 | structure. B is the minor version number, incremented for less |
| 319 | earth-shattering changes to a release. C is the patchlevel -- it is |
| 320 | incremented for each new release. Note that in the past, patches have |
| 321 | added significant changes; in fact the changeover from 0.9.9 to 1.0.0 |
| 322 | was the first time that either A or B changed! |
| 323 | |
Guido van Rossum | c50158e | 1994-05-31 09:18:50 +0000 | [diff] [blame] | 324 | 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] | 325 | |
| 326 | A. An interesting ftp site for Python users is ftp.markv.com |
| 327 | (192.122.251.1); the directory pub/python contains a growing |
| 328 | collection of interesting Python scripts. To submit a script for |
| 329 | inclusion, place it together with a readme file (with extension |
| 330 | .readme) in the publicly writable directory /incoming/python. This |
| 331 | service is maintained by Lance Ellinghouse <lance@markv.com>. |
| 332 | |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 333 | 1.11. Q. Are there copyright restrictions on the use of Python? |
| 334 | |
| 335 | A. Hardly. You can do anything you want with the source, as long as |
| 336 | you leave the copyrights in, display those copyrights in any |
| 337 | documentation about Python that you produce, don't use the author's |
| 338 | institute's name in publicity without prior written permission, and |
| 339 | don't hold them responsible for anything (read the actual copyright |
| 340 | for a precise legal wording). |
Guido van Rossum | 5333c5d | 1994-04-11 11:06:22 +0000 | [diff] [blame] | 341 | |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 342 | |
| 343 | 2. Python in the real world |
| 344 | =========================== |
| 345 | |
| 346 | 2.1. Q. How many people are using Python? |
| 347 | |
Guido van Rossum | 5333c5d | 1994-04-11 11:06:22 +0000 | [diff] [blame] | 348 | A. I don't know, but the maximum number of simultaneous subscriptions |
| 349 | to the Python mailing list before it was gatewayed into the newsgroup |
| 350 | was about 180 (several of which were local redistribution lists). I |
| 351 | believe that many active Python users don't bother to subscribe to the |
| 352 | 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] | 353 | is even less meaningful. I see new names on the newsgroup all the |
| 354 | time and my best guess is that there are currently at least several |
| 355 | thousands of users. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 356 | |
| 357 | 2.2. Q. Have any significant projects been done in Python? |
| 358 | |
| 359 | A. Here at CWI (the home of Python), we have written a 20,000 line |
| 360 | authoring environment for transportable hypermedia presentations, a |
Guido van Rossum | 5333c5d | 1994-04-11 11:06:22 +0000 | [diff] [blame] | 361 | 5,000 line multimedia teleconferencing tool, as well as many many |
| 362 | smaller programs. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 363 | |
| 364 | The University of Virginia uses Python to control a virtual reality |
| 365 | engine. Contact: Matt Conway <conway@virginia.edu>. |
| 366 | |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame^] | 367 | If you have done a significant project in Python that you'd like to be |
| 368 | included in the list above, send me email! |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 369 | |
| 370 | 2.3. Q. Are there any commercial projects going on using Python? |
| 371 | |
| 372 | A. Several companies have revealed to me that they are planning or |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame^] | 373 | considering to use Python in a future product. |
| 374 | |
| 375 | The furthest is Sunrise Software, who already have a product out using |
| 376 | Python -- they use Python for a GUI management application and an SNMP |
| 377 | network manangement application. Contact: <info@sunrise.com>. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 378 | |
Guido van Rossum | f8c76d0 | 1994-08-17 12:19:53 +0000 | [diff] [blame] | 379 | Infoseek is using python to implement their commercial WWW information |
| 380 | retrieval service. Contact: <info@infoseek.com>. |
| 381 | |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame^] | 382 | Michael Powers of daVinci Time & Space is "writing tons-o-python for |
| 383 | interactive television entertainment." Contact: <powers@dvts.com>. |
| 384 | |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 385 | Individuals at many other companies are using Python for |
| 386 | internal development (witness their contributions to the Python |
Guido van Rossum | 5333c5d | 1994-04-11 11:06:22 +0000 | [diff] [blame] | 387 | mailing list or newsgroup). |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 388 | |
| 389 | Python has also been elected as an extension language by MADE, a |
| 390 | consortium supported by the European Committee's ESPRIT program and |
| 391 | consisting of Bull, CWI and some other European companies. Contact: |
| 392 | Ivan Herman <ivan@cwi.nl>. |
| 393 | |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame^] | 394 | If you'd like to be included in the list above, send me email! |
| 395 | |
Guido van Rossum | 95f61a7 | 1994-01-26 17:23:37 +0000 | [diff] [blame] | 396 | 2.4. Q. How stable is Python? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 397 | |
Guido van Rossum | 3de2736 | 1994-07-25 14:19:33 +0000 | [diff] [blame] | 398 | A. Very stable. While the current version number would suggest it is |
| 399 | in the early stages of development, in fact new, stable releases |
| 400 | (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] | 401 | months for the past four years. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 402 | |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame^] | 403 | 2.5. Q. When will the next version be released? |
| 404 | |
| 405 | A. I am planning to release 1.1 before October 15. It will contain |
| 406 | tons of changes, including (again) improved portability, especially |
| 407 | better support for DOS, Windows, Windows NT an the Macintosh. A few |
| 408 | modules will have been converted to the new naming scheme. A working |
| 409 | signal module will be present. |
| 410 | |
| 411 | 2.6. Q. What new developments are expected for Python in the future? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 412 | |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 413 | A. A proposal is being worked out to change the semantics of operator |
| 414 | overloading (__add__, __mul__ etc.) to make them more useful for |
| 415 | implementing types that don't resemble numbers. Additions will be |
| 416 | __call__ (to call an instance as if it were a function) and __eq__, |
| 417 | _lt__ etc. (to override individual comparisons). A (new) pthreads |
| 418 | interface has been submitted which will be integrated in the next |
| 419 | release. The X11/Motif interface will be improved. There are ideas |
| 420 | about built-in help using strings placed into function objects, and |
| 421 | possibly a switch statement. |
| 422 | |
| 423 | 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] | 424 | OS/2. The Windows ports will support dynamically loaded modules using |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame^] | 425 | DLLs. |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 426 | |
| 427 | Also planned is improved support for embedding Python in other |
| 428 | applications, e.g. by renaming most global symbols to have a "Py" |
| 429 | prefix and providing more documentation and threading support. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 430 | |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame^] | 431 | 2.7. Q. Is it reasonable to propose incompatible changes to Python? |
Guido van Rossum | 3de2736 | 1994-07-25 14:19:33 +0000 | [diff] [blame] | 432 | |
| 433 | A. In general, no. There are already millions of lines of Python code |
| 434 | around the world, so any changes in the language that invalidates more |
| 435 | than a very small fraction of existing programs has to be frowned |
| 436 | upon. Even if you can provide a conversion program, there still is |
| 437 | the problem of updating all documentation. Providing a gradual |
| 438 | upgrade path is the only way if a feature has to be changed. |
| 439 | |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 440 | |
| 441 | 3. Building Python |
| 442 | ================== |
| 443 | |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 444 | 3.1. Q. Is there a test set? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 445 | |
| 446 | A. Yes, simply do "import testall" (or "import autotest" if you aren't |
| 447 | interested in the output). The standard modules whose name begins |
| 448 | with "test" together comprise the test. The test set doesn't test |
| 449 | *all* features of Python but it goes a long way to confirm that a new |
| 450 | port is actually working. The Makefile contains an entry "make test" |
| 451 | which runs the autotest module. |
| 452 | |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 453 | 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] | 454 | operations, but when playing with floating point operations I cannot |
| 455 | find anything wrong with them. |
| 456 | |
| 457 | A. The test set makes occasional unwarranted assumptions about the |
| 458 | semantics of C floating point operations. Until someone donates a |
| 459 | better floating point test set, you will have to comment out the |
| 460 | offending floating point tests and execute similar tests manually. |
| 461 | |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 462 | 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] | 463 | |
| 464 | A. Rebuild STDWIN, specifying "CC=cc -cckr" in the Makefile. |
| 465 | |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 466 | 3.4. Q. Link errors after rerunning the configure script. |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 467 | |
| 468 | A. It is generally necessary to run "make clean" after a configuration |
| 469 | change. |
| 470 | |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 471 | 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] | 472 | script (after the script name). |
| 473 | |
| 474 | 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] | 475 | Don't. The reason for the complaint is that GNU getopt, unlike System |
| 476 | V getopt and other getopt implementations, doesn't consider a |
| 477 | non-option to be the end of the option list. A quick (and compatible) |
| 478 | fix for scripts is to add "--" to the interpreter, like this: |
| 479 | |
| 480 | #! /usr/local/bin/python -- |
| 481 | |
| 482 | You can also use this interactively: |
| 483 | |
| 484 | python -- script.py [options] |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 485 | |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 486 | 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] | 487 | glmodule.c, but python hasn't been built or installed yet. |
| 488 | |
| 489 | A. Comment out the line mentioning glmodule.c in Setup and build a |
| 490 | python without gl first; install it or make sure it is in your $PATH, |
| 491 | then edit the Setup file again to turn on the gl module, and make |
| 492 | again. You don't need to do "make clean"; you do need to run "make |
| 493 | Makefile" in the Modules subdirectory (or just run "make" at the |
| 494 | toplevel). |
| 495 | |
Guido van Rossum | 3de2736 | 1994-07-25 14:19:33 +0000 | [diff] [blame] | 496 | 3.7. Q. Python built with gcc for the DEC Alpha doesn't work. |
| 497 | |
| 498 | People have reported problems with both gcc 2.5.8 and 2.6.0. The DEC |
| 499 | OSF/1 cc compiler does not have these problems so it's probably gcc's |
| 500 | fault. One person reported that the problem went away when using -g |
| 501 | instead of -O so this may be an option if you insist on using gcc. If |
| 502 | someone tracks it down more completely I'd like to hear about it! |
| 503 | |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame^] | 504 | 3.8. Q. I use VPATH but some targets are built in the source directory. |
| 505 | |
| 506 | A. On some systems (e.g. Sun), if the target already exists in the |
| 507 | source directory, it is created there instead of in the build |
| 508 | directory. This is usually because you have previously built without |
| 509 | VPATH. Try running "make clobber" in the source directory. |
| 510 | |
| 511 | 3.9. Q. Trouble building or linking with the GNU readline library. |
| 512 | |
| 513 | A. Consider using readline 2.0. From the Python 1.1 README: |
| 514 | |
| 515 | - You can use the GNU readline library to improve the interactive user |
| 516 | interface: this gives you line editing and command history when |
| 517 | calling python interactively. You need to configure build the GNU |
| 518 | readline library before running the configure script. Its sources are |
| 519 | no longer distributed with Python; you can ftp them from any GNU |
| 520 | mirror site, or from its home site: |
| 521 | ftp://slc2.ins.cwru.edu/pub/dist/readline-2.0.tar.gz (or a higher |
| 522 | version number -- using version 1.x is not recommended). Pass the |
| 523 | Python configure script the option --with-readline=DIRECTORY where |
| 524 | DIRECTORY is the absolute pathname of the directory where you've built |
| 525 | the readline library. Some hints on building and using the readline |
| 526 | library: |
| 527 | |
| 528 | - On SGI IRIX 5, you may have to add the following |
| 529 | to rldefs.h: |
| 530 | |
| 531 | #ifndef sigmask |
| 532 | #define sigmask(sig) (1L << ((sig)-1)) |
| 533 | #endif |
| 534 | |
| 535 | - On most systems, you will have to add #include "rldefs.h" to the |
| 536 | top of several source files, and if you use the VPATH feature, you |
| 537 | will have to add dependencies of the form foo.o: foo.c to the |
| 538 | Makefile for several values of foo. |
| 539 | |
| 540 | - The readline library requires use of the termcap library. A |
| 541 | known problem with this is that it contains entry points which |
| 542 | cause conflicts with the STDWIN and SGI GL libraries. The stdwin |
| 543 | conflict can be solved (and will be, in the next release of |
| 544 | stdwin) by adding a line saying '#define werase w_erase' to the |
| 545 | stdwin.h file (in the stdwin distribution, subdirectory H). The |
| 546 | GL conflict has been solved in the Python configure script by a |
| 547 | hack that forces use of the static version of the termcap library. |
| 548 | |
| 549 | - Check the newsgroup gnu.bash.bugs for specific problems with the |
| 550 | readline library (I don't get this group here but I've been told |
| 551 | that it is the place for readline bugs.) |
| 552 | |
| 553 | 3.10. Q. Trouble building Python on Linux. |
| 554 | |
| 555 | A. There shouldn't be any -- I've seen several complaints but more |
| 556 | reports on successful "out-of-the-box" ports on Linux. The standard |
| 557 | configure script runs just fine on Linux. |
| 558 | |
| 559 | 3.11. Q. Other trouble building Python on platform X. |
Guido van Rossum | 95f61a7 | 1994-01-26 17:23:37 +0000 | [diff] [blame] | 560 | |
| 561 | 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] | 562 | |
| 563 | |
| 564 | 4. Programming in Python |
| 565 | ======================== |
| 566 | |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 567 | 4.1. Q. Is there a source code level debugger with breakpoints, step, |
| 568 | etc.? |
| 569 | |
| 570 | A. Yes. Check out module pdb; pdb.help() prints the documentation (or |
| 571 | you can read it as Lib/pdb.doc). If you use the STDWIN option, |
| 572 | there's also a windowing interface, wdb. You can write your own |
| 573 | debugger by using the code for pdb or wdb as an example. |
| 574 | |
| 575 | 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] | 576 | C and others in Python (e.g. through inheritance)? (Also phrased as: |
| 577 | Can I use a built-in type as base class?) |
| 578 | |
| 579 | A. No, but you can easily create a Python class which serves as a |
| 580 | wrapper around a built-in object, e.g. (for dictionaries): |
| 581 | |
| 582 | # A user-defined class behaving almost identical |
| 583 | # to a built-in dictionary. |
| 584 | class UserDict: |
| 585 | def __init__(self): self.data = {} |
| 586 | def __repr__(self): return repr(self.data) |
| 587 | def __cmp__(self, dict): |
| 588 | if type(dict) == type(self.data): |
| 589 | return cmp(self.data, dict) |
| 590 | else: |
| 591 | return cmp(self.data, dict.data) |
| 592 | def __len__(self): return len(self.data) |
| 593 | def __getitem__(self, key): return self.data[key] |
| 594 | def __setitem__(self, key, item): self.data[key] = item |
| 595 | def __delitem__(self, key): del self.data[key] |
| 596 | def keys(self): return self.data.keys() |
| 597 | def items(self): return self.data.items() |
| 598 | def values(self): return self.data.values() |
| 599 | def has_key(self, key): return self.data.has_key(key) |
| 600 | |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 601 | 4.3. Q. Is there a curses/termcap package for Python? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 602 | |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame^] | 603 | A. Yes -- Lance Ellinghouse has written a module that interfaces to |
| 604 | System V's "ncurses". If you know a little curses and some Python, |
| 605 | it's straightforward to use. |
| 606 | |
| 607 | You could also consider using the "alfa" (== character cell) version |
| 608 | of STDWIN. (STDWIN == Standard Windows, a portable windowing system |
| 609 | interface by the same author, URL: "ftp://ftp.cwi.nl/pub/stdwin".) This |
| 610 | will also prepare your program for porting to windowing environments |
| 611 | such as X11 or the Macintosh. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 612 | |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 613 | 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] | 614 | |
| 615 | A. Yes, if you import sys and assign a function to sys.exitfunc, it |
| 616 | will be called when your program exits, is killed by an unhandled |
| 617 | exception, or (on UNIX) receives a SIGHUP or SIGTERM signal. |
| 618 | |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 619 | 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] | 620 | nested function seemingly can't access the local variables of the |
| 621 | outer function. What is going on? How do I pass local data to a |
| 622 | nested function? |
| 623 | |
| 624 | A. Python does not have arbitrarily nested scopes. When you need to |
| 625 | create a function that needs to access some data which you have |
| 626 | available locally, create a new class to hold the data and return a |
| 627 | method of an instance of that class, e.g.: |
| 628 | |
| 629 | class MultiplierClass: |
| 630 | def __init__(self, factor): |
| 631 | self.factor = factor |
| 632 | def multiplier(self, argument): |
| 633 | return argument * self.factor |
| 634 | |
| 635 | def generate_multiplier(factor): |
| 636 | return MultiplierClass(factor).multiplier |
| 637 | |
| 638 | twice = generate_multiplier(2) |
| 639 | print twice(10) |
| 640 | # Output: 20 |
| 641 | |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 642 | 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] | 643 | |
| 644 | A. If it is a list, the fastest solution is |
| 645 | |
| 646 | list.reverse() |
| 647 | try: |
| 648 | for x in list: |
| 649 | "do something with x" |
| 650 | finally: |
| 651 | list.reverse() |
| 652 | |
| 653 | This has the disadvantage that while you are in the loop, the list |
| 654 | is temporarily reversed. If you don't like this, you can make a copy. |
| 655 | This appears expensive but is actually faster than other solutions: |
| 656 | |
| 657 | rev = list[:] |
| 658 | rev.reverse() |
| 659 | for x in rev: |
| 660 | <do something with x> |
| 661 | |
| 662 | If it isn't a list, a more general but slower solution is: |
| 663 | |
| 664 | i = len(list) |
| 665 | while i > 0: |
| 666 | i = i-1 |
| 667 | x = list[i] |
| 668 | <do something with x> |
| 669 | |
| 670 | A more elegant solution, is to define a class which acts as a sequence |
| 671 | and yields the elements in reverse order (solution due to Steve |
| 672 | Majewski): |
| 673 | |
| 674 | class Rev: |
| 675 | def __init__(self, seq): |
| 676 | self.forw = seq |
| 677 | def __len__(self): |
| 678 | return len(self.forw) |
| 679 | def __getitem__(self, i): |
| 680 | return self.forw[-(i + 1)] |
| 681 | |
| 682 | You can now simply write: |
| 683 | |
| 684 | for x in Rev(list): |
| 685 | <do something with x> |
| 686 | |
| 687 | Unfortunately, this solution is slowest of all, due the the method |
| 688 | call overhead... |
| 689 | |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 690 | 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] | 691 | |
| 692 | A. That's a tough one, in general. There are many tricks to speed up |
| 693 | Python code; I would consider rewriting parts in C only as a last |
| 694 | resort. One thing to notice is that function and (especially) method |
| 695 | calls are rather expensive; if you have designed a purely OO interface |
| 696 | with lots of tiny functions that don't do much more than get or set an |
| 697 | instance variable or call another method, you may consider using a |
| 698 | more direct way, e.g. directly accessing instance variables. Also see |
| 699 | the standard module "profile" (described in the file |
| 700 | "python/lib/profile.doc") which makes it possible to find out where |
| 701 | your program is spending most of its time (if you have some patience |
| 702 | -- the profiling itself can slow your program down by an order of |
| 703 | magnitude). |
| 704 | |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 705 | 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] | 706 | again (into the same Python process), the changes don't seem to take |
| 707 | place. What is going on? |
| 708 | |
| 709 | A. For efficiency reasons, Python only reads the module file on the |
| 710 | first time a module is imported (otherwise a program consisting of |
| 711 | many modules, each of which imports the same basic module, would read |
| 712 | the basic module over and over again). To force a changed module |
| 713 | being read again, do this: |
| 714 | |
| 715 | import modname |
| 716 | reload(modname) |
| 717 | |
| 718 | Warning: this technique is not 100% fool-proof. In particular, |
| 719 | modules containing statements like |
| 720 | |
| 721 | from modname import some_objects |
| 722 | |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame^] | 723 | will continue to work with the old version of the imported objects. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 724 | |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame^] | 725 | 4.9. Q. How do I find the current module name? |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 726 | |
| 727 | A. A module can find out its own module name by alooking at the |
| 728 | (predefined) global variable __name__. If this has the value |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame^] | 729 | '__main__' you are running as a script. |
| 730 | |
| 731 | 4.10. Q. I have a module in which I want to execute some extra code when it |
| 732 | is run as a script. How do I find out whether I am running as a |
| 733 | script? |
| 734 | |
| 735 | A. See the previous question. E.g. if you put the following on the |
| 736 | last line of your module, main() is called only when your module is |
| 737 | running as a script: |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 738 | |
| 739 | if __name__ == '__main__': main() |
| 740 | |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame^] | 741 | 4.11. Q. I try to run a program from the Demo directory but it fails with |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 742 | ImportError: No module named ...; what gives? |
| 743 | |
| 744 | A. This is probably an optional module which hasn't been configured on |
| 745 | your system. This especially happens with modules like "stdwin", |
| 746 | "gl", "Xt" or "Xm". For stdwin and many other modules, see |
| 747 | Modules/Setup.in for info on how to add these modules to your Python, |
| 748 | if it is possible at all. Sometimes you will have to ftp and build |
| 749 | another package first (e.g. stdwin). Sometimes the module only works |
| 750 | on specific platforms (e.g. gl only works on SGI machines). For |
| 751 | X-related modules (Xt and Xm) you will have to do more work: they are |
| 752 | currently not part of the standard Python distribution. You will have |
| 753 | to ftp the file "extensions.tar.gz" file from a Python ftp repository |
| 754 | (e.g. ftp.cwi.nl) and follow the instructions there. Note: the X |
| 755 | related modules are still somewhat flakey, so don't try this unless |
| 756 | you know a bit or two about building X applications on your platform. |
| 757 | |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame^] | 758 | 4.12. Q. I have successfully built Python with STDWIN but it can't find some |
| 759 | modules (e.g. stdwinevents). |
| 760 | |
| 761 | A. There's a subdirectory of the library directory named 'stdwin' |
| 762 | which should be in the default module search path. There's a line in |
| 763 | Modules/Setup(.in) that you have to enable for this purpose -- |
| 764 | unfortuunately in the latest release it's not near the other |
| 765 | STDWIN-related lines so it's easy to miss it. |
| 766 | |
| 767 | 4.13. Q. What GUI toolkits exist for Python? |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 768 | |
| 769 | A. Depending on what platform(s) you are aiming at, there are several. |
| 770 | |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame^] | 771 | - There's a neat object-oriented interface to the Tcl/Tk widget set, |
| 772 | called Tkinter. You can ftp it from ftp.cwi.nl as |
| 773 | pub/python/tkinter.tar.gz. This is probably the easiest to install |
| 774 | and use, and the most complete widget set. |
| 775 | |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 776 | - The standard Python distribution comes with an interface to STDWIN, |
| 777 | a platform-independent low-level windowing interface (you have to ftp |
| 778 | the source for stdwin separately, e.g. from ftp.cwi.nl in pub/stdwin |
| 779 | or gatekeeper.dec.com in pub/misc/stdwin). STDWIN runs under X11 or |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame^] | 780 | the Mac; a Windows port has been attempted but I can't seem to get it |
| 781 | working. Note that STDWIN is really not powerful enough to implement |
| 782 | a modern GUI (no widgets, etc.) and that I don't have the time to |
| 783 | maintain or extend it, so you may be better off using Tkinter or the |
| 784 | Motif interface, unless you require portability to the Mac (which is |
| 785 | also offered by SUIT, by the way -- see below). |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 786 | |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame^] | 787 | - For SGI IRIX only, there's an interface to the complete GL (Graphics |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 788 | Library -- low level but very good 3D capabilities) as well as to |
| 789 | FORMS (a buttons-and-sliders-etc package built on top of GL by Mark |
| 790 | Overmars -- ftp'able from ftp.cs.ruu.nl in pub/SGI/FORMS). |
| 791 | |
| 792 | - There's an interface to X11, including the Athena and Motif widget |
| 793 | sets (and a few individual widgets, like Mosaic's HTML widget and |
| 794 | SGI's GL widget) in the Extensions set, which is separately ftp'able |
| 795 | from ftp.cwi.nl as pub/python/extensions.tar.gz. |
| 796 | |
| 797 | - There's an interface to SUIT, the U of Virginia's Simple User |
| 798 | Interface Toolkit; it can be ftp'ed from uvacs.cs.virginia.edu as |
| 799 | pub/suit/python/SUIT_python.tar.Z. A PC binary of Python 1.0.2 |
| 800 | compiled with DJGPP and with SUIT support built-in has been made |
| 801 | available by Antonio Costa on ftp site asterix.inescn.pt, directory |
| 802 | pub/PC/python, file pyt102su.exe (a self-extracting archive). |
| 803 | |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 804 | - There's an interface to WAFE, a Tcl interface to the X11 Motif and |
| 805 | Athena widget sets. Last I heard about it it was included in the |
| 806 | WAFE 1.0 prerelease, ftp'able from ftp.wu-wien.ac.at as |
| 807 | pub/src/X11/wafe/wafe-1.0.tar.gz-prerelease. |
| 808 | |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame^] | 809 | 4.14. Q. Are there any interfaces to commercial database in Python? |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 810 | |
| 811 | A. There's an interface to SYBASE by John Redford |
| 812 | <jredford@lehman.com>. |
| 813 | |
| 814 | There's also an interface to metalbase by Lance Ellinghouse |
| 815 | <lance@markv.com>. |
| 816 | |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame^] | 817 | Anthony Baxter <anthony.baxter@aaii.oz.au> has written an interface to |
| 818 | mSQL (mini-SQL). Ftp it from ftp.cwi.nl:/pub/python/PymSQL.tar.gz. |
| 819 | |
| 820 | 4.15. Q. Is it possible to write obfuscated one-liners in Python? |
Guido van Rossum | c24da7c | 1994-09-23 14:08:41 +0000 | [diff] [blame] | 821 | |
| 822 | A. Yes. See the following three examples, due to Ulf Bartelt: |
| 823 | |
| 824 | # Primes < 1000 |
| 825 | print filter(None,map(lambda y:y*reduce(lambda x,y:x*y!=0,\ |
| 826 | map(lambda x,y=y:y%x,range(2,int(pow(y,0.5)+1))),1),range(2,1000))) |
| 827 | |
| 828 | # First 10 Fibonacci numbers |
| 829 | print map(lambda x,f=lambda x,f:(x<=1) or (f(x-1,f)+f(x-2,f)): f(x,f),\ |
| 830 | range(10)) |
| 831 | |
| 832 | # Mandelbrot set |
| 833 | print (lambda Ru,Ro,Iu,Io,IM,Sx,Sy:reduce(lambda x,y:x+y,map(lambda y,\ |
| 834 | Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,Sy=Sy,L=lambda yc,Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,i=IM,\ |
| 835 | Sx=Sx,Sy=Sy:reduce(lambda x,y:x+y,map(lambda x,xc=Ru,yc=yc,Ru=Ru,Ro=Ro,\ |
| 836 | 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\ |
| 837 | >=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(\ |
| 838 | 64+F(Ru+x*(Ro-Ru)/Sx,yc,0,0,i)),range(Sx))):L(Iu+y*(Io-Iu)/Sy),range(Sy\ |
| 839 | ))))(-2.1, 0.7, -1.2, 1.2, 30, 80, 24) |
| 840 | # \___ ___/ \___ ___/ | | |__ lines on screen |
| 841 | # V V | |______ columns on screen |
| 842 | # | | |__________ maximum of "iterations" |
| 843 | # | |_________________ range on y axis |
| 844 | # |____________________________ range on x axis |
| 845 | |
| 846 | Don't try this at home, kids! |
| 847 | |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame^] | 848 | 4.16. Q. Is there an equivalent of C's "?:" ternary operator? |
Guido van Rossum | c24da7c | 1994-09-23 14:08:41 +0000 | [diff] [blame] | 849 | |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame^] | 850 | A. Not directly. In many cases you can mimic a?b:c with "a and b or |
| 851 | c", but there's a flaw: if b is zero (or empty, or None -- anything |
| 852 | that tests false) then c will be selected instead. In many cases you |
| 853 | can prove by looking at the code that this can't happen (e.g. because |
| 854 | b is a constant or has a type that can never be false), but in general |
| 855 | this can be a problem. |
| 856 | |
| 857 | Steve Majewski (or was it Tim Peters?) suggested the following |
| 858 | solution: (a and [b] or [c])[0]. Because [b] is a singleton list it |
| 859 | is never false, so the wrong path is never taken; then applying [0] to |
| 860 | the whole thing gets the b or c that you really wanted. Ugly, but it |
| 861 | gets you there in the rare cases where it is really inconvenient to |
| 862 | rewrite your code using 'if'. |
| 863 | |
| 864 | 4.17. Q. My class defines __del__ but it is not called when I delete the |
| 865 | object. |
| 866 | |
| 867 | A. There are several possible reasons for this. |
| 868 | |
| 869 | - The del statement does not necessarily call __del__ -- it simply |
| 870 | decrements the object's reference count, and if this reaches zero |
| 871 | __del__ is called. |
| 872 | |
| 873 | - If your data structures contain circular links (e.g. a tree where |
| 874 | each child has a parent pointer and each parent has a list of |
| 875 | children) the reference counts will never go back to zero. You'll |
| 876 | have to define an explicit close() method which remvoes those |
| 877 | pointers. Please don't ever call __del__ directly -- __del__ should |
| 878 | call close() and close() should make sure that it can be called mor |
| 879 | than once for the same object. |
| 880 | |
| 881 | - If the object has ever been a local variable (or argument, which is |
| 882 | really the same thing) to a function that caught an expression in an |
| 883 | except clause, chances are that a reference to the object still exists |
| 884 | in that function's stack frame as contained in the stack trace. |
| 885 | Normally, deleting (better: assigning None to) sys.exc_traceback will |
| 886 | take care of this. If you a stack was printed for an unhandled |
| 887 | exception in an interactive interpreter, delete sys.last_traceback |
| 888 | instead. |
| 889 | |
| 890 | - There is code that deletes all objects when the interpreter exits, |
| 891 | but if your Python has been configured to support threads, it is not |
| 892 | called (because other threads may still be active). You can define |
| 893 | your own cleanp function using sys.exitfunc (see question 4.4). |
| 894 | |
| 895 | 4.18. Q. How do I change the shell environment for programs called using |
| 896 | os.popen() or os.system()? Changing os.environ doesn't work. |
| 897 | |
| 898 | A. Modifying the environment passed to subshells was left out of the |
| 899 | interpreter because there seemed to be no well-established portable |
| 900 | way to do it. |
| 901 | |
| 902 | However if all you want is to pass environment variables to the |
| 903 | commands run by os.system() or os.popen(), there's a simple solution: |
| 904 | prefix the command string with a couple of variable assignments and |
| 905 | export statements. I guess the following would be universal for popen |
| 906 | (untested): |
| 907 | |
| 908 | import os |
| 909 | from commands import mkarg # nifty routine to add shell quoting |
| 910 | def epopen(cmd, mode, env = {}): |
| 911 | # env is a dictionary of environment variables |
| 912 | prefix = '' |
| 913 | for key, value in env.values(): |
| 914 | prefix = prefix + '%s=%s\n' % (key, mkarg(value)) |
| 915 | prefix = prefix + 'export %s\n' % key |
| 916 | return os.popen(prefix + cmd, mode) |
| 917 | |
| 918 | 4.19. Q. What is a class? |
| 919 | |
| 920 | A. A class is the particular object type that is created by executing |
| 921 | a class statement. |
| 922 | |
| 923 | 4.20. Q. What is a method? |
| 924 | |
| 925 | A. A method is a function that you normally call as |
| 926 | x.name(arguments...) for some object x. The word is used for methods |
| 927 | of classes and class instances as well as for methods of built-in |
| 928 | objects. The latter have a completely different implementation and |
| 929 | only share the way their calls look in Python code. |
| 930 | |
| 931 | 4.21. Q. What is self? |
| 932 | |
| 933 | A. Self is merely a conventional name for the first argument of a |
| 934 | method -- i.e. a function defined inside a class definition. A method |
| 935 | defined as meth(self, a, b, c) should be called as x.meth(a, b, c) for |
| 936 | some instance x of the class in which the definition occurs; |
| 937 | the called method will think it is called as meth(x, a, b, c). |
| 938 | |
| 939 | 4.22. Q. What is a unbound method? |
| 940 | |
| 941 | A. An unbound method is a method defined in a class that is not yet |
| 942 | bound to an instance. You get an unbound method if you ask for a |
| 943 | class attribute that happens to be a function. You get a bound method |
| 944 | if you ask for an instance attribute. A bound method knows which |
| 945 | instance it belongs to and calling it supplies the instance automatic; |
| 946 | an unbound method only knows which class it wants for its first |
| 947 | argument (a derived class is also OK). Calling an unbound method |
| 948 | doesn't "magically" derive the first argument from the context -- you |
| 949 | have to provide it explicitly. |
| 950 | |
| 951 | 4.23. Q. How do I call a method defined in a base class from a derived class |
| 952 | that overrides it? |
| 953 | |
| 954 | A. If your class definition starts with "class Derived(Base): ..." |
| 955 | then you can call method meth defined in Base (or one of Base's base |
| 956 | classes) as Base.meth(self, arguments...). Here, Base.meth is an |
| 957 | unbound method (see previous question). |
| 958 | |
| 959 | 4.24. Q. How do I call a method from a base class without using the name of |
| 960 | the base class? |
| 961 | |
| 962 | A. DON'T DO THIS. REALLY. I MEAN IT. It appears that you could call |
| 963 | self.__class__.__bases__[0].meth(self, arguments...) but this fails when |
| 964 | a doubly-derived method is derived from your clas: for its instances, |
| 965 | self.__class__.__bases__[0] is your class, not its base class -- so |
| 966 | (assuming you are doing this from within Derived.meth) you would start |
| 967 | a recursive call. |
| 968 | |
| 969 | 4.25. Q. How can I organize my code to make it easier to change the base |
| 970 | class? |
| 971 | |
| 972 | A. You could define an alias for the base class, assign the real base |
| 973 | class to it before your class definition, and use the alias throughout |
| 974 | your class. Then all you have to change is the value assigned to the |
| 975 | alias. Incidentally, this trick is also handy if you want to decide |
| 976 | dynamically (e.g. depending on availability of resources) which base |
| 977 | class to use. Example: |
| 978 | |
| 979 | BaseAlias = <real base class> |
| 980 | class Derived(BaseAlias): |
| 981 | def meth(self): |
| 982 | BaseAlias.meth(self) |
| 983 | ... |
| 984 | |
| 985 | 4.26. Q. How can I find the methods or attributes of an object? |
| 986 | |
| 987 | A. This depends on the object type. |
| 988 | |
| 989 | For an instance x of a user-defined class, instance attributes are |
| 990 | found in the dictionary x.__dict__, and methods and attributes defined |
| 991 | by its class are found in x.__class__.__bases__[i].__dict__ (for i in |
| 992 | range(len(x.__class__.__bases__))). You'll have to walk the tree of |
| 993 | base classes to find *all* class methods and attributes. |
| 994 | |
| 995 | Many, but not all built-in types define a list of their method names |
| 996 | in x.__methods__, and if they have data attributes, their names may be |
| 997 | found in x.__members__. However this is only a convention. |
| 998 | |
| 999 | For more information, read the source of the standard (but |
| 1000 | undocumented) module newdir. |
| 1001 | |
| 1002 | 4.27. Q. I can't seem to use os.read() on a pipe created with os.popen(). |
| 1003 | |
| 1004 | A. os.read() is a low-level function which takes a file descriptor (a |
| 1005 | small integer). os.popen() creates a high-level file object -- the |
| 1006 | same type used for sys.std{in,out,err} and returned by the builtin |
| 1007 | open() function. Thus, to read n bytes from a pipe p created with |
| 1008 | os.popen(), you need to use p.read(n). |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 1009 | |
| 1010 | 5. Extending Python |
| 1011 | =================== |
| 1012 | |
| 1013 | 5.1. Q. Can I create my own functions in C? |
| 1014 | |
| 1015 | A. Yes, you can create built-in modules containing functions, |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 1016 | variables, exceptions and even new types in C. This is explained in |
| 1017 | the document "Extending and Embedding the Python Interpreter" (the |
| 1018 | 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] | 1019 | |
| 1020 | 5.2. Q. Can I create my own functions in C++? |
| 1021 | |
| 1022 | A. Yes, using the C-compatibility features found in C++. Basically |
| 1023 | you place extern "C" { ... } around the Python include files and put |
| 1024 | extern "C" before each function that is going to be called by the |
| 1025 | Python interpreter. Global or static C++ objects with constructors |
| 1026 | are probably not a good idea. |
| 1027 | |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 1028 | 5.3. Q. How can I execute arbitrary Python statements from C? |
| 1029 | |
| 1030 | A. The highest-level function to do this is run_command() which takes |
| 1031 | a single string argument which is executed in the context of module |
| 1032 | __main__ and returns 0 for success and -1 when an exception occurred |
| 1033 | (including SyntaxError). If you want more control, use run_string(); |
| 1034 | see the source for run_command() in Python/pythonrun.c. |
| 1035 | |
| 1036 | 5.4. Q. How can I evaluate an arbitrary Python expression from C? |
| 1037 | |
| 1038 | A. Call the function run_string() from the previous question with the |
| 1039 | start symbol eval_input; it then parses an expression, evaluates it |
| 1040 | and returns its value. See exec_eval() in Python/bltinmodule.c. |
| 1041 | |
| 1042 | 5.5. Q. How do I extract C values from a Python object? |
| 1043 | |
| 1044 | A. That depends on the object's type. If it's a tuple, |
| 1045 | gettuplesize(o) returns its length and gettupleitem(o, i) returns its |
| 1046 | i'th item; similar for lists with getlistsize(o) and getlistitem(o, |
| 1047 | i). For strings, getstringsize(o) returns its length and |
| 1048 | getstringvalue(o) a pointer to its value (note that Python strings may |
| 1049 | contain null bytes so strlen() is not safe). To test which type an |
| 1050 | object is, first make sure it isn't NULL, and then use |
| 1051 | is_stringobject(o), is_tupleobject(o), is_listobject(o) etc. |
| 1052 | |
| 1053 | 5.6. Q. How do I use mkvalue() to create a tuple of arbitrary length? |
| 1054 | |
| 1055 | A. You can't. Use t = newtupleobject(n) instead, and fill it with |
| 1056 | objects using settupleitem(t, i, o) -- note that this "eats" a |
| 1057 | reference count of o. Similar for lists with newlistobject(n) and |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame^] | 1058 | setlistitem(l, i, o). Note that you *must* set all the tuple items to |
| 1059 | some value before you pass the tuple to Python code -- |
| 1060 | newtupleobject(n) initializes them to NULL, which isn't a valid Python |
| 1061 | value. |
| 1062 | |
| 1063 | 5.7. Q. What happened to mktuple(), featuring in an example in the |
| 1064 | Extensions manual? |
| 1065 | |
| 1066 | A. It's a typo, I meant newtupleobject() (see previous question). |
| 1067 | |
| 1068 | 5.8. Q. How do I call an object's method from C? |
| 1069 | |
| 1070 | A. Here's a function (untested) that might become part of the next |
| 1071 | release in some form. It uses <stdarg.h> to allow passing the |
| 1072 | argument list on to vmkvalue(): |
| 1073 | |
| 1074 | object *call_method(object *inst, char *methodname, char *format, ...) |
| 1075 | { |
| 1076 | object *method; |
| 1077 | object *args; |
| 1078 | object *result; |
| 1079 | va_list va; |
| 1080 | method = getattr(inst, methodname); |
| 1081 | if (method == NULL) return NULL; |
| 1082 | va_start(va, format); |
| 1083 | args = vmkvalue(format, va); |
| 1084 | va_end(va); |
| 1085 | if (args == NULL) { |
| 1086 | DECREF(method); |
| 1087 | return NULL; |
| 1088 | } |
| 1089 | result = call_object(method, args); |
| 1090 | DECREF(method); |
| 1091 | DECREF(args); |
| 1092 | return result; |
| 1093 | } |
| 1094 | |
| 1095 | This works for any instance that has methods -- whether built-in or |
| 1096 | user-defined. You are responsible for eventually DECREF'ing the |
| 1097 | return value. |
| 1098 | |
| 1099 | To call, e.g., a file object's "seek" method with arguments 10, 0 |
| 1100 | (assuming the file object pointer is "f"): |
| 1101 | |
| 1102 | res = call_method(f, "seek", "(OO)", 10, 0); |
| 1103 | if (res == NULL) { |
| 1104 | ... an exception occurred ... |
| 1105 | } |
| 1106 | else { |
| 1107 | DECREF(res); |
| 1108 | } |
| 1109 | |
| 1110 | Note that since call_object() *always* wants a tuple for the argument |
| 1111 | list, to call a function without arguments, pass "()" for the format, |
| 1112 | and to call a function with one argument, surround the argument in |
| 1113 | parentheses, e.g. "(i)". |
| 1114 | |
| 1115 | 5.9. Q. How do I catch the output from print_error()? |
| 1116 | |
| 1117 | A. (Due to Mark Hammond): |
| 1118 | |
| 1119 | * in Python code, define an object that supports the "write()" method. |
| 1120 | FWIW, there seems to be a small problem that requires the 'softspace' |
| 1121 | attribute to be defined too (I cant remember exact details of the |
| 1122 | problem). |
| 1123 | |
| 1124 | * redirect sys.stdout and sys.stderr to this object. |
| 1125 | |
| 1126 | * call print_error, or just allow the standard traceback mechansim to |
| 1127 | work. |
| 1128 | |
| 1129 | Then, the output will go whereever your write() method sends it. |
| 1130 | |
| 1131 | 5.10. Q. How do I access a module written in Python from C? |
| 1132 | |
| 1133 | A. You can get a pointer to the module object as follows: |
| 1134 | |
| 1135 | module = import_module("<modulename>"); |
| 1136 | |
| 1137 | If the module hasn't been imported yet (i.e. it is not yet present in |
| 1138 | sys.modules), this initializes the module; otherwise it simply returns |
| 1139 | the value of sys.modules["<modulename>"]. Note that it doesn't enter |
| 1140 | the module into any namespace -- it only ensures it has been |
| 1141 | initialized and is stored in sys.modules. |
| 1142 | |
| 1143 | You can then access the module's attributes (i.e. any name defined in |
| 1144 | the module) as follows: |
| 1145 | |
| 1146 | attr = getattr(module, "<attrname>"); |
| 1147 | |
| 1148 | Calling setattr(), to assign to variables in the module, also works. |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 1149 | |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 1150 | |
| 1151 | 6. Python's design |
| 1152 | ================== |
| 1153 | |
| 1154 | 6.1. Q. Why isn't there a generic copying operation for objects in |
| 1155 | Python? |
| 1156 | |
| 1157 | A. Hmm. Maybe there should be one, but it's difficult to assign a |
| 1158 | useful meaning to copying of open files, sockets and windows, or |
| 1159 | recursive data structures. As long as you design all your classes |
| 1160 | yourself you are of course free to define a standard base class that |
| 1161 | defines an overridable copying operation for all the objects you care |
| 1162 | about. (One practical point: it would have to be a built-in function, |
| 1163 | not a standard method name, since not all built-in object types have |
| 1164 | methods; e.g. strings, integers and tuples don't.) |
| 1165 | |
| 1166 | 6.2. Q. Why isn't there a generic way to implement persistent objects |
| 1167 | in Python? (Persistent == automatically saved to and restored from |
| 1168 | disk.) |
| 1169 | |
| 1170 | A. Hmm, hmm. Basically for the same reasons as why there is no |
| 1171 | generic copying operation. |
| 1172 | |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame^] | 1173 | However, since there is a real desire to have persistent operations, |
| 1174 | I'm thinking of extending the marshal module to support object sharing |
| 1175 | (and hence recursive objects) and to extend the list of supported |
| 1176 | types considerably. For user-defined classes, hooks with __*__ names |
| 1177 | will allow the class to modify the way their instances are dumped and |
| 1178 | loaded. Built-in types (including those defined by new extensions) |
| 1179 | may also define dump/load hooks. There are several problems still to |
| 1180 | solve, e.g. how do you reliably find the class of which an object is |
| 1181 | an instance at load time -- assuming the class itself is loaded from |
| 1182 | some module, and not part of the dumped data. It is also necessary to |
| 1183 | separate the functionality of converting a set of objects into a byte |
| 1184 | stream from which they can be reloaded, from the ability to save these |
| 1185 | byte streams as files and being able to reference an object by a |
| 1186 | persistent global name. |
| 1187 | |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 1188 | 6.3. Q. Why isn't there a switch or case statement in Python? |
| 1189 | |
| 1190 | A. You can do this easily enough with a sequence of |
| 1191 | if... elif... elif... else. There have been some proposals for switch |
| 1192 | statement syntax, but there is no concensus (yet) on whether and how |
| 1193 | to do range tests. |
| 1194 | |
Guido van Rossum | c50158e | 1994-05-31 09:18:50 +0000 | [diff] [blame] | 1195 | 6.4. Q. Why does Python use indentation for grouping of statements? |
| 1196 | |
| 1197 | A. Basically I believe that using indentation for grouping is |
| 1198 | extremely elegant and contributes a lot to the clarity of the average |
| 1199 | Python program. Most people learn to love this feature after a while. |
| 1200 | Some arguments for it: |
| 1201 | |
| 1202 | - Since there are no begin/end brackets there cannot be a disagreement |
| 1203 | between grouping perceived by the parser and the human reader. I |
| 1204 | remember long ago seeing a C fragment like this: |
| 1205 | |
| 1206 | if (x <= y) |
| 1207 | x++; |
| 1208 | y--; |
| 1209 | z++; |
| 1210 | |
| 1211 | and staring a long time at it wondering why y was being decremented |
| 1212 | even for x > y... (And I wasn't a C newbie then either.) |
| 1213 | |
| 1214 | - Since there are no begin/end brackets there can be no conflicting |
| 1215 | coding styles. In C there are loads of different ways to place the |
| 1216 | braces (including the choice whether to place braces around single |
| 1217 | statements in certain cases, for consistency). If you're used to |
| 1218 | reading (and writing) code that uses one style, you will feel at least |
| 1219 | slightly uneasy when reading (or being required to write) another |
| 1220 | style. |
| 1221 | |
| 1222 | - Many coding styles place begin/end brackets on a line by themself. |
| 1223 | This makes programs considerably longer and wastes valuable screen |
| 1224 | space, making it harder to get a good overview over a program. |
| 1225 | Ideally, a function should fit on one basic tty screen (say, 20 |
| 1226 | lines). 20 lines of Python are worth a LOT more than 20 lines of C. |
| 1227 | This is not solely due to the lack of begin/end brackets (the lack of |
| 1228 | declarations also helps, and the powerful operations of course), but |
| 1229 | it certainly helps! |
| 1230 | |
Guido van Rossum | 3de2736 | 1994-07-25 14:19:33 +0000 | [diff] [blame] | 1231 | 6.5. Q. Why are Python strings immutable? |
| 1232 | |
| 1233 | A. There are two advantages. One is performance: knowing that a |
| 1234 | string is immutable makes it easy to lay it out at construction time |
| 1235 | -- fixed and unchanging storage requirements. (This is also one of |
| 1236 | the reasons for the the distinction between tuples and lists.) The |
| 1237 | other is that strings in Python are considered as "elemental" as |
| 1238 | numbers. No amount of activity will change the value 8 to anything |
| 1239 | else, and in Python, no amount of activity will change the string |
| 1240 | "eight" to anything else. (Adapted from Jim Roskind) |
| 1241 | |
| 1242 | 6.6. Q. Why don't strings have methods like index() or sort(), like |
| 1243 | lists? |
| 1244 | |
| 1245 | A. Good question. Strings currently don't have methods at all |
| 1246 | (likewise tuples and numbers). Long ago, it seemed unnecessary to |
| 1247 | implement any of these functions in C, so a standard library module |
| 1248 | "string" written in Python was created that performs string related |
| 1249 | operations. Since then, the cry for performance has moved most of |
| 1250 | 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] | 1251 | string, which is still the preferred interface, without loss of |
Guido van Rossum | 3de2736 | 1994-07-25 14:19:33 +0000 | [diff] [blame] | 1252 | performance except during initialization). Some of these functions |
| 1253 | (e.g. index()) could easily be implemented as string methods instead, |
| 1254 | but others (e.g. sort()) can't, since their interface prescribes that |
| 1255 | they modify the object, while strings are immutable (see the previous |
| 1256 | question). |
| 1257 | |
| 1258 | 6.7. Q. Why does Python use methods for some functionality |
| 1259 | (e.g. list.index()) but functions for other (e.g. len(list))? |
| 1260 | |
| 1261 | A. Functions are used for those operations that are generic for a |
| 1262 | group of types and which should work even for objects that don't have |
| 1263 | methods at all (e.g. numbers, strings, tuples). Also, implementing |
| 1264 | len(), max(), min() as a built-in function is actually less code than |
| 1265 | implementing them as methods for each type. One can quibble about |
| 1266 | individual cases but it's really too late to change such things |
| 1267 | fundamentally now. |
| 1268 | |
| 1269 | 6.8. Q. Why can't I derive a class from built-in types (e.g. lists or |
| 1270 | files)? |
| 1271 | |
| 1272 | A. This is caused by the relatively late addition of (user-defined) |
| 1273 | classes to the language -- the implementation framework doesn't easily |
| 1274 | allow it. See the answer to question 4.2 for a work-around. This |
| 1275 | *may* be fixed in the (distant) future. |
| 1276 | |
| 1277 | 6.9. Q. Why must 'self' be declared and used explicitly in method |
| 1278 | definitions and calls? |
| 1279 | |
| 1280 | A. By asking this question you reveal your C++ background. :-) |
| 1281 | When I added classes, this was (again) the simplest way of |
| 1282 | implementing methods without too many changes to the interpreter. I |
| 1283 | borrowed the idea from Modula-3. It turns out to be very useful, for |
| 1284 | a variety of reasons. |
| 1285 | |
| 1286 | First, it makes it more obvious that you are using a method or |
| 1287 | instance attribute instead of a local variable. Reading "self.x" or |
| 1288 | "self.meth()" makes it absolutely clear that an instance variable or |
| 1289 | method is used even if you don't know the class definition by heart. |
| 1290 | In C++, you can sort of tell by the lack of a local variable |
| 1291 | declaration (assuming globals are rare or reasily recognizable) -- but |
| 1292 | in Python, there are no local variable declarations, so you'd have to |
| 1293 | look up the class definition to be sure. |
| 1294 | |
| 1295 | Second, it means that no special syntax is necessary if you want to |
| 1296 | explicitly reference or call the method from a particular class. In |
| 1297 | C++, if you want to use a method from base class that is overridden in |
| 1298 | a derived class, you have to use the :: operator -- in Python you can |
| 1299 | write baseclass.methodname(self, <argument list>). This is |
| 1300 | particularly useful for __init__() methods, and in general in cases |
| 1301 | where a derived class method wants to extend the base class method of |
| 1302 | the same name and thus has to call the base class method somehow. |
| 1303 | |
| 1304 | Lastly, for instance variables, it solves a syntactic problem with |
| 1305 | assignment: since local variables in Python are (by definition!) those |
| 1306 | variables to which a value assigned in a function body (and that |
| 1307 | aren't explicitly declared global), there has to be some way to tell |
| 1308 | the interpreter that an assignment was meant to assign to an instance |
| 1309 | variable instead of to a local variable, and it should preferably be |
| 1310 | syntactic (for efficiency reasons). C++ does this through |
| 1311 | declarations, but Python doesn't have declarations and it would be a |
| 1312 | pity having to introduce them just for this purpose. Using the |
| 1313 | explicit "self.var" solves this nicely. Similarly, for using instance |
| 1314 | variables, having to write "self.var" means that references to |
| 1315 | unqualified names inside a method don't have to search the instance's |
| 1316 | directories. |
| 1317 | |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame^] | 1318 | 6.10. Q. Can't you emulate threads in the interpreter instead of |
| 1319 | relying on an OS-specific thread implementation? |
| 1320 | |
| 1321 | A. Unfortunately, the interpreter pushes at least one C stack frame |
| 1322 | for each Python stack frame. Also, extensions can call back into |
| 1323 | Python at almost random moments. Therefore a complete threads |
| 1324 | implementation requires thread support for C. |
| 1325 | |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 1326 | |
| 1327 | 7. Using Python on non-UNIX platforms |
| 1328 | ===================================== |
| 1329 | |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 1330 | 7.1. Q. Is there a Mac version of Python? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 1331 | |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 1332 | A. Yes. It is on most ftp sites carrying Python as python.sea.hqx -- |
| 1333 | this is a self-extracting archive containing the application binary as |
| 1334 | well as the Lib modules. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 1335 | |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 1336 | 7.2. Q. Is there a DOS version of Python? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 1337 | |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 1338 | A. Yes. More than one, actually: 16python.exe runs in standard DOS |
| 1339 | mode on 186 CPUs or higher; 32python.exe uses a DOS extender and only |
| 1340 | runs on a 386 or higher CPUs. Although 16python.exe does not pass the |
| 1341 | test set because test_grammar is too big for the parser, it actually |
| 1342 | has about 270 kbyte of allocatable heap space, which is sufficient for |
| 1343 | fairly large programs. 32python.exe is distributed as a tar file |
| 1344 | containing the required DOS extended and 387 emulator. Both are on |
| 1345 | most ftp sites carrying Python. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 1346 | |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame^] | 1347 | The file dosbuild.tar.gz on the standard ftp sites |
| 1348 | (e.g. ftp.cwi.nl:/pub/python/) contains rudimentary Makefiles and |
| 1349 | instructions. |
| 1350 | |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 1351 | 7.3. Q. Is there a Windows version of Python? |
| 1352 | |
| 1353 | A. Yes. Use qwpython.exe. The only problem with it: ^C |
| 1354 | unconditionally kills the entire program -- it does not raise |
| 1355 | KeyboardInterrupt. You can also run 16python.exe or 32python.exe in a |
| 1356 | "DOS box", but qwpython.exe appears to be slightly faster. |
| 1357 | |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame^] | 1358 | There aren't any Makefiles at this moment. Sorry. |
| 1359 | |
| 1360 | Using Win32s (a free NT compatibility package by Microsoft) you can |
| 1361 | also use the NT version by Mark Hammond -- the Win32s package is also |
| 1362 | in that directory (you'll need several MB of disk space to install |
| 1363 | it). See the next question. |
| 1364 | |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 1365 | 7.4. Q. Is there a Windows NT version of Python? |
| 1366 | |
| 1367 | A. Yes. Use ntpython.exe. This is for Intel CPUs. If you want a |
| 1368 | Windows user interface, use qwpython.exe. |
| 1369 | |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame^] | 1370 | Mark Hammond <MHammond@cmutual.com.au> is building a better NT port. |
| 1371 | This supports using DLLs for dynamic loading of Python modules, and |
| 1372 | includes an interface to the Microsoft Foundation Classes and a Python |
| 1373 | programming environment using it that's written mostly in Python. A |
| 1374 | prerelease (source and binaries) can be ftp'ed from |
| 1375 | ftp.cwi.nl:/pub/python/nt/ -- most mirrors will also have this. A |
| 1376 | thread module is also planned but currently low on Mark's list of |
| 1377 | priorities. |
| 1378 | |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 1379 | 7.5. Q. Is there an OS/2 version of Python? |
| 1380 | |
| 1381 | A. Yes. You can ftp it (from ftp.cwi.nl in pub/python, or from the |
| 1382 | mirror sites) as pyth_os2.zip. This contains both an executable and |
| 1383 | Makefiles for those fortunate enough to have a C compiler. |
| 1384 | |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame^] | 1385 | 7.6. Q. Is there a VMS version of Python? |
| 1386 | |
| 1387 | A. I think not (yet). This question has been asked on the list |
| 1388 | several times and I've never seen an answer. Maybe someone with a VMS |
| 1389 | C compiler could attempt a port? Probably coming up with proper |
| 1390 | Makefiles, config.h and config.c should be sufficient. If you succeed |
| 1391 | (or even if you get stuck halfway), please let me know! (Info as of |
| 1392 | 23 September 1994.) |
| 1393 | |
| 1394 | 7.7. Q. What about IBM mainframes, or other esoteric non-UNIX |
| 1395 | platforms? |
| 1396 | |
| 1397 | A. Basically, the same story as for VMS... (Info as of 23 September |
| 1398 | 1994.) |
| 1399 | |
| 1400 | For ports of Windows NT to non-Intel platforms, the normal NT port |
| 1401 | should work except you may have to use a different Makefile. |
| 1402 | (Unconfirmed suggestions from the Python list.) |
| 1403 | |
| 1404 | 7.8. Q. Where are the source or Makefiles for the non-UNIX versions? |
| 1405 | |
| 1406 | A. The standard sources can (almost) be used. See the previous |
| 1407 | questions for availability of Makefiles/projects or patches. If you |
| 1408 | find things in the standard sources that don't work on your particular |
| 1409 | platform, please let me know and I'll integrate a solution into the |
| 1410 | next release of the standard sources. If you submit a fix, please use |
| 1411 | some kind of #ifdef so as to keep the source working for other |
| 1412 | platforms. In particular, if the patch works around the availability |
| 1413 | of a particular function of header file, you should mimic the |
| 1414 | HAVE_... style used by the configure script -- you can then submit a |
| 1415 | config.h file for a particular platform so there are no absolutely |
| 1416 | platform-specific #ifdefs in the rest of the sources. |
| 1417 | |
| 1418 | 7.9. Q. What is the status and support for the non-UNIX versions? |
| 1419 | |
| 1420 | A. I don't have access to most of these platforms, so in general I am |
| 1421 | dependent on material submitted by volunteers(*). However I strive to |
| 1422 | integrate all changes needed to get it to compile on a particular |
| 1423 | platform back into the standard sources, so porting of the next |
| 1424 | version to the various non-UNIX platforms should be easy. |
| 1425 | |
| 1426 | (*) For the Macintosh, that volunteer is me. |
| 1427 | |
| 1428 | 7.10. Q. I have the PC version but it appears to be only a binary. |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 1429 | Where's the library? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 1430 | |
| 1431 | 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] | 1432 | "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] | 1433 | you can get the file pythonlib<version>.tar.gz from most ftp sites |
| 1434 | carrying Python; this is a subset of the distribution containing just |
| 1435 | those file. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 1436 | |
Guido van Rossum | 5333c5d | 1994-04-11 11:06:22 +0000 | [diff] [blame] | 1437 | Once you have installed the library, you need to point sys.path to it. |
| 1438 | Assuming the library is in C:\misc\python\lib, the following commands |
| 1439 | will point your Python interpreter to it (note the doubled backslashes |
| 1440 | -- you can also use single forward slashes instead): |
| 1441 | |
| 1442 | >>> import sys |
| 1443 | >>> sys.path.insert(0, 'C:\\misc\\python\\lib') |
| 1444 | >>> |
| 1445 | |
| 1446 | For a more permanent effect, set the environment variable PYTHONPATH, |
| 1447 | as follows (talking to a DOS prompt): |
| 1448 | |
| 1449 | C> SET PYTHONPATH=C:\misc\python\lib |
| 1450 | |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame^] | 1451 | 7.11. Q. Where's the documentation for the Mac or PC version? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 1452 | |
| 1453 | 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] | 1454 | applies to the Mac and PC versions. Where applicable, differences |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 1455 | are indicated in the text. |
| 1456 | |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame^] | 1457 | 7.12. 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] | 1458 | creating or editing programs apart from entering it interactively, and |
| 1459 | there seems to be no way to save code that was entered interactively. |
| 1460 | How do I create a Python program on the Mac (PC)? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 1461 | |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame^] | 1462 | A. Use an external editor. On the Mac, BBEdit seems to be a popular |
| 1463 | no-frills text editor. I work like this: start the interpreter; edit |
| 1464 | a module file using BBedit; import and test it in the interpreter; |
| 1465 | edit again in BBedit; then use the built-in function reload() to |
| 1466 | re-read the imported module; etc. |
Guido van Rossum | 5333c5d | 1994-04-11 11:06:22 +0000 | [diff] [blame] | 1467 | |
| 1468 | Regarding the same question for the PC, Kurt Wm. Hemr writes: "While |
| 1469 | anyone with a pulse could certainly figure out how to do the same on |
| 1470 | MS-Windows, I would recommend the NotGNU Emacs clone for MS-Windows. |
| 1471 | Not only can you easily resave and "reload()" from Python after making |
| 1472 | changes, but since WinNot auto-copies to the clipboard any text you |
| 1473 | select, you can simply select the entire procedure (function) which |
| 1474 | you changed in WinNot, switch to QWPython, and shift-ins to reenter |
| 1475 | the changed program unit." |