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