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