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