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