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) |
Guido van Rossum | 7be34a8 | 1995-05-31 15:17:12 +0000 | [diff] [blame] | 5 | Reply-to: guido@cnri.reston.va.us (Guido van Rossum) |
Guido van Rossum | 05151e0 | 1995-09-28 13:24:46 +0000 | [diff] [blame^] | 6 | Expires: Wed, 1 Nov 1995 00:00:00 GMT |
| 7 | Supersedes: <DE1DI9.8MF@cwi.nl> |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 8 | Approved: news-answers-request@MIT.Edu |
| 9 | |
| 10 | Archive-name: python-faq/part1 |
Guido van Rossum | 0777935 | 1995-02-07 16:59:56 +0000 | [diff] [blame] | 11 | Submitted-by: Guido van Rossum <guido@cwi.nl> |
Guido van Rossum | 05151e0 | 1995-09-28 13:24:46 +0000 | [diff] [blame^] | 12 | Version: 1.28 |
| 13 | Last-modified: 28 September 1995 |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 14 | |
| 15 | This article contains answers to Frequently Asked Questions about |
| 16 | Python (an object-oriented interpreted programming language -- see |
| 17 | the answer to question 1.1 for a short overview). |
| 18 | |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 19 | Copyright 1993-1995 Guido van Rossum. Unchanged electronic |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 20 | redistribution of this FAQ is allowed. Printed redistribution only |
| 21 | with permission of the author. No warranties. |
| 22 | |
| 23 | Author's address: |
Guido van Rossum | b0a2ce5 | 1995-08-28 19:41:35 +0000 | [diff] [blame] | 24 | Guido van Rossum |
| 25 | C.N.R.I. |
| 26 | 1895 Preston White Drive |
| 27 | Reston, VA 22091 |
| 28 | U.S.A. |
| 29 | Email: <guido@python.org>, <guido@cnri.reston.va.us>, <guido@cwi.nl> |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 30 | |
| 31 | The latest version of this FAQ is available by anonymous ftp from |
Guido van Rossum | 7be34a8 | 1995-05-31 15:17:12 +0000 | [diff] [blame] | 32 | <URL:ftp://ftp.python.org/pub/python/FAQ>. It will also be posted |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 33 | regularly to the newsgroups comp.answers <URL:news:comp.answers> and |
| 34 | comp.lang.python <URL:news:comp.lang.python>. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 35 | |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 36 | Many FAQs, including this one, are available by anonymous ftp |
| 37 | <URL:ftp://rtfm.mit.edu/pub/usenet/news.answers/>. The name under |
| 38 | which a FAQ is archived appears in the Archive-name line at the top of |
| 39 | the article. This FAQ is archived as python-faq/part1 |
| 40 | <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] | 41 | |
| 42 | There's a mail server on that machine which will send you files from |
| 43 | 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] | 44 | message to <mail-server@rtfm.mit.edu> containing the single word help |
| 45 | in the message body to receive instructions. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 46 | |
| 47 | This FAQ is divided in the following chapters: |
| 48 | |
| 49 | 1. General information and availability |
| 50 | 2. Python in the real world |
Guido van Rossum | b0a2ce5 | 1995-08-28 19:41:35 +0000 | [diff] [blame] | 51 | 3. Building Python and Other Known Bugs |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 52 | 4. Programming in Python |
| 53 | 5. Extending Python |
| 54 | 6. Python's design |
| 55 | 7. Using Python on non-UNIX platforms |
| 56 | |
| 57 | To find the start of a particular chapter, search for the chapter number |
| 58 | followed by a dot and a space at the beginning of a line (e.g. to |
| 59 | find chapter 4 in vi, type /^4\. /). |
| 60 | |
| 61 | Here's an overview of the questions per chapter: |
| 62 | |
| 63 | 1. General information and availability |
| 64 | 1.1. Q. What is Python? |
| 65 | 1.2. Q. Why is it called Python? |
| 66 | 1.3. Q. How do I obtain a copy of the Python source? |
| 67 | 1.4. Q. How do I get documentation on Python? |
Guido van Rossum | c50158e | 1994-05-31 09:18:50 +0000 | [diff] [blame] | 68 | 1.5. Q. Are there other ftp sites that mirror the Python distribution? |
| 69 | 1.6. Q. Is there a newsgroup or mailing list devoted to Python? |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 70 | 1.7. Q. Is there a WWW page devoted to Python? |
Guido van Rossum | a8a8d4a | 1995-03-10 16:19:31 +0000 | [diff] [blame] | 71 | 1.8. Q. Is the Python documentation available on the WWW? |
| 72 | 1.9. Q. Is there a book on Python, or will there be one out soon? |
| 73 | 1.10. Q. Are there any published articles about Python that I can quote? |
| 74 | 1.11. Q. Are there short introductory papers or talks on Python? |
| 75 | 1.12. Q. How does the Python version numbering scheme work? |
| 76 | 1.13. Q. How do I get a beta test version of Python? |
Guido van Rossum | 05151e0 | 1995-09-28 13:24:46 +0000 | [diff] [blame^] | 77 | 1.14. Q. Are there copyright restrictions on the use of Python? |
| 78 | 1.15. Q. Why was Python created in the first place? |
| 79 | 1.16. Q. What happened to Tim Peters (the author of python-mode.el, and many |
Guido van Rossum | 7be34a8 | 1995-05-31 15:17:12 +0000 | [diff] [blame] | 80 | entertaining and enlightening pieces of Python email)? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 81 | |
| 82 | 2. Python in the real world |
| 83 | 2.1. Q. How many people are using Python? |
| 84 | 2.2. Q. Have any significant projects been done in Python? |
| 85 | 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] | 86 | 2.4. Q. How stable is Python? |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 87 | 2.5. Q. When will the next version be released? |
| 88 | 2.6. Q. What new developments are expected for Python in the future? |
| 89 | 2.7. Q. Is it reasonable to propose incompatible changes to Python? |
Guido van Rossum | b0a2ce5 | 1995-08-28 19:41:35 +0000 | [diff] [blame] | 90 | 2.8. Q. What is the future of Python? |
Guido van Rossum | 05151e0 | 1995-09-28 13:24:46 +0000 | [diff] [blame^] | 91 | 2.9. Q. What is the PSA, anyway? |
| 92 | 2.10. Q. How do I join the PSA? |
| 93 | 2.11. Q. What are the benefits of joining the PSA? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 94 | |
Guido van Rossum | b0a2ce5 | 1995-08-28 19:41:35 +0000 | [diff] [blame] | 95 | 3. Building Python and Other Known Bugs |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 96 | 3.1. Q. Is there a test set? |
| 97 | 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] | 98 | operations, but when playing with floating point operations I cannot |
| 99 | find anything wrong with them. |
Guido van Rossum | 7be34a8 | 1995-05-31 15:17:12 +0000 | [diff] [blame] | 100 | 3.3. Q. Link errors after rerunning the configure script. |
| 101 | 3.4. Q. The python interpreter complains about options passed to a |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 102 | script (after the script name). |
Guido van Rossum | 7be34a8 | 1995-05-31 15:17:12 +0000 | [diff] [blame] | 103 | 3.5. 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] | 104 | glmodule.c, but python hasn't been built or installed yet. |
Guido van Rossum | 7be34a8 | 1995-05-31 15:17:12 +0000 | [diff] [blame] | 105 | 3.6. Q. Python built with gcc for the DEC Alpha doesn't work. |
| 106 | 3.7. Q. I use VPATH but some targets are built in the source directory. |
| 107 | 3.8. Q. Trouble building or linking with the GNU readline library. |
| 108 | 3.9. Q. Trouble building Python on Linux. |
| 109 | 3.10. Q. Trouble with prototypes on Ultrix. |
| 110 | 3.11. Q. Trouble with posix.listdir on NeXTSTEP 3.2. |
| 111 | 3.12. Q. Other trouble building Python on platform X. |
| 112 | 3.13. Q. How to configure dynamic loading on Lixux. |
Guido van Rossum | 3fc9d73 | 1995-07-25 15:10:56 +0000 | [diff] [blame] | 113 | 3.14. Q. Under Solaris 2.x, using GCC, how do I use shared libraries? |
| 114 | 3.15. Q. Errors when linking with a shared library containing C++ code. |
Guido van Rossum | 05151e0 | 1995-09-28 13:24:46 +0000 | [diff] [blame^] | 115 | 3.16. Q. Tk menus or radiobuttons don't work properly in Python 1.2. |
| 116 | 3.17. Q. I built with tkintermodule.c enabled but get "Tkinter not found". |
| 117 | 3.18. Q. I built with Tk 4.0 but Tkinter complains about the Tk version. |
| 118 | 3.19. Q. Tk doesn't work right on DEC Alpha. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 119 | |
| 120 | 4. Programming in Python |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 121 | 4.1. Q. Is there a source code level debugger with breakpoints, step, |
| 122 | etc.? |
| 123 | 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] | 124 | C and others in Python (e.g. through inheritance)? (Also phrased as: |
| 125 | Can I use a built-in type as base class?) |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 126 | 4.3. Q. Is there a curses/termcap package for Python? |
| 127 | 4.4. Q. Is there an equivalent to C's onexit() in Python? |
| 128 | 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] | 129 | nested function seemingly can't access the local variables of the |
| 130 | outer function. What is going on? How do I pass local data to a |
| 131 | nested function? |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 132 | 4.6. Q. How do I iterate over a sequence in reverse order? |
| 133 | 4.7. Q. My program is too slow. How do I speed it up? |
| 134 | 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] | 135 | again (into the same Python process), the changes don't seem to take |
| 136 | place. What is going on? |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 137 | 4.9. Q. How do I find the current module name? |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 138 | 4.10. Q. I have a module in which I want to execute some extra code |
| 139 | when it is run as a script. How do I find out whether I am running as |
| 140 | a script? |
| 141 | 4.11. Q. I try to run a program from the Demo directory but it fails |
| 142 | with ImportError: No module named ...; what gives? |
Guido van Rossum | 061f182 | 1994-10-06 16:03:45 +0000 | [diff] [blame] | 143 | 4.12. Q. I have successfully built Python with STDWIN but it can't |
| 144 | find some modules (e.g. stdwinevents). |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 145 | 4.13. Q. What GUI toolkits exist for Python? |
Guido van Rossum | 061f182 | 1994-10-06 16:03:45 +0000 | [diff] [blame] | 146 | 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] | 147 | 4.15. Q. Is it possible to write obfuscated one-liners in Python? |
| 148 | 4.16. Q. Is there an equivalent of C's "?:" ternary operator? |
| 149 | 4.17. Q. My class defines __del__ but it is not called when I delete the |
| 150 | object. |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 151 | 4.18. Q. How do I change the shell environment for programs called |
| 152 | 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] | 153 | 4.19. Q. What is a class? |
| 154 | 4.20. Q. What is a method? |
| 155 | 4.21. Q. What is self? |
| 156 | 4.22. Q. What is a unbound method? |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 157 | 4.23. Q. How do I call a method defined in a base class from a derived |
| 158 | class that overrides it? |
| 159 | 4.24. Q. How do I call a method from a base class without using the |
| 160 | name of the base class? |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 161 | 4.25. Q. How can I organize my code to make it easier to change the base |
| 162 | class? |
| 163 | 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] | 164 | 4.27. Q. I can't seem to use os.read() on a pipe created with os.popen(). |
| 165 | 4.28. Q. How can I create a stand-alone binary from a Python script? |
Guido van Rossum | 7be34a8 | 1995-05-31 15:17:12 +0000 | [diff] [blame] | 166 | 4.29. Q. What WWW tools are there for Python? |
| 167 | 4.30. Q. How do I run a subprocess with pipes connected to both input |
Guido van Rossum | 796b259 | 1995-01-20 23:05:52 +0000 | [diff] [blame] | 168 | and output? |
Guido van Rossum | 7be34a8 | 1995-05-31 15:17:12 +0000 | [diff] [blame] | 169 | 4.31. Q. How do I call a function if I have the arguments in a tuple? |
| 170 | 4.32. Q. How do I enable font-lock-mode for Python in Emacs? |
| 171 | 4.33. Q. Is there an inverse to the format operator (a la C's scanf())? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 172 | |
| 173 | 5. Extending Python |
| 174 | 5.1. Q. Can I create my own functions in C? |
| 175 | 5.2. Q. Can I create my own functions in C++? |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 176 | 5.3. Q. How can I execute arbitrary Python statements from C? |
| 177 | 5.4. Q. How can I evaluate an arbitrary Python expression from C? |
| 178 | 5.5. Q. How do I extract C values from a Python object? |
| 179 | 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] | 180 | 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] | 181 | Extensions manual? |
| 182 | 5.8. Q. How do I call an object's method from C? |
| 183 | 5.9. Q. How do I catch the output from print_error()? |
| 184 | 5.10. Q. How do I access a module written in Python from C? |
Guido van Rossum | b0a2ce5 | 1995-08-28 19:41:35 +0000 | [diff] [blame] | 185 | 5.11. Q. How do I interface to C++ objects from Python? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 186 | |
| 187 | 6. Python's design |
Guido van Rossum | b0a2ce5 | 1995-08-28 19:41:35 +0000 | [diff] [blame] | 188 | 6.1. Q. Why isn't there a generic copying operation for objects in Python? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 189 | 6.2. Q. Why isn't there a generic way to implement persistent objects |
| 190 | in Python? (Persistent == automatically saved to and restored from |
| 191 | disk.) |
| 192 | 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] | 193 | 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] | 194 | 6.5. Q. Why are Python strings immutable? |
| 195 | 6.6. Q. Why don't strings have methods like index() or sort(), like |
| 196 | lists? |
| 197 | 6.7. Q. Why does Python use methods for some functionality |
| 198 | (e.g. list.index()) but functions for other (e.g. len(list))? |
| 199 | 6.8. Q. Why can't I derive a class from built-in types (e.g. lists or |
| 200 | files)? |
| 201 | 6.9. Q. Why must 'self' be declared and used explicitly in method |
| 202 | definitions and calls? |
Guido van Rossum | 061f182 | 1994-10-06 16:03:45 +0000 | [diff] [blame] | 203 | 6.10. Q. Can't you emulate threads in the interpreter instead of |
| 204 | relying on an OS-specific thread implementation? |
| 205 | 6.11. Q. Why can't lambda forms contain statements? |
| 206 | 6.12. Q. Why is there no more efficient way of iterating over a dictionary |
| 207 | than first constructing the list of keys()? |
Guido van Rossum | b0a2ce5 | 1995-08-28 19:41:35 +0000 | [diff] [blame] | 208 | 6.13. Q. Can Python be compiled to machine code, C or some other language? |
Guido van Rossum | 05151e0 | 1995-09-28 13:24:46 +0000 | [diff] [blame^] | 209 | 6.14. Q. Why doesn't Python use proper garbage collection? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 210 | |
| 211 | 7. Using Python on non-UNIX platforms |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 212 | 7.1. Q. Is there a Mac version of Python? |
| 213 | 7.2. Q. Is there a DOS version of Python? |
Guido van Rossum | e530c58 | 1995-04-10 12:32:16 +0000 | [diff] [blame] | 214 | 7.3. Q. Is there a Windows 3.1(1) version of Python? |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 215 | 7.4. Q. Is there a Windows NT version of Python? |
Guido van Rossum | e530c58 | 1995-04-10 12:32:16 +0000 | [diff] [blame] | 216 | 7.5. Q. Is there a Windows 95 version of Python? |
| 217 | 7.6. Q. Is there an OS/2 version of Python? |
| 218 | 7.7. Q. Is there a VMS version of Python? |
Guido van Rossum | bf8e7d5 | 1995-08-28 03:09:13 +0000 | [diff] [blame] | 219 | 7.8. Q. What about IBM mainframes, or other non-UNIX platforms? |
Guido van Rossum | e530c58 | 1995-04-10 12:32:16 +0000 | [diff] [blame] | 220 | 7.9. Q. Where are the source or Makefiles for the non-UNIX versions? |
| 221 | 7.10. Q. What is the status and support for the non-UNIX versions? |
| 222 | 7.11. Q. I have a PC version but it appears to be only a binary. |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 223 | Where's the library? |
Guido van Rossum | e530c58 | 1995-04-10 12:32:16 +0000 | [diff] [blame] | 224 | 7.12. Q. Where's the documentation for the Mac or PC version? |
| 225 | 7.13. Q. The Mac (PC) version doesn't seem to have any facilities for |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 226 | creating or editing programs apart from entering it interactively, and |
| 227 | there seems to be no way to save code that was entered interactively. |
| 228 | How do I create a Python program on the Mac (PC)? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 229 | |
| 230 | To find a particular question, search for the question number followed |
| 231 | by a dot, a space, and a Q at the beginning of a line (e.g. to find |
| 232 | question 4.2 in vi, type /^4\.2\. Q/). |
| 233 | |
| 234 | |
| 235 | 1. General information and availability |
Guido van Rossum | b0a2ce5 | 1995-08-28 19:41:35 +0000 | [diff] [blame] | 236 | ======================================= |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 237 | |
| 238 | 1.1. Q. What is Python? |
| 239 | |
| 240 | A. Python is an interpreted, interactive, object-oriented programming |
| 241 | language. It incorporates modules, exceptions, dynamic typing, very |
| 242 | high level dynamic data types, and classes. Python combines |
| 243 | remarkable power with very clear syntax. It has interfaces to many |
| 244 | system calls and libraries, as well as to various window systems, and |
| 245 | is extensible in C or C++. It is also usable as an extension language |
| 246 | for applications that need a programmable interface. Finally, Python |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 247 | is portable: it runs on many brands of UNIX, on the Mac, and on PCs |
| 248 | under MS-DOS, Windows, Windows NT, and OS/2. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 249 | |
| 250 | To find out more, the best thing to do is to start reading the |
| 251 | tutorial from the documentation set (see a few questions further |
| 252 | down). |
| 253 | |
| 254 | 1.2. Q. Why is it called Python? |
| 255 | |
Guido van Rossum | 796b259 | 1995-01-20 23:05:52 +0000 | [diff] [blame] | 256 | 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] | 257 | 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] | 258 | the -- unlikely -- case you didn't know). It occurred to me one day |
| 259 | that I needed a name that was short, unique, and slightly mysterious. |
| 260 | And I happened to be reading some scripts from the series at the |
| 261 | time... So then I decided to call my language Python. But Python is |
| 262 | 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] | 263 | either! (If you need an icon, use an image of the 16-ton weight from |
| 264 | the TV series or of a can of SPAM :-) |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 265 | |
| 266 | 1.3. Q. How do I obtain a copy of the Python source? |
| 267 | |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 268 | A. The latest complete Python source distribution is always available |
| 269 | by anonymous ftp, e.g. |
Guido van Rossum | 7be34a8 | 1995-05-31 15:17:12 +0000 | [diff] [blame] | 270 | <URL:ftp://ftp.python.org/pub/python/src/python1.2.tar.gz>. It is a |
Guido van Rossum | e530c58 | 1995-04-10 12:32:16 +0000 | [diff] [blame] | 271 | gzipped tar file containing the complete C source, LaTeX |
| 272 | documentation, Python library modules, example programs, and several |
| 273 | useful pieces of freely distributable software. This will compile and |
| 274 | run out of the box on most UNIX platforms. (See section 7 for |
| 275 | non-UNIX information.) |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 276 | |
| 277 | Occasionally a set of patches is issued which has to be applied using |
| 278 | the patch program. These patches are placed in the same directory, |
Guido van Rossum | 7be34a8 | 1995-05-31 15:17:12 +0000 | [diff] [blame] | 279 | e.g. <URL:ftp://ftp.python.org/pub/python/src/patch1.1.1>. (At the time |
Guido van Rossum | e530c58 | 1995-04-10 12:32:16 +0000 | [diff] [blame] | 280 | of writing, no patches exist for 1.2.) |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 281 | |
| 282 | An index of said ftp directory can be found in the file INDEX. An |
| 283 | HTML version of the index can be found in the file index.html, |
Guido van Rossum | 7be34a8 | 1995-05-31 15:17:12 +0000 | [diff] [blame] | 284 | <URL:ftp://ftp.python.org/pub/python/index.html>. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 285 | |
| 286 | 1.4. Q. How do I get documentation on Python? |
| 287 | |
Guido van Rossum | a8a8d4a | 1995-03-10 16:19:31 +0000 | [diff] [blame] | 288 | A. The LaTeX source for the documentation is part of the source |
| 289 | distribution. If you don't have LaTeX, the latest Python |
| 290 | documentation set is always available by anonymous ftp, e.g. |
Guido van Rossum | 7be34a8 | 1995-05-31 15:17:12 +0000 | [diff] [blame] | 291 | <URL:ftp://ftp.python.org/pub/python/doc/postscript.tar.gz>. It is a |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 292 | gzipped tar file containing PostScript files of the reference manual, |
Guido van Rossum | 3de2736 | 1994-07-25 14:19:33 +0000 | [diff] [blame] | 293 | the library manual, and the tutorial. Note that the library manual is |
| 294 | the most important one of the set, as much of Python's power stems |
| 295 | from the standard or built-in types, functions and modules, all of |
| 296 | which are described here. PostScript for a high-level description of |
Guido van Rossum | a8a8d4a | 1995-03-10 16:19:31 +0000 | [diff] [blame] | 297 | Python is in the file nluug-paper.ps (a separate file on the ftp |
| 298 | site). |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 299 | |
Guido van Rossum | c50158e | 1994-05-31 09:18:50 +0000 | [diff] [blame] | 300 | 1.5. Q. Are there other ftp sites that mirror the Python distribution? |
| 301 | |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 302 | A. The following anonymous ftp sites keep mirrors of the Python |
| 303 | distribution: |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 304 | |
Guido van Rossum | b0a2ce5 | 1995-08-28 19:41:35 +0000 | [diff] [blame] | 305 | <URL:ftp://ftp.cwi.nl/pub/python/> |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 306 | <URL:ftp://gatekeeper.dec.com/pub/plan/python/> |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 307 | <URL:ftp://ftp.wustl.edu/graphics/graphics/sgi-stuff/python/> |
Guido van Rossum | 05151e0 | 1995-09-28 13:24:46 +0000 | [diff] [blame^] | 308 | <URL:ftp://ftp.uu.net/languages/python/> |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 309 | <URL:ftp://ftp.funet.fi/pub/languages/python/> |
| 310 | <URL:ftp://ftp.sunet.se/pub/lang/python/> |
Guido van Rossum | 3fc9d73 | 1995-07-25 15:10:56 +0000 | [diff] [blame] | 311 | <URL:ftp://unix.hensa.ac.uk/mirrors/uunet/languages/python/> |
| 312 | <URL:ftp://ftp.sterling.com/programming/languages/python/> |
Guido van Rossum | 05151e0 | 1995-09-28 13:24:46 +0000 | [diff] [blame^] | 313 | <URL:ftp://uiarchive.cso.uiuc.edu/pub/lang/python/> |
Guido van Rossum | b0a2ce5 | 1995-08-28 19:41:35 +0000 | [diff] [blame] | 314 | <URL:ftp://ftp.ibp.fr/pub/python/> |
Guido van Rossum | 3fc9d73 | 1995-07-25 15:10:56 +0000 | [diff] [blame] | 315 | <URL:ftp://ftp.switch.ch/software/sources/python/> |
| 316 | <URL:ftp://ftp.informatik.tu-muenchen.de/pub/comp/programming/languages/python/> |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 317 | |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 318 | Or try archie on the string "python". |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 319 | |
Guido van Rossum | c50158e | 1994-05-31 09:18:50 +0000 | [diff] [blame] | 320 | 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] | 321 | |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 322 | A. There is a newsgroup, comp.lang.python <URL:news:comp.lang.python>, |
| 323 | and a mailing list. The newsgroup and mailing list are gatewayed into |
| 324 | each other -- if you can read news it's unnecessary to subscribe to |
| 325 | the mailing list. Send e-mail to <python-list-request@cwi.nl> to |
Guido van Rossum | e530c58 | 1995-04-10 12:32:16 +0000 | [diff] [blame] | 326 | (un)subscribe to the mailing list. Hypermail archives of (nearly) |
| 327 | everything posted to the mailing list (and thus the newsgroup) are |
| 328 | available on our WWW server, |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 329 | <URL:http://www.cwi.nl/~guido/hypermail/index.html>. The raw archives |
| 330 | are also available by ftp, e.g. |
Guido van Rossum | 7be34a8 | 1995-05-31 15:17:12 +0000 | [diff] [blame] | 331 | <URL:ftp://ftp.python.org/pub/python/mail/mailinglist.gz>. The |
Guido van Rossum | e530c58 | 1995-04-10 12:32:16 +0000 | [diff] [blame] | 332 | uncompressed versions of these files can be read with the standard |
| 333 | UNIX Mail program ("Mail -f file") or with nn ("nn file"). To read |
Guido van Rossum | bf8e7d5 | 1995-08-28 03:09:13 +0000 | [diff] [blame] | 334 | them using MH, you could use "inc -file file". (The archival service |
| 335 | has stopped archiving new articles around the end of April 1995. I |
| 336 | hope to revive it on the PSA server www.python.org sometime in the |
| 337 | future.) |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 338 | |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 339 | 1.7. Q. Is there a WWW page devoted to Python? |
| 340 | |
Guido van Rossum | 7be34a8 | 1995-05-31 15:17:12 +0000 | [diff] [blame] | 341 | A. Yes, <URL:http://www.python.org/> is the official Python home page. |
| 342 | At the time of writing, this page is not yet completely operational; |
| 343 | you may have a look at the old Python home page: |
| 344 | <URL:http://www.cwi.nl/~guido/Python.html> or at the U.S. copy: |
| 345 | <URL:http://www.python.org/~guido/Python.html>. |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 346 | |
Guido van Rossum | a8a8d4a | 1995-03-10 16:19:31 +0000 | [diff] [blame] | 347 | 1.8. Q. Is the Python documentation available on the WWW? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 348 | |
Guido van Rossum | 7be34a8 | 1995-05-31 15:17:12 +0000 | [diff] [blame] | 349 | A. Yes, see <URL:http://www.python.org/> (Python's home page). It |
| 350 | contains pointers to hypertext versions of the whole documentation set |
| 351 | (as hypertext, not just PostScript). |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 352 | |
Guido van Rossum | a8a8d4a | 1995-03-10 16:19:31 +0000 | [diff] [blame] | 353 | If you wish to browse this collection of HTML files on your own |
| 354 | machine, it is available bundled up by anonymous ftp, |
Guido van Rossum | 7be34a8 | 1995-05-31 15:17:12 +0000 | [diff] [blame] | 355 | e.g. <URL:ftp://ftp.python.org/pub/python/doc/html.tar.gz>. |
Guido van Rossum | a8a8d4a | 1995-03-10 16:19:31 +0000 | [diff] [blame] | 356 | |
| 357 | An Emacs-INFO set containing the library manual is also available by |
Guido van Rossum | 7be34a8 | 1995-05-31 15:17:12 +0000 | [diff] [blame] | 358 | ftp, e.g. <URL:ftp://ftp.python.org/pub/python/doc/lib-info.tar.gz>. |
Guido van Rossum | a8a8d4a | 1995-03-10 16:19:31 +0000 | [diff] [blame] | 359 | |
| 360 | 1.9. Q. Is there a book on Python, or will there be one out soon? |
| 361 | |
Guido van Rossum | 3fc9d73 | 1995-07-25 15:10:56 +0000 | [diff] [blame] | 362 | A. Mark Lutz is writing a Python book for O'Reilly and Associates, to |
| 363 | be published early 1996. See the outline (in PostScript): |
| 364 | <URL:http://www.python.org/workshops/1995-05/outlinep.eps>. |
Guido van Rossum | a8a8d4a | 1995-03-10 16:19:31 +0000 | [diff] [blame] | 365 | |
| 366 | 1.10. 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] | 367 | |
| 368 | A. So far the only refereed and published article that describes |
| 369 | Python in some detail is: |
| 370 | |
| 371 | Guido van Rossum and Jelke de Boer, "Interactively Testing Remote |
| 372 | Servers Using the Python Programming Language", CWI Quarterly, Volume |
| 373 | 4, Issue 4 (December 1991), Amsterdam, pp 283-303. |
| 374 | |
| 375 | LaTeX source for this paper is available as part of the Python source |
| 376 | distribution. |
| 377 | |
Guido van Rossum | 05151e0 | 1995-09-28 13:24:46 +0000 | [diff] [blame^] | 378 | See also the next section (supposedly Aaron Watters' paper has been |
| 379 | refereed). |
| 380 | |
Guido van Rossum | a8a8d4a | 1995-03-10 16:19:31 +0000 | [diff] [blame] | 381 | 1.11. Q. Are there short introductory papers or talks on Python? |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 382 | |
Guido van Rossum | 05151e0 | 1995-09-28 13:24:46 +0000 | [diff] [blame^] | 383 | A. A recent, very entertaining introduction to Python is the tutorial by |
| 384 | Aaron Watters in UnixWorld Online: |
| 385 | |
| 386 | Aaron R. Watters: "The What, Why, Who, and Where of Python", |
| 387 | <URL:http://www.wcmh.com/uworld/archives/95/tutorial/005.html> |
| 388 | |
| 389 | An olded paper is: |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 390 | |
| 391 | Guido van Rossum, "An Introduction to Python for UNIX/C |
| 392 | Programmers", in the proceedings of the NLUUG najaarsconferentie |
Guido van Rossum | 061f182 | 1994-10-06 16:03:45 +0000 | [diff] [blame] | 393 | 1993 (dutch UNIX users group meeting November 1993). |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 394 | |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 395 | PostScript for this paper and for the slides used for the accompanying |
| 396 | presentation is available by ftp as |
Guido van Rossum | 7be34a8 | 1995-05-31 15:17:12 +0000 | [diff] [blame] | 397 | <URL:ftp://ftp.python.org/pub/python/doc/nluug-paper.ps> and |
| 398 | <URL:ftp://ftp.python.org/pub/python/doc/nluug-slides.ps>, respectively. |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 399 | |
| 400 | Slides for a talk on Python that I gave at the Usenix Symposium on |
Guido van Rossum | 05151e0 | 1995-09-28 13:24:46 +0000 | [diff] [blame^] | 401 | Very High Level Languages in Santa Fe, NM, USA in October 1994 are |
Guido van Rossum | 7be34a8 | 1995-05-31 15:17:12 +0000 | [diff] [blame] | 402 | available as <URL:ftp://ftp.python.org/pub/python/doc/vhll-slides.ps>. |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 403 | |
Guido van Rossum | a8a8d4a | 1995-03-10 16:19:31 +0000 | [diff] [blame] | 404 | 1.12. Q. How does the Python version numbering scheme work? |
Guido van Rossum | 95f61a7 | 1994-01-26 17:23:37 +0000 | [diff] [blame] | 405 | |
Guido van Rossum | 05151e0 | 1995-09-28 13:24:46 +0000 | [diff] [blame^] | 406 | A. Python versions are numbered A.B.C or A.B. A is the major version |
| 407 | number -- it is only incremented for major changes in functionality or |
| 408 | source structure. B is the minor version number, incremented for less |
Guido van Rossum | 95f61a7 | 1994-01-26 17:23:37 +0000 | [diff] [blame] | 409 | earth-shattering changes to a release. C is the patchlevel -- it is |
Guido van Rossum | 05151e0 | 1995-09-28 13:24:46 +0000 | [diff] [blame^] | 410 | incremented for each new patch release. Not all releases have patch |
| 411 | releases. Note that in the past, patches have added significant |
| 412 | changes; in fact the changeover from 0.9.9 to 1.0.0 was the first time |
| 413 | that either A or B changed! |
Guido van Rossum | 95f61a7 | 1994-01-26 17:23:37 +0000 | [diff] [blame] | 414 | |
Guido van Rossum | a8a8d4a | 1995-03-10 16:19:31 +0000 | [diff] [blame] | 415 | Beta versions have an additional suffix of "-beta-N" for some small |
| 416 | number N. Note that (for instance) all versions labeled 1.2-beta-N |
Guido van Rossum | 05151e0 | 1995-09-28 13:24:46 +0000 | [diff] [blame^] | 417 | *precede* the actual release of 1.2. 1.3b1 is short for 1.3-beta-1. |
Guido van Rossum | a8a8d4a | 1995-03-10 16:19:31 +0000 | [diff] [blame] | 418 | |
| 419 | 1.13. Q. How do I get a beta test version of Python? |
| 420 | |
Guido van Rossum | 05151e0 | 1995-09-28 13:24:46 +0000 | [diff] [blame^] | 421 | A. If there are any beta releases, they are published in the normal |
| 422 | source directory (e.g. <URL:ftp://ftp.python.org/pub/python/src/>). |
Guido van Rossum | a8a8d4a | 1995-03-10 16:19:31 +0000 | [diff] [blame] | 423 | |
Guido van Rossum | 05151e0 | 1995-09-28 13:24:46 +0000 | [diff] [blame^] | 424 | 1.14. Q. Are there copyright restrictions on the use of Python? |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 425 | |
| 426 | 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] | 427 | you leave the copyrights in, and display those copyrights in any |
| 428 | documentation about Python that you produce. Also, don't use the |
| 429 | author's institute's name in publicity without prior written |
| 430 | permission, and don't hold them responsible for anything (read the |
| 431 | actual copyright for a precise legal wording). |
| 432 | |
| 433 | In particular, if you honor the copyright rules, it's OK to use Python |
| 434 | for commercial use, to sell copies of Python in source or binary form, |
| 435 | or to sell products that enhance Python or incorporate Python (or part |
| 436 | of it) in some form. I would still like to know about all commercial |
| 437 | use of Python! |
Guido van Rossum | 5333c5d | 1994-04-11 11:06:22 +0000 | [diff] [blame] | 438 | |
Guido van Rossum | 05151e0 | 1995-09-28 13:24:46 +0000 | [diff] [blame^] | 439 | 1.15. Q. Why was Python created in the first place? |
Guido van Rossum | 7be34a8 | 1995-05-31 15:17:12 +0000 | [diff] [blame] | 440 | |
| 441 | A. Here's a *very* brief summary of what got me started: |
| 442 | |
| 443 | - I had extensive experience with implementing an interpreted language |
| 444 | in the ABC group at CWI, and from working with this group I had |
| 445 | learned a lot about language design. This is the origin of many |
| 446 | Python features, including the use of indentation for statement |
| 447 | grouping and the inclusion of very-high-level data types (although the |
| 448 | details are all different in Python). |
| 449 | |
| 450 | - I had a number of gripes about the ABC language, but also liked many |
| 451 | of its features. It was impossible to extend the ABC language (or its |
| 452 | implementation) to remedy my complaints -- in fact its lack of |
| 453 | extensibility was one of its biggest problems. |
| 454 | |
| 455 | - I had some experience with using Modula-2+ and talked with the |
| 456 | designers of Modula-3 (and read the M3 report). M3 is the origin of |
| 457 | the syntax and semantics used for exceptions, and some other Python |
| 458 | features. |
| 459 | |
| 460 | - I was working in the Amoeba distributed operating system group at |
| 461 | CWI. We needed a better way to do system administration than by |
| 462 | writing either C programs or Bourne shell scripts, since Amoeba had |
| 463 | its own system call interface which wasn't easily accessible from the |
Guido van Rossum | bf8e7d5 | 1995-08-28 03:09:13 +0000 | [diff] [blame] | 464 | Bourne shell. My experience with error handling in Amoeba made me |
| 465 | acutely aware of the importance of exceptions as a programming |
| 466 | language feature. |
Guido van Rossum | 7be34a8 | 1995-05-31 15:17:12 +0000 | [diff] [blame] | 467 | |
| 468 | - It occurred to me that a scripting language with a syntax like ABC |
| 469 | but with access to the Amoeba system calls would fill the need. I |
| 470 | realized that it would be foolish to write an Amoeba-specific |
| 471 | language, so I decided that I needed a language that was generally |
| 472 | extensible. |
| 473 | |
| 474 | - During the 1989 Christmas holidays, I had a lot of time on my hand, |
| 475 | so I decided to give it a try. During the next year, while still |
| 476 | mostly working on it in my own time, Python was used in the Amoeba |
| 477 | project with increasing success, and the feedback from colleagues made |
| 478 | me add many early improvements. |
| 479 | |
| 480 | - In February 1991, after just over a year of development, I decided |
| 481 | to post to USENET. The rest is in the Misc/HISTORY file. |
| 482 | |
Guido van Rossum | 05151e0 | 1995-09-28 13:24:46 +0000 | [diff] [blame^] | 483 | 1.16. Q. What happened to Tim Peters (the author of python-mode.el, and many |
Guido van Rossum | 7be34a8 | 1995-05-31 15:17:12 +0000 | [diff] [blame] | 484 | entertaining and enlightening pieces of Python email)? |
| 485 | |
| 486 | A. He worked at KSR (Kendall Square Research, a start-up building a |
| 487 | new kind of massively parallel processor). When KSR folded down a |
| 488 | couple of years ago, Tim lost his email access. He hasn't surfaced |
| 489 | on the net since then. |
| 490 | |
| 491 | Missing-him-too-ly yours... |
| 492 | |
Guido van Rossum | b0a2ce5 | 1995-08-28 19:41:35 +0000 | [diff] [blame] | 493 | PS: support for Python's Emacs mode (Misc/python-mode.el in the |
| 494 | distribution) has been taken up by Barry Warsaw. Questions about it |
| 495 | should be mailed to <python-mode@python.org>. |
| 496 | |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 497 | |
| 498 | 2. Python in the real world |
| 499 | =========================== |
| 500 | |
| 501 | 2.1. Q. How many people are using Python? |
| 502 | |
Guido van Rossum | 5333c5d | 1994-04-11 11:06:22 +0000 | [diff] [blame] | 503 | A. I don't know, but the maximum number of simultaneous subscriptions |
| 504 | to the Python mailing list before it was gatewayed into the newsgroup |
| 505 | was about 180 (several of which were local redistribution lists). I |
| 506 | believe that many active Python users don't bother to subscribe to the |
| 507 | 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] | 508 | is even less meaningful. I see new names on the newsgroup all the |
| 509 | time and my best guess is that there are currently at least several |
| 510 | thousands of users. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 511 | |
Guido van Rossum | b0a2ce5 | 1995-08-28 19:41:35 +0000 | [diff] [blame] | 512 | Another statistic is the number of accesses to the Python WWW server. |
| 513 | Have a look at <URL:http://www.python.org/stats/>. |
| 514 | |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 515 | 2.2. Q. Have any significant projects been done in Python? |
| 516 | |
| 517 | A. Here at CWI (the home of Python), we have written a 20,000 line |
| 518 | authoring environment for transportable hypermedia presentations, a |
Guido van Rossum | 5333c5d | 1994-04-11 11:06:22 +0000 | [diff] [blame] | 519 | 5,000 line multimedia teleconferencing tool, as well as many many |
| 520 | smaller programs. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 521 | |
| 522 | The University of Virginia uses Python to control a virtual reality |
| 523 | engine. Contact: Matt Conway <conway@virginia.edu>. |
| 524 | |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 525 | The ILU project at Xerox PARC can generate Python glue for ILU |
| 526 | interfaces. See <URL:ftp://ftp.parc.xerox.com/pub/ilu/ilu.html>. |
| 527 | |
Guido van Rossum | ac3f212 | 1995-04-10 11:53:42 +0000 | [diff] [blame] | 528 | The University of California, Irvine uses a student administration |
| 529 | system called TELE-Vision written entirely in Python. Contact: Ray |
| 530 | Price <rlprice@uci.edu>. |
| 531 | |
Guido van Rossum | b0a2ce5 | 1995-08-28 19:41:35 +0000 | [diff] [blame] | 532 | See also the next question. |
| 533 | |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 534 | If you have done a significant project in Python that you'd like to be |
| 535 | included in the list above, send me email! |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 536 | |
| 537 | 2.3. Q. Are there any commercial projects going on using Python? |
| 538 | |
| 539 | 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] | 540 | considering use of Python in a future product. |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 541 | |
Guido van Rossum | bf8e7d5 | 1995-08-28 03:09:13 +0000 | [diff] [blame] | 542 | Sunrise Software has a product out using Python -- they use Python |
Guido van Rossum | ac3f212 | 1995-04-10 11:53:42 +0000 | [diff] [blame] | 543 | for a GUI management application and an SNMP network management |
| 544 | application. Contact: <info@sunrise.com>. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 545 | |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 546 | Infoseek uses Python to implement their commercial WWW information |
| 547 | retrieval service <URL:http://www.infoseek.com/>. Contact: |
| 548 | <info@infoseek.com>. |
Guido van Rossum | f8c76d0 | 1994-08-17 12:19:53 +0000 | [diff] [blame] | 549 | |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 550 | Michael Powers of daVinci Time & Space is "writing tons-o-python for |
| 551 | interactive television entertainment." Contact: <powers@dvts.com>. |
| 552 | |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 553 | Paul Everitt of Connecting Minds is planning a Lotus Notes gateway. |
| 554 | Contact: <Paul.Everitt@cminds.com>. Or see their WWW server |
| 555 | <URL:http://www.cminds.com/>. |
| 556 | |
Guido van Rossum | ac3f212 | 1995-04-10 11:53:42 +0000 | [diff] [blame] | 557 | KaPRE in Boulder, CO is using Python for on-site customization of C++ |
| 558 | applications, rapid-prototyping/development, |
| 559 | language-based-components, and possibly more. This is pretty solid: |
| 560 | Python's being shipped with their tool-set now, to beta sites. |
| 561 | Contact: <lutz@KaPRE.COM> (Mark Lutz). |
| 562 | |
| 563 | Individuals at many other companies are using Python for internal |
| 564 | development or for as yet unannounced products (witness their |
| 565 | contributions to the Python mailing list or newsgroup). |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 566 | |
Guido van Rossum | b0a2ce5 | 1995-08-28 19:41:35 +0000 | [diff] [blame] | 567 | SGI has advertised in the Python list looking for Python programmers |
| 568 | for a project involving interactive television. |
| 569 | |
| 570 | See also the workshop minutes at |
| 571 | <URL:http://www.python.org/workshops/> -- in general the WWW server is |
| 572 | more up to date than the FAQ for these issues. |
| 573 | |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 574 | Python has also been elected as an extension language by MADE, a |
| 575 | consortium supported by the European Committee's ESPRIT program and |
| 576 | consisting of Bull, CWI and some other European companies. Contact: |
| 577 | Ivan Herman <ivan@cwi.nl>. |
| 578 | |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 579 | If you'd like to be included in the list above, send me email! |
| 580 | |
Guido van Rossum | 95f61a7 | 1994-01-26 17:23:37 +0000 | [diff] [blame] | 581 | 2.4. Q. How stable is Python? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 582 | |
Guido van Rossum | 3de2736 | 1994-07-25 14:19:33 +0000 | [diff] [blame] | 583 | A. Very stable. While the current version number would suggest it is |
| 584 | in the early stages of development, in fact new, stable releases |
Guido van Rossum | bf8e7d5 | 1995-08-28 03:09:13 +0000 | [diff] [blame] | 585 | (numbered 0.9.x through 1.2) have been coming out roughly every 3 to |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 586 | 6 months for the past four years. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 587 | |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 588 | 2.5. Q. When will the next version be released? |
| 589 | |
Guido van Rossum | b0a2ce5 | 1995-08-28 19:41:35 +0000 | [diff] [blame] | 590 | A. I am planning to release 1.3 in early October 1995. It will |
| 591 | contain keyword parameters as the most important new language feature. |
| 592 | A beta version was made available in August, more beta versions may |
| 593 | appear. |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 594 | |
| 595 | 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] | 596 | |
Guido van Rossum | 7be34a8 | 1995-05-31 15:17:12 +0000 | [diff] [blame] | 597 | A. See my Work-In-Progress web page, currently at |
| 598 | <URL:http://www.python.org:~guido/WIP.html>, and the pages for the |
| 599 | Second Python Workshop (best reached via the Python home page, |
Guido van Rossum | b0a2ce5 | 1995-08-28 19:41:35 +0000 | [diff] [blame] | 600 | <URL:http://www.python.org/>). Also follow the newsgroup discussions! |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 601 | |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 602 | 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] | 603 | |
| 604 | A. In general, no. There are already millions of lines of Python code |
| 605 | around the world, so any changes in the language that invalidates more |
| 606 | than a very small fraction of existing programs has to be frowned |
| 607 | upon. Even if you can provide a conversion program, there still is |
| 608 | the problem of updating all documentation. Providing a gradual |
| 609 | upgrade path is the only way if a feature has to be changed. |
| 610 | |
Guido van Rossum | b0a2ce5 | 1995-08-28 19:41:35 +0000 | [diff] [blame] | 611 | 2.8. Q. What is the future of Python? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 612 | |
Guido van Rossum | b0a2ce5 | 1995-08-28 19:41:35 +0000 | [diff] [blame] | 613 | A. If I knew, I'd be rich :-) |
| 614 | |
| 615 | Seriously, the formation of the PSA (Pyton Software Activity, see |
| 616 | <URL:http://www.python.org/psa/>) ensures some kind of support even in |
| 617 | the (unlikely! event that I'd be hit by a bus (actually, here in the |
| 618 | US, a car accident would be more likely :-), were to join a nunnery, |
| 619 | or would be head-hunted. A large number of Python users have become |
| 620 | experts at Python programming as well as maintenance of the |
| 621 | implementation, and would easily fill the vacuum created by my |
| 622 | disappearance. |
| 623 | |
| 624 | In the mean time, I have no plans to disappear -- rather, I am |
| 625 | committed to improving Python, and my current benefactor, CNRI (see |
| 626 | <URL:http://www.cnri.reston.va.us>) is just as committed to continue |
| 627 | its support of Python and the PSA. In fact, we have great plans for |
| 628 | Python -- we just can't tell yet! |
| 629 | |
Guido van Rossum | 05151e0 | 1995-09-28 13:24:46 +0000 | [diff] [blame^] | 630 | 2.9. Q. What is the PSA, anyway? |
| 631 | |
| 632 | A. The Python Software Activity <URL:http://www.python.org/psa/> was |
| 633 | created by a number of Python aficionados who want Python to be more |
| 634 | than the product and responsibility of a single individual. It has |
| 635 | found a home at CNRI <URL:http://www.cnri.reston.va.us>. Anybody who |
| 636 | wishes Python well should join the PSA. |
| 637 | |
| 638 | 2.10. Q. How do I join the PSA? |
| 639 | |
| 640 | A. The full scoop is available on the web, see |
| 641 | <URL:http://www.python.org/psa/Joining.html>. Summary: send a check |
| 642 | of at least $50 to CNRI/PSA, 1895 Preston White Drive, Suite 100, in |
| 643 | Reston, VA 22091. Full-time students pay $25. Companies can join for |
| 644 | a mere $500. |
| 645 | |
| 646 | 2.11. Q. What are the benefits of joining the PSA? |
| 647 | |
| 648 | A. Like National Public Radio, if not enough people join, Python will |
| 649 | wither. Your name will be mentioned on the PSA's web server. |
| 650 | Workshops organized by the PSA <URL:http://www.python.org/workshops/> |
| 651 | are only accessible to PSA members (you can join at the door). The |
| 652 | PSA is working on additional benefits, such as reduced prices for |
| 653 | books and software, and early access to beta versions of Python. |
| 654 | |
Guido van Rossum | b0a2ce5 | 1995-08-28 19:41:35 +0000 | [diff] [blame] | 655 | |
| 656 | 3. Building Python and Other Known Bugs |
| 657 | ======================================= |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 658 | |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 659 | 3.1. Q. Is there a test set? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 660 | |
| 661 | A. Yes, simply do "import testall" (or "import autotest" if you aren't |
| 662 | interested in the output). The standard modules whose name begins |
| 663 | with "test" together comprise the test. The test set doesn't test |
| 664 | *all* features of Python but it goes a long way to confirm that a new |
| 665 | port is actually working. The Makefile contains an entry "make test" |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 666 | which runs the autotest module. NOTE: if "make test" fails, run the |
| 667 | tests manually ("import testall") to see what goes wrong before |
| 668 | reporting the error. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 669 | |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 670 | 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] | 671 | operations, but when playing with floating point operations I cannot |
| 672 | find anything wrong with them. |
| 673 | |
| 674 | A. The test set makes occasional unwarranted assumptions about the |
| 675 | semantics of C floating point operations. Until someone donates a |
| 676 | better floating point test set, you will have to comment out the |
| 677 | offending floating point tests and execute similar tests manually. |
| 678 | |
Guido van Rossum | 7be34a8 | 1995-05-31 15:17:12 +0000 | [diff] [blame] | 679 | 3.3. Q. Link errors after rerunning the configure script. |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 680 | |
| 681 | A. It is generally necessary to run "make clean" after a configuration |
| 682 | change. |
| 683 | |
Guido van Rossum | 7be34a8 | 1995-05-31 15:17:12 +0000 | [diff] [blame] | 684 | 3.4. Q. The python interpreter complains about options passed to a |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 685 | script (after the script name). |
| 686 | |
| 687 | 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] | 688 | Don't. The reason for the complaint is that GNU getopt, unlike System |
| 689 | V getopt and other getopt implementations, doesn't consider a |
| 690 | non-option to be the end of the option list. A quick (and compatible) |
| 691 | fix for scripts is to add "--" to the interpreter, like this: |
| 692 | |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 693 | #! /usr/local/bin/python -- |
Guido van Rossum | f8c76d0 | 1994-08-17 12:19:53 +0000 | [diff] [blame] | 694 | |
| 695 | You can also use this interactively: |
| 696 | |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 697 | python -- script.py [options] |
| 698 | |
| 699 | Note that a working getopt implementation is provided in the Python |
| 700 | distribution (in Python/getopt.c) but not automatically used. |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 701 | |
Guido van Rossum | 7be34a8 | 1995-05-31 15:17:12 +0000 | [diff] [blame] | 702 | 3.5. 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] | 703 | glmodule.c, but python hasn't been built or installed yet. |
| 704 | |
| 705 | A. Comment out the line mentioning glmodule.c in Setup and build a |
| 706 | python without gl first; install it or make sure it is in your $PATH, |
| 707 | then edit the Setup file again to turn on the gl module, and make |
| 708 | again. You don't need to do "make clean"; you do need to run "make |
| 709 | Makefile" in the Modules subdirectory (or just run "make" at the |
| 710 | toplevel). |
| 711 | |
Guido van Rossum | 7be34a8 | 1995-05-31 15:17:12 +0000 | [diff] [blame] | 712 | 3.6. 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] | 713 | |
Guido van Rossum | a8a8d4a | 1995-03-10 16:19:31 +0000 | [diff] [blame] | 714 | People have reported problems with gcc 2.5.8 up to 2.6.3. The DEC |
| 715 | OSF/1 cc compiler does not have these problems so it's likely a gcc |
Guido van Rossum | e530c58 | 1995-04-10 12:32:16 +0000 | [diff] [blame] | 716 | bug. The latest news is that this has been fixed in Python 1.2 by a |
| 717 | source change (I gave up waiting for a fixed gcc). |
Guido van Rossum | 3de2736 | 1994-07-25 14:19:33 +0000 | [diff] [blame] | 718 | |
Guido van Rossum | 7be34a8 | 1995-05-31 15:17:12 +0000 | [diff] [blame] | 719 | 3.7. 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] | 720 | |
| 721 | A. On some systems (e.g. Sun), if the target already exists in the |
| 722 | source directory, it is created there instead of in the build |
| 723 | directory. This is usually because you have previously built without |
| 724 | VPATH. Try running "make clobber" in the source directory. |
| 725 | |
Guido van Rossum | 7be34a8 | 1995-05-31 15:17:12 +0000 | [diff] [blame] | 726 | 3.8. Q. Trouble building or linking with the GNU readline library. |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 727 | |
Guido van Rossum | ac3f212 | 1995-04-10 11:53:42 +0000 | [diff] [blame] | 728 | A. Consider using readline 2.0. Some hints: |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 729 | |
| 730 | - You can use the GNU readline library to improve the interactive user |
| 731 | interface: this gives you line editing and command history when |
Guido van Rossum | 796b259 | 1995-01-20 23:05:52 +0000 | [diff] [blame] | 732 | calling python interactively. You need to configure and build the GNU |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 733 | readline library before running the configure script. Its sources are |
| 734 | 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] | 735 | mirror site, or from its home site |
| 736 | <URL:ftp://slc2.ins.cwru.edu/pub/dist/readline-2.0.tar.gz> (or a |
| 737 | higher version number -- using version 1.x is not recommended). Pass |
| 738 | the Python configure script the option --with-readline=DIRECTORY where |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 739 | DIRECTORY is the absolute pathname of the directory where you've built |
| 740 | the readline library. Some hints on building and using the readline |
| 741 | library: |
| 742 | |
| 743 | - On SGI IRIX 5, you may have to add the following |
| 744 | to rldefs.h: |
| 745 | |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 746 | #ifndef sigmask |
| 747 | #define sigmask(sig) (1L << ((sig)-1)) |
| 748 | #endif |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 749 | |
| 750 | - On most systems, you will have to add #include "rldefs.h" to the |
| 751 | top of several source files, and if you use the VPATH feature, you |
| 752 | will have to add dependencies of the form foo.o: foo.c to the |
| 753 | Makefile for several values of foo. |
| 754 | |
| 755 | - The readline library requires use of the termcap library. A |
| 756 | known problem with this is that it contains entry points which |
Guido van Rossum | 061f182 | 1994-10-06 16:03:45 +0000 | [diff] [blame] | 757 | cause conflicts with the STDWIN and SGI GL libraries. The STDWIN |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 758 | 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] | 759 | STDWIN) by adding a line saying '#define werase w_erase' to the |
| 760 | stdwin.h file (in the STDWIN distribution, subdirectory H). The |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 761 | GL conflict has been solved in the Python configure script by a |
| 762 | hack that forces use of the static version of the termcap library. |
| 763 | |
Guido van Rossum | ac3f212 | 1995-04-10 11:53:42 +0000 | [diff] [blame] | 764 | - Check the newsgroup gnu.bash.bug <URL:news:gnu.bash.bug> for |
| 765 | specific problems with the readline library (I don't read this group |
| 766 | 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] | 767 | |
Guido van Rossum | 7be34a8 | 1995-05-31 15:17:12 +0000 | [diff] [blame] | 768 | 3.9. Q. Trouble building Python on Linux. |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 769 | |
Guido van Rossum | ac3f212 | 1995-04-10 11:53:42 +0000 | [diff] [blame] | 770 | A. If you're building Python 1.2, Slackware 2.2 has a buggy bash |
| 771 | (version 1.14.3) which breaks a sed script that is used to build |
| 772 | Modules/Makefile. Replace /bin/sh with /bin/ash in both makesetup and |
| 773 | Makefile.pre.in. |
| 774 | |
| 775 | In 1.1 and 1.1.1, there's a bug in the reference counting logic of |
Guido van Rossum | a8a8d4a | 1995-03-10 16:19:31 +0000 | [diff] [blame] | 776 | ternary pow() which is only tripped by very picky mallocs, like the |
| 777 | GNU malloc on Linux. This has been fixed in 1.2. To continue the |
Guido van Rossum | ac3f212 | 1995-04-10 11:53:42 +0000 | [diff] [blame] | 778 | tests in 1.1(.1), just disable the tests of pow() with three arguments |
| 779 | from Lib/test/test_b2.py. |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 780 | |
Guido van Rossum | ac3f212 | 1995-04-10 11:53:42 +0000 | [diff] [blame] | 781 | Apart from this, Python builds and runs fine on most Linux versions |
| 782 | (if you run into trouble on an old Linux version, consider upgrading). |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 783 | |
Guido van Rossum | 7be34a8 | 1995-05-31 15:17:12 +0000 | [diff] [blame] | 784 | 3.10. Q. Trouble with prototypes on Ultrix. |
Guido van Rossum | 72eb83c | 1994-10-07 11:33:28 +0000 | [diff] [blame] | 785 | |
| 786 | A. Ultrix cc seems broken -- use gcc, or edit config.h to #undef |
| 787 | HAVE_PROTOTYPES. |
| 788 | |
Guido van Rossum | 7be34a8 | 1995-05-31 15:17:12 +0000 | [diff] [blame] | 789 | 3.11. Q. Trouble with posix.listdir on NeXTSTEP 3.2. |
Guido van Rossum | 9351fdb | 1994-11-10 23:03:51 +0000 | [diff] [blame] | 790 | |
Guido van Rossum | 3fc9d73 | 1995-07-25 15:10:56 +0000 | [diff] [blame] | 791 | A. (This often manifests itself as a weird error from the |
| 792 | compileall.py script run by "make libinstall".) |
Guido van Rossum | 9351fdb | 1994-11-10 23:03:51 +0000 | [diff] [blame] | 793 | |
Guido van Rossum | 3fc9d73 | 1995-07-25 15:10:56 +0000 | [diff] [blame] | 794 | Don't use gcc, use the Next C compiler (cc). Even though it is |
| 795 | derived from (an old version of) gcc, its interpretation of the |
| 796 | "-posix" switch is different; in this particular case, cc is right and |
| 797 | gcc is wrong. |
Guido van Rossum | 9351fdb | 1994-11-10 23:03:51 +0000 | [diff] [blame] | 798 | |
Guido van Rossum | 7be34a8 | 1995-05-31 15:17:12 +0000 | [diff] [blame] | 799 | 3.12. Q. Other trouble building Python on platform X. |
Guido van Rossum | 95f61a7 | 1994-01-26 17:23:37 +0000 | [diff] [blame] | 800 | |
Guido van Rossum | 796b259 | 1995-01-20 23:05:52 +0000 | [diff] [blame] | 801 | A. Please email the details to <guido@cwi.nl> and I'll look into it. |
| 802 | Please provide as many details as possible. In particular, if you |
| 803 | don't tell me what type of computer and what operating system (and |
| 804 | version) you are using it will be difficult for me to figure out what |
| 805 | is the matter. If you get a specific error message, please email it |
| 806 | to me too. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 807 | |
Guido van Rossum | 7be34a8 | 1995-05-31 15:17:12 +0000 | [diff] [blame] | 808 | 3.13. Q. How to configure dynamic loading on Lixux. |
Guido van Rossum | 0777935 | 1995-02-07 16:59:56 +0000 | [diff] [blame] | 809 | |
Guido van Rossum | 3fc9d73 | 1995-07-25 15:10:56 +0000 | [diff] [blame] | 810 | A. There are two answers, depending on whether you are using the new |
| 811 | ELF object format or not. |
Guido van Rossum | 0777935 | 1995-02-07 16:59:56 +0000 | [diff] [blame] | 812 | |
Guido van Rossum | 3fc9d73 | 1995-07-25 15:10:56 +0000 | [diff] [blame] | 813 | For ELF, this seems to be the procedure (thanks to Martin von Loewis): |
Guido van Rossum | 0777935 | 1995-02-07 16:59:56 +0000 | [diff] [blame] | 814 | |
Guido van Rossum | 3fc9d73 | 1995-07-25 15:10:56 +0000 | [diff] [blame] | 815 | Compile Python to an ELF binary. |
Guido van Rossum | a8a8d4a | 1995-03-10 16:19:31 +0000 | [diff] [blame] | 816 | |
Guido van Rossum | 3fc9d73 | 1995-07-25 15:10:56 +0000 | [diff] [blame] | 817 | In addition, you have to use the following flags: |
Guido van Rossum | 0777935 | 1995-02-07 16:59:56 +0000 | [diff] [blame] | 818 | |
Guido van Rossum | 3fc9d73 | 1995-07-25 15:10:56 +0000 | [diff] [blame] | 819 | - when linking python: -rdynamic -ldl |
| 820 | - when compiling an object that goes into a shared module: -fPIC |
| 821 | - when linking a shared module: -shared -ldl |
Guido van Rossum | 0777935 | 1995-02-07 16:59:56 +0000 | [diff] [blame] | 822 | |
Guido van Rossum | 3fc9d73 | 1995-07-25 15:10:56 +0000 | [diff] [blame] | 823 | Furthermore, it appears that some Python releases did not understand |
| 824 | that Linux has dynamic linking. Python 1.2 did it right, but you |
| 825 | should check wether the generated config.h indicates the use of -ldl |
| 826 | (i.e. dlopen, dlsym). Finally, you can load a shared module by saying |
| 827 | 'use foo'. Make sure the module is in your PYTHONPATH. |
Guido van Rossum | 0777935 | 1995-02-07 16:59:56 +0000 | [diff] [blame] | 828 | |
Guido van Rossum | 3fc9d73 | 1995-07-25 15:10:56 +0000 | [diff] [blame] | 829 | For pre-ELF systems (thanks to Andrew Kuchling): |
Guido van Rossum | 0777935 | 1995-02-07 16:59:56 +0000 | [diff] [blame] | 830 | |
Guido van Rossum | 3fc9d73 | 1995-07-25 15:10:56 +0000 | [diff] [blame] | 831 | Pre-ELF Linux requires that you use the GNU DLD library. The stages |
| 832 | of using dynamic libraries on Linux are: |
| 833 | |
| 834 | 1) Get dld 3.2.5 from a Linux site. Be careful here; the most |
| 835 | recent GNU version is 3.2.3, and doesn't support Linux; be sure to |
| 836 | get it from a Linux mirror, not a GNU mirror (3.2.4 should also |
| 837 | work). Compile it and install the library libdld.a somewhere; I |
| 838 | used /usr/local/lib. |
| 839 | |
| 840 | Suitable URLs for the dld distribution are currently: |
| 841 | <URL:ftp://sunsite.unc.edu/pub/Linux/libs/dld-3.2.5.src.tar.gz> and |
| 842 | <URL:ftp://tsx-11.mit.edu/pub/linux/sources/libs/dld-3.2.5.src.tar.gz>. |
| 843 | There's also a binary distribution of it: |
| 844 | <URL:ftp://sunsite.unc.edu/pub/Linux/libs/dld-3.2.5.bin.tar.gz>. |
| 845 | |
| 846 | 2) Get Jack Jansen's DL library; its location is given in the |
| 847 | _Extending Python_ manual as <URL:ftp://ftp.cwi.nl/pub/dynload/>. |
| 848 | Compile it and install libdl.a wherever you put libdld.a. |
| 849 | |
| 850 | 3) Run Python's configure script, giving it the --with-dl-dld option, |
| 851 | which requires a parameter giving the directory where you put the |
| 852 | libraries. |
| 853 | |
| 854 | 4) Recompile Python. |
| 855 | |
| 856 | 3.14. Q. Under Solaris 2.x, using GCC, how do I use shared libraries? |
| 857 | |
| 858 | A. Use the linker in /usr/ucb/ld, not the GNU linker. The latter |
| 859 | cannot create shared libraries. |
| 860 | |
| 861 | 3.15. Q. Errors when linking with a shared library containing C++ code. |
| 862 | |
| 863 | A. Link the main Python binary with C++. Change the definition of |
| 864 | LINKCC in Modules/Makefile to be your C++ compiler. You may have to |
| 865 | edit config.c slightly to make it compilable with C++. |
Guido van Rossum | 0777935 | 1995-02-07 16:59:56 +0000 | [diff] [blame] | 866 | |
Guido van Rossum | 05151e0 | 1995-09-28 13:24:46 +0000 | [diff] [blame^] | 867 | 3.16. Q. Tk menus or radiobuttons don't work properly in Python 1.2. |
Guido van Rossum | b0a2ce5 | 1995-08-28 19:41:35 +0000 | [diff] [blame] | 868 | |
| 869 | A. There's a bug in Tkinter.py; remove ClassType and InstanceType from |
| 870 | the definition of CallableTypes near the top of Lib/tkinter/Tkinter.py. |
| 871 | |
| 872 | 3.17. Q. I built with tkintermodule.c enabled but get "Tkinter not found". |
| 873 | |
| 874 | A. Tkinter.py (note: upper case T) lives in a subdirectory of Lib, |
| 875 | Lib/tkinter. If you are using the default module search path, you |
| 876 | probably didn't enable the line in the Modules/Setup file defining |
| 877 | TKPATH; if you use the environment variable PYTHONPATH, you'll have to |
| 878 | add the proper tkinter subdirectory. |
| 879 | |
| 880 | 3.18. Q. I built with Tk 4.0 but Tkinter complains about the Tk version. |
| 881 | |
| 882 | A. Several things could cause this. You most likely have a Tk 3.6 |
| 883 | installation that wasn't completely eradicated by the Tk 4.0 |
| 884 | installation (which tends to add "4.0" to its installed files). You |
| 885 | may have the Tk 3.6 support library installed in the place where the |
| 886 | Tk 4.0 support files should be (default /usr/local/lib/tk/); you may |
| 887 | have compiled Python with the old tk.h header file (yes, this actually |
| 888 | compiles!); you may actually have linked with Tk 3.6 even though Tk |
| 889 | 4.0 is also around. Similar for Tcl 7.4 vs. Tcl 7.3. |
| 890 | |
| 891 | 3.19. Q. Tk doesn't work right on DEC Alpha. |
| 892 | |
| 893 | A. You probably compiled either Tcl, Tk or Python with gcc. Don't. |
| 894 | For this platform, which has 64-bit integers, gcc is known to generate |
| 895 | broken code. The standard cc (which comes bundled with the OS!) |
| 896 | works. If you still prefer gcc, at least try recompiling with cc |
| 897 | before reporting problems to the newsgroup or the author; if this |
| 898 | fixes the problem, report the bug to the gcc developers instead. (As |
| 899 | far as we know, there are no problem with gcc on other platforms -- |
| 900 | the instabilities seem to be restricted to the DEC Alpha.) See also |
| 901 | question 3.6. |
| 902 | |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 903 | |
| 904 | 4. Programming in Python |
| 905 | ======================== |
| 906 | |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 907 | 4.1. Q. Is there a source code level debugger with breakpoints, step, |
| 908 | etc.? |
| 909 | |
| 910 | A. Yes. Check out module pdb; pdb.help() prints the documentation (or |
| 911 | you can read it as Lib/pdb.doc). If you use the STDWIN option, |
| 912 | there's also a windowing interface, wdb. You can write your own |
| 913 | debugger by using the code for pdb or wdb as an example. |
| 914 | |
| 915 | 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] | 916 | C and others in Python (e.g. through inheritance)? (Also phrased as: |
| 917 | Can I use a built-in type as base class?) |
| 918 | |
| 919 | A. No, but you can easily create a Python class which serves as a |
| 920 | wrapper around a built-in object, e.g. (for dictionaries): |
| 921 | |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 922 | # A user-defined class behaving almost identical |
| 923 | # to a built-in dictionary. |
| 924 | class UserDict: |
| 925 | def __init__(self): self.data = {} |
| 926 | def __repr__(self): return repr(self.data) |
| 927 | def __cmp__(self, dict): |
| 928 | if type(dict) == type(self.data): |
| 929 | return cmp(self.data, dict) |
| 930 | else: |
| 931 | return cmp(self.data, dict.data) |
| 932 | def __len__(self): return len(self.data) |
| 933 | def __getitem__(self, key): return self.data[key] |
| 934 | def __setitem__(self, key, item): self.data[key] = item |
| 935 | def __delitem__(self, key): del self.data[key] |
| 936 | def keys(self): return self.data.keys() |
| 937 | def items(self): return self.data.items() |
| 938 | def values(self): return self.data.values() |
| 939 | def has_key(self, key): return self.data.has_key(key) |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 940 | |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 941 | 4.3. Q. Is there a curses/termcap package for Python? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 942 | |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 943 | A. Yes -- Lance Ellinghaus has written a module that interfaces to |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 944 | 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] | 945 | it's straightforward to use. It is part of the standard Python |
| 946 | distribution, but not configured by default -- you must enable it by |
| 947 | editing Modules/Setup. It requires a System V curses implementation. |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 948 | |
| 949 | You could also consider using the "alfa" (== character cell) version |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 950 | of STDWIN. (Standard Window System Interface, a portable windowing |
| 951 | 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] | 952 | will also prepare your program for porting to windowing environments |
| 953 | such as X11 or the Macintosh. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 954 | |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 955 | 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] | 956 | |
| 957 | A. Yes, if you import sys and assign a function to sys.exitfunc, it |
| 958 | will be called when your program exits, is killed by an unhandled |
| 959 | exception, or (on UNIX) receives a SIGHUP or SIGTERM signal. |
| 960 | |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 961 | 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] | 962 | nested function seemingly can't access the local variables of the |
| 963 | outer function. What is going on? How do I pass local data to a |
| 964 | nested function? |
| 965 | |
| 966 | A. Python does not have arbitrarily nested scopes. When you need to |
| 967 | create a function that needs to access some data which you have |
| 968 | available locally, create a new class to hold the data and return a |
| 969 | method of an instance of that class, e.g.: |
| 970 | |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 971 | class MultiplierClass: |
| 972 | def __init__(self, factor): |
| 973 | self.factor = factor |
| 974 | def multiplier(self, argument): |
| 975 | return argument * self.factor |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 976 | |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 977 | def generate_multiplier(factor): |
| 978 | return MultiplierClass(factor).multiplier |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 979 | |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 980 | twice = generate_multiplier(2) |
| 981 | print twice(10) |
| 982 | # Output: 20 |
| 983 | |
| 984 | An alternative solution uses default arguments, e.g.: |
| 985 | |
| 986 | def generate_multiplier(factor): |
| 987 | def multiplier(arg, fact = factor): |
| 988 | return arg*fact |
| 989 | return multiplier |
| 990 | |
| 991 | twice = generate_multiplier(2) |
| 992 | print twice(10) |
| 993 | # Output: 20 |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 994 | |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 995 | 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] | 996 | |
| 997 | A. If it is a list, the fastest solution is |
| 998 | |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 999 | list.reverse() |
| 1000 | try: |
| 1001 | for x in list: |
| 1002 | "do something with x" |
| 1003 | finally: |
| 1004 | list.reverse() |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 1005 | |
| 1006 | This has the disadvantage that while you are in the loop, the list |
| 1007 | is temporarily reversed. If you don't like this, you can make a copy. |
| 1008 | This appears expensive but is actually faster than other solutions: |
| 1009 | |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 1010 | rev = list[:] |
| 1011 | rev.reverse() |
| 1012 | for x in rev: |
| 1013 | <do something with x> |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 1014 | |
| 1015 | If it isn't a list, a more general but slower solution is: |
| 1016 | |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 1017 | i = len(list) |
| 1018 | while i > 0: |
| 1019 | i = i-1 |
| 1020 | x = list[i] |
| 1021 | <do something with x> |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 1022 | |
| 1023 | A more elegant solution, is to define a class which acts as a sequence |
| 1024 | and yields the elements in reverse order (solution due to Steve |
| 1025 | Majewski): |
| 1026 | |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 1027 | class Rev: |
| 1028 | def __init__(self, seq): |
| 1029 | self.forw = seq |
| 1030 | def __len__(self): |
| 1031 | return len(self.forw) |
| 1032 | def __getitem__(self, i): |
| 1033 | return self.forw[-(i + 1)] |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 1034 | |
| 1035 | You can now simply write: |
| 1036 | |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 1037 | for x in Rev(list): |
| 1038 | <do something with x> |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 1039 | |
Guido van Rossum | 061f182 | 1994-10-06 16:03:45 +0000 | [diff] [blame] | 1040 | Unfortunately, this solution is slowest of all, due to the method |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 1041 | call overhead... |
| 1042 | |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 1043 | 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] | 1044 | |
| 1045 | A. That's a tough one, in general. There are many tricks to speed up |
| 1046 | Python code; I would consider rewriting parts in C only as a last |
| 1047 | resort. One thing to notice is that function and (especially) method |
| 1048 | calls are rather expensive; if you have designed a purely OO interface |
| 1049 | with lots of tiny functions that don't do much more than get or set an |
| 1050 | instance variable or call another method, you may consider using a |
| 1051 | more direct way, e.g. directly accessing instance variables. Also see |
| 1052 | the standard module "profile" (described in the file |
| 1053 | "python/lib/profile.doc") which makes it possible to find out where |
| 1054 | your program is spending most of its time (if you have some patience |
| 1055 | -- the profiling itself can slow your program down by an order of |
| 1056 | magnitude). |
| 1057 | |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 1058 | 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] | 1059 | again (into the same Python process), the changes don't seem to take |
| 1060 | place. What is going on? |
| 1061 | |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 1062 | 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] | 1063 | 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] | 1064 | program consisting of many modules, each of which imports the same |
Guido van Rossum | 796b259 | 1995-01-20 23:05:52 +0000 | [diff] [blame] | 1065 | basic module, would read the basic module over and over again.) To |
| 1066 | force rereading of a changed module, do this: |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 1067 | |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 1068 | import modname |
| 1069 | reload(modname) |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 1070 | |
| 1071 | Warning: this technique is not 100% fool-proof. In particular, |
| 1072 | modules containing statements like |
| 1073 | |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 1074 | from modname import some_objects |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 1075 | |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 1076 | 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] | 1077 | |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 1078 | 4.9. Q. How do I find the current module name? |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 1079 | |
Guido van Rossum | 061f182 | 1994-10-06 16:03:45 +0000 | [diff] [blame] | 1080 | 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] | 1081 | (predefined) global variable __name__. If this has the value |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 1082 | '__main__' you are running as a script. |
| 1083 | |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 1084 | 4.10. Q. I have a module in which I want to execute some extra code |
| 1085 | when it is run as a script. How do I find out whether I am running as |
| 1086 | a script? |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 1087 | |
| 1088 | A. See the previous question. E.g. if you put the following on the |
| 1089 | last line of your module, main() is called only when your module is |
| 1090 | running as a script: |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 1091 | |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 1092 | if __name__ == '__main__': main() |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 1093 | |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 1094 | 4.11. Q. I try to run a program from the Demo directory but it fails |
| 1095 | with ImportError: No module named ...; what gives? |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 1096 | |
Guido van Rossum | 061f182 | 1994-10-06 16:03:45 +0000 | [diff] [blame] | 1097 | A. This is probably an optional module (written in C!) which hasn't |
| 1098 | been configured on your system. This especially happens with modules |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 1099 | like "Tkinter", "stdwin", "gl", "Xt" or "Xm". For Tkinter, STDWIN and |
| 1100 | many other modules, see Modules/Setup.in for info on how to add these |
| 1101 | modules to your Python, if it is possible at all. Sometimes you will |
| 1102 | have to ftp and build another package first (e.g. STDWIN). Sometimes |
| 1103 | the module only works on specific platforms (e.g. gl only works on SGI |
| 1104 | machines). |
| 1105 | |
| 1106 | NOTE: if the complaint is about "Tkinter" (upper case T) and you have |
| 1107 | already configured module "tkinter" (lower case t), the solution is |
| 1108 | *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] | 1109 | 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] | 1110 | sys.path. |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 1111 | |
Guido van Rossum | 061f182 | 1994-10-06 16:03:45 +0000 | [diff] [blame] | 1112 | For X-related modules (Xt and Xm) you will have to do more work: they |
| 1113 | are currently not part of the standard Python distribution. You will |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 1114 | have to ftp the Extensions tar file, e.g. |
Guido van Rossum | 7be34a8 | 1995-05-31 15:17:12 +0000 | [diff] [blame] | 1115 | <URL:ftp://ftp.python.org/pub/python/src/extensions.tar.gz> and follow |
| 1116 | the instructions there. |
Guido van Rossum | 061f182 | 1994-10-06 16:03:45 +0000 | [diff] [blame] | 1117 | |
| 1118 | See also the next question. |
| 1119 | |
| 1120 | 4.12. Q. I have successfully built Python with STDWIN but it can't |
| 1121 | find some modules (e.g. stdwinevents). |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 1122 | |
| 1123 | A. There's a subdirectory of the library directory named 'stdwin' |
| 1124 | which should be in the default module search path. There's a line in |
| 1125 | Modules/Setup(.in) that you have to enable for this purpose -- |
Guido van Rossum | 061f182 | 1994-10-06 16:03:45 +0000 | [diff] [blame] | 1126 | unfortunately in the latest release it's not near the other |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 1127 | STDWIN-related lines so it's easy to miss it. |
| 1128 | |
| 1129 | 4.13. Q. What GUI toolkits exist for Python? |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 1130 | |
| 1131 | A. Depending on what platform(s) you are aiming at, there are several. |
| 1132 | |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 1133 | - 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] | 1134 | called Tkinter. As of python 1.1, it is part of the standard Python |
| 1135 | distribution -- all you need to do is enable it in Modules/Setup |
| 1136 | (provided you have already installed Tk and Tcl). This is probably |
| 1137 | the easiest to install and use, and the most complete widget set. It |
| 1138 | is also very likely that in the future the standard Python GUI API |
| 1139 | will be based on or at least look very much like the Tkinter |
| 1140 | interface. For more info about Tk, including pointers to the source, |
Guido van Rossum | bf8e7d5 | 1995-08-28 03:09:13 +0000 | [diff] [blame] | 1141 | see the Tcl/Tk home page <URL:http://www.sunlabs.com/research/tcl/>. |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 1142 | |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 1143 | - The standard Python distribution comes with an interface to STDWIN, |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 1144 | a platform-independent low-level windowing interface. You have to ftp |
| 1145 | the source for STDWIN separately, |
| 1146 | e.g. <URL:ftp://ftp.cwi.nl/pub/stdwin/> or gatekeeper.dec.com in |
| 1147 | pub/misc/stdwin <URL:ftp://gatekeeper.dec.com/pub/misc/stdwin/>. |
| 1148 | STDWIN runs under X11 or the Mac; a Windows port has been attempted |
| 1149 | but I can't seem to get it working. Note that STDWIN is really not |
| 1150 | powerful enough to implement a modern GUI (no widgets, etc.) and that |
| 1151 | I don't have the time to maintain or extend it, so you may be better |
| 1152 | off using Tkinter or the Motif interface, unless you require |
| 1153 | portability to the Mac (which is also offered by SUIT, by the way -- |
| 1154 | see below). |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 1155 | |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 1156 | - 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] | 1157 | Library -- low level but very good 3D capabilities) as well as to |
| 1158 | 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] | 1159 | 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] | 1160 | |
| 1161 | - There's an interface to X11, including the Athena and Motif widget |
| 1162 | sets (and a few individual widgets, like Mosaic's HTML widget and |
| 1163 | SGI's GL widget) in the Extensions set, which is separately ftp'able |
Guido van Rossum | 7be34a8 | 1995-05-31 15:17:12 +0000 | [diff] [blame] | 1164 | <URL:ftp://ftp.python.org/pub/python/src/extensions.tar.gz>. |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 1165 | |
| 1166 | - 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] | 1167 | Interface Toolkit; it can be ftp'ed from |
| 1168 | <URL:ftp://uvacs.cs.virginia.edu/pub/suit/python/SUIT_python.tar.Z>. |
| 1169 | A PC binary of Python 1.0.2 compiled with DJGPP and with SUIT support |
| 1170 | built-in has been made available by Antonio Costa |
| 1171 | <URL:ftp://asterix.inescn.pt/pub/PC/python/pyt102su.exe> (a |
| 1172 | self-extracting archive). Note that the UVa people themselves have |
| 1173 | expressed doubts about SUIT, and are planning to build a Python GUI |
| 1174 | API based upon Tk (though not necessarily on Tkinter); see |
| 1175 | <URL:http://server.cs.virginia.edu/~tnb2d/IT/IT.html>. |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 1176 | |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 1177 | - 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] | 1178 | Athena widget sets. Last I heard about it it was included in the WAFE |
| 1179 | 1.0 prerelease |
| 1180 | <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] | 1181 | |
Guido van Rossum | bf8e7d5 | 1995-08-28 03:09:13 +0000 | [diff] [blame] | 1182 | - The NT port by Mark Hammond (see question 7.4) includes an interface |
| 1183 | to the Microsoft Foundation Classes and a Python programming |
| 1184 | environment using it that's written mostly in Python. See |
| 1185 | <URL:ftp://ftp.python.org/pub/python/nt/>. |
| 1186 | |
Guido van Rossum | a8a8d4a | 1995-03-10 16:19:31 +0000 | [diff] [blame] | 1187 | - There's an interface to wxWindows. wxWindows is a portable GUI |
| 1188 | class library written in C++. It supports XView, Motif, MS-Windows as |
| 1189 | targets. There is some support for Macs and CURSES as well. wxWindows |
| 1190 | preserves the look and feel of the underlying graphics toolkit. See |
| 1191 | the wxPython WWW page at |
| 1192 | <URL:http://www.aiai.ed.ac.uk/~jacs/wx/wxpython/wxpython.html>. |
| 1193 | |
Guido van Rossum | 3fc9d73 | 1995-07-25 15:10:56 +0000 | [diff] [blame] | 1194 | - There's an object-oriented GUI based on the Microsoft Foundation |
| 1195 | Classes model called WPY. Programs written in WPY run unchanged and |
| 1196 | with native look and feel on NT, Windows 3.1 (using win32s) and on |
| 1197 | Unix (using Tk). Source and binaries for NT and Linux are available |
| 1198 | in <URL:ftp://ftp.python.org/pub/python/wpy>. |
| 1199 | |
Guido van Rossum | a8a8d4a | 1995-03-10 16:19:31 +0000 | [diff] [blame] | 1200 | - Python has been mentioned on the "Futurism" subpage of the Fresco |
| 1201 | home page <URL:http://www.faslab.com/fresco/HomePage.html>. "Pesto" |
| 1202 | is a Python interface to the CORBA dynamic invocation interface, and |
| 1203 | thus Fresco. A Pesto prototype is running and is currently being |
Guido van Rossum | 7be34a8 | 1995-05-31 15:17:12 +0000 | [diff] [blame] | 1204 | packaged up for inclusion in the Fresco snapshot. See also the Pesto |
| 1205 | web pages: <URL:http://www.faslab.com/fresco/pesto/Index.html>. |
Guido van Rossum | a8a8d4a | 1995-03-10 16:19:31 +0000 | [diff] [blame] | 1206 | |
Guido van Rossum | 061f182 | 1994-10-06 16:03:45 +0000 | [diff] [blame] | 1207 | 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] | 1208 | |
| 1209 | A. There's an interface to SYBASE by John Redford |
| 1210 | <jredford@lehman.com>. |
| 1211 | |
Guido van Rossum | 796b259 | 1995-01-20 23:05:52 +0000 | [diff] [blame] | 1212 | There's an interface to metalbase by Lance Ellinghaus |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 1213 | <lance@markv.com>; it is part of the separate Extensions distribution |
Guido van Rossum | 7be34a8 | 1995-05-31 15:17:12 +0000 | [diff] [blame] | 1214 | <URL:ftp://ftp.python.org/pub/python/src/extensions.tar.gz>. |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 1215 | |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 1216 | Anthony Baxter <anthony.baxter@aaii.oz.au> has written an interface to |
Guido van Rossum | e530c58 | 1995-04-10 12:32:16 +0000 | [diff] [blame] | 1217 | mSQL (mini-SQL). |
Guido van Rossum | 7be34a8 | 1995-05-31 15:17:12 +0000 | [diff] [blame] | 1218 | <URL:ftp://ftp.python.org/pub/python/contrib/PymSQL.tar.gz>. |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 1219 | |
Guido van Rossum | 7be34a8 | 1995-05-31 15:17:12 +0000 | [diff] [blame] | 1220 | Tom Culliton <culliton@clark.net> has written an Oracle module. |
| 1221 | See <URL:ftp://ftp.clark.net/pub/culliton/oramod.tgz>. |
Guido van Rossum | 796b259 | 1995-01-20 23:05:52 +0000 | [diff] [blame] | 1222 | |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 1223 | 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] | 1224 | |
| 1225 | A. Yes. See the following three examples, due to Ulf Bartelt: |
| 1226 | |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 1227 | # Primes < 1000 |
| 1228 | print filter(None,map(lambda y:y*reduce(lambda x,y:x*y!=0, |
| 1229 | 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] | 1230 | |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 1231 | # First 10 Fibonacci numbers |
| 1232 | print map(lambda x,f=lambda x,f:(x<=1) or (f(x-1,f)+f(x-2,f)): f(x,f), |
| 1233 | range(10)) |
Guido van Rossum | c24da7c | 1994-09-23 14:08:41 +0000 | [diff] [blame] | 1234 | |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 1235 | # Mandelbrot set |
| 1236 | print (lambda Ru,Ro,Iu,Io,IM,Sx,Sy:reduce(lambda x,y:x+y,map(lambda y, |
| 1237 | Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,Sy=Sy,L=lambda yc,Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,i=IM, |
| 1238 | Sx=Sx,Sy=Sy:reduce(lambda x,y:x+y,map(lambda x,xc=Ru,yc=yc,Ru=Ru,Ro=Ro, |
| 1239 | 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 |
| 1240 | >=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( |
| 1241 | 64+F(Ru+x*(Ro-Ru)/Sx,yc,0,0,i)),range(Sx))):L(Iu+y*(Io-Iu)/Sy),range(Sy |
| 1242 | ))))(-2.1, 0.7, -1.2, 1.2, 30, 80, 24) |
| 1243 | # \___ ___/ \___ ___/ | | |__ lines on screen |
| 1244 | # V V | |______ columns on screen |
| 1245 | # | | |__________ maximum of "iterations" |
| 1246 | # | |_________________ range on y axis |
| 1247 | # |____________________________ range on x axis |
Guido van Rossum | c24da7c | 1994-09-23 14:08:41 +0000 | [diff] [blame] | 1248 | |
| 1249 | Don't try this at home, kids! |
| 1250 | |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 1251 | 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] | 1252 | |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 1253 | A. Not directly. In many cases you can mimic a?b:c with "a and b or |
| 1254 | c", but there's a flaw: if b is zero (or empty, or None -- anything |
| 1255 | that tests false) then c will be selected instead. In many cases you |
| 1256 | can prove by looking at the code that this can't happen (e.g. because |
| 1257 | b is a constant or has a type that can never be false), but in general |
| 1258 | this can be a problem. |
| 1259 | |
| 1260 | Steve Majewski (or was it Tim Peters?) suggested the following |
| 1261 | solution: (a and [b] or [c])[0]. Because [b] is a singleton list it |
| 1262 | is never false, so the wrong path is never taken; then applying [0] to |
| 1263 | the whole thing gets the b or c that you really wanted. Ugly, but it |
| 1264 | gets you there in the rare cases where it is really inconvenient to |
| 1265 | rewrite your code using 'if'. |
| 1266 | |
| 1267 | 4.17. Q. My class defines __del__ but it is not called when I delete the |
| 1268 | object. |
| 1269 | |
| 1270 | A. There are several possible reasons for this. |
| 1271 | |
| 1272 | - The del statement does not necessarily call __del__ -- it simply |
| 1273 | decrements the object's reference count, and if this reaches zero |
| 1274 | __del__ is called. |
| 1275 | |
| 1276 | - If your data structures contain circular links (e.g. a tree where |
| 1277 | each child has a parent pointer and each parent has a list of |
| 1278 | 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] | 1279 | have to define an explicit close() method which removes those |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 1280 | pointers. Please don't ever call __del__ directly -- __del__ should |
Guido van Rossum | 061f182 | 1994-10-06 16:03:45 +0000 | [diff] [blame] | 1281 | 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] | 1282 | than once for the same object. |
| 1283 | |
| 1284 | - If the object has ever been a local variable (or argument, which is |
| 1285 | really the same thing) to a function that caught an expression in an |
| 1286 | except clause, chances are that a reference to the object still exists |
| 1287 | in that function's stack frame as contained in the stack trace. |
| 1288 | Normally, deleting (better: assigning None to) sys.exc_traceback will |
| 1289 | take care of this. If you a stack was printed for an unhandled |
| 1290 | exception in an interactive interpreter, delete sys.last_traceback |
| 1291 | instead. |
| 1292 | |
| 1293 | - There is code that deletes all objects when the interpreter exits, |
| 1294 | but if your Python has been configured to support threads, it is not |
| 1295 | called (because other threads may still be active). You can define |
Guido van Rossum | 061f182 | 1994-10-06 16:03:45 +0000 | [diff] [blame] | 1296 | your own cleanup function using sys.exitfunc (see question 4.4). |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 1297 | |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 1298 | - Finally, there are some obscure bugs if your __del__ method does |
| 1299 | complicated things such as updating dictionaries or lists or |
| 1300 | references globals. I hope to have fixed these in release 1.2. |
| 1301 | |
| 1302 | 4.18. Q. How do I change the shell environment for programs called |
| 1303 | 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] | 1304 | |
| 1305 | A. Modifying the environment passed to subshells was left out of the |
| 1306 | interpreter because there seemed to be no well-established portable |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 1307 | way to do it (in particular, some systems, have putenv(), others have |
| 1308 | setenv(), and some have none at all). |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 1309 | |
| 1310 | However if all you want is to pass environment variables to the |
| 1311 | commands run by os.system() or os.popen(), there's a simple solution: |
| 1312 | prefix the command string with a couple of variable assignments and |
Guido van Rossum | 796b259 | 1995-01-20 23:05:52 +0000 | [diff] [blame] | 1313 | export statements. The following would be universal for popen: |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 1314 | |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 1315 | import os |
| 1316 | from commands import mkarg # nifty routine to add shell quoting |
| 1317 | def epopen(cmd, mode, env = {}): |
| 1318 | # env is a dictionary of environment variables |
| 1319 | prefix = '' |
Guido van Rossum | 796b259 | 1995-01-20 23:05:52 +0000 | [diff] [blame] | 1320 | for key, value in env.items(): |
| 1321 | prefix = prefix + '%s=%s\n' % (key, mkarg(value)[1:]) |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 1322 | prefix = prefix + 'export %s\n' % key |
| 1323 | return os.popen(prefix + cmd, mode) |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 1324 | |
| 1325 | 4.19. Q. What is a class? |
| 1326 | |
| 1327 | 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] | 1328 | a class statement. Class objects are used as templates, to create |
| 1329 | class instance objects, which embody both the data structure and |
| 1330 | program routines specific to a datatype. |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 1331 | |
| 1332 | 4.20. Q. What is a method? |
| 1333 | |
| 1334 | A. A method is a function that you normally call as |
Guido van Rossum | 796b259 | 1995-01-20 23:05:52 +0000 | [diff] [blame] | 1335 | 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] | 1336 | 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] | 1337 | objects. (The latter have a completely different implementation and |
| 1338 | only share the way their calls look in Python code.) Methods of |
| 1339 | classes (and class instances) are defined as functions inside the |
| 1340 | class definition. |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 1341 | |
| 1342 | 4.21. Q. What is self? |
| 1343 | |
| 1344 | A. Self is merely a conventional name for the first argument of a |
| 1345 | method -- i.e. a function defined inside a class definition. A method |
| 1346 | defined as meth(self, a, b, c) should be called as x.meth(a, b, c) for |
| 1347 | some instance x of the class in which the definition occurs; |
| 1348 | the called method will think it is called as meth(x, a, b, c). |
| 1349 | |
| 1350 | 4.22. Q. What is a unbound method? |
| 1351 | |
| 1352 | A. An unbound method is a method defined in a class that is not yet |
| 1353 | bound to an instance. You get an unbound method if you ask for a |
| 1354 | class attribute that happens to be a function. You get a bound method |
| 1355 | 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] | 1356 | instance it belongs to and calling it supplies the instance automatically; |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 1357 | an unbound method only knows which class it wants for its first |
| 1358 | argument (a derived class is also OK). Calling an unbound method |
| 1359 | doesn't "magically" derive the first argument from the context -- you |
| 1360 | have to provide it explicitly. |
| 1361 | |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 1362 | 4.23. Q. How do I call a method defined in a base class from a derived |
| 1363 | class that overrides it? |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 1364 | |
| 1365 | A. If your class definition starts with "class Derived(Base): ..." |
| 1366 | then you can call method meth defined in Base (or one of Base's base |
| 1367 | classes) as Base.meth(self, arguments...). Here, Base.meth is an |
| 1368 | unbound method (see previous question). |
| 1369 | |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 1370 | 4.24. Q. How do I call a method from a base class without using the |
| 1371 | name of the base class? |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 1372 | |
| 1373 | A. DON'T DO THIS. REALLY. I MEAN IT. It appears that you could call |
| 1374 | self.__class__.__bases__[0].meth(self, arguments...) but this fails when |
Guido van Rossum | 061f182 | 1994-10-06 16:03:45 +0000 | [diff] [blame] | 1375 | 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] | 1376 | self.__class__.__bases__[0] is your class, not its base class -- so |
| 1377 | (assuming you are doing this from within Derived.meth) you would start |
| 1378 | a recursive call. |
| 1379 | |
| 1380 | 4.25. Q. How can I organize my code to make it easier to change the base |
| 1381 | class? |
| 1382 | |
| 1383 | A. You could define an alias for the base class, assign the real base |
| 1384 | class to it before your class definition, and use the alias throughout |
| 1385 | your class. Then all you have to change is the value assigned to the |
| 1386 | alias. Incidentally, this trick is also handy if you want to decide |
| 1387 | dynamically (e.g. depending on availability of resources) which base |
| 1388 | class to use. Example: |
| 1389 | |
| 1390 | BaseAlias = <real base class> |
| 1391 | class Derived(BaseAlias): |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 1392 | def meth(self): |
| 1393 | BaseAlias.meth(self) |
| 1394 | ... |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 1395 | |
| 1396 | 4.26. Q. How can I find the methods or attributes of an object? |
| 1397 | |
| 1398 | A. This depends on the object type. |
| 1399 | |
| 1400 | For an instance x of a user-defined class, instance attributes are |
| 1401 | found in the dictionary x.__dict__, and methods and attributes defined |
| 1402 | by its class are found in x.__class__.__bases__[i].__dict__ (for i in |
| 1403 | range(len(x.__class__.__bases__))). You'll have to walk the tree of |
| 1404 | base classes to find *all* class methods and attributes. |
| 1405 | |
| 1406 | Many, but not all built-in types define a list of their method names |
| 1407 | in x.__methods__, and if they have data attributes, their names may be |
| 1408 | found in x.__members__. However this is only a convention. |
| 1409 | |
| 1410 | For more information, read the source of the standard (but |
| 1411 | undocumented) module newdir. |
| 1412 | |
| 1413 | 4.27. Q. I can't seem to use os.read() on a pipe created with os.popen(). |
| 1414 | |
| 1415 | A. os.read() is a low-level function which takes a file descriptor (a |
| 1416 | small integer). os.popen() creates a high-level file object -- the |
| 1417 | same type used for sys.std{in,out,err} and returned by the builtin |
| 1418 | open() function. Thus, to read n bytes from a pipe p created with |
| 1419 | os.popen(), you need to use p.read(n). |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 1420 | |
Guido van Rossum | 061f182 | 1994-10-06 16:03:45 +0000 | [diff] [blame] | 1421 | 4.28. Q. How can I create a stand-alone binary from a Python script? |
| 1422 | |
| 1423 | The demo script "Demo/scripts/freeze.py" does what you want. (It's |
| 1424 | actually not a demo but a support tool -- there is some extra code in |
| 1425 | the interpreter to accommodate it.) It requires that you have the |
| 1426 | Python build tree handy, complete with all the lib*.a files. |
| 1427 | |
| 1428 | This works by scanning your source recursively for import statements |
| 1429 | (both forms) and looking for the modules on the standard Python path |
| 1430 | as well as in the source directory (for built-in modules). It then |
| 1431 | "compiles" the modules written in Python to C code (array initializers |
| 1432 | that can be turned into code objects using the marshal module) and |
| 1433 | creates a custom-made config file that only contains those built-in |
| 1434 | modules which are actually used in the program. It then compiles the |
| 1435 | generated C code and links it with the rest of the Python interpreter |
| 1436 | to form a self-contained binary which acts exactly like your script. |
| 1437 | |
Guido van Rossum | 7be34a8 | 1995-05-31 15:17:12 +0000 | [diff] [blame] | 1438 | Hint: the freeze program only works if your script's filename ends in |
| 1439 | ".py". |
Guido van Rossum | 061f182 | 1994-10-06 16:03:45 +0000 | [diff] [blame] | 1440 | |
Guido van Rossum | 7be34a8 | 1995-05-31 15:17:12 +0000 | [diff] [blame] | 1441 | 4.29. Q. What WWW tools are there for Python? |
Guido van Rossum | 061f182 | 1994-10-06 16:03:45 +0000 | [diff] [blame] | 1442 | |
Guido van Rossum | 7be34a8 | 1995-05-31 15:17:12 +0000 | [diff] [blame] | 1443 | A. There's a whole lot in the 1.2 release; see the section on |
| 1444 | "Internet and WWW" in the Library Reference Manual. Highlights: a CGI |
| 1445 | parser, a url retriever, an html parser. |
Guido van Rossum | 061f182 | 1994-10-06 16:03:45 +0000 | [diff] [blame] | 1446 | |
| 1447 | Steve Miale <smiale@cs.indiana.edu> has written a modular WWW browser |
| 1448 | called Dancer. An alpha version can be FTP'ed from |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 1449 | <URL:ftp://ftp.cs.indiana.edu/pub/smiale/dancer.tar.gz>. (There are a |
| 1450 | few articles about Dancer in the (hyper)mail archive |
| 1451 | <URL:http://www.cwi.nl/~guido/hypermail/python-1994q3/index.html>.) |
Guido van Rossum | 061f182 | 1994-10-06 16:03:45 +0000 | [diff] [blame] | 1452 | |
Guido van Rossum | 7be34a8 | 1995-05-31 15:17:12 +0000 | [diff] [blame] | 1453 | 4.30. Q. How do I run a subprocess with pipes connected to both input |
Guido van Rossum | 796b259 | 1995-01-20 23:05:52 +0000 | [diff] [blame] | 1454 | and output? |
| 1455 | |
| 1456 | A. This is really a UNIX question. Also, in general, it is unwise to |
| 1457 | do so, because you can easily cause a deadlock where the parent |
| 1458 | process is blocked waiting for output from the child, while the child |
| 1459 | is blocked waiting for input from the child. This can be caused |
| 1460 | because the parent expects the child to output more text than it does, |
| 1461 | or it can be caused by data being stuck in stdio buffers due to lack |
| 1462 | of flushing. The Python parent can of course explicitly flush the data |
| 1463 | it sends to the child before it reads any output, but if the child is |
| 1464 | a naive C program it can easily have been written to never explicitly |
| 1465 | flush its output, even if it is interactive, since flushing is |
| 1466 | normally automatic. |
| 1467 | |
| 1468 | In many cases, all you really need is to run some data through a |
| 1469 | command and get the result back. Unless the data is infinite in size, |
| 1470 | the easiest (and often the most efficient!) way to do this is to write |
| 1471 | it to a temporary file and run the command with that temporary file as |
| 1472 | input. The standard module tempfile exports a function mktemp() which |
| 1473 | generates unique temporary file names. |
| 1474 | |
| 1475 | If after reading all of the above you still want to connect two pipes |
| 1476 | to a subprocess's standard input and output, here's a simple solution, |
| 1477 | due to Jack Jansen: |
| 1478 | |
| 1479 | import os |
| 1480 | import sys |
| 1481 | import string |
| 1482 | |
| 1483 | MAXFD = 100 # Max number of file descriptors in this system |
| 1484 | |
| 1485 | def popen2(cmd): |
| 1486 | cmd = string.split(cmd) |
| 1487 | p2cread, p2cwrite = os.pipe() |
| 1488 | c2pread, c2pwrite = os.pipe() |
| 1489 | pid = os.fork() |
| 1490 | if pid == 0: |
| 1491 | # Child |
| 1492 | os.close(0) |
| 1493 | os.close(1) |
| 1494 | if os.dup(p2cread) <> 0: |
| 1495 | sys.stderr.write('popen2: bad read dup\n') |
| 1496 | if os.dup(c2pwrite) <> 1: |
| 1497 | sys.stderr.write('popen2: bad write dup\n') |
| 1498 | for i in range(3, MAXFD): |
| 1499 | try: |
| 1500 | os.close(i) |
| 1501 | except: |
| 1502 | pass |
| 1503 | try: |
| 1504 | os.execv(cmd[0], cmd) |
| 1505 | finally: |
| 1506 | os._exit(1) |
| 1507 | os.close(p2cread) |
| 1508 | tochild = os.fdopen(p2cwrite, 'w') |
| 1509 | os.close(c2pwrite) |
| 1510 | fromchild = os.fdopen(c2pread, 'r') |
| 1511 | return fromchild, tochild |
| 1512 | |
| 1513 | Note that many interactive programs (e.g. vi) don't work well with |
| 1514 | pipes substituted for standard input and output. You will have to use |
| 1515 | pseudo ttys ("ptys") instead of pipes. There is some undocumented |
| 1516 | code to use these in the library module pty.py -- I'm afraid you're on |
Guido van Rossum | 3fc9d73 | 1995-07-25 15:10:56 +0000 | [diff] [blame] | 1517 | your own here. |
| 1518 | |
| 1519 | A different answer is a Python interface to Don Libes' "expect" |
| 1520 | library. A prerelease of this is available on the Python ftp mirror |
| 1521 | sites in the contrib subdirectory as expy-0.3.tar.gz, e.g. |
| 1522 | <URL:ftp://ftp.python.org/pub/python/contrib/expy-0.3.tar.gz>. |
Guido van Rossum | 796b259 | 1995-01-20 23:05:52 +0000 | [diff] [blame] | 1523 | |
Guido van Rossum | 7be34a8 | 1995-05-31 15:17:12 +0000 | [diff] [blame] | 1524 | 4.31. Q. How do I call a function if I have the arguments in a tuple? |
Guido van Rossum | ac3f212 | 1995-04-10 11:53:42 +0000 | [diff] [blame] | 1525 | |
| 1526 | A. Use the built-in function apply(). For instance, |
| 1527 | |
| 1528 | func(1, 2, 3) |
| 1529 | |
| 1530 | is equivalent to |
| 1531 | |
| 1532 | args = (1, 2, 3) |
| 1533 | apply(func, args) |
| 1534 | |
| 1535 | Note that func(args) is not the same -- it calls func() with exactly |
| 1536 | one argument, the tuple args, instead of three arguments, the integers |
| 1537 | 1, 2 and 3. |
| 1538 | |
Guido van Rossum | 7be34a8 | 1995-05-31 15:17:12 +0000 | [diff] [blame] | 1539 | 4.32. Q. How do I enable font-lock-mode for Python in Emacs? |
| 1540 | |
| 1541 | A. Assuming you're already using python-mode and font-lock-mode |
| 1542 | separately, all you need to do is put this in your .emacs file: |
| 1543 | |
| 1544 | (defun my-python-mode-hook () |
| 1545 | (setq font-lock-keywords python-font-lock-keywords) |
| 1546 | (font-lock-mode 1)) |
| 1547 | (add-hook 'python-mode-hook 'my-python-mode-hook) |
| 1548 | |
| 1549 | 4.33. Q. Is there an inverse to the format operator (a la C's scanf())? |
| 1550 | |
| 1551 | A. Not as such. |
| 1552 | |
| 1553 | For simple input parsing, the easiest approach is usually to split |
| 1554 | the line into whitespace-delimited words using string.split(), and to |
| 1555 | convert decimal strings to numeric values using string.atoi(), |
| 1556 | string.atol() or string.atof(). (Python's atoi() is 32-bit and its |
| 1557 | atol() is arbitrary precision.) If you want to use another delimiter |
| 1558 | than whitespace, use string.splitfield() (possibly combining it with |
| 1559 | string.strip() which removes surrounding whitespace from a string). |
| 1560 | |
| 1561 | For more complicated input parsing, regular expressions (see module |
| 1562 | regex) are better suited and more powerful than C's scanf(). |
| 1563 | |
Guido van Rossum | 061f182 | 1994-10-06 16:03:45 +0000 | [diff] [blame] | 1564 | |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 1565 | 5. Extending Python |
| 1566 | =================== |
| 1567 | |
| 1568 | 5.1. Q. Can I create my own functions in C? |
| 1569 | |
| 1570 | A. Yes, you can create built-in modules containing functions, |
Guido van Rossum | 2434999 | 1994-02-02 14:12:45 +0000 | [diff] [blame] | 1571 | variables, exceptions and even new types in C. This is explained in |
| 1572 | the document "Extending and Embedding the Python Interpreter" (the |
| 1573 | 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] | 1574 | |
| 1575 | 5.2. Q. Can I create my own functions in C++? |
| 1576 | |
| 1577 | A. Yes, using the C-compatibility features found in C++. Basically |
| 1578 | you place extern "C" { ... } around the Python include files and put |
| 1579 | extern "C" before each function that is going to be called by the |
| 1580 | Python interpreter. Global or static C++ objects with constructors |
| 1581 | are probably not a good idea. |
| 1582 | |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 1583 | 5.3. Q. How can I execute arbitrary Python statements from C? |
| 1584 | |
| 1585 | A. The highest-level function to do this is run_command() which takes |
| 1586 | a single string argument which is executed in the context of module |
| 1587 | __main__ and returns 0 for success and -1 when an exception occurred |
| 1588 | (including SyntaxError). If you want more control, use run_string(); |
| 1589 | see the source for run_command() in Python/pythonrun.c. |
| 1590 | |
| 1591 | 5.4. Q. How can I evaluate an arbitrary Python expression from C? |
| 1592 | |
| 1593 | A. Call the function run_string() from the previous question with the |
| 1594 | start symbol eval_input; it then parses an expression, evaluates it |
| 1595 | and returns its value. See exec_eval() in Python/bltinmodule.c. |
| 1596 | |
| 1597 | 5.5. Q. How do I extract C values from a Python object? |
| 1598 | |
| 1599 | A. That depends on the object's type. If it's a tuple, |
| 1600 | gettuplesize(o) returns its length and gettupleitem(o, i) returns its |
| 1601 | i'th item; similar for lists with getlistsize(o) and getlistitem(o, |
| 1602 | i). For strings, getstringsize(o) returns its length and |
| 1603 | getstringvalue(o) a pointer to its value (note that Python strings may |
| 1604 | contain null bytes so strlen() is not safe). To test which type an |
| 1605 | object is, first make sure it isn't NULL, and then use |
| 1606 | is_stringobject(o), is_tupleobject(o), is_listobject(o) etc. |
| 1607 | |
| 1608 | 5.6. Q. How do I use mkvalue() to create a tuple of arbitrary length? |
| 1609 | |
| 1610 | A. You can't. Use t = newtupleobject(n) instead, and fill it with |
| 1611 | objects using settupleitem(t, i, o) -- note that this "eats" a |
| 1612 | reference count of o. Similar for lists with newlistobject(n) and |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 1613 | setlistitem(l, i, o). Note that you *must* set all the tuple items to |
| 1614 | some value before you pass the tuple to Python code -- |
| 1615 | newtupleobject(n) initializes them to NULL, which isn't a valid Python |
| 1616 | value. |
| 1617 | |
Guido van Rossum | 796b259 | 1995-01-20 23:05:52 +0000 | [diff] [blame] | 1618 | 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] | 1619 | Extensions manual? |
| 1620 | |
| 1621 | A. It's a typo, I meant newtupleobject() (see previous question). |
| 1622 | |
| 1623 | 5.8. Q. How do I call an object's method from C? |
| 1624 | |
| 1625 | A. Here's a function (untested) that might become part of the next |
| 1626 | release in some form. It uses <stdarg.h> to allow passing the |
| 1627 | argument list on to vmkvalue(): |
| 1628 | |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 1629 | object *call_method(object *inst, char *methodname, char *format, ...) |
| 1630 | { |
| 1631 | object *method; |
| 1632 | object *args; |
| 1633 | object *result; |
| 1634 | va_list va; |
| 1635 | method = getattr(inst, methodname); |
| 1636 | if (method == NULL) return NULL; |
| 1637 | va_start(va, format); |
| 1638 | args = vmkvalue(format, va); |
| 1639 | va_end(va); |
| 1640 | if (args == NULL) { |
| 1641 | DECREF(method); |
| 1642 | return NULL; |
| 1643 | } |
| 1644 | result = call_object(method, args); |
| 1645 | DECREF(method); |
| 1646 | DECREF(args); |
| 1647 | return result; |
| 1648 | } |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 1649 | |
| 1650 | This works for any instance that has methods -- whether built-in or |
| 1651 | user-defined. You are responsible for eventually DECREF'ing the |
| 1652 | return value. |
| 1653 | |
| 1654 | To call, e.g., a file object's "seek" method with arguments 10, 0 |
| 1655 | (assuming the file object pointer is "f"): |
| 1656 | |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 1657 | res = call_method(f, "seek", "(OO)", 10, 0); |
| 1658 | if (res == NULL) { |
| 1659 | ... an exception occurred ... |
| 1660 | } |
| 1661 | else { |
| 1662 | DECREF(res); |
| 1663 | } |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 1664 | |
| 1665 | Note that since call_object() *always* wants a tuple for the argument |
| 1666 | list, to call a function without arguments, pass "()" for the format, |
| 1667 | and to call a function with one argument, surround the argument in |
| 1668 | parentheses, e.g. "(i)". |
| 1669 | |
| 1670 | 5.9. Q. How do I catch the output from print_error()? |
| 1671 | |
| 1672 | A. (Due to Mark Hammond): |
| 1673 | |
| 1674 | * in Python code, define an object that supports the "write()" method. |
| 1675 | FWIW, there seems to be a small problem that requires the 'softspace' |
Guido van Rossum | a8a8d4a | 1995-03-10 16:19:31 +0000 | [diff] [blame] | 1676 | attribute to be defined too (fixed in 1.2). |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 1677 | |
| 1678 | * redirect sys.stdout and sys.stderr to this object. |
| 1679 | |
Guido van Rossum | 061f182 | 1994-10-06 16:03:45 +0000 | [diff] [blame] | 1680 | * call print_error, or just allow the standard traceback mechanism to |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 1681 | work. |
| 1682 | |
Guido van Rossum | 061f182 | 1994-10-06 16:03:45 +0000 | [diff] [blame] | 1683 | Then, the output will go wherever your write() method sends it. |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 1684 | |
| 1685 | 5.10. Q. How do I access a module written in Python from C? |
| 1686 | |
| 1687 | A. You can get a pointer to the module object as follows: |
| 1688 | |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 1689 | module = import_module("<modulename>"); |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 1690 | |
| 1691 | If the module hasn't been imported yet (i.e. it is not yet present in |
| 1692 | sys.modules), this initializes the module; otherwise it simply returns |
| 1693 | the value of sys.modules["<modulename>"]. Note that it doesn't enter |
| 1694 | the module into any namespace -- it only ensures it has been |
| 1695 | initialized and is stored in sys.modules. |
| 1696 | |
| 1697 | You can then access the module's attributes (i.e. any name defined in |
| 1698 | the module) as follows: |
| 1699 | |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 1700 | attr = getattr(module, "<attrname>"); |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 1701 | |
| 1702 | Calling setattr(), to assign to variables in the module, also works. |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 1703 | |
Guido van Rossum | b0a2ce5 | 1995-08-28 19:41:35 +0000 | [diff] [blame] | 1704 | 5.11. Q. How do I interface to C++ objects from Python? |
| 1705 | |
| 1706 | A. Depending on your requirements, there are many approaches. Begin |
| 1707 | by reading the "Extending and Embedding" document (Doc/ext.tex, see |
| 1708 | also <URL:http://www.python.org/doc/>). Realize that for the Python |
| 1709 | run-time system, there isn't a whole lot of difference between C and |
| 1710 | C++ -- so the strategy to build a new Python type around a C structure |
| 1711 | (pointer) type will also work for C++ objects. |
| 1712 | |
| 1713 | Automatic generation of interfaces between Python and C++ is still at |
| 1714 | the horizon -- parsing C++ header files requires an almost complete |
| 1715 | C++ parser, and many features aren't easily translated from C++ to |
| 1716 | Python: certain forms of operator oveloading, function overloading |
| 1717 | (best approached by a varargs function which explicitly type-checks |
| 1718 | its arguments), and reference arguments are just a number of features |
| 1719 | that are hard to translate correctly if at all. |
| 1720 | |
| 1721 | The hardest problem is to transparently translate the C++ class |
| 1722 | hierarchy to Python, so that Python programs derive classes from C++ |
| 1723 | classes. Given suitable constraints, this may be possible, but it |
| 1724 | would require more space than I have in this FAQ to explain how. |
| 1725 | In any case, you can get quite a bit done without this, using just the |
| 1726 | existing classes from Python. |
| 1727 | |
| 1728 | If this all seems rather daunting, that may be because it is -- C++ |
| 1729 | isn't exactly a baby to handle without gloves! However, people have |
| 1730 | accomplished amazing feats of interfacing between Python and C++, and |
| 1731 | a detailed question posted to the Python list is likely to elicit some |
| 1732 | interesting and useful responses. |
| 1733 | |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 1734 | |
| 1735 | 6. Python's design |
| 1736 | ================== |
| 1737 | |
Guido van Rossum | b0a2ce5 | 1995-08-28 19:41:35 +0000 | [diff] [blame] | 1738 | 6.1. Q. Why isn't there a generic copying operation for objects in Python? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 1739 | |
Guido van Rossum | b0a2ce5 | 1995-08-28 19:41:35 +0000 | [diff] [blame] | 1740 | A. Historically, there wasn't. However, starting in Python 1.2, |
| 1741 | there's a library module "copy" which supports both shallow and deep |
| 1742 | copying of most common Python objects (though not things like open |
| 1743 | files, sockets or windows), including an extensible mechanism to copy |
| 1744 | class instances. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 1745 | |
| 1746 | 6.2. Q. Why isn't there a generic way to implement persistent objects |
| 1747 | in Python? (Persistent == automatically saved to and restored from |
| 1748 | disk.) |
| 1749 | |
Guido van Rossum | b0a2ce5 | 1995-08-28 19:41:35 +0000 | [diff] [blame] | 1750 | A. Like the previous question, historically, there wasn't. The |
| 1751 | library module "pickle" now solves this in a very general way (though |
| 1752 | you still can't store things like open files, sockests or windows), |
| 1753 | and the library module "shelve" uses pickle and (g)dbm to create |
| 1754 | presistent mappings containing arbitrary Python objects. There are |
| 1755 | some problems with shelve when using gdbm which will be solved in |
| 1756 | Python 1.3. |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 1757 | |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 1758 | 6.3. Q. Why isn't there a switch or case statement in Python? |
| 1759 | |
| 1760 | A. You can do this easily enough with a sequence of |
| 1761 | if... elif... elif... else. There have been some proposals for switch |
Guido van Rossum | 061f182 | 1994-10-06 16:03:45 +0000 | [diff] [blame] | 1762 | 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] | 1763 | to do range tests. |
| 1764 | |
Guido van Rossum | c50158e | 1994-05-31 09:18:50 +0000 | [diff] [blame] | 1765 | 6.4. Q. Why does Python use indentation for grouping of statements? |
| 1766 | |
| 1767 | A. Basically I believe that using indentation for grouping is |
| 1768 | extremely elegant and contributes a lot to the clarity of the average |
| 1769 | Python program. Most people learn to love this feature after a while. |
| 1770 | Some arguments for it: |
| 1771 | |
| 1772 | - Since there are no begin/end brackets there cannot be a disagreement |
| 1773 | between grouping perceived by the parser and the human reader. I |
| 1774 | remember long ago seeing a C fragment like this: |
| 1775 | |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 1776 | if (x <= y) |
| 1777 | x++; |
| 1778 | y--; |
| 1779 | z++; |
Guido van Rossum | c50158e | 1994-05-31 09:18:50 +0000 | [diff] [blame] | 1780 | |
| 1781 | and staring a long time at it wondering why y was being decremented |
| 1782 | even for x > y... (And I wasn't a C newbie then either.) |
| 1783 | |
Guido van Rossum | 7be34a8 | 1995-05-31 15:17:12 +0000 | [diff] [blame] | 1784 | - Since there are no begin/end brackets, Python is much less prone to |
| 1785 | coding-style conflicts. In C there are loads of different ways to |
| 1786 | place the braces (including the choice whether to place braces around |
| 1787 | single statements in certain cases, for consistency). If you're used |
| 1788 | to reading (and writing) code that uses one style, you will feel at |
| 1789 | least slightly uneasy when reading (or being required to write) |
| 1790 | another style. |
Guido van Rossum | c50158e | 1994-05-31 09:18:50 +0000 | [diff] [blame] | 1791 | |
| 1792 | - Many coding styles place begin/end brackets on a line by themself. |
| 1793 | This makes programs considerably longer and wastes valuable screen |
| 1794 | space, making it harder to get a good overview over a program. |
| 1795 | Ideally, a function should fit on one basic tty screen (say, 20 |
| 1796 | lines). 20 lines of Python are worth a LOT more than 20 lines of C. |
| 1797 | This is not solely due to the lack of begin/end brackets (the lack of |
| 1798 | declarations also helps, and the powerful operations of course), but |
| 1799 | it certainly helps! |
| 1800 | |
Guido van Rossum | 3de2736 | 1994-07-25 14:19:33 +0000 | [diff] [blame] | 1801 | 6.5. Q. Why are Python strings immutable? |
| 1802 | |
| 1803 | A. There are two advantages. One is performance: knowing that a |
| 1804 | string is immutable makes it easy to lay it out at construction time |
| 1805 | -- fixed and unchanging storage requirements. (This is also one of |
Guido van Rossum | 061f182 | 1994-10-06 16:03:45 +0000 | [diff] [blame] | 1806 | the reasons for the distinction between tuples and lists.) The |
Guido van Rossum | 3de2736 | 1994-07-25 14:19:33 +0000 | [diff] [blame] | 1807 | other is that strings in Python are considered as "elemental" as |
| 1808 | numbers. No amount of activity will change the value 8 to anything |
| 1809 | else, and in Python, no amount of activity will change the string |
| 1810 | "eight" to anything else. (Adapted from Jim Roskind) |
| 1811 | |
| 1812 | 6.6. Q. Why don't strings have methods like index() or sort(), like |
| 1813 | lists? |
| 1814 | |
| 1815 | A. Good question. Strings currently don't have methods at all |
| 1816 | (likewise tuples and numbers). Long ago, it seemed unnecessary to |
| 1817 | implement any of these functions in C, so a standard library module |
| 1818 | "string" written in Python was created that performs string related |
| 1819 | operations. Since then, the cry for performance has moved most of |
| 1820 | 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] | 1821 | string, which is still the preferred interface, without loss of |
Guido van Rossum | 3de2736 | 1994-07-25 14:19:33 +0000 | [diff] [blame] | 1822 | performance except during initialization). Some of these functions |
| 1823 | (e.g. index()) could easily be implemented as string methods instead, |
| 1824 | but others (e.g. sort()) can't, since their interface prescribes that |
| 1825 | they modify the object, while strings are immutable (see the previous |
| 1826 | question). |
| 1827 | |
| 1828 | 6.7. Q. Why does Python use methods for some functionality |
| 1829 | (e.g. list.index()) but functions for other (e.g. len(list))? |
| 1830 | |
| 1831 | A. Functions are used for those operations that are generic for a |
| 1832 | group of types and which should work even for objects that don't have |
| 1833 | methods at all (e.g. numbers, strings, tuples). Also, implementing |
| 1834 | len(), max(), min() as a built-in function is actually less code than |
| 1835 | implementing them as methods for each type. One can quibble about |
| 1836 | individual cases but it's really too late to change such things |
| 1837 | fundamentally now. |
| 1838 | |
| 1839 | 6.8. Q. Why can't I derive a class from built-in types (e.g. lists or |
| 1840 | files)? |
| 1841 | |
| 1842 | A. This is caused by the relatively late addition of (user-defined) |
| 1843 | classes to the language -- the implementation framework doesn't easily |
| 1844 | allow it. See the answer to question 4.2 for a work-around. This |
| 1845 | *may* be fixed in the (distant) future. |
| 1846 | |
| 1847 | 6.9. Q. Why must 'self' be declared and used explicitly in method |
| 1848 | definitions and calls? |
| 1849 | |
| 1850 | A. By asking this question you reveal your C++ background. :-) |
| 1851 | When I added classes, this was (again) the simplest way of |
| 1852 | implementing methods without too many changes to the interpreter. I |
| 1853 | borrowed the idea from Modula-3. It turns out to be very useful, for |
| 1854 | a variety of reasons. |
| 1855 | |
| 1856 | First, it makes it more obvious that you are using a method or |
| 1857 | instance attribute instead of a local variable. Reading "self.x" or |
| 1858 | "self.meth()" makes it absolutely clear that an instance variable or |
| 1859 | method is used even if you don't know the class definition by heart. |
| 1860 | 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] | 1861 | declaration (assuming globals are rare or easily recognizable) -- but |
Guido van Rossum | 3de2736 | 1994-07-25 14:19:33 +0000 | [diff] [blame] | 1862 | in Python, there are no local variable declarations, so you'd have to |
| 1863 | look up the class definition to be sure. |
| 1864 | |
| 1865 | Second, it means that no special syntax is necessary if you want to |
| 1866 | explicitly reference or call the method from a particular class. In |
| 1867 | C++, if you want to use a method from base class that is overridden in |
| 1868 | a derived class, you have to use the :: operator -- in Python you can |
| 1869 | write baseclass.methodname(self, <argument list>). This is |
| 1870 | particularly useful for __init__() methods, and in general in cases |
| 1871 | where a derived class method wants to extend the base class method of |
| 1872 | the same name and thus has to call the base class method somehow. |
| 1873 | |
| 1874 | Lastly, for instance variables, it solves a syntactic problem with |
| 1875 | assignment: since local variables in Python are (by definition!) those |
| 1876 | variables to which a value assigned in a function body (and that |
| 1877 | aren't explicitly declared global), there has to be some way to tell |
| 1878 | the interpreter that an assignment was meant to assign to an instance |
| 1879 | variable instead of to a local variable, and it should preferably be |
| 1880 | syntactic (for efficiency reasons). C++ does this through |
| 1881 | declarations, but Python doesn't have declarations and it would be a |
| 1882 | pity having to introduce them just for this purpose. Using the |
| 1883 | explicit "self.var" solves this nicely. Similarly, for using instance |
| 1884 | variables, having to write "self.var" means that references to |
| 1885 | unqualified names inside a method don't have to search the instance's |
| 1886 | directories. |
| 1887 | |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 1888 | 6.10. Q. Can't you emulate threads in the interpreter instead of |
| 1889 | relying on an OS-specific thread implementation? |
| 1890 | |
| 1891 | A. Unfortunately, the interpreter pushes at least one C stack frame |
| 1892 | for each Python stack frame. Also, extensions can call back into |
| 1893 | Python at almost random moments. Therefore a complete threads |
| 1894 | implementation requires thread support for C. |
| 1895 | |
Guido van Rossum | 061f182 | 1994-10-06 16:03:45 +0000 | [diff] [blame] | 1896 | 6.11. Q. Why can't lambda forms contain statements? |
| 1897 | |
| 1898 | A. Python lambda forms cannot contain statements because Python's |
Guido van Rossum | 796b259 | 1995-01-20 23:05:52 +0000 | [diff] [blame] | 1899 | syntactic framework can't handle statements nested inside expressions. |
Guido van Rossum | 061f182 | 1994-10-06 16:03:45 +0000 | [diff] [blame] | 1900 | |
| 1901 | However, in Python, this is not a serious problem. Unlike lambda |
| 1902 | forms in other languages, where they add functionality, Python lambdas |
| 1903 | are only a shorthand notation if you're too lazy to define a function. |
| 1904 | |
| 1905 | Functions are already first class objects in Python, and can be |
| 1906 | declared in a local scope. Therefore the only advantage of using a |
| 1907 | lambda form instead of a locally-defined function is that you'll have |
| 1908 | to invent a name for the function -- but that's just a local variable |
| 1909 | to which the function object (which is exactly the same type of object |
| 1910 | that a lambda form yields) is assigned! |
| 1911 | |
| 1912 | 6.12. Q. Why is there no more efficient way of iterating over a dictionary |
| 1913 | than first constructing the list of keys()? |
| 1914 | |
| 1915 | A. Have you tried it? I bet it's fast enough for your purposes! In |
| 1916 | most cases such a list takes only a few percent of the space occupied |
| 1917 | by the dictionary -- it needs only 4 bytes (the size of a pointer) per |
| 1918 | key -- a dictionary costs 8 bytes per key plus between 30 and 70 |
| 1919 | percent hash table overhead, plus the space for the keys and values -- |
| 1920 | by necessity all keys are unique objects and a string object (the most |
| 1921 | common key type) costs at least 18 bytes plus the length of the |
| 1922 | string. Add to that the values contained in the dictionary, and you |
| 1923 | see that 4 bytes more per item really isn't that much more memory... |
| 1924 | |
| 1925 | A call to dict.keys() makes one fast scan over the dictionary |
| 1926 | (internally, the iteration function does exist) copying the pointers |
| 1927 | to the key objects into a pre-allocated list object of the right size. |
| 1928 | The iteration time isn't lost (since you'll have to iterate anyway -- |
| 1929 | unless in the majority of cases your loop terminates very prematurely |
| 1930 | (which I doubt since you're getting the keys in random order). |
| 1931 | |
| 1932 | I don't expose the dictionary iteration operation to Python |
| 1933 | programmers because the dictionary shouldn't be modified during the |
| 1934 | entire iteration -- if it is, there's a very small chance that the |
| 1935 | dictionary is reorganized because the hash table becomes too full, and |
| 1936 | then the iteration may miss some items and see others twice. Exactly |
| 1937 | because this only occurs rarely, it would lead to hidden bugs in |
| 1938 | programs: it's easy never to have it happen during test runs if you |
| 1939 | only insert or delete a few items per iteration -- but your users will |
| 1940 | surely hit upon it sooner or later. |
| 1941 | |
Guido van Rossum | b0a2ce5 | 1995-08-28 19:41:35 +0000 | [diff] [blame] | 1942 | 6.13. Q. Can Python be compiled to machine code, C or some other language? |
| 1943 | |
| 1944 | A. Not easily. Python's high level data types, dynamic typing of |
| 1945 | objects and run-time invocation of the interpreter (using eval() or |
| 1946 | exec) together mean that a "compiled" Python program would probably |
| 1947 | consist mostly of calls into the Python run-time system, even for |
| 1948 | seemingly simple operations like "x+1". Thus, the performance gain |
| 1949 | would probably be minimal. |
| 1950 | |
| 1951 | Internally, Python source code is always translated into a "virtual |
| 1952 | machine code" or "byte code" representation before it is interpreted |
| 1953 | (by the "Python virtual machine" or "bytecode interpreter"). In order |
| 1954 | to avoid the overhead of parsing and translating modules that rarely |
| 1955 | change over and over again, this byte code is written on a file whose |
| 1956 | name ends in ".pyc" whenever a module is parsed (from a file whose |
| 1957 | name ends in ".py"). When the corresponding .py file is changed, it |
| 1958 | is parsed and translated again and the .pyc file is rewritten. There |
| 1959 | is no performance difference once the .pyc file has been loaded (the |
| 1960 | bytecode read from the .pyc file is exactly the same as the bytecode |
| 1961 | created by direct translation). The only difference is that loading |
| 1962 | code from a .pyc file is faster than parsing and translating a .py |
| 1963 | file, so the presence of precompiled .pyc files will generally improve |
| 1964 | start-up time of Python scripts. If desired, the Lib/compileall.py |
| 1965 | module/script can be used to force creation of valid .pyc files for a |
| 1966 | given set of modules. |
| 1967 | |
| 1968 | If you are looking for a way to translate Python programs in order to |
| 1969 | distribute them in binary form, without the need to distribute the |
| 1970 | interpreter and library as well, have a look at the freeze.py script |
| 1971 | in the Tools/freeze directory. This creates a single binary file |
| 1972 | incorporating your program, the Python interpreter, and those parts of |
| 1973 | the Python library that are needed by your program. Of course, the |
| 1974 | resulting binary will only run on the same type of platform as that |
| 1975 | used to create it. |
| 1976 | |
| 1977 | Hints for proper usage of freeze.py: |
| 1978 | |
| 1979 | - the script must be in a file whose name ends in .py |
| 1980 | |
| 1981 | - you must have installed Python fully: |
| 1982 | |
| 1983 | make install |
| 1984 | make libinstall |
| 1985 | make inclinstall |
| 1986 | make libainstall |
| 1987 | |
Guido van Rossum | 05151e0 | 1995-09-28 13:24:46 +0000 | [diff] [blame^] | 1988 | 6.14. Q. Why doesn't Python use proper garbage collection? |
| 1989 | |
| 1990 | A. It's looking less and less likely that Python will ever get |
| 1991 | "automatic" garbage collection (GC). For one thing, unless this were |
| 1992 | added to C as a standard feature, it's a portability pain in the ass. |
| 1993 | And yes, I know about the Xerox library. It has bits of assembler |
| 1994 | code for *most* *common* platforms. Not for all. And although it is |
| 1995 | mostly transparent, it isn't completely transparent (when I once |
| 1996 | linked Python with it, it dumped core). |
| 1997 | |
| 1998 | "Proper" GC also becomes a problem when Python gets embedded into |
| 1999 | other applications. While in a stand-alone Python it may be fine to |
| 2000 | replace the standard malloc() and free() with versions provided by the |
| 2001 | GC library, an application embedding Python may want to have its *own* |
| 2002 | substitute for malloc() and free(), and may not want Python's. Right |
| 2003 | now, Python works with anything that implements malloc() and free() |
| 2004 | properly. |
| 2005 | |
| 2006 | Besides, the predictability of destructor calls in Python is kind of |
| 2007 | attractive. With GC, the following code (which is fine in current |
| 2008 | Python) will run out of file descriptors long before it runs out of |
| 2009 | memory: |
| 2010 | |
| 2011 | for file in <very long list of files>: |
| 2012 | f = open(file) |
| 2013 | c = file.read(1) |
| 2014 | |
| 2015 | Using the current reference counting and destructor scheme, each new |
| 2016 | assignment to f closes the previous file. Using GC, this is not |
| 2017 | guaranteed. Sure, you can think of ways to fix this. But it's not |
| 2018 | off-the-shelf technology. |
| 2019 | |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 2020 | |
| 2021 | 7. Using Python on non-UNIX platforms |
| 2022 | ===================================== |
| 2023 | |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 2024 | 7.1. Q. Is there a Mac version of Python? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 2025 | |
Guido van Rossum | e530c58 | 1995-04-10 12:32:16 +0000 | [diff] [blame] | 2026 | A. Yes, see the "mac" subdirectory of the distribution sites, |
Guido van Rossum | 7be34a8 | 1995-05-31 15:17:12 +0000 | [diff] [blame] | 2027 | e.g. <URL:ftp://ftp.python.org/pub/python/mac/>. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 2028 | |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 2029 | 7.2. Q. Is there a DOS version of Python? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 2030 | |
Guido van Rossum | e530c58 | 1995-04-10 12:32:16 +0000 | [diff] [blame] | 2031 | A. Yes, see the "pc" subdirectory of the distribution sites, |
Guido van Rossum | 7be34a8 | 1995-05-31 15:17:12 +0000 | [diff] [blame] | 2032 | e.g. <URL:ftp://ftp.python.org/pub/python/pc/>. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 2033 | |
Guido van Rossum | e530c58 | 1995-04-10 12:32:16 +0000 | [diff] [blame] | 2034 | 7.3. Q. Is there a Windows 3.1(1) version of Python? |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 2035 | |
Guido van Rossum | e530c58 | 1995-04-10 12:32:16 +0000 | [diff] [blame] | 2036 | A. Yes, also see the "pc" subdirectory of the distribution sites, |
Guido van Rossum | 3fc9d73 | 1995-07-25 15:10:56 +0000 | [diff] [blame] | 2037 | e.g. <URL:ftp://ftp.python.org/pub/python/pc/>. You may also be able |
| 2038 | to run either of the Windows NT versions (see next question) if you |
| 2039 | have Microsoft's "win32s". |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 2040 | |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 2041 | 7.4. Q. Is there a Windows NT version of Python? |
| 2042 | |
Guido van Rossum | 3fc9d73 | 1995-07-25 15:10:56 +0000 | [diff] [blame] | 2043 | A. There are two, both sporting DLL support for dynamic loading of |
| 2044 | Python modules, and extensions to access the Win32 GUI API. |
| 2045 | |
| 2046 | Mark Hammond <MHammond@cmutual.com.au> maintains an NT port which |
| 2047 | includes an interface to the Microsoft Foundation Classes and a Python |
| 2048 | programming environment using it that's written mostly in Python. See |
| 2049 | <URL:ftp://ftp.python.org/pub/python/nt/>. |
| 2050 | |
| 2051 | Jim Ahlstrom's WPY portable GUI runs on Windows NT and is modeled |
| 2052 | after the Microsoft Foundation Classes. Source and binaries are |
| 2053 | available in <URL:ftp://ftp.python.org/pub/python/wpy>. |
Guido van Rossum | 91f6083 | 1994-02-15 15:52:27 +0000 | [diff] [blame] | 2054 | |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 2055 | Sam Rushing <rushing@squirl.oau.org> once announced he knows how to |
| 2056 | build Python for the Windows NT on the DEC Alpha AXP. |
Guido van Rossum | 061f182 | 1994-10-06 16:03:45 +0000 | [diff] [blame] | 2057 | |
| 2058 | Note that currently there is no unified compilation environment for |
| 2059 | all NT platforms -- hopefully Microsoft will fix this with the release |
| 2060 | of Visual C++ 2.0. |
| 2061 | |
Guido van Rossum | e530c58 | 1995-04-10 12:32:16 +0000 | [diff] [blame] | 2062 | 7.5. Q. Is there a Windows 95 version of Python? |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 2063 | |
Guido van Rossum | 3fc9d73 | 1995-07-25 15:10:56 +0000 | [diff] [blame] | 2064 | A. The Windows NT versions might work, otherwise the Windows 3.1(1) |
Guido van Rossum | e530c58 | 1995-04-10 12:32:16 +0000 | [diff] [blame] | 2065 | version should work (isn't Windows 95 supposed to be backwards |
| 2066 | compatible?). |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 2067 | |
Guido van Rossum | e530c58 | 1995-04-10 12:32:16 +0000 | [diff] [blame] | 2068 | 7.6. Q. Is there an OS/2 version of Python? |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 2069 | |
Guido van Rossum | e530c58 | 1995-04-10 12:32:16 +0000 | [diff] [blame] | 2070 | A. Yes, also see the "pc" subdirectory of the distribution sites, |
Guido van Rossum | 7be34a8 | 1995-05-31 15:17:12 +0000 | [diff] [blame] | 2071 | e.g. <URL:ftp://ftp.python.org/pub/python/pc/>. |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 2072 | |
Guido van Rossum | e530c58 | 1995-04-10 12:32:16 +0000 | [diff] [blame] | 2073 | 7.7. Q. Is there a VMS version of Python? |
| 2074 | |
Guido van Rossum | 3fc9d73 | 1995-07-25 15:10:56 +0000 | [diff] [blame] | 2075 | A. Donn Cave <donn@cac.washington.edu> did a partial port. The |
| 2076 | results of his efforts are on public display in |
Guido van Rossum | bf8e7d5 | 1995-08-28 03:09:13 +0000 | [diff] [blame] | 2077 | <<URL:ftp://ftp.python.org/pub/python/contrib/vms.tar.gz/>. Someone |
| 2078 | else is working on a more complete port, for details watch the list. |
Guido van Rossum | e530c58 | 1995-04-10 12:32:16 +0000 | [diff] [blame] | 2079 | |
Guido van Rossum | 3fc9d73 | 1995-07-25 15:10:56 +0000 | [diff] [blame] | 2080 | 7.8. Q. What about IBM mainframes, or other non-UNIX platforms? |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 2081 | |
Guido van Rossum | b0a2ce5 | 1995-08-28 19:41:35 +0000 | [diff] [blame] | 2082 | A. I haven't heard about these, except I remember hearing about an |
| 2083 | OS/9 port and a port to Vxworks (both operating systems for embedded |
| 2084 | systems). If you're interested in any of this, go directly to the |
| 2085 | newsgroup and ask there, you may find exactly what you need. For |
| 2086 | example, a port to MPE/iX 5.0 on HP3000 computers was just announced, |
| 2087 | see <URL:http://www.allegro.com/software/>. |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 2088 | |
Guido van Rossum | e530c58 | 1995-04-10 12:32:16 +0000 | [diff] [blame] | 2089 | 7.9. Q. Where are the source or Makefiles for the non-UNIX versions? |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 2090 | |
Guido van Rossum | e530c58 | 1995-04-10 12:32:16 +0000 | [diff] [blame] | 2091 | A. The standard sources can (almost) be used. Additional sources can |
| 2092 | be found in the platform-specific subdirectories of the distribution. |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 2093 | |
Guido van Rossum | e530c58 | 1995-04-10 12:32:16 +0000 | [diff] [blame] | 2094 | 7.10. Q. What is the status and support for the non-UNIX versions? |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 2095 | |
| 2096 | A. I don't have access to most of these platforms, so in general I am |
| 2097 | dependent on material submitted by volunteers(*). However I strive to |
| 2098 | integrate all changes needed to get it to compile on a particular |
| 2099 | platform back into the standard sources, so porting of the next |
| 2100 | version to the various non-UNIX platforms should be easy. |
| 2101 | |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 2102 | (*) For the Macintosh, that volunteer is me, with help from Jack |
| 2103 | Jansen <jack@cwi.nl>. |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 2104 | |
Guido van Rossum | e530c58 | 1995-04-10 12:32:16 +0000 | [diff] [blame] | 2105 | 7.11. Q. I have a PC version but it appears to be only a binary. |
Guido van Rossum | 7ce61c1 | 1994-06-13 15:13:56 +0000 | [diff] [blame] | 2106 | Where's the library? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 2107 | |
| 2108 | 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] | 2109 | "python/Lib" to your system. If you don't have the full distribution, |
Guido van Rossum | e530c58 | 1995-04-10 12:32:16 +0000 | [diff] [blame] | 2110 | you can get the file lib<version>.tar.gz from most ftp sites carrying |
| 2111 | Python; this is a subset of the distribution containing just those |
Guido van Rossum | 7be34a8 | 1995-05-31 15:17:12 +0000 | [diff] [blame] | 2112 | files, e.g. <URL:ftp://ftp.python.org/pub/python/src/lib1.1.tar.gz>. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 2113 | |
Guido van Rossum | 5333c5d | 1994-04-11 11:06:22 +0000 | [diff] [blame] | 2114 | Once you have installed the library, you need to point sys.path to it. |
| 2115 | Assuming the library is in C:\misc\python\lib, the following commands |
| 2116 | will point your Python interpreter to it (note the doubled backslashes |
| 2117 | -- you can also use single forward slashes instead): |
| 2118 | |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 2119 | >>> import sys |
| 2120 | >>> sys.path.insert(0, 'C:\\misc\\python\\lib') |
| 2121 | >>> |
Guido van Rossum | 5333c5d | 1994-04-11 11:06:22 +0000 | [diff] [blame] | 2122 | |
| 2123 | For a more permanent effect, set the environment variable PYTHONPATH, |
| 2124 | as follows (talking to a DOS prompt): |
| 2125 | |
Guido van Rossum | a6c707c | 1995-01-02 17:32:28 +0000 | [diff] [blame] | 2126 | C> SET PYTHONPATH=C:\misc\python\lib |
Guido van Rossum | 5333c5d | 1994-04-11 11:06:22 +0000 | [diff] [blame] | 2127 | |
Guido van Rossum | e530c58 | 1995-04-10 12:32:16 +0000 | [diff] [blame] | 2128 | 7.12. Q. Where's the documentation for the Mac or PC version? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 2129 | |
Guido van Rossum | e530c58 | 1995-04-10 12:32:16 +0000 | [diff] [blame] | 2130 | A. The documentation for the Unix version also applies to the Mac and |
| 2131 | PC versions. Where applicable, differences are indicated in the text. |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 2132 | |
Guido van Rossum | e530c58 | 1995-04-10 12:32:16 +0000 | [diff] [blame] | 2133 | 7.13. 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] | 2134 | creating or editing programs apart from entering it interactively, and |
| 2135 | there seems to be no way to save code that was entered interactively. |
| 2136 | How do I create a Python program on the Mac (PC)? |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 2137 | |
Guido van Rossum | b34e8aa | 1994-09-23 14:20:38 +0000 | [diff] [blame] | 2138 | A. Use an external editor. On the Mac, BBEdit seems to be a popular |
| 2139 | no-frills text editor. I work like this: start the interpreter; edit |
| 2140 | a module file using BBedit; import and test it in the interpreter; |
| 2141 | edit again in BBedit; then use the built-in function reload() to |
| 2142 | re-read the imported module; etc. |
Guido van Rossum | 5333c5d | 1994-04-11 11:06:22 +0000 | [diff] [blame] | 2143 | |
| 2144 | Regarding the same question for the PC, Kurt Wm. Hemr writes: "While |
| 2145 | anyone with a pulse could certainly figure out how to do the same on |
| 2146 | MS-Windows, I would recommend the NotGNU Emacs clone for MS-Windows. |
| 2147 | Not only can you easily resave and "reload()" from Python after making |
| 2148 | changes, but since WinNot auto-copies to the clipboard any text you |
| 2149 | select, you can simply select the entire procedure (function) which |
| 2150 | you changed in WinNot, switch to QWPython, and shift-ins to reenter |
| 2151 | the changed program unit." |