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