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