~ubuntu-branches/ubuntu/raring/python3.3/raring-proposed

« back to all changes in this revision

Viewing changes to debian/FAQ.html

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2012-03-22 06:14:01 UTC
  • Revision ID: package-import@ubuntu.com-20120322061401-vvrgvw3nvi68rtqq
Tags: 3.3.0~a1-1
* Python 3.3.0 alpha1 release.
* Update to 20120321 from the trunk.
* Update debian/copyright.
* Build-depend on expat (>= 2.1~).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<HTML>
 
2
<HEAD>
 
3
<TITLE>The Whole Python FAQ</TITLE>
 
4
</HEAD>
 
5
 
 
6
<BODY BACKGROUND="http://www.python.org/pics/RedShort.gif"
 
7
      BGCOLOR="#FFFFFF"
 
8
      TEXT="#000000"
 
9
      LINK="#AA0000"
 
10
      VLINK="#906A6A">
 
11
<H1>The Whole Python FAQ</H1>
 
12
Last changed on Wed Feb 12 21:31:08 2003 CET
 
13
 
 
14
<P>(Entries marked with ** were changed within the last 24 hours;
 
15
entries marked with * were changed within the last 7 days.)
 
16
<P>
 
17
 
 
18
<P>
 
19
<HR>
 
20
<H2>1. General information and availability</H2>
 
21
<UL>
 
22
<LI><A HREF="#1.1">1.1. What is Python?</A>
 
23
<LI><A HREF="#1.2">1.2. Why is it called Python?</A>
 
24
<LI><A HREF="#1.3">1.3. How do I obtain a copy of the Python source?</A>
 
25
<LI><A HREF="#1.4">1.4. How do I get documentation on Python?</A>
 
26
<LI><A HREF="#1.5">1.5. Are there other ftp sites that mirror the Python distribution?</A>
 
27
<LI><A HREF="#1.6">1.6. Is there a newsgroup or mailing list devoted to Python?</A>
 
28
<LI><A HREF="#1.7">1.7. Is there a WWW page devoted to Python?</A>
 
29
<LI><A HREF="#1.8">1.8. Is the Python documentation available on the WWW?</A>
 
30
<LI><A HREF="#1.9">1.9. Are there any books on Python?</A>
 
31
<LI><A HREF="#1.10">1.10. Are there any published articles about Python that I can reference?</A>
 
32
<LI><A HREF="#1.11">1.11. Are there short introductory papers or talks on Python?</A>
 
33
<LI><A HREF="#1.12">1.12. How does the Python version numbering scheme work?</A>
 
34
<LI><A HREF="#1.13">1.13. How do I get a beta test version of Python?</A>
 
35
<LI><A HREF="#1.14">1.14. Are there copyright restrictions on the use of Python?</A>
 
36
<LI><A HREF="#1.15">1.15. Why was Python created in the first place?</A>
 
37
<LI><A HREF="#1.16">1.16. Do I have to like &quot;Monty Python's Flying Circus&quot;?</A>
 
38
<LI><A HREF="#1.17">1.17. What is Python good for?</A>
 
39
<LI><A HREF="#1.18">1.18. Can I use the FAQ Wizard software to maintain my own FAQ?</A>
 
40
<LI><A HREF="#1.19">1.19. Which editor has good support for editing Python source code?</A>
 
41
<LI><A HREF="#1.20">1.20. I've never programmed before. Is there a Python tutorial?</A>
 
42
<LI><A HREF="#1.21">1.21. Where in the world is www.python.org located?</A>
 
43
 
 
44
</UL>
 
45
 
 
46
<P>
 
47
<HR>
 
48
<H2>2. Python in the real world</H2>
 
49
<UL>
 
50
<LI><A HREF="#2.1">2.1. How many people are using Python?</A>
 
51
<LI><A HREF="#2.2">2.2. Have any significant projects been done in Python?</A>
 
52
<LI><A HREF="#2.3">2.3. Are there any commercial projects going on using Python?</A>
 
53
<LI><A HREF="#2.4">2.4. How stable is Python?</A>
 
54
<LI><A HREF="#2.5">2.5. What new developments are expected for Python in the future?</A>
 
55
<LI><A HREF="#2.6">2.6. Is it reasonable to propose incompatible changes to Python?</A>
 
56
<LI><A HREF="#2.7">2.7. What is the future of Python?</A>
 
57
<LI><A HREF="#2.8">2.8. What was the PSA, anyway?</A>
 
58
<LI><A HREF="#2.9">2.9. Deleted</A>
 
59
<LI><A HREF="#2.10">2.10. Deleted</A>
 
60
<LI><A HREF="#2.11">2.11. Is Python Y2K (Year 2000) Compliant?</A>
 
61
<LI><A HREF="#2.12">2.12. Is Python a good language in a class for beginning programmers?</A>
 
62
 
 
63
</UL>
 
64
 
 
65
<P>
 
66
<HR>
 
67
<H2>3. Building Python and Other Known Bugs</H2>
 
68
<UL>
 
69
<LI><A HREF="#3.1">3.1. Is there a test set?</A>
 
70
<LI><A HREF="#3.2">3.2. When running the test set, I get complaints about floating point operations, but when playing with floating point operations I cannot find anything wrong with them.</A>
 
71
<LI><A HREF="#3.3">3.3. Link errors after rerunning the configure script.</A>
 
72
<LI><A HREF="#3.4">3.4. The python interpreter complains about options passed to a script (after the script name).</A>
 
73
<LI><A HREF="#3.5">3.5. When building on the SGI, make tries to run python to create glmodule.c, but python hasn't been built or installed yet.</A>
 
74
<LI><A HREF="#3.6">3.6. I use VPATH but some targets are built in the source directory.</A>
 
75
<LI><A HREF="#3.7">3.7. Trouble building or linking with the GNU readline library.</A>
 
76
<LI><A HREF="#3.8">3.8. Trouble with socket I/O on older Linux 1.x versions.</A>
 
77
<LI><A HREF="#3.9">3.9. Trouble with prototypes on Ultrix.</A>
 
78
<LI><A HREF="#3.10">3.10. Other trouble building Python on platform X.</A>
 
79
<LI><A HREF="#3.11">3.11. How to configure dynamic loading on Linux.</A>
 
80
<LI><A HREF="#3.12">3.12. I can't get shared modules to work on Linux 2.0 (Slackware96)?</A>
 
81
<LI><A HREF="#3.13">3.13. Trouble when making modules shared on Linux.</A>
 
82
<LI><A HREF="#3.14">3.14. [deleted]</A>
 
83
<LI><A HREF="#3.15">3.15. Errors when linking with a shared library containing C++ code.</A>
 
84
<LI><A HREF="#3.16">3.16. Deleted</A>
 
85
<LI><A HREF="#3.17">3.17. Deleted.</A>
 
86
<LI><A HREF="#3.18">3.18. Compilation or link errors for the _tkinter module</A>
 
87
<LI><A HREF="#3.19">3.19. I configured and built Python for Tcl/Tk but &quot;import Tkinter&quot; fails.</A>
 
88
<LI><A HREF="#3.20">3.20. [deleted]</A>
 
89
<LI><A HREF="#3.21">3.21. Several common system calls are missing from the posix module.</A>
 
90
<LI><A HREF="#3.22">3.22. ImportError: No module named string, on MS Windows.</A>
 
91
<LI><A HREF="#3.23">3.23. Core dump on SGI when using the gl module.</A>
 
92
<LI><A HREF="#3.24">3.24. &quot;Initializer not a constant&quot; while building DLL on MS-Windows</A>
 
93
<LI><A HREF="#3.25">3.25. Output directed to a pipe or file disappears on Linux.</A>
 
94
<LI><A HREF="#3.26">3.26. [deleted]</A>
 
95
<LI><A HREF="#3.27">3.27. [deleted]</A>
 
96
<LI><A HREF="#3.28">3.28. How can I test if Tkinter is working?</A>
 
97
<LI><A HREF="#3.29">3.29. Is there a way to get the interactive mode of the python interpreter to perform function/variable name completion?</A>
 
98
<LI><A HREF="#3.30">3.30. Why is the Python interpreter not built as a shared library?</A>
 
99
<LI><A HREF="#3.31">3.31. Build with GCC on Solaris 2.6 (SunOS 5.6) fails</A>
 
100
<LI><A HREF="#3.32">3.32. Running &quot;make clean&quot; seems to leave problematic files that cause subsequent builds to fail.</A>
 
101
<LI><A HREF="#3.33">3.33. Submitting bug reports and patches</A>
 
102
<LI><A HREF="#3.34">3.34. I can't load shared libraries under Python 1.5.2, Solaris 7, and gcc 2.95.2</A>
 
103
<LI><A HREF="#3.35">3.35. In the regression test, test___all__ fails for the profile module. What's wrong?</A>
 
104
<LI><A HREF="#3.36">3.36. relocations remain against allocatable but non-writable sections</A>
 
105
 
 
106
</UL>
 
107
 
 
108
<P>
 
109
<HR>
 
110
<H2>4. Programming in Python</H2>
 
111
<UL>
 
112
<LI><A HREF="#4.1">4.1. Is there a source code level debugger with breakpoints, step, etc.?</A>
 
113
<LI><A HREF="#4.2">4.2. Can I create an object class with some methods implemented in C and others in Python (e.g. through inheritance)? (Also phrased as: Can I use a built-in type as base class?)</A>
 
114
<LI><A HREF="#4.3">4.3. Is there a curses/termcap package for Python?</A>
 
115
<LI><A HREF="#4.4">4.4. Is there an equivalent to C's onexit() in Python?</A>
 
116
<LI><A HREF="#4.5">4.5. [deleted]</A>
 
117
<LI><A HREF="#4.6">4.6. How do I iterate over a sequence in reverse order?</A>
 
118
<LI><A HREF="#4.7">4.7. My program is too slow. How do I speed it up?</A>
 
119
<LI><A HREF="#4.8">4.8. When I have imported a module, then edit it, and import it again (into the same Python process), the changes don't seem to take place. What is going on?</A>
 
120
<LI><A HREF="#4.9">4.9. How do I find the current module name?</A>
 
121
<LI><A HREF="#4.10">4.10. I have a module in which I want to execute some extra code when it is run as a script. How do I find out whether I am running as a script?</A>
 
122
<LI><A HREF="#4.11">4.11. I try to run a program from the Demo directory but it fails with ImportError: No module named ...; what gives?</A>
 
123
<LI><A HREF="#4.12">4.12. [deleted]</A>
 
124
<LI><A HREF="#4.13">4.13. What GUI toolkits exist for Python?</A>
 
125
<LI><A HREF="#4.14">4.14. Are there any interfaces to database packages in Python?</A>
 
126
<LI><A HREF="#4.15">4.15. Is it possible to write obfuscated one-liners in Python?</A>
 
127
<LI><A HREF="#4.16">4.16. Is there an equivalent of C's &quot;?:&quot; ternary operator?</A>
 
128
<LI><A HREF="#4.17">4.17. My class defines __del__ but it is not called when I delete the object.</A>
 
129
<LI><A HREF="#4.18">4.18. How do I change the shell environment for programs called using os.popen() or os.system()? Changing os.environ doesn't work.</A>
 
130
<LI><A HREF="#4.19">4.19. What is a class?</A>
 
131
<LI><A HREF="#4.20">4.20. What is a method?</A>
 
132
<LI><A HREF="#4.21">4.21. What is self?</A>
 
133
<LI><A HREF="#4.22">4.22. What is an unbound method?</A>
 
134
<LI><A HREF="#4.23">4.23. How do I call a method defined in a base class from a derived class that overrides it?</A>
 
135
<LI><A HREF="#4.24">4.24. How do I call a method from a base class without using the name of the base class?</A>
 
136
<LI><A HREF="#4.25">4.25. How can I organize my code to make it easier to change the base class?</A>
 
137
<LI><A HREF="#4.26">4.26. How can I find the methods or attributes of an object?</A>
 
138
<LI><A HREF="#4.27">4.27. I can't seem to use os.read() on a pipe created with os.popen().</A>
 
139
<LI><A HREF="#4.28">4.28. How can I create a stand-alone binary from a Python script?</A>
 
140
<LI><A HREF="#4.29">4.29. What WWW tools are there for Python?</A>
 
141
<LI><A HREF="#4.30">4.30. How do I run a subprocess with pipes connected to both input and output?</A>
 
142
<LI><A HREF="#4.31">4.31. How do I call a function if I have the arguments in a tuple?</A>
 
143
<LI><A HREF="#4.32">4.32. How do I enable font-lock-mode for Python in Emacs?</A>
 
144
<LI><A HREF="#4.33">4.33. Is there a scanf() or sscanf() equivalent?</A>
 
145
<LI><A HREF="#4.34">4.34. Can I have Tk events handled while waiting for I/O?</A>
 
146
<LI><A HREF="#4.35">4.35. How do I write a function with output parameters (call by reference)?</A>
 
147
<LI><A HREF="#4.36">4.36. Please explain the rules for local and global variables in Python.</A>
 
148
<LI><A HREF="#4.37">4.37. How can I have modules that mutually import each other?</A>
 
149
<LI><A HREF="#4.38">4.38. How do I copy an object in Python?</A>
 
150
<LI><A HREF="#4.39">4.39. How to implement persistent objects in Python? (Persistent == automatically saved to and restored from disk.)</A>
 
151
<LI><A HREF="#4.40">4.40. I try to use __spam and I get an error about _SomeClassName__spam.</A>
 
152
<LI><A HREF="#4.41">4.41. How do I delete a file? And other file questions.</A>
 
153
<LI><A HREF="#4.42">4.42. How to modify urllib or httplib to support HTTP/1.1?</A>
 
154
<LI><A HREF="#4.43">4.43. Unexplicable syntax errors in compile() or exec.</A>
 
155
<LI><A HREF="#4.44">4.44. How do I convert a string to a number?</A>
 
156
<LI><A HREF="#4.45">4.45. How do I convert a number to a string?</A>
 
157
<LI><A HREF="#4.46">4.46. How do I copy a file?</A>
 
158
<LI><A HREF="#4.47">4.47. How do I check if an object is an instance of a given class or of a subclass of it?</A>
 
159
<LI><A HREF="#4.48">4.48. What is delegation?</A>
 
160
<LI><A HREF="#4.49">4.49. How do I test a Python program or component.</A>
 
161
<LI><A HREF="#4.50">4.50. My multidimensional list (array) is broken! What gives?</A>
 
162
<LI><A HREF="#4.51">4.51. I want to do a complicated sort: can you do a Schwartzian Transform in Python?</A>
 
163
<LI><A HREF="#4.52">4.52. How to convert between tuples and lists?</A>
 
164
<LI><A HREF="#4.53">4.53. Files retrieved with urllib contain leading garbage that looks like email headers.</A>
 
165
<LI><A HREF="#4.54">4.54. How do I get a list of all instances of a given class?</A>
 
166
<LI><A HREF="#4.55">4.55. A regular expression fails with regex.error: match failure.</A>
 
167
<LI><A HREF="#4.56">4.56. I can't get signal handlers to work.</A>
 
168
<LI><A HREF="#4.57">4.57. I can't use a global variable in a function? Help!</A>
 
169
<LI><A HREF="#4.58">4.58. What's a negative index? Why doesn't list.insert() use them?</A>
 
170
<LI><A HREF="#4.59">4.59. How can I sort one list by values from another list?</A>
 
171
<LI><A HREF="#4.60">4.60. Why doesn't dir() work on builtin types like files and lists?</A>
 
172
<LI><A HREF="#4.61">4.61. How can I mimic CGI form submission (METHOD=POST)?</A>
 
173
<LI><A HREF="#4.62">4.62. If my program crashes with a bsddb (or anydbm) database open, it gets corrupted. How come?</A>
 
174
<LI><A HREF="#4.63">4.63. How do I make a Python script executable on Unix?</A>
 
175
<LI><A HREF="#4.64">4.64. How do you remove duplicates from a list?</A>
 
176
<LI><A HREF="#4.65">4.65. Are there any known year 2000 problems in Python?</A>
 
177
<LI><A HREF="#4.66">4.66. I want a version of map that applies a method to a sequence of objects! Help!</A>
 
178
<LI><A HREF="#4.67">4.67. How do I generate random numbers in Python?</A>
 
179
<LI><A HREF="#4.68">4.68. How do I access the serial (RS232) port?</A>
 
180
<LI><A HREF="#4.69">4.69. Images on Tk-Buttons don't work in Py15?</A>
 
181
<LI><A HREF="#4.70">4.70. Where is the math.py (socket.py, regex.py, etc.) source file?</A>
 
182
<LI><A HREF="#4.71">4.71. How do I send mail from a Python script?</A>
 
183
<LI><A HREF="#4.72">4.72. How do I avoid blocking in connect() of a socket?</A>
 
184
<LI><A HREF="#4.73">4.73. How do I specify hexadecimal and octal integers?</A>
 
185
<LI><A HREF="#4.74">4.74. How to get a single keypress at a time?</A>
 
186
<LI><A HREF="#4.75">4.75. How can I overload constructors (or methods) in Python?</A>
 
187
<LI><A HREF="#4.76">4.76. How do I pass keyword arguments from one method to another?</A>
 
188
<LI><A HREF="#4.77">4.77. What module should I use to help with generating HTML?</A>
 
189
<LI><A HREF="#4.78">4.78. How do I create documentation from doc strings?</A>
 
190
<LI><A HREF="#4.79">4.79. How do I read (or write) binary data?</A>
 
191
<LI><A HREF="#4.80">4.80. I can't get key bindings to work in Tkinter</A>
 
192
<LI><A HREF="#4.81">4.81. &quot;import crypt&quot; fails</A>
 
193
<LI><A HREF="#4.82">4.82. Are there coding standards or a style guide for Python programs?</A>
 
194
<LI><A HREF="#4.83">4.83. How do I freeze Tkinter applications?</A>
 
195
<LI><A HREF="#4.84">4.84. How do I create static class data and static class methods?</A>
 
196
<LI><A HREF="#4.85">4.85. __import__('x.y.z') returns &lt;module 'x'&gt;; how do I get z?</A>
 
197
<LI><A HREF="#4.86">4.86. Basic thread wisdom</A>
 
198
<LI><A HREF="#4.87">4.87. Why doesn't closing sys.stdout (stdin, stderr) really close it?</A>
 
199
<LI><A HREF="#4.88">4.88. What kinds of global value mutation are thread-safe?</A>
 
200
<LI><A HREF="#4.89">4.89. How do I modify a string in place?</A>
 
201
<LI><A HREF="#4.90">4.90. How to pass on keyword/optional parameters/arguments</A>
 
202
<LI><A HREF="#4.91">4.91. How can I get a dictionary to display its keys in a consistent order?</A>
 
203
<LI><A HREF="#4.92">4.92. Is there a Python tutorial?</A>
 
204
<LI><A HREF="#4.93">4.93. Deleted</A>
 
205
<LI><A HREF="#4.94">4.94. How do I get a single keypress without blocking?</A>
 
206
<LI><A HREF="#4.95">4.95. Is there an equivalent to Perl chomp()? (Remove trailing newline from string)</A>
 
207
<LI><A HREF="#4.96">4.96. Why is join() a string method when I'm really joining the elements of a (list, tuple, sequence)?</A>
 
208
<LI><A HREF="#4.97">4.97. How can my code discover the name of an object?</A>
 
209
<LI><A HREF="#4.98">4.98. Why are floating point calculations so inaccurate?</A>
 
210
<LI><A HREF="#4.99">4.99. I tried to open Berkeley DB file, but bsddb produces bsddb.error: (22, 'Invalid argument'). Help! How can I restore my data?</A>
 
211
<LI><A HREF="#4.100">4.100. What are the &quot;best practices&quot; for using import in a module?</A>
 
212
<LI><A HREF="#4.101">4.101. Is there a tool to help find bugs or perform static analysis?</A>
 
213
<LI><A HREF="#4.102">4.102. UnicodeError: ASCII [decoding,encoding] error: ordinal not in range(128)</A>
 
214
<LI><A HREF="#4.103">4.103. Using strings to call functions/methods</A>
 
215
<LI><A HREF="#4.104">4.104. How fast are exceptions?</A>
 
216
<LI><A HREF="#4.105">4.105. Sharing global variables across modules</A>
 
217
<LI><A HREF="#4.106">4.106. Why is cPickle so slow?</A>
 
218
<LI><A HREF="#4.107">4.107. When importing module XXX, why do I get &quot;undefined symbol: PyUnicodeUCS2_...&quot; ?</A>
 
219
<LI><A HREF="#4.108">4.108. How do I create a .pyc file?</A>
 
220
 
 
221
</UL>
 
222
 
 
223
<P>
 
224
<HR>
 
225
<H2>5. Extending Python</H2>
 
226
<UL>
 
227
<LI><A HREF="#5.1">5.1. Can I create my own functions in C?</A>
 
228
<LI><A HREF="#5.2">5.2. Can I create my own functions in C++?</A>
 
229
<LI><A HREF="#5.3">5.3. How can I execute arbitrary Python statements from C?</A>
 
230
<LI><A HREF="#5.4">5.4. How can I evaluate an arbitrary Python expression from C?</A>
 
231
<LI><A HREF="#5.5">5.5. How do I extract C values from a Python object?</A>
 
232
<LI><A HREF="#5.6">5.6. How do I use Py_BuildValue() to create a tuple of arbitrary length?</A>
 
233
<LI><A HREF="#5.7">5.7. How do I call an object's method from C?</A>
 
234
<LI><A HREF="#5.8">5.8. How do I catch the output from PyErr_Print() (or anything that prints to stdout/stderr)?</A>
 
235
<LI><A HREF="#5.9">5.9. How do I access a module written in Python from C?</A>
 
236
<LI><A HREF="#5.10">5.10. How do I interface to C++ objects from Python?</A>
 
237
<LI><A HREF="#5.11">5.11. mSQLmodule (or other old module) won't build with Python 1.5 (or later)</A>
 
238
<LI><A HREF="#5.12">5.12. I added a module using the Setup file and the make fails! Huh?</A>
 
239
<LI><A HREF="#5.13">5.13. I want to compile a Python module on my Red Hat Linux system, but some files are missing.</A>
 
240
<LI><A HREF="#5.14">5.14. What does &quot;SystemError: _PyImport_FixupExtension: module yourmodule not loaded&quot; mean?</A>
 
241
<LI><A HREF="#5.15">5.15. How to tell &quot;incomplete input&quot; from &quot;invalid input&quot;?</A>
 
242
<LI><A HREF="#5.16">5.16. How do I debug an extension?</A>
 
243
<LI><A HREF="#5.17">5.17. How do I find undefined Linux g++ symbols, __builtin_new or __pure_virtural</A>
 
244
<LI><A HREF="#5.18">5.18. How do I define and create objects corresponding to built-in/extension types</A>
 
245
 
 
246
</UL>
 
247
 
 
248
<P>
 
249
<HR>
 
250
<H2>6. Python's design</H2>
 
251
<UL>
 
252
<LI><A HREF="#6.1">6.1. Why isn't there a switch or case statement in Python?</A>
 
253
<LI><A HREF="#6.2">6.2. Why does Python use indentation for grouping of statements?</A>
 
254
<LI><A HREF="#6.3">6.3. Why are Python strings immutable?</A>
 
255
<LI><A HREF="#6.4">6.4. Delete</A>
 
256
<LI><A HREF="#6.5">6.5. Why does Python use methods for some functionality (e.g. list.index()) but functions for other (e.g. len(list))?</A>
 
257
<LI><A HREF="#6.6">6.6. Why can't I derive a class from built-in types (e.g. lists or files)?</A>
 
258
<LI><A HREF="#6.7">6.7. Why must 'self' be declared and used explicitly in method definitions and calls?</A>
 
259
<LI><A HREF="#6.8">6.8. Can't you emulate threads in the interpreter instead of relying on an OS-specific thread implementation?</A>
 
260
<LI><A HREF="#6.9">6.9. Why can't lambda forms contain statements?</A>
 
261
<LI><A HREF="#6.10">6.10. [deleted]</A>
 
262
<LI><A HREF="#6.11">6.11. [deleted]</A>
 
263
<LI><A HREF="#6.12">6.12. Why is there no more efficient way of iterating over a dictionary than first constructing the list of keys()?</A>
 
264
<LI><A HREF="#6.13">6.13. Can Python be compiled to machine code, C or some other language?</A>
 
265
<LI><A HREF="#6.14">6.14. How does Python manage memory?</A>
 
266
<LI><A HREF="#6.15">6.15. Why are there separate tuple and list data types?</A>
 
267
<LI><A HREF="#6.16">6.16. How are lists implemented?</A>
 
268
<LI><A HREF="#6.17">6.17. How are dictionaries implemented?</A>
 
269
<LI><A HREF="#6.18">6.18. Why must dictionary keys be immutable?</A>
 
270
<LI><A HREF="#6.19">6.19. How the heck do you make an array in Python?</A>
 
271
<LI><A HREF="#6.20">6.20. Why doesn't list.sort() return the sorted list?</A>
 
272
<LI><A HREF="#6.21">6.21. How do you specify and enforce an interface spec in Python?</A>
 
273
<LI><A HREF="#6.22">6.22. Why do all classes have the same type? Why do instances all have the same type?</A>
 
274
<LI><A HREF="#6.23">6.23. Why isn't all memory freed when Python exits?</A>
 
275
<LI><A HREF="#6.24">6.24. Why no class methods or mutable class variables?</A>
 
276
<LI><A HREF="#6.25">6.25. Why are default values sometimes shared between objects?</A>
 
277
<LI><A HREF="#6.26">6.26. Why no goto?</A>
 
278
<LI><A HREF="#6.27">6.27. How do you make a higher order function in Python?</A>
 
279
<LI><A HREF="#6.28">6.28. Why do I get a SyntaxError for a 'continue' inside a 'try'?</A>
 
280
<LI><A HREF="#6.29">6.29. Why can't raw strings (r-strings) end with a backslash?</A>
 
281
<LI><A HREF="#6.30">6.30. Why can't I use an assignment in an expression?</A>
 
282
<LI><A HREF="#6.31">6.31. Why doesn't Python have a &quot;with&quot; statement like some other languages?</A>
 
283
<LI><A HREF="#6.32">6.32. Why are colons required for if/while/def/class?</A>
 
284
<LI><A HREF="#6.33">6.33. Can't we get rid of the Global Interpreter Lock?</A>
 
285
 
 
286
</UL>
 
287
 
 
288
<P>
 
289
<HR>
 
290
<H2>7. Using Python on non-UNIX platforms</H2>
 
291
<UL>
 
292
<LI><A HREF="#7.1">7.1. Is there a Mac version of Python?</A>
 
293
<LI><A HREF="#7.2">7.2. Are there DOS and Windows versions of Python?</A>
 
294
<LI><A HREF="#7.3">7.3. Is there an OS/2 version of Python?</A>
 
295
<LI><A HREF="#7.4">7.4. Is there a VMS version of Python?</A>
 
296
<LI><A HREF="#7.5">7.5. What about IBM mainframes, or other non-UNIX platforms?</A>
 
297
<LI><A HREF="#7.6">7.6. Where are the source or Makefiles for the non-UNIX versions?</A>
 
298
<LI><A HREF="#7.7">7.7. What is the status and support for the non-UNIX versions?</A>
 
299
<LI><A HREF="#7.8">7.8. I have a PC version but it appears to be only a binary. Where's the library?</A>
 
300
<LI><A HREF="#7.9">7.9. Where's the documentation for the Mac or PC version?</A>
 
301
<LI><A HREF="#7.10">7.10. How do I create a Python program file on the Mac or PC?</A>
 
302
<LI><A HREF="#7.11">7.11. How can I use Tkinter on Windows 95/NT?</A>
 
303
<LI><A HREF="#7.12">7.12. cgi.py (or other CGI programming) doesn't work sometimes on NT or win95!</A>
 
304
<LI><A HREF="#7.13">7.13. Why doesn't os.popen() work in PythonWin on NT?</A>
 
305
<LI><A HREF="#7.14">7.14. How do I use different functionality on different platforms with the same program?</A>
 
306
<LI><A HREF="#7.15">7.15. Is there an Amiga version of Python?</A>
 
307
<LI><A HREF="#7.16">7.16. Why doesn't os.popen()/win32pipe.popen() work on Win9x?</A>
 
308
 
 
309
</UL>
 
310
 
 
311
<P>
 
312
<HR>
 
313
<H2>8. Python on Windows</H2>
 
314
<UL>
 
315
<LI><A HREF="#8.1">8.1. Using Python for CGI on Microsoft Windows</A>
 
316
<LI><A HREF="#8.2">8.2. How to check for a keypress without blocking?</A>
 
317
<LI><A HREF="#8.3">8.3. $PYTHONPATH</A>
 
318
<LI><A HREF="#8.4">8.4. dedent syntax errors</A>
 
319
<LI><A HREF="#8.5">8.5. How do I emulate os.kill() in Windows?</A>
 
320
<LI><A HREF="#8.6">8.6. Why does os.path.isdir() fail on NT shared directories?</A>
 
321
<LI><A HREF="#8.7">8.7. PyRun_SimpleFile() crashes on Windows but not on Unix</A>
 
322
<LI><A HREF="#8.8">8.8. Import of _tkinter fails on Windows 95/98</A>
 
323
<LI><A HREF="#8.9">8.9. Can't extract the downloaded documentation on Windows</A>
 
324
<LI><A HREF="#8.10">8.10. Can't get Py_RunSimpleFile() to work.</A>
 
325
<LI><A HREF="#8.11">8.11. Where is Freeze for Windows?</A>
 
326
<LI><A HREF="#8.12">8.12. Is a *.pyd file the same as a DLL?</A>
 
327
<LI><A HREF="#8.13">8.13. Missing cw3215mt.dll (or missing cw3215.dll)</A>
 
328
<LI><A HREF="#8.14">8.14. How to make python scripts executable:</A>
 
329
<LI><A HREF="#8.15">8.15. Warning about CTL3D32 version from installer</A>
 
330
<LI><A HREF="#8.16">8.16. How can I embed Python into a Windows application?</A>
 
331
<LI><A HREF="#8.17">8.17. Setting up IIS 5 to use Python for CGI</A>
 
332
<LI><A HREF="#8.18">8.18. How do I run a Python program under Windows?</A>
 
333
 
 
334
</UL>
 
335
 
 
336
<HR>
 
337
<H1>1. General information and availability</H1>
 
338
 
 
339
<HR>
 
340
<H2><A NAME="1.1">1.1. What is Python?</A></H2>
 
341
Python is an interpreted, interactive, object-oriented programming
 
342
language.  It incorporates modules, exceptions, dynamic typing, very
 
343
high level dynamic data types, and classes.  Python combines
 
344
remarkable power with very clear syntax.  It has interfaces to many
 
345
system calls and libraries, as well as to various window systems, and
 
346
is extensible in C or C++.  It is also usable as an extension language
 
347
for applications that need a programmable interface.  Finally, Python
 
348
is portable: it runs on many brands of UNIX, on the Mac, and on PCs
 
349
under MS-DOS, Windows, Windows NT, and OS/2.
 
350
<P>
 
351
To find out more, the best thing to do is to start reading the
 
352
tutorial from the documentation set (see a few questions further
 
353
down).
 
354
<P>
 
355
See also question 1.17 (what is Python good for).
 
356
<P>
 
357
 
 
358
<A HREF="faqw.py?req=edit&amp;file=faq01.001.htp">Edit this entry</A> /
 
359
<A HREF="faqw.py?req=log&amp;file=faq01.001.htp">Log info</A>
 
360
 
 
361
/ Last changed on Mon May 26 16:05:18 1997 by
 
362
<A HREF="mailto:guido@cnri.reston.va.us">GvR</A>
 
363
<P>
 
364
 
 
365
<HR>
 
366
<H2><A NAME="1.2">1.2. Why is it called Python?</A></H2>
 
367
Apart from being a computer scientist, I'm also a fan of "Monty
 
368
Python's Flying Circus" (a BBC comedy series from the seventies, in
 
369
the -- unlikely -- case you didn't know).  It occurred to me one day
 
370
that I needed a name that was short, unique, and slightly mysterious.
 
371
And I happened to be reading some scripts from the series at the
 
372
time...  So then I decided to call my language Python.
 
373
<P>
 
374
By now I don't care any more whether you use a Python, some other
 
375
snake, a foot or 16-ton weight, or a wood rat as a logo for Python!
 
376
<P>
 
377
 
 
378
<A HREF="faqw.py?req=edit&amp;file=faq01.002.htp">Edit this entry</A> /
 
379
<A HREF="faqw.py?req=log&amp;file=faq01.002.htp">Log info</A>
 
380
 
 
381
/ Last changed on Thu Aug 24 00:50:41 2000 by
 
382
<A HREF="mailto:guido@beopen.com">GvR</A>
 
383
<P>
 
384
 
 
385
<HR>
 
386
<H2><A NAME="1.3">1.3. How do I obtain a copy of the Python source?</A></H2>
 
387
The latest Python source distribution is always available from
 
388
python.org, at <A HREF="http://www.python.org/download">http://www.python.org/download</A>.  The latest development sources can be obtained via anonymous CVS from SourceForge, at <A HREF="http://www.sf.net/projects/python">http://www.sf.net/projects/python</A> .
 
389
<P>
 
390
The source distribution is a gzipped tar file containing the complete C source, LaTeX
 
391
documentation, Python library modules, example programs, and several
 
392
useful pieces of freely distributable software.  This will compile and
 
393
run out of the box on most UNIX platforms.  (See section 7 for
 
394
non-UNIX information.)
 
395
<P>
 
396
Older versions of Python are also available from python.org.
 
397
<P>
 
398
 
 
399
<A HREF="faqw.py?req=edit&amp;file=faq01.003.htp">Edit this entry</A> /
 
400
<A HREF="faqw.py?req=log&amp;file=faq01.003.htp">Log info</A>
 
401
 
 
402
/ Last changed on Tue Apr  9 17:06:16 2002 by
 
403
<A HREF="mailto:akuchlin@mems-exchange.org">A.M. Kuchling</A>
 
404
<P>
 
405
 
 
406
<HR>
 
407
<H2><A NAME="1.4">1.4. How do I get documentation on Python?</A></H2>
 
408
All documentation is available on-line, starting at <A HREF="http://www.python.org/doc">http://www.python.org/doc</A>/. 
 
409
<P>
 
410
The LaTeX source for the documentation is part of the source
 
411
distribution.  If you don't have LaTeX, the latest Python
 
412
documentation set is available, in various formats like postscript 
 
413
and html, by anonymous ftp - visit the above URL for links to the
 
414
current versions.
 
415
<P>
 
416
PostScript for a high-level description of Python is in the file nluug-paper.ps 
 
417
(a separate file on the ftp site).
 
418
<P>
 
419
 
 
420
<A HREF="faqw.py?req=edit&amp;file=faq01.004.htp">Edit this entry</A> /
 
421
<A HREF="faqw.py?req=log&amp;file=faq01.004.htp">Log info</A>
 
422
 
 
423
/ Last changed on Wed Jan 21 12:02:55 1998 by
 
424
<A HREF="mailto:klm@python.org">Ken Manheimer</A>
 
425
<P>
 
426
 
 
427
<HR>
 
428
<H2><A NAME="1.5">1.5. Are there other ftp sites that mirror the Python distribution?</A></H2>
 
429
The following anonymous ftp sites keep mirrors of the Python
 
430
distribution:
 
431
<P>
 
432
USA:
 
433
<P>
 
434
<PRE>
 
435
        <A HREF="ftp://ftp.python.org/pub/python">ftp://ftp.python.org/pub/python</A>/
 
436
        <A HREF="ftp://gatekeeper.dec.com/pub/plan/python">ftp://gatekeeper.dec.com/pub/plan/python</A>/
 
437
        <A HREF="ftp://ftp.uu.net/languages/python">ftp://ftp.uu.net/languages/python</A>/
 
438
        <A HREF="ftp://ftp.wustl.edu/graphics/graphics/sgi-stuff/python">ftp://ftp.wustl.edu/graphics/graphics/sgi-stuff/python</A>/
 
439
        <A HREF="ftp://ftp.sterling.com/programming/languages/python">ftp://ftp.sterling.com/programming/languages/python</A>/
 
440
        <A HREF="ftp://uiarchive.cso.uiuc.edu/pub/lang/python">ftp://uiarchive.cso.uiuc.edu/pub/lang/python</A>/
 
441
        <A HREF="ftp://ftp.pht.com/mirrors/python/python">ftp://ftp.pht.com/mirrors/python/python</A>/
 
442
        <A HREF="ftp://ftp.cdrom.com/pub/python">ftp://ftp.cdrom.com/pub/python</A>/
 
443
</PRE>
 
444
Europe:
 
445
<P>
 
446
<PRE>
 
447
        <A HREF="ftp://ftp.cwi.nl/pub/python">ftp://ftp.cwi.nl/pub/python</A>/
 
448
        <A HREF="ftp://ftp.funet.fi/pub/languages/python">ftp://ftp.funet.fi/pub/languages/python</A>/
 
449
        <A HREF="ftp://ftp.sunet.se/pub/lang/python">ftp://ftp.sunet.se/pub/lang/python</A>/
 
450
        <A HREF="ftp://unix.hensa.ac.uk/mirrors/uunet/languages/python">ftp://unix.hensa.ac.uk/mirrors/uunet/languages/python</A>/
 
451
        <A HREF="ftp://ftp.lip6.fr/pub/python">ftp://ftp.lip6.fr/pub/python</A>/
 
452
        <A HREF="ftp://sunsite.cnlab-switch.ch/mirror/python">ftp://sunsite.cnlab-switch.ch/mirror/python</A>/
 
453
        <A HREF="ftp://ftp.informatik.tu-muenchen.de/pub/comp/programming/languages/python">ftp://ftp.informatik.tu-muenchen.de/pub/comp/programming/languages/python</A>/
 
454
</PRE>
 
455
Australia:
 
456
<P>
 
457
<PRE>
 
458
        <A HREF="ftp://ftp.dstc.edu.au/pub/python">ftp://ftp.dstc.edu.au/pub/python</A>/
 
459
</PRE>
 
460
<P>
 
461
 
 
462
<A HREF="faqw.py?req=edit&amp;file=faq01.005.htp">Edit this entry</A> /
 
463
<A HREF="faqw.py?req=log&amp;file=faq01.005.htp">Log info</A>
 
464
 
 
465
/ Last changed on Wed Mar 24 09:20:49 1999 by
 
466
<A HREF="mailto:akuchlin@cnri.reston.va.us">A.M. Kuchling</A>
 
467
<P>
 
468
 
 
469
<HR>
 
470
<H2><A NAME="1.6">1.6. Is there a newsgroup or mailing list devoted to Python?</A></H2>
 
471
There is a newsgroup, comp.lang.python,
 
472
and a mailing list.  The newsgroup and mailing list are gatewayed into
 
473
each other -- if you can read news it's unnecessary to subscribe to
 
474
the mailing list.  To subscribe to the mailing list
 
475
(<A HREF="mailto:python-list@python.org">python-list@python.org</A>) visit its Mailman webpage at
 
476
<A HREF="http://www.python.org/mailman/listinfo/python-list">http://www.python.org/mailman/listinfo/python-list</A>
 
477
<P>
 
478
More info about the newsgroup and mailing list, and about other lists,
 
479
can be found at
 
480
<A HREF="http://www.python.org/psa/MailingLists.html">http://www.python.org/psa/MailingLists.html</A>.
 
481
<P>
 
482
Archives of the newsgroup are kept by Deja News and accessible
 
483
through the "Python newsgroup search" web page,
 
484
<A HREF="http://www.python.org/search/search_news.html">http://www.python.org/search/search_news.html</A>.
 
485
This page also contains pointer to other archival collections.
 
486
<P>
 
487
 
 
488
<A HREF="faqw.py?req=edit&amp;file=faq01.006.htp">Edit this entry</A> /
 
489
<A HREF="faqw.py?req=log&amp;file=faq01.006.htp">Log info</A>
 
490
 
 
491
/ Last changed on Wed Jun 23 09:29:36 1999 by
 
492
<A HREF="mailto:guido@python.org">GvR</A>
 
493
<P>
 
494
 
 
495
<HR>
 
496
<H2><A NAME="1.7">1.7. Is there a WWW page devoted to Python?</A></H2>
 
497
Yes, <A HREF="http://www.python.org">http://www.python.org</A>/ is the official Python home page.
 
498
<P>
 
499
 
 
500
<A HREF="faqw.py?req=edit&amp;file=faq01.007.htp">Edit this entry</A> /
 
501
<A HREF="faqw.py?req=log&amp;file=faq01.007.htp">Log info</A>
 
502
 
 
503
/ Last changed on Fri May 23 14:42:59 1997 by
 
504
<A HREF="mailto:klm@python.org">Ken Manheimer</A>
 
505
<P>
 
506
 
 
507
<HR>
 
508
<H2><A NAME="1.8">1.8. Is the Python documentation available on the WWW?</A></H2>
 
509
Yes. Python 2.0 documentation is available from
 
510
<A HREF="http://www.pythonlabs.com/tech/python2.0/doc">http://www.pythonlabs.com/tech/python2.0/doc</A>/ and from
 
511
<A HREF="http://www.python.org/doc">http://www.python.org/doc</A>/.  Note that most documentation
 
512
is available for on-line browsing as well as for downloading.
 
513
<P>
 
514
 
 
515
<A HREF="faqw.py?req=edit&amp;file=faq01.008.htp">Edit this entry</A> /
 
516
<A HREF="faqw.py?req=log&amp;file=faq01.008.htp">Log info</A>
 
517
 
 
518
/ Last changed on Tue Jan  2 03:14:08 2001 by
 
519
<A HREF="mailto:moshez@zadka.site.co.il">Moshe Zadka</A>
 
520
<P>
 
521
 
 
522
<HR>
 
523
<H2><A NAME="1.9">1.9. Are there any books on Python?</A></H2>
 
524
Yes, many, and more are being published.  See
 
525
the python.org Wiki at <A HREF="http://www.python.org/cgi-bin/moinmoin/PythonBooks">http://www.python.org/cgi-bin/moinmoin/PythonBooks</A> for a list.
 
526
<P>
 
527
You can also search online bookstores for "Python"
 
528
(and filter out the Monty Python references; or
 
529
perhaps search for "Python" and "language").
 
530
<P>
 
531
 
 
532
<A HREF="faqw.py?req=edit&amp;file=faq01.009.htp">Edit this entry</A> /
 
533
<A HREF="faqw.py?req=log&amp;file=faq01.009.htp">Log info</A>
 
534
 
 
535
/ Last changed on Mon Aug  5 19:08:49 2002 by
 
536
<A HREF="mailto:akuchlin@mems-exchange.org">amk</A>
 
537
<P>
 
538
 
 
539
<HR>
 
540
<H2><A NAME="1.10">1.10. Are there any published articles about Python that I can reference?</A></H2>
 
541
If you can't reference the web site, and you don't want to reference the books
 
542
(see previous question), there are several articles on Python that you could
 
543
reference.
 
544
<P>
 
545
Most publications about Python are collected on the Python web site:
 
546
<P>
 
547
<PRE>
 
548
    <A HREF="http://www.python.org/doc/Publications.html">http://www.python.org/doc/Publications.html</A>
 
549
</PRE>
 
550
It is no longer recommended to reference this
 
551
very old article by Python's author:
 
552
<P>
 
553
<PRE>
 
554
    Guido van Rossum and Jelke de Boer, "Interactively Testing Remote
 
555
    Servers Using the Python Programming Language", CWI Quarterly, Volume
 
556
    4, Issue 4 (December 1991), Amsterdam, pp 283-303.
 
557
</PRE>
 
558
<P>
 
559
 
 
560
<A HREF="faqw.py?req=edit&amp;file=faq01.010.htp">Edit this entry</A> /
 
561
<A HREF="faqw.py?req=log&amp;file=faq01.010.htp">Log info</A>
 
562
 
 
563
/ Last changed on Sat Jul  4 20:52:31 1998 by
 
564
<A HREF="mailto:guido@python.org">GvR</A>
 
565
<P>
 
566
 
 
567
<HR>
 
568
<H2><A NAME="1.11">1.11. Are there short introductory papers or talks on Python?</A></H2>
 
569
There are several - you can find links to some of them collected at
 
570
<A HREF="http://www.python.org/doc/Hints.html#intros">http://www.python.org/doc/Hints.html#intros</A>.
 
571
<P>
 
572
 
 
573
<A HREF="faqw.py?req=edit&amp;file=faq01.011.htp">Edit this entry</A> /
 
574
<A HREF="faqw.py?req=log&amp;file=faq01.011.htp">Log info</A>
 
575
 
 
576
/ Last changed on Fri May 23 15:04:05 1997 by
 
577
<A HREF="mailto:klm@python.org">Ken Manheimer</A>
 
578
<P>
 
579
 
 
580
<HR>
 
581
<H2><A NAME="1.12">1.12. How does the Python version numbering scheme work?</A></H2>
 
582
Python versions are numbered A.B.C or A.B.  A is the major version
 
583
number -- it is only incremented for really major changes in the
 
584
language.  B is the minor version number, incremented for less
 
585
earth-shattering changes.  C is the micro-level -- it is
 
586
incremented for each bugfix release.  See PEP 6 for more information
 
587
about bugfix releases.
 
588
<P>
 
589
Not all releases have bugfix releases.
 
590
Note that in the past (ending with 1.5.2),
 
591
micro releases have added significant changes;
 
592
in fact the changeover from 0.9.9 to 1.0.0 was the first time
 
593
that either A or B changed!
 
594
<P>
 
595
Alpha, beta and release candidate versions have an additional suffixes.
 
596
The suffix for an alpha version is "aN" for some small number N, the
 
597
suffix for a beta version is "bN" for some small number N, and the
 
598
suffix for a release candidate version is "cN" for some small number N.
 
599
<P>
 
600
Note that (for instance) all versions labeled 2.0aN precede the
 
601
versions labeled 2.0bN, which precede versions labeled 2.0cN, and
 
602
<I>those</I> precede 2.0.
 
603
<P>
 
604
As a rule, no changes are made between release candidates and the final
 
605
release unless there are show-stopper bugs.
 
606
<P>
 
607
You may also find version numbers with a "+" suffix, e.g. "2.2+".
 
608
These are unreleased versions, built directly from the CVS trunk.
 
609
<P>
 
610
See also the documentation for sys.version, sys.hexversion, and
 
611
sys.version_info.
 
612
<P>
 
613
 
 
614
<A HREF="faqw.py?req=edit&amp;file=faq01.012.htp">Edit this entry</A> /
 
615
<A HREF="faqw.py?req=log&amp;file=faq01.012.htp">Log info</A>
 
616
 
 
617
/ Last changed on Mon Jan 14 06:34:17 2002 by
 
618
<A HREF="mailto:guido@python.org">GvR</A>
 
619
<P>
 
620
 
 
621
<HR>
 
622
<H2><A NAME="1.13">1.13. How do I get a beta test version of Python?</A></H2>
 
623
All releases, including alphas, betas and release candidates, are announced on
 
624
comp.lang.python and comp.lang.python.announce newsgroups,
 
625
which are gatewayed into the <A HREF="mailto:python-list@python.org">python-list@python.org</A> and
 
626
<A HREF="mailto:python-announce@python.org">python-announce@python.org</A>. In addition, all these announcements appear on
 
627
the Python home page, at <A HREF="http://www.python.org">http://www.python.org</A>.
 
628
<P>
 
629
You can also access the development version of Python through CVS.  See <A HREF="http://sourceforge.net/cvs/?group_id=5470">http://sourceforge.net/cvs/?group_id=5470</A> for details.  If you're not familiar with CVS, documents like <A HREF="http://linux.oreillynet.com/pub/a/linux/2002/01/03/cvs_intro.html">http://linux.oreillynet.com/pub/a/linux/2002/01/03/cvs_intro.html</A>
 
630
provide an introduction.
 
631
<P>
 
632
 
 
633
<A HREF="faqw.py?req=edit&amp;file=faq01.013.htp">Edit this entry</A> /
 
634
<A HREF="faqw.py?req=log&amp;file=faq01.013.htp">Log info</A>
 
635
 
 
636
/ Last changed on Mon Jun  3 00:57:08 2002 by
 
637
<A HREF="mailto:neal@metaslash.com">Neal Norwitz</A>
 
638
<P>
 
639
 
 
640
<HR>
 
641
<H2><A NAME="1.14">1.14. Are there copyright restrictions on the use of Python?</A></H2>
 
642
Hardly.  You can do anything you want with the source, as long as
 
643
you leave the copyrights in, and display those copyrights in any
 
644
documentation about Python that you produce.  Also, don't use the
 
645
author's institute's name in publicity without prior written
 
646
permission, and don't hold them responsible for anything (read the
 
647
actual copyright for a precise legal wording).
 
648
<P>
 
649
In particular, if you honor the copyright rules, it's OK to use Python
 
650
for commercial use, to sell copies of Python in source or binary form,
 
651
or to sell products that enhance Python or incorporate Python (or part
 
652
of it) in some form.  I would still like to know about all commercial
 
653
use of Python!
 
654
<P>
 
655
 
 
656
<A HREF="faqw.py?req=edit&amp;file=faq01.014.htp">Edit this entry</A> /
 
657
<A HREF="faqw.py?req=log&amp;file=faq01.014.htp">Log info</A>
 
658
<P>
 
659
 
 
660
<HR>
 
661
<H2><A NAME="1.15">1.15. Why was Python created in the first place?</A></H2>
 
662
Here's a <I>very</I> brief summary of what got me started:
 
663
<P>
 
664
I had extensive experience with implementing an interpreted language
 
665
in the ABC group at CWI, and from working with this group I had
 
666
learned a lot about language design.  This is the origin of many
 
667
Python features, including the use of indentation for statement
 
668
grouping and the inclusion of very-high-level data types (although the
 
669
details are all different in Python).
 
670
<P>
 
671
I had a number of gripes about the ABC language, but also liked many
 
672
of its features.  It was impossible to extend the ABC language (or its
 
673
implementation) to remedy my complaints -- in fact its lack of
 
674
extensibility was one of its biggest problems.
 
675
I had some experience with using Modula-2+ and talked with the
 
676
designers of Modula-3 (and read the M3 report).  M3 is the origin of
 
677
the syntax and semantics used for exceptions, and some other Python
 
678
features.
 
679
<P>
 
680
I was working in the Amoeba distributed operating system group at
 
681
CWI.  We needed a better way to do system administration than by
 
682
writing either C programs or Bourne shell scripts, since Amoeba had
 
683
its own system call interface which wasn't easily accessible from the
 
684
Bourne shell.  My experience with error handling in Amoeba made me
 
685
acutely aware of the importance of exceptions as a programming
 
686
language feature.
 
687
<P>
 
688
It occurred to me that a scripting language with a syntax like ABC
 
689
but with access to the Amoeba system calls would fill the need.  I
 
690
realized that it would be foolish to write an Amoeba-specific
 
691
language, so I decided that I needed a language that was generally
 
692
extensible.
 
693
<P>
 
694
During the 1989 Christmas holidays, I had a lot of time on my hand,
 
695
so I decided to give it a try.  During the next year, while still
 
696
mostly working on it in my own time, Python was used in the Amoeba
 
697
project with increasing success, and the feedback from colleagues made
 
698
me add many early improvements.
 
699
<P>
 
700
In February 1991, after just over a year of development, I decided
 
701
to post to USENET.  The rest is in the Misc/HISTORY file.
 
702
<P>
 
703
 
 
704
<A HREF="faqw.py?req=edit&amp;file=faq01.015.htp">Edit this entry</A> /
 
705
<A HREF="faqw.py?req=log&amp;file=faq01.015.htp">Log info</A>
 
706
 
 
707
/ Last changed on Fri May 23 00:06:23 1997 by
 
708
<A HREF="mailto:guido@python.org">GvR</A>
 
709
<P>
 
710
 
 
711
<HR>
 
712
<H2><A NAME="1.16">1.16. Do I have to like &quot;Monty Python's Flying Circus&quot;?</A></H2>
 
713
No, but it helps.  Pythonistas like the occasional reference to SPAM,
 
714
and of course, nobody expects the Spanish Inquisition
 
715
<P>
 
716
The two main reasons to use Python are:
 
717
<P>
 
718
<PRE>
 
719
 - Portable
 
720
 - Easy to learn
 
721
</PRE>
 
722
The <I>three</I> main reasons to use Python are:
 
723
<P>
 
724
<PRE>
 
725
 - Portable
 
726
 - Easy to learn
 
727
 - Powerful standard library
 
728
</PRE>
 
729
(And nice red uniforms.)
 
730
<P>
 
731
And remember, there is <I>no</I> rule six.
 
732
<P>
 
733
 
 
734
<A HREF="faqw.py?req=edit&amp;file=faq01.016.htp">Edit this entry</A> /
 
735
<A HREF="faqw.py?req=log&amp;file=faq01.016.htp">Log info</A>
 
736
 
 
737
/ Last changed on Wed May 28 10:39:21 1997 by
 
738
<A HREF="mailto:guido@cnri.reston.va.us">GvR</A>
 
739
<P>
 
740
 
 
741
<HR>
 
742
<H2><A NAME="1.17">1.17. What is Python good for?</A></H2>
 
743
Python is used in many situations where a great deal of dynamism,
 
744
ease of use, power, and flexibility are required.  
 
745
<P>
 
746
In the area of basic text
 
747
manipulation core Python (without any non-core extensions) is easier
 
748
to use and is roughly as fast as just  about any language, and this makes Python
 
749
good for many system administration type tasks and for CGI programming
 
750
and other application areas that manipulate text and strings and such.
 
751
<P>
 
752
When augmented with
 
753
standard extensions (such as PIL, COM, Numeric, oracledb, kjbuckets, 
 
754
tkinter, win32api, etc.)
 
755
or special purpose extensions (that you write, perhaps using helper tools such
 
756
as SWIG, or using object protocols such as ILU/CORBA or COM) Python 
 
757
becomes a very convenient "glue" or "steering"
 
758
language that helps make heterogeneous collections of unrelated
 
759
software packages work together.
 
760
For example by combining Numeric with oracledb you can help your
 
761
SQL database do statistical analysis, or even Fourier transforms.
 
762
One of the features that makes Python excel in the "glue language" role
 
763
is Python's simple, usable, and powerful C language runtime API.
 
764
<P>
 
765
Many developers also use Python extensively as a graphical user
 
766
interface development aide.
 
767
<P>
 
768
 
 
769
<A HREF="faqw.py?req=edit&amp;file=faq01.017.htp">Edit this entry</A> /
 
770
<A HREF="faqw.py?req=log&amp;file=faq01.017.htp">Log info</A>
 
771
 
 
772
/ Last changed on Sat May 24 10:13:11 1997 by
 
773
<A HREF="mailto:aaron_watters@msn.com">Aaron Watters</A>
 
774
<P>
 
775
 
 
776
<HR>
 
777
<H2><A NAME="1.18">1.18. Can I use the FAQ Wizard software to maintain my own FAQ?</A></H2>
 
778
Sure.  It's in Tools/faqwiz/ of the python source tree.
 
779
<P>
 
780
 
 
781
<A HREF="faqw.py?req=edit&amp;file=faq01.018.htp">Edit this entry</A> /
 
782
<A HREF="faqw.py?req=log&amp;file=faq01.018.htp">Log info</A>
 
783
 
 
784
/ Last changed on Fri Mar 29 06:50:32 2002 by
 
785
<A HREF="mailto:aahz@pythoncraft.com">Aahz</A>
 
786
<P>
 
787
 
 
788
<HR>
 
789
<H2><A NAME="1.19">1.19. Which editor has good support for editing Python source code?</A></H2>
 
790
On Unix, the first choice is Emacs/XEmacs.  There's an elaborate
 
791
mode for editing Python code, which is available from the Python
 
792
source distribution (Misc/python-mode.el).  It's also bundled
 
793
with XEmacs (we're still working on legal details to make it possible
 
794
to bundle it with FSF Emacs).  And it has its own web page:
 
795
<P>
 
796
<PRE>
 
797
    <A HREF="http://www.python.org/emacs/python-mode/index.html">http://www.python.org/emacs/python-mode/index.html</A>
 
798
</PRE>
 
799
There are many other choices, for Unix, Windows or Macintosh.
 
800
Richard Jones compiled a table from postings on the Python newsgroup:
 
801
<P>
 
802
<PRE>
 
803
    <A HREF="http://www.bofh.asn.au/~richard/editors.html">http://www.bofh.asn.au/~richard/editors.html</A>
 
804
</PRE>
 
805
See also FAQ question 7.10 for some more Mac and Win options.
 
806
<P>
 
807
 
 
808
<A HREF="faqw.py?req=edit&amp;file=faq01.019.htp">Edit this entry</A> /
 
809
<A HREF="faqw.py?req=log&amp;file=faq01.019.htp">Log info</A>
 
810
 
 
811
/ Last changed on Mon Jun 15 23:21:04 1998 by
 
812
<A HREF="mailto:guido@python.org">Gvr</A>
 
813
<P>
 
814
 
 
815
<HR>
 
816
<H2><A NAME="1.20">1.20. I've never programmed before. Is there a Python tutorial?</A></H2>
 
817
There are several, and at least one book.  
 
818
All information for beginning Python programmers is collected here:
 
819
<P>
 
820
<PRE>
 
821
    <A HREF="http://www.python.org/doc/Newbies.html">http://www.python.org/doc/Newbies.html</A>
 
822
</PRE>
 
823
<P>
 
824
 
 
825
<A HREF="faqw.py?req=edit&amp;file=faq01.020.htp">Edit this entry</A> /
 
826
<A HREF="faqw.py?req=log&amp;file=faq01.020.htp">Log info</A>
 
827
 
 
828
/ Last changed on Wed Sep  5 05:34:07 2001 by
 
829
<A HREF="mailto:guido@python.org">GvR</A>
 
830
<P>
 
831
 
 
832
<HR>
 
833
<H2><A NAME="1.21">1.21. Where in the world is www.python.org located?</A></H2>
 
834
It's currently in Amsterdam, graciously hosted by XS4ALL:
 
835
<P>
 
836
<PRE>
 
837
    <A HREF="http://www.xs4all.nl">http://www.xs4all.nl</A>
 
838
</PRE>
 
839
Thanks to Thomas Wouters for setting this up!!!!
 
840
<P>
 
841
 
 
842
<A HREF="faqw.py?req=edit&amp;file=faq01.021.htp">Edit this entry</A> /
 
843
<A HREF="faqw.py?req=log&amp;file=faq01.021.htp">Log info</A>
 
844
 
 
845
/ Last changed on Fri Aug  3 21:49:27 2001 by
 
846
<A HREF="mailto:guido@python.org">GvR</A>
 
847
<P>
 
848
 
 
849
<HR>
 
850
<H1>2. Python in the real world</H1>
 
851
 
 
852
<HR>
 
853
<H2><A NAME="2.1">2.1. How many people are using Python?</A></H2>
 
854
Certainly thousands, and quite probably tens of thousands of users.
 
855
More are seeing the light each day.  The comp.lang.python newsgroup is
 
856
very active, but overall there is no accurate estimate of the number of subscribers or Python users.
 
857
<P>
 
858
Jacek Artymiak has created a Python Users Counter; you can see the 
 
859
current count by visiting
 
860
<A HREF="http://www.wszechnica.safenet.pl/cgi-bin/checkpythonuserscounter.py">http://www.wszechnica.safenet.pl/cgi-bin/checkpythonuserscounter.py</A>
 
861
(this will not increment the counter; use the link there if you haven't
 
862
added yourself already). Most Python users appear not to have registered themselves.
 
863
<P>
 
864
 
 
865
<A HREF="faqw.py?req=edit&amp;file=faq02.001.htp">Edit this entry</A> /
 
866
<A HREF="faqw.py?req=log&amp;file=faq02.001.htp">Log info</A>
 
867
 
 
868
/ Last changed on Thu Feb 21 23:29:18 2002 by
 
869
<A HREF="mailto:guido@python.org">GvR</A>
 
870
<P>
 
871
 
 
872
<HR>
 
873
<H2><A NAME="2.2">2.2. Have any significant projects been done in Python?</A></H2>
 
874
At CWI (the former home of Python), we have written a 20,000 line
 
875
authoring environment for transportable hypermedia presentations, a
 
876
5,000 line multimedia teleconferencing tool, as well as many many
 
877
smaller programs.
 
878
<P>
 
879
At CNRI (Python's new home), we have written two large applications:
 
880
Grail, a fully featured web browser (see
 
881
<A HREF="http://grail.cnri.reston.va.us">http://grail.cnri.reston.va.us</A>),
 
882
and the Knowbot Operating Environment,
 
883
a distributed environment for mobile code.
 
884
<P>
 
885
The University of Virginia uses Python to control a virtual reality
 
886
engine.  See <A HREF="http://alice.cs.cmu.edu">http://alice.cs.cmu.edu</A>.
 
887
<P>
 
888
The ILU project at Xerox PARC can generate Python glue for ILU
 
889
interfaces.  See <A HREF="ftp://ftp.parc.xerox.com/pub/ilu/ilu.html">ftp://ftp.parc.xerox.com/pub/ilu/ilu.html</A>.  ILU
 
890
is a free CORBA compliant ORB which supplies distributed object
 
891
connectivity to a host of platforms using a host of languages.
 
892
<P>
 
893
Mark Hammond and Greg Stein and others are interfacing Python to
 
894
Microsoft's COM and ActiveX architectures.  This means, among other
 
895
things, that Python may be used in active server pages or as a COM
 
896
controller (for example to automatically extract from or insert information
 
897
into Excel or MSAccess or any other COM aware application).
 
898
Mark claims Python can even be a ActiveX scripting host (which
 
899
means you could embed JScript inside a Python application, if you
 
900
had a strange sense of humor).  Python/AX/COM is distributed as part
 
901
of the PythonWin distribution.
 
902
<P>
 
903
The University of California, Irvine uses a student administration
 
904
system called TELE-Vision written entirely in Python.  Contact: Ray
 
905
Price <A HREF="mailto:rlprice@uci.edu">rlprice@uci.edu</A>.
 
906
<P>
 
907
The Melbourne Cricket Ground (MCG) in Australia (a 100,000+ person venue)
 
908
has it's scoreboard system written largely in Python on MS Windows.
 
909
Python expressions are used to create almost every scoring entry that
 
910
appears on the board.  The move to Python/C++ away from exclusive C++
 
911
has provided a level of functionality that would simply not have been
 
912
viable otherwise.
 
913
<P>
 
914
See also the next question.
 
915
<P>
 
916
Note: this FAQ entry is really old.
 
917
See <A HREF="http://www.python.org/psa/Users.html">http://www.python.org/psa/Users.html</A> for a more recent list.
 
918
<P>
 
919
 
 
920
<A HREF="faqw.py?req=edit&amp;file=faq02.002.htp">Edit this entry</A> /
 
921
<A HREF="faqw.py?req=log&amp;file=faq02.002.htp">Log info</A>
 
922
 
 
923
/ Last changed on Wed Oct 25 13:24:15 2000 by
 
924
<A HREF="mailto:guido@python.org">GvR</A>
 
925
<P>
 
926
 
 
927
<HR>
 
928
<H2><A NAME="2.3">2.3. Are there any commercial projects going on using Python?</A></H2>
 
929
Yes, there's lots of commercial activity using Python.  See
 
930
<A HREF="http://www.python.org/psa/Users.html">http://www.python.org/psa/Users.html</A> for a list.
 
931
<P>
 
932
 
 
933
<A HREF="faqw.py?req=edit&amp;file=faq02.003.htp">Edit this entry</A> /
 
934
<A HREF="faqw.py?req=log&amp;file=faq02.003.htp">Log info</A>
 
935
 
 
936
/ Last changed on Wed Oct 14 18:17:33 1998 by
 
937
<A HREF="mailto:klm@python.org">ken</A>
 
938
<P>
 
939
 
 
940
<HR>
 
941
<H2><A NAME="2.4">2.4. How stable is Python?</A></H2>
 
942
Very stable.  New, stable releases have been coming out roughly every 3 to 12 months since 1991, and this seems likely to continue.
 
943
<P>
 
944
With the introduction of retrospective "bugfix" releases the stability of the language implementations can be, and is being, improved independently of the new features offered by more recent major or minor releases. Bugfix releases, indicated by a third component of the version number, only fix known problems and do not gratuitously introduce new and possibly incompatible features or modified library functionality.
 
945
<P>
 
946
Release 2.2 got its first bugfix on April 10, 2002. The new version
 
947
number is now 2.2.1. The 2.1 release, at 2.1.3, can probably be
 
948
considered the "most stable" platform because it has been bugfixed
 
949
twice.
 
950
<P>
 
951
 
 
952
<A HREF="faqw.py?req=edit&amp;file=faq02.004.htp">Edit this entry</A> /
 
953
<A HREF="faqw.py?req=log&amp;file=faq02.004.htp">Log info</A>
 
954
 
 
955
/ Last changed on Tue Jul 23 10:20:04 2002 by
 
956
<A HREF="mailto:kubieziel@gmx.de">Jens Kubieziel</A>
 
957
<P>
 
958
 
 
959
<HR>
 
960
<H2><A NAME="2.5">2.5. What new developments are expected for Python in the future?</A></H2>
 
961
See <A HREF="http://www.python.org/peps">http://www.python.org/peps</A>/ for the Python Enhancement 
 
962
Proposals (PEPs). PEPs are design
 
963
documents 
 
964
describing a  suggested new feature for Python, providing
 
965
a concise technical specification and a rationale.
 
966
<P>
 
967
Also, follow the discussions on the python-dev mailing list.
 
968
<P>
 
969
 
 
970
<A HREF="faqw.py?req=edit&amp;file=faq02.005.htp">Edit this entry</A> /
 
971
<A HREF="faqw.py?req=log&amp;file=faq02.005.htp">Log info</A>
 
972
 
 
973
/ Last changed on Tue Apr  9 17:09:51 2002 by
 
974
<A HREF="mailto:akuchlin@mems-exchange.org">A.M. Kuchling</A>
 
975
<P>
 
976
 
 
977
<HR>
 
978
<H2><A NAME="2.6">2.6. Is it reasonable to propose incompatible changes to Python?</A></H2>
 
979
In general, no.  There are already millions of lines of Python code
 
980
around the world, so any changes in the language that invalidates more
 
981
than a very small fraction of existing programs has to be frowned
 
982
upon.  Even if you can provide a conversion program, there still is
 
983
the problem of updating all documentation.  Providing a gradual
 
984
upgrade path is the only way if a feature has to be changed.
 
985
<P>
 
986
See <A HREF="http://www.python.org/peps/pep-0005.html">http://www.python.org/peps/pep-0005.html</A> for the proposed
 
987
mechanism for creating backwards-incompatibilities.
 
988
<P>
 
989
 
 
990
<A HREF="faqw.py?req=edit&amp;file=faq02.006.htp">Edit this entry</A> /
 
991
<A HREF="faqw.py?req=log&amp;file=faq02.006.htp">Log info</A>
 
992
 
 
993
/ Last changed on Mon Apr  1 22:13:47 2002 by
 
994
<A HREF="mailto:fdrake@acm.org">Fred Drake</A>
 
995
<P>
 
996
 
 
997
<HR>
 
998
<H2><A NAME="2.7">2.7. What is the future of Python?</A></H2>
 
999
Please see <A HREF="http://www.python.org/peps">http://www.python.org/peps</A>/ for proposals of future
 
1000
activities. One of the PEPs (Python Enhancement Proposals) deals
 
1001
with the PEP process and PEP format -- see
 
1002
<A HREF="http://www.python.org/peps/pep-0001.html">http://www.python.org/peps/pep-0001.html</A> if you want to
 
1003
submit a PEP. In <A HREF="http://www.python.org/peps/pep-0042.html">http://www.python.org/peps/pep-0042.html</A> there
 
1004
is a list of wishlists the Python Development team plans to tackle.
 
1005
<P>
 
1006
 
 
1007
<A HREF="faqw.py?req=edit&amp;file=faq02.007.htp">Edit this entry</A> /
 
1008
<A HREF="faqw.py?req=log&amp;file=faq02.007.htp">Log info</A>
 
1009
 
 
1010
/ Last changed on Mon Apr  1 22:15:46 2002 by
 
1011
<A HREF="mailto:fdrake@acm.org">Fred Drake</A>
 
1012
<P>
 
1013
 
 
1014
<HR>
 
1015
<H2><A NAME="2.8">2.8. What was the PSA, anyway?</A></H2>
 
1016
The Python Software Activity was
 
1017
created by a number of Python aficionados who want Python to be more
 
1018
than the product and responsibility of a single individual.
 
1019
The PSA was not an independent organization, but lived
 
1020
under the umbrealla of CNRI.
 
1021
<P>
 
1022
The PSA has been superseded by the Python Software Foundation,
 
1023
an independent non-profit organization.  The PSF's home page
 
1024
is at <A HREF="http://www.python.org/psf">http://www.python.org/psf</A>/.
 
1025
<P>
 
1026
Some pages created by the PSA still live at
 
1027
<A HREF="http://www.python.org/psa">http://www.python.org/psa</A>/
 
1028
<P>
 
1029
 
 
1030
<A HREF="faqw.py?req=edit&amp;file=faq02.008.htp">Edit this entry</A> /
 
1031
<A HREF="faqw.py?req=log&amp;file=faq02.008.htp">Log info</A>
 
1032
 
 
1033
/ Last changed on Thu Jul 25 18:19:44 2002 by
 
1034
<A HREF="mailto:guido@python.org">GvR</A>
 
1035
<P>
 
1036
 
 
1037
<HR>
 
1038
<H2><A NAME="2.9">2.9. Deleted</A></H2>
 
1039
<P>
 
1040
<P>
 
1041
 
 
1042
<A HREF="faqw.py?req=edit&amp;file=faq02.009.htp">Edit this entry</A> /
 
1043
<A HREF="faqw.py?req=log&amp;file=faq02.009.htp">Log info</A>
 
1044
 
 
1045
/ Last changed on Tue Jan  2 02:51:30 2001 by
 
1046
<A HREF="mailto:moshez@zadka.site.co.il">Moshe Zadka</A>
 
1047
<P>
 
1048
 
 
1049
<HR>
 
1050
<H2><A NAME="2.10">2.10. Deleted</A></H2>
 
1051
<P>
 
1052
<P>
 
1053
 
 
1054
<A HREF="faqw.py?req=edit&amp;file=faq02.010.htp">Edit this entry</A> /
 
1055
<A HREF="faqw.py?req=log&amp;file=faq02.010.htp">Log info</A>
 
1056
 
 
1057
/ Last changed on Tue Jan  2 02:52:19 2001 by
 
1058
<A HREF="mailto:moshez@zadka.site.co.il">Moshe Zadka</A>
 
1059
<P>
 
1060
 
 
1061
<HR>
 
1062
<H2><A NAME="2.11">2.11. Is Python Y2K (Year 2000) Compliant?</A></H2>
 
1063
As of January, 2001 no major problems have been reported and Y2K
 
1064
compliance seems to be a non-issue.
 
1065
<P>
 
1066
Since Python is available free of charge, there are no absolute
 
1067
guarantees.  If there <I>are</I> unforeseen problems, liability is the
 
1068
user's rather than the developers', and there is nobody you can sue for damages.
 
1069
<P>
 
1070
Python does few 
 
1071
date manipulations, and what it does is all based on the Unix
 
1072
representation for time (even on non-Unix systems) which uses seconds
 
1073
since 1970 and won't overflow until 2038.
 
1074
<P>
 
1075
 
 
1076
<A HREF="faqw.py?req=edit&amp;file=faq02.011.htp">Edit this entry</A> /
 
1077
<A HREF="faqw.py?req=log&amp;file=faq02.011.htp">Log info</A>
 
1078
 
 
1079
/ Last changed on Mon Jan  8 17:19:32 2001 by
 
1080
<A HREF="mailto:sholden@holdenweb.com">Steve Holden</A>
 
1081
<P>
 
1082
 
 
1083
<HR>
 
1084
<H2><A NAME="2.12">2.12. Is Python a good language in a class for beginning programmers?</A></H2>
 
1085
Yes.  This long answer attempts to address any concerns you might
 
1086
have with teaching Python as a programmer's first language.  
 
1087
(If you want to discuss Python's use in education, then 
 
1088
you may be interested in joining the edu-sig mailinglist.
 
1089
See <A HREF="http://www.python.org/sigs/edu-sig">http://www.python.org/sigs/edu-sig</A>/ )
 
1090
<P>
 
1091
It is still common to start students with a procedural
 
1092
(subset of a) statically typed language such as Pascal, C, or
 
1093
a subset of C++ or Java.  I think that students may be better
 
1094
served by learning Python as their first language.  Python has
 
1095
a very simple and consistent syntax and a large standard library.
 
1096
Most importantly, using Python in a beginning programming course
 
1097
permits students to concentrate on important programming skills,
 
1098
such as problem decomposition and data type design.
 
1099
<P>
 
1100
With Python, students can be quickly introduced to basic concepts
 
1101
such as loops and procedures.  They can even probably work with
 
1102
user-defined objects in their very first course.  They could
 
1103
implement a tree structure as nested Python lists, for example.
 
1104
They could be introduced to objects in their first course if
 
1105
desired.  For a student who has never programmed before, using
 
1106
a statically typed language seems unnatural.  It presents
 
1107
additional complexity that the student must master and slows
 
1108
the pace of the course.  The students are trying to learn to
 
1109
think like a computer, decompose problems, design consistent
 
1110
interfaces, and encapsulate data.  While learning to use a
 
1111
statically typed language is important, it is not necessarily the
 
1112
best topic to address in the students' first programming course.
 
1113
<P>
 
1114
Many other aspects of Python make it a good first language.
 
1115
Python has a large standard library (like Java) so that
 
1116
students can be assigned programming projects very early in the
 
1117
course that <I>do</I> something.  Assignments aren't restricted to the
 
1118
standard four-function calculator and check balancing programs.
 
1119
By using the standard library, students can gain the satisfaction
 
1120
of working on realistic applications as they learn the fundamentals
 
1121
of programming.  Using the standard library also teaches students
 
1122
about code reuse.
 
1123
<P>
 
1124
Python's interactive interpreter also enables students to
 
1125
test language features while they're programming.  They can keep
 
1126
a window with the interpreter running while they enter their
 
1127
programs' source in another window.  If they can't remember the
 
1128
methods for a list, they can do something like this:
 
1129
<P>
 
1130
<PRE>
 
1131
 >>> L = []
 
1132
 >>> dir(L)
 
1133
 ['append', 'count', 'extend', 'index', 'insert', 'pop', 'remove',
 
1134
 'reverse', 'sort']
 
1135
 >>> print L.append.__doc__
 
1136
 L.append(object) -- append object to end
 
1137
 >>> L.append(1)
 
1138
 >>> L
 
1139
 [1]
 
1140
</PRE>
 
1141
With the interpreter, documentation is never far from the
 
1142
student as he's programming.
 
1143
<P>
 
1144
There are also good IDEs for Python.  Guido van Rossum's IDLE
 
1145
is a cross-platform IDE for Python that is written in Python
 
1146
using Tk.  There is also a Windows specific IDE called PythonWin.
 
1147
Emacs users will be happy to know that there is a very good Python
 
1148
mode for Emacs.  All of these programming environments provide
 
1149
syntax highlighting, auto-indenting, and access to the interactive
 
1150
interpreter while coding.  For more information about IDEs, see XXX.
 
1151
<P>
 
1152
If your department is currently using Pascal because it was
 
1153
designed to be a teaching language, then you'll be happy to
 
1154
know that Guido van Rossum designed Python to be simple to
 
1155
teach to everyone but powerful enough to implement real world
 
1156
applications.  Python makes a good language for first time
 
1157
programmers because that was one of Python's design goals.
 
1158
There are papers at <A HREF="http://www.python.org/doc/essays">http://www.python.org/doc/essays</A>/ on the Python website 
 
1159
by Python's creator explaining his objectives for the language.
 
1160
One that may interest you is titled "Computer Programming for Everybody" 
 
1161
<A HREF="http://www.python.org/doc/essays/cp4e.html">http://www.python.org/doc/essays/cp4e.html</A>
 
1162
<P>
 
1163
If you're seriously considering Python as a language for your
 
1164
school,  Guido van Rossum may even be willing to correspond with
 
1165
you about how the language would fit in your curriculum.
 
1166
See <A HREF="http://www.python.org/doc/FAQ.html#2.2">http://www.python.org/doc/FAQ.html#2.2</A> for examples of
 
1167
Python's use in the "real world."
 
1168
<P>
 
1169
While Python, its source code, and its IDEs are freely
 
1170
available, this consideration should not rule 
 
1171
out other languages.  There are other free languages (Java, 
 
1172
free C compilers), and many companies are willing to waive some
 
1173
or all of their fees for student programming tools if it
 
1174
guarantees that a whole graduating class will know how to
 
1175
use their tools.  That is, if one of the requirements for 
 
1176
the language that will be taught is that it be freely 
 
1177
available, then Python qualifies, but this requirement 
 
1178
does not preclude other languages.
 
1179
<P>
 
1180
While Python jobs may not be as prevalent as C/C++/Java jobs,
 
1181
teachers should not worry about teaching students critical job
 
1182
skills in their first course.  The skills that win students a
 
1183
job are those they learn in their senior classes and internships.
 
1184
Their first programming courses are there to lay a solid
 
1185
foundation in programming fundamentals.  The primary question
 
1186
in choosing the language for such a course should be which
 
1187
language permits the students to learn this material without
 
1188
hindering or limiting them.
 
1189
<P>
 
1190
Another argument for Python is that there are many tasks for
 
1191
which something like C++ is overkill.  That's where languages
 
1192
like Python, Perl, Tcl, and Visual Basic thrive.  It's critical
 
1193
for students to know something about these languages.   (Every
 
1194
employer for whom I've worked used at least one such language.)
 
1195
Of the languages listed above, Python probably makes the best
 
1196
language in a programming curriculum since its syntax is simple,
 
1197
consistent, and not unlike other languages (C/C++/Java) that
 
1198
are probably in the curriculum.  By starting students with
 
1199
Python, a department simultaneously lays the foundations for
 
1200
other programming courses and introduces students to the type
 
1201
of language that is often used as a "glue" language.  As an
 
1202
added bonus, Python can be used to interface with Microsoft's
 
1203
COM components (thanks to Mark Hammond).  There is also Jython, 
 
1204
a Java implementation of the Python interpreter, that can be 
 
1205
used to connect Java components.
 
1206
<P>
 
1207
If you currently start students with Pascal or C/C++ or Java,
 
1208
you may be worried they will have trouble learning a statically
 
1209
typed language after starting with Python.  I think that this
 
1210
fear most often stems from the fact that the teacher started
 
1211
with a statically typed language, and we tend to like to teach
 
1212
others in the same way we were taught.  In reality, the
 
1213
transition from Python to one of these other languages is
 
1214
quite simple.
 
1215
<P>
 
1216
To motivate a statically typed language such as C++, begin the
 
1217
course by explaining that unlike Python, their first language,
 
1218
C++ is compiled to a machine dependent executable.  Explain
 
1219
that the point is to make a very fast executable.  To permit
 
1220
the compiler to make optimizations, programmers must help it
 
1221
by specifying the "types" of variables.  By restricting each
 
1222
variable to a specific type, the compiler can reduce the
 
1223
book-keeping it has to do to permit dynamic types.  The compiler
 
1224
also has to resolve references at compile time.  Thus, the
 
1225
language gains speed by sacrificing some of Python's dynamic
 
1226
features.  Then again, the C++ compiler provides type safety
 
1227
and catches many bugs at compile time instead of run time (a
 
1228
critical consideration for many commercial applications).  C++
 
1229
is also designed for very large programs where one may want to
 
1230
guarantee that others don't touch an object's implementation.
 
1231
C++ provides very strong language features to separate an object's
 
1232
implementation from its interface.  Explain why this separation
 
1233
is a good thing.
 
1234
<P>
 
1235
The first day of a C++ course could then be a whirlwind introduction
 
1236
to what C++ requires and provides.  The point here is that after
 
1237
a semester or two of Python, students are hopefully competent
 
1238
programmers.  They know how to handle loops and write procedures.
 
1239
They've also worked with objects, thought about the benefits of
 
1240
consistent interfaces, and used the technique of subclassing to
 
1241
specialize behavior.  Thus, a whirlwind introduction to C++ could
 
1242
show them how objects and subclassing looks in C++.  The
 
1243
potentially difficult concepts of object-oriented design were
 
1244
taught without the additional obstacles presented by a language
 
1245
such as C++ or Java.  When learning one of these languages,
 
1246
the students would already understand the "road map."  They
 
1247
understand objects; they would just be learning how objects
 
1248
fit in a statically typed languages.  Language requirements
 
1249
and compiler errors that seem unnatural to beginning programmers
 
1250
make sense in this new context.  Many students will find it
 
1251
helpful to be able to write a fast prototype of their algorithms
 
1252
in Python.  Thus, they can test and debug their ideas before
 
1253
they attempt to write the code in the new language, saving the
 
1254
effort of working with C++ types for when they've discovered a
 
1255
working solution for their assignments.  When they get annoyed
 
1256
with the rigidity of types, they'll be happy to learn about
 
1257
containers and templates to regain some of the lost flexibility
 
1258
Python afforded them.  Students may also gain an appreciation
 
1259
for the fact that no language is best for every task.  They'll
 
1260
see that C++ is faster, but they'll know that they can gain
 
1261
flexibility and development speed with a Python when execution
 
1262
speed isn't critical.
 
1263
<P>
 
1264
If you have any concerns that weren't addressed here, try
 
1265
posting to the Python newsgroup.  Others there have done some
 
1266
work with using Python as an instructional tool.  Good luck.
 
1267
We'd love to hear about it if you choose Python for your course.
 
1268
<P>
 
1269
 
 
1270
<A HREF="faqw.py?req=edit&amp;file=faq02.012.htp">Edit this entry</A> /
 
1271
<A HREF="faqw.py?req=log&amp;file=faq02.012.htp">Log info</A>
 
1272
 
 
1273
/ Last changed on Mon Dec  2 19:32:35 2002 by
 
1274
<A HREF="mailto:sconce@in-spec-inc.com">Bill Sconce</A>
 
1275
<P>
 
1276
 
 
1277
<HR>
 
1278
<H1>3. Building Python and Other Known Bugs</H1>
 
1279
 
 
1280
<HR>
 
1281
<H2><A NAME="3.1">3.1. Is there a test set?</A></H2>
 
1282
Sure.  You can run it after building with "make test", or you can
 
1283
run it manually with this command at the Python prompt:
 
1284
<P>
 
1285
<PRE>
 
1286
 import test.autotest
 
1287
</PRE>
 
1288
In Python 1.4 or earlier, use
 
1289
<P>
 
1290
<PRE>
 
1291
 import autotest
 
1292
</PRE>
 
1293
The test set doesn't test <I>all</I> features of Python,
 
1294
but it goes a long way to confirm that Python is actually working.
 
1295
<P>
 
1296
NOTE: if "make test" fails, don't just mail the output to the
 
1297
newsgroup -- this doesn't give enough information to debug the
 
1298
problem.  Instead, find out which test fails, and run that test
 
1299
manually from an interactive interpreter.  For example, if
 
1300
"make test" reports that test_spam fails, try this interactively:
 
1301
<P>
 
1302
<PRE>
 
1303
 import test.test_spam
 
1304
</PRE>
 
1305
This generally produces more verbose output which can be diagnosed
 
1306
to debug the problem.  If you find a bug in Python or the libraries, or in the tests, please report this in the Python bug tracker at SourceForge:
 
1307
<P>
 
1308
<A HREF="http://sourceforge.net/tracker/?func=add&amp;group_id=5470&amp;atid=105470">http://sourceforge.net/tracker/?func=add&amp;group_id=5470&amp;atid=105470</A>
 
1309
<P>
 
1310
 
 
1311
<A HREF="faqw.py?req=edit&amp;file=faq03.001.htp">Edit this entry</A> /
 
1312
<A HREF="faqw.py?req=log&amp;file=faq03.001.htp">Log info</A>
 
1313
 
 
1314
/ Last changed on Fri Apr 27 10:29:36 2001 by
 
1315
<A HREF="mailto:fdrake@acm.org">Fred Drake</A>
 
1316
<P>
 
1317
 
 
1318
<HR>
 
1319
<H2><A NAME="3.2">3.2. When running the test set, I get complaints about floating point operations, but when playing with floating point operations I cannot find anything wrong with them.</A></H2>
 
1320
The test set makes occasional unwarranted assumptions about the
 
1321
semantics of C floating point operations.  Until someone donates a
 
1322
better floating point test set, you will have to comment out the
 
1323
offending floating point tests and execute similar tests manually.
 
1324
<P>
 
1325
 
 
1326
<A HREF="faqw.py?req=edit&amp;file=faq03.002.htp">Edit this entry</A> /
 
1327
<A HREF="faqw.py?req=log&amp;file=faq03.002.htp">Log info</A>
 
1328
<P>
 
1329
 
 
1330
<HR>
 
1331
<H2><A NAME="3.3">3.3. Link errors after rerunning the configure script.</A></H2>
 
1332
It is generally necessary to run "make clean" after a configuration
 
1333
change.
 
1334
<P>
 
1335
 
 
1336
<A HREF="faqw.py?req=edit&amp;file=faq03.003.htp">Edit this entry</A> /
 
1337
<A HREF="faqw.py?req=log&amp;file=faq03.003.htp">Log info</A>
 
1338
<P>
 
1339
 
 
1340
<HR>
 
1341
<H2><A NAME="3.4">3.4. The python interpreter complains about options passed to a script (after the script name).</A></H2>
 
1342
You are probably linking with GNU getopt, e.g. through -liberty.
 
1343
Don't.  The reason for the complaint is that GNU getopt, unlike System
 
1344
V getopt and other getopt implementations, doesn't consider a
 
1345
non-option to be the end of the option list.  A quick (and compatible)
 
1346
fix for scripts is to add "--" to the interpreter, like this:
 
1347
<P>
 
1348
<PRE>
 
1349
        #! /usr/local/bin/python --
 
1350
</PRE>
 
1351
You can also use this interactively:
 
1352
<P>
 
1353
<PRE>
 
1354
        python -- script.py [options]
 
1355
</PRE>
 
1356
Note that a working getopt implementation is provided in the Python
 
1357
distribution (in Python/getopt.c) but not automatically used.
 
1358
<P>
 
1359
 
 
1360
<A HREF="faqw.py?req=edit&amp;file=faq03.004.htp">Edit this entry</A> /
 
1361
<A HREF="faqw.py?req=log&amp;file=faq03.004.htp">Log info</A>
 
1362
<P>
 
1363
 
 
1364
<HR>
 
1365
<H2><A NAME="3.5">3.5. When building on the SGI, make tries to run python to create glmodule.c, but python hasn't been built or installed yet.</A></H2>
 
1366
Comment out the line mentioning glmodule.c in Setup and build a
 
1367
python without gl first; install it or make sure it is in your $PATH,
 
1368
then edit the Setup file again to turn on the gl module, and make
 
1369
again.  You don't need to do "make clean"; you do need to run "make
 
1370
Makefile" in the Modules subdirectory (or just run "make" at the
 
1371
toplevel).
 
1372
<P>
 
1373
 
 
1374
<A HREF="faqw.py?req=edit&amp;file=faq03.005.htp">Edit this entry</A> /
 
1375
<A HREF="faqw.py?req=log&amp;file=faq03.005.htp">Log info</A>
 
1376
<P>
 
1377
 
 
1378
<HR>
 
1379
<H2><A NAME="3.6">3.6. I use VPATH but some targets are built in the source directory.</A></H2>
 
1380
On some systems (e.g. Sun), if the target already exists in the
 
1381
source directory, it is created there instead of in the build
 
1382
directory.  This is usually because you have previously built without
 
1383
VPATH.  Try running "make clobber" in the source directory.
 
1384
<P>
 
1385
 
 
1386
<A HREF="faqw.py?req=edit&amp;file=faq03.006.htp">Edit this entry</A> /
 
1387
<A HREF="faqw.py?req=log&amp;file=faq03.006.htp">Log info</A>
 
1388
<P>
 
1389
 
 
1390
<HR>
 
1391
<H2><A NAME="3.7">3.7. Trouble building or linking with the GNU readline library.</A></H2>
 
1392
You can use the GNU readline library to improve the interactive user
 
1393
interface: this gives you line editing and command history when
 
1394
calling python interactively. Its sources are distributed with 
 
1395
Python (at least for 2.0).  Uncomment the line 
 
1396
<P>
 
1397
#readline readline.c -lreadline -ltermcap
 
1398
<P>
 
1399
in Modules/Setup.  The configuration option --with-readline 
 
1400
is no longer supported, at least in Python 2.0.  Some hints on 
 
1401
building and using the readline library:
 
1402
On SGI IRIX 5, you may have to add the following
 
1403
to rldefs.h:
 
1404
<P>
 
1405
<PRE>
 
1406
        #ifndef sigmask
 
1407
        #define sigmask(sig) (1L &lt;&lt; ((sig)-1))
 
1408
        #endif
 
1409
</PRE>
 
1410
On some systems, you will have to add #include "rldefs.h" to the
 
1411
top of several source files, and if you use the VPATH feature, you
 
1412
will have to add dependencies of the form foo.o: foo.c to the
 
1413
Makefile for several values of foo.
 
1414
The readline library requires use of the termcap library. A
 
1415
known problem with this is that it contains entry points which
 
1416
cause conflicts with the STDWIN and SGI GL libraries. The STDWIN
 
1417
conflict can be solved by adding a line saying '#define werase w_erase' to the
 
1418
stdwin.h file (in the STDWIN distribution, subdirectory H). The
 
1419
GL conflict has been solved in the Python configure script by a
 
1420
hack that forces use of the static version of the termcap library.
 
1421
Check the newsgroup gnu.bash.bug news:gnu.bash.bug for
 
1422
specific problems with the readline library (I don't read this group
 
1423
but I've been told that it is the place for readline bugs).
 
1424
<P>
 
1425
 
 
1426
<A HREF="faqw.py?req=edit&amp;file=faq03.007.htp">Edit this entry</A> /
 
1427
<A HREF="faqw.py?req=log&amp;file=faq03.007.htp">Log info</A>
 
1428
 
 
1429
/ Last changed on Sat Dec  2 18:23:48 2000 by
 
1430
<A HREF="mailto:trotts@llnl.gov">Issac Trotts</A>
 
1431
<P>
 
1432
 
 
1433
<HR>
 
1434
<H2><A NAME="3.8">3.8. Trouble with socket I/O on older Linux 1.x versions.</A></H2>
 
1435
Once you've built Python, use it to run the regen script in the
 
1436
Lib/plat-linux2 directory.  Apparently the files as distributed don't match the system headers on some Linux versions.
 
1437
<P>
 
1438
Note that this FAQ entry only applies to Linux kernel versions 1.x.y;
 
1439
these are hardly around any more.
 
1440
<P>
 
1441
 
 
1442
<A HREF="faqw.py?req=edit&amp;file=faq03.008.htp">Edit this entry</A> /
 
1443
<A HREF="faqw.py?req=log&amp;file=faq03.008.htp">Log info</A>
 
1444
 
 
1445
/ Last changed on Tue Jul 30 20:05:52 2002 by
 
1446
<A HREF="mailto:kubieziel@gmx.de">Jens Kubieziel</A>
 
1447
<P>
 
1448
 
 
1449
<HR>
 
1450
<H2><A NAME="3.9">3.9. Trouble with prototypes on Ultrix.</A></H2>
 
1451
Ultrix cc seems broken -- use gcc, or edit config.h to #undef
 
1452
HAVE_PROTOTYPES.
 
1453
<P>
 
1454
 
 
1455
<A HREF="faqw.py?req=edit&amp;file=faq03.009.htp">Edit this entry</A> /
 
1456
<A HREF="faqw.py?req=log&amp;file=faq03.009.htp">Log info</A>
 
1457
<P>
 
1458
 
 
1459
<HR>
 
1460
<H2><A NAME="3.10">3.10. Other trouble building Python on platform X.</A></H2>
 
1461
Please submit the details to the SourceForge bug tracker:
 
1462
<P>
 
1463
<PRE>
 
1464
  <A HREF="http://sourceforge.net/tracker/?group_id=5470&amp;atid=105470">http://sourceforge.net/tracker/?group_id=5470&amp;atid=105470</A>
 
1465
</PRE>
 
1466
and we'll look
 
1467
into it.  Please provide as many details as possible.  In particular,
 
1468
if you don't tell us what type of computer and what operating system
 
1469
(and version) you are using it will be difficult for us to figure out
 
1470
what is the matter.  If you have compilation output logs,
 
1471
please use file uploads -- don't paste everything in the message box.
 
1472
<P>
 
1473
In many cases, we won't have access to the same hardware or operating system version, so <I>please</I>, if you have a SourceForge account, log in before filing your report, or if you don't have an account, include an email address at which we can reach you for further questions.  Logging in to SourceForge first will also cause SourceForge to send you updates as we act on your report.
 
1474
<P>
 
1475
 
 
1476
<A HREF="faqw.py?req=edit&amp;file=faq03.010.htp">Edit this entry</A> /
 
1477
<A HREF="faqw.py?req=log&amp;file=faq03.010.htp">Log info</A>
 
1478
 
 
1479
/ Last changed on Fri Apr 27 10:53:18 2001 by
 
1480
<A HREF="mailto:fdrake@acm.org">Fred Drake</A>
 
1481
<P>
 
1482
 
 
1483
<HR>
 
1484
<H2><A NAME="3.11">3.11. How to configure dynamic loading on Linux.</A></H2>
 
1485
This is now automatic as long as your Linux version uses the ELF
 
1486
object format (all recent Linuxes do).
 
1487
<P>
 
1488
 
 
1489
<A HREF="faqw.py?req=edit&amp;file=faq03.011.htp">Edit this entry</A> /
 
1490
<A HREF="faqw.py?req=log&amp;file=faq03.011.htp">Log info</A>
 
1491
<P>
 
1492
 
 
1493
<HR>
 
1494
<H2><A NAME="3.12">3.12. I can't get shared modules to work on Linux 2.0 (Slackware96)?</A></H2>
 
1495
This is a bug in the Slackware96 release. The fix is simple: Make sure
 
1496
that there is a link from /lib/libdl.so to /lib/libdl.so.1 so that the
 
1497
following links are setup: /lib/libdl.so -&gt; /lib/libdl.so.1
 
1498
/lib/libdl.so.1 -&gt; /lib/libdl.so.1.7.14 You may have to rerun the
 
1499
configure script, after rm'ing the config.cache file, before you
 
1500
attempt to rebuild python after this fix.
 
1501
<P>
 
1502
 
 
1503
<A HREF="faqw.py?req=edit&amp;file=faq03.012.htp">Edit this entry</A> /
 
1504
<A HREF="faqw.py?req=log&amp;file=faq03.012.htp">Log info</A>
 
1505
 
 
1506
/ Last changed on Wed May 21 15:45:03 1997 by
 
1507
<A HREF="mailto:guido2@python.org">GvR</A>
 
1508
<P>
 
1509
 
 
1510
<HR>
 
1511
<H2><A NAME="3.13">3.13. Trouble when making modules shared on Linux.</A></H2>
 
1512
This happens when you have built Python for static linking and then
 
1513
enable
 
1514
<PRE>
 
1515
  *shared*
 
1516
</PRE>
 
1517
in the Setup file.  Shared library code must be
 
1518
compiled with "-fpic".  If a .o file for the module already exist that
 
1519
was compiled for static linking, you must remove it or do "make clean"
 
1520
in the Modules directory.
 
1521
<P>
 
1522
 
 
1523
<A HREF="faqw.py?req=edit&amp;file=faq03.013.htp">Edit this entry</A> /
 
1524
<A HREF="faqw.py?req=log&amp;file=faq03.013.htp">Log info</A>
 
1525
 
 
1526
/ Last changed on Fri May 23 13:42:30 1997 by
 
1527
<A HREF="mailto:guido@python.org">GvR</A>
 
1528
<P>
 
1529
 
 
1530
<HR>
 
1531
<H2><A NAME="3.14">3.14. [deleted]</A></H2>
 
1532
[ancient information on threads on linux (when thread support
 
1533
was not standard) used to be here]
 
1534
<P>
 
1535
 
 
1536
<A HREF="faqw.py?req=edit&amp;file=faq03.014.htp">Edit this entry</A> /
 
1537
<A HREF="faqw.py?req=log&amp;file=faq03.014.htp">Log info</A>
 
1538
 
 
1539
/ Last changed on Sun Jun  2 17:27:13 2002 by
 
1540
<A HREF="mailto:erno-pyfaq@erno.iki.fi">Erno Kuusela</A>
 
1541
<P>
 
1542
 
 
1543
<HR>
 
1544
<H2><A NAME="3.15">3.15. Errors when linking with a shared library containing C++ code.</A></H2>
 
1545
Link the main Python binary with C++.  Change the definition of
 
1546
LINKCC in Modules/Makefile to be your C++ compiler.  You may have to
 
1547
edit config.c slightly to make it compilable with C++.
 
1548
<P>
 
1549
 
 
1550
<A HREF="faqw.py?req=edit&amp;file=faq03.015.htp">Edit this entry</A> /
 
1551
<A HREF="faqw.py?req=log&amp;file=faq03.015.htp">Log info</A>
 
1552
<P>
 
1553
 
 
1554
<HR>
 
1555
<H2><A NAME="3.16">3.16. Deleted</A></H2>
 
1556
<P>
 
1557
<P>
 
1558
 
 
1559
<A HREF="faqw.py?req=edit&amp;file=faq03.016.htp">Edit this entry</A> /
 
1560
<A HREF="faqw.py?req=log&amp;file=faq03.016.htp">Log info</A>
 
1561
 
 
1562
/ Last changed on Tue Sep 11 16:02:22 2001 by
 
1563
<A HREF="mailto:guido@python.org">GvR</A>
 
1564
<P>
 
1565
 
 
1566
<HR>
 
1567
<H2><A NAME="3.17">3.17. Deleted.</A></H2>
 
1568
<P>
 
1569
<P>
 
1570
 
 
1571
<A HREF="faqw.py?req=edit&amp;file=faq03.017.htp">Edit this entry</A> /
 
1572
<A HREF="faqw.py?req=log&amp;file=faq03.017.htp">Log info</A>
 
1573
 
 
1574
/ Last changed on Tue Sep 11 15:54:57 2001 by
 
1575
<A HREF="mailto:guido@python.org">GvR</A>
 
1576
<P>
 
1577
 
 
1578
<HR>
 
1579
<H2><A NAME="3.18">3.18. Compilation or link errors for the _tkinter module</A></H2>
 
1580
Most likely, there's a version mismatch between the Tcl/Tk header
 
1581
files (tcl.h and tk.h) and the Tcl/Tk libraries you are using e.g.
 
1582
"-ltk8.0" and "-ltcl8.0" arguments for _tkinter in the Setup file).
 
1583
It is possible to install several versions of the Tcl/Tk libraries,
 
1584
but there can only be one version of the tcl.h and tk.h header
 
1585
files.  If the library doesn't match the header, you'll get
 
1586
problems, either when linking the module, or when importing it.
 
1587
Fortunately, the version number is clearly stated in each file,
 
1588
so this is easy to find.  Reinstalling and using the latest
 
1589
version usually fixes the problem.
 
1590
<P>
 
1591
(Also note that when compiling unpatched Python 1.5.1 against
 
1592
Tcl/Tk 7.6/4.2 or older, you get an error on Tcl_Finalize.  See
 
1593
the 1.5.1 patch page at <A HREF="http://www.python.org/1.5/patches-1.5.1">http://www.python.org/1.5/patches-1.5.1</A>/.)
 
1594
<P>
 
1595
 
 
1596
<A HREF="faqw.py?req=edit&amp;file=faq03.018.htp">Edit this entry</A> /
 
1597
<A HREF="faqw.py?req=log&amp;file=faq03.018.htp">Log info</A>
 
1598
 
 
1599
/ Last changed on Thu Jun 11 00:49:14 1998 by
 
1600
<A HREF="mailto:guido@python.org">Gvr</A>
 
1601
<P>
 
1602
 
 
1603
<HR>
 
1604
<H2><A NAME="3.19">3.19. I configured and built Python for Tcl/Tk but &quot;import Tkinter&quot; fails.</A></H2>
 
1605
Most likely, you forgot to enable the line in Setup that says
 
1606
"TKPATH=:$(DESTLIB)/tkinter".
 
1607
<P>
 
1608
 
 
1609
<A HREF="faqw.py?req=edit&amp;file=faq03.019.htp">Edit this entry</A> /
 
1610
<A HREF="faqw.py?req=log&amp;file=faq03.019.htp">Log info</A>
 
1611
<P>
 
1612
 
 
1613
<HR>
 
1614
<H2><A NAME="3.20">3.20. [deleted]</A></H2>
 
1615
[ancient information on a gcc+tkinter bug on alpha was here]
 
1616
<P>
 
1617
 
 
1618
<A HREF="faqw.py?req=edit&amp;file=faq03.020.htp">Edit this entry</A> /
 
1619
<A HREF="faqw.py?req=log&amp;file=faq03.020.htp">Log info</A>
 
1620
 
 
1621
/ Last changed on Mon Jun  3 16:46:23 2002 by
 
1622
<A HREF="mailto:erno-pyfaq@erno.iki.fi">Erno Kuusela</A>
 
1623
<P>
 
1624
 
 
1625
<HR>
 
1626
<H2><A NAME="3.21">3.21. Several common system calls are missing from the posix module.</A></H2>
 
1627
Most likely, <I>all</I> test compilations run by the configure script
 
1628
are failing for some reason or another.  Have a look in config.log to
 
1629
see what could be the reason.  A common reason is specifying a
 
1630
directory to the --with-readline option that doesn't contain the
 
1631
libreadline.a file.
 
1632
<P>
 
1633
 
 
1634
<A HREF="faqw.py?req=edit&amp;file=faq03.021.htp">Edit this entry</A> /
 
1635
<A HREF="faqw.py?req=log&amp;file=faq03.021.htp">Log info</A>
 
1636
<P>
 
1637
 
 
1638
<HR>
 
1639
<H2><A NAME="3.22">3.22. ImportError: No module named string, on MS Windows.</A></H2>
 
1640
Most likely, your PYTHONPATH environment variable should be set to
 
1641
something like:
 
1642
<P>
 
1643
set PYTHONPATH=c:\python;c:\python\lib;c:\python\scripts
 
1644
<P>
 
1645
(assuming Python was installed in c:\python)
 
1646
<P>
 
1647
 
 
1648
<A HREF="faqw.py?req=edit&amp;file=faq03.022.htp">Edit this entry</A> /
 
1649
<A HREF="faqw.py?req=log&amp;file=faq03.022.htp">Log info</A>
 
1650
<P>
 
1651
 
 
1652
<HR>
 
1653
<H2><A NAME="3.23">3.23. Core dump on SGI when using the gl module.</A></H2>
 
1654
There are conflicts between entry points in the termcap and curses
 
1655
libraries and an entry point in the GL library.  There's a hack of a
 
1656
fix for the termcap library if it's needed for the GNU readline
 
1657
library, but it doesn't work when you're using curses.  Concluding,
 
1658
you can't build a Python binary containing both the curses and gl
 
1659
modules.
 
1660
<P>
 
1661
 
 
1662
<A HREF="faqw.py?req=edit&amp;file=faq03.023.htp">Edit this entry</A> /
 
1663
<A HREF="faqw.py?req=log&amp;file=faq03.023.htp">Log info</A>
 
1664
<P>
 
1665
 
 
1666
<HR>
 
1667
<H2><A NAME="3.24">3.24. &quot;Initializer not a constant&quot; while building DLL on MS-Windows</A></H2>
 
1668
Static type object initializers in extension modules may cause compiles to
 
1669
fail with an error message like "initializer not a constant".  
 
1670
Fredrik Lundh &lt;<A HREF="mailto:Fredrik.Lundh@image.combitech.se">Fredrik.Lundh@image.combitech.se</A>&gt; explains:
 
1671
<P>
 
1672
This shows up when building DLL under MSVC.  There's two ways to
 
1673
address this: either compile the module as C++, or change your code to
 
1674
something like:
 
1675
<P>
 
1676
<PRE>
 
1677
  statichere PyTypeObject bstreamtype = {
 
1678
      PyObject_HEAD_INIT(NULL) /* must be set by init function */
 
1679
      0,
 
1680
      "bstream",
 
1681
      sizeof(bstreamobject),
 
1682
</PRE>
 
1683
<PRE>
 
1684
  ...
 
1685
</PRE>
 
1686
<PRE>
 
1687
  void
 
1688
  initbstream()
 
1689
  {
 
1690
      /* Patch object type */
 
1691
      bstreamtype.ob_type = &amp;PyType_Type;
 
1692
      Py_InitModule("bstream", functions);
 
1693
      ...
 
1694
  }
 
1695
</PRE>
 
1696
<P>
 
1697
 
 
1698
<A HREF="faqw.py?req=edit&amp;file=faq03.024.htp">Edit this entry</A> /
 
1699
<A HREF="faqw.py?req=log&amp;file=faq03.024.htp">Log info</A>
 
1700
 
 
1701
/ Last changed on Sun May 25 14:58:05 1997 by
 
1702
<A HREF="mailto:aaron_watters@msn.com">Aaron Watters</A>
 
1703
<P>
 
1704
 
 
1705
<HR>
 
1706
<H2><A NAME="3.25">3.25. Output directed to a pipe or file disappears on Linux.</A></H2>
 
1707
Some people have reported that when they run their script
 
1708
interactively, it runs great, but that when they redirect it
 
1709
to a pipe or file, no output appears.
 
1710
<P>
 
1711
<PRE>
 
1712
    % python script.py
 
1713
    ...some output...
 
1714
    % python script.py >file
 
1715
    % cat file
 
1716
    % # no output
 
1717
    % python script.py | cat
 
1718
    % # no output
 
1719
    %
 
1720
</PRE>
 
1721
This was a bug in Linux kernel. It is fixed and should not appear anymore. So most Linux users are <I>not</I> affected by this.
 
1722
<P>
 
1723
If redirection doesn't work on your Linux system, check what shell you are using. Shells like (t)csh doesn't support redirection.
 
1724
<P>
 
1725
 
 
1726
<A HREF="faqw.py?req=edit&amp;file=faq03.025.htp">Edit this entry</A> /
 
1727
<A HREF="faqw.py?req=log&amp;file=faq03.025.htp">Log info</A>
 
1728
 
 
1729
/ Last changed on Thu Jan 16 13:38:30 2003 by
 
1730
<A HREF="mailto:kubieziel@gmx.de">Jens Kubieziel</A>
 
1731
<P>
 
1732
 
 
1733
<HR>
 
1734
<H2><A NAME="3.26">3.26. [deleted]</A></H2>
 
1735
[ancient libc/linux problem was here]
 
1736
<P>
 
1737
 
 
1738
<A HREF="faqw.py?req=edit&amp;file=faq03.026.htp">Edit this entry</A> /
 
1739
<A HREF="faqw.py?req=log&amp;file=faq03.026.htp">Log info</A>
 
1740
 
 
1741
/ Last changed on Mon Jun  3 16:48:08 2002 by
 
1742
<A HREF="mailto:erno-pyfaq@erno.iki.fi">Erno Kuusela</A>
 
1743
<P>
 
1744
 
 
1745
<HR>
 
1746
<H2><A NAME="3.27">3.27. [deleted]</A></H2>
 
1747
[ancient linux + threads + tk problem was described here]
 
1748
<P>
 
1749
 
 
1750
<A HREF="faqw.py?req=edit&amp;file=faq03.027.htp">Edit this entry</A> /
 
1751
<A HREF="faqw.py?req=log&amp;file=faq03.027.htp">Log info</A>
 
1752
 
 
1753
/ Last changed on Mon Jun  3 16:49:08 2002 by
 
1754
<A HREF="mailto:erno-pyfaq@erno.iki.fi">Erno Kuusela</A>
 
1755
<P>
 
1756
 
 
1757
<HR>
 
1758
<H2><A NAME="3.28">3.28. How can I test if Tkinter is working?</A></H2>
 
1759
Try the following:
 
1760
<P>
 
1761
<PRE>
 
1762
  python
 
1763
  >>> import _tkinter
 
1764
  >>> import Tkinter
 
1765
  >>> Tkinter._test()
 
1766
</PRE>
 
1767
This should pop up a window with two buttons,
 
1768
one "Click me" and one "Quit".
 
1769
<P>
 
1770
If the first statement (import _tkinter) fails, your Python
 
1771
installation probably has not been configured to support Tcl/Tk.
 
1772
On Unix, if you have installed Tcl/Tk, you have to rebuild Python
 
1773
after editing the Modules/Setup file to enable the _tkinter module
 
1774
and the TKPATH environment variable.
 
1775
<P>
 
1776
It is also possible to get complaints about Tcl/Tk version
 
1777
number mismatches or missing TCL_LIBRARY or TK_LIBRARY
 
1778
environment variables.  These have to do with Tcl/Tk installation
 
1779
problems.
 
1780
<P>
 
1781
A common problem is to have installed versions of tcl.h and tk.h
 
1782
that don't match the installed version of the Tcl/Tk libraries;
 
1783
this usually results in linker errors or (when using dynamic
 
1784
loading) complaints about missing symbols during loading
 
1785
the shared library.
 
1786
<P>
 
1787
 
 
1788
<A HREF="faqw.py?req=edit&amp;file=faq03.028.htp">Edit this entry</A> /
 
1789
<A HREF="faqw.py?req=log&amp;file=faq03.028.htp">Log info</A>
 
1790
 
 
1791
/ Last changed on Thu Aug 28 17:01:46 1997 by
 
1792
<A HREF="mailto:guido@python.org">Guido van Rossum</A>
 
1793
<P>
 
1794
 
 
1795
<HR>
 
1796
<H2><A NAME="3.29">3.29. Is there a way to get the interactive mode of the python interpreter to perform function/variable name completion?</A></H2>
 
1797
(From a posting by Guido van Rossum)
 
1798
<P>
 
1799
On Unix, if you have enabled the readline module (i.e. if Emacs-style
 
1800
command line editing and bash-style history works for you), you can
 
1801
add this by importing the undocumented standard library module
 
1802
"rlcompleter".  When completing a simple identifier, it
 
1803
completes keywords, built-ins and globals in __main__; when completing
 
1804
NAME.NAME..., it evaluates (!) the expression up to the last dot and
 
1805
completes its attributes.
 
1806
<P>
 
1807
This way, you can do "import string", type "string.", hit the
 
1808
completion key twice, and see the list of names defined by the
 
1809
string module.
 
1810
<P>
 
1811
Tip: to use the tab key as the completion key, call
 
1812
<P>
 
1813
<PRE>
 
1814
    readline.parse_and_bind("tab: complete")
 
1815
</PRE>
 
1816
You can put this in a ~/.pythonrc file, and set the PYTHONSTARTUP
 
1817
environment variable to ~/.pythonrc.  This will cause the completion to be enabled
 
1818
whenever you run Python interactively.  
 
1819
<P>
 
1820
Notes (see the docstring for rlcompleter.py for more information):
 
1821
<P>
 
1822
* The evaluation of the NAME.NAME... form may cause arbitrary 
 
1823
application defined code to be executed if an object with a
 
1824
__getattr__ hook is found.  Since it is the responsibility of the
 
1825
application (or the user) to enable this feature, I consider this an
 
1826
acceptable risk.  More complicated expressions (e.g. function calls or
 
1827
indexing operations) are <I>not</I> evaluated.
 
1828
<P>
 
1829
* GNU readline is also used by the built-in functions input() and
 
1830
raw_input(), and thus these also benefit/suffer from the complete
 
1831
features.  Clearly an interactive application can benefit by
 
1832
specifying its own completer function and using raw_input() for all
 
1833
its input.
 
1834
<P>
 
1835
* When stdin is not a tty device, GNU readline is never
 
1836
used, and this module (and the readline module) are silently inactive.
 
1837
<P>
 
1838
 
 
1839
<A HREF="faqw.py?req=edit&amp;file=faq03.029.htp">Edit this entry</A> /
 
1840
<A HREF="faqw.py?req=log&amp;file=faq03.029.htp">Log info</A>
 
1841
 
 
1842
/ Last changed on Fri Jun 12 09:55:24 1998 by
 
1843
<A HREF="mailto:akuchlin@cnri.reston.va.us">A.M. Kuchling</A>
 
1844
<P>
 
1845
 
 
1846
<HR>
 
1847
<H2><A NAME="3.30">3.30. Why is the Python interpreter not built as a shared library?</A></H2>
 
1848
(This is a Unix question; on Mac and Windows, it <I>is</I> a shared
 
1849
library.)
 
1850
<P>
 
1851
It's just a nightmare to get this to work on all different platforms.
 
1852
Shared library portability is a pain.  And yes, I know about GNU libtool
 
1853
-- but it requires me to use its conventions for filenames etc, and it 
 
1854
would require a complete and utter rewrite of all the makefile and
 
1855
config tools I'm currently using.
 
1856
<P>
 
1857
In practice, few applications embed Python -- it's much more common to
 
1858
have Python extensions, which already are shared libraries.  Also,
 
1859
serious embedders often want total control over which Python version
 
1860
and configuration they use so they wouldn't want to use a standard
 
1861
shared library anyway.  So while the motivation of saving space
 
1862
when lots of apps embed Python is nice in theory, I
 
1863
doubt that it will save much in practice.  (Hence the low priority I
 
1864
give to making a shared library.)
 
1865
<P>
 
1866
For Linux systems, the simplest method of producing libpython1.5.so seems to 
 
1867
be (originally from the Minotaur project web page,  
 
1868
<A HREF="http://www.equi4.com/minotaur/minotaur.html">http://www.equi4.com/minotaur/minotaur.html</A>): 
 
1869
<P>
 
1870
<PRE>
 
1871
  make distclean 
 
1872
  ./configure 
 
1873
  make OPT="-fpic -O2" 
 
1874
  mkdir .extract 
 
1875
  (cd .extract; ar xv ../libpython1.5.a) 
 
1876
  gcc -shared -o libpython1.5.so .extract/*.o 
 
1877
  rm -rf .extract
 
1878
</PRE>
 
1879
In Python 2.3 this will be supported by the standard build routine
 
1880
(at least on Linux) with --enable-shared.  Note however that there
 
1881
is little advantage, and it slows down Python because of the need
 
1882
for PIC code and the extra cost at startup time to find the library.
 
1883
<P>
 
1884
 
 
1885
<A HREF="faqw.py?req=edit&amp;file=faq03.030.htp">Edit this entry</A> /
 
1886
<A HREF="faqw.py?req=log&amp;file=faq03.030.htp">Log info</A>
 
1887
 
 
1888
/ Last changed on Thu May 30 13:36:55 2002 by
 
1889
<A HREF="mailto:guido@python.org">GvR</A>
 
1890
<P>
 
1891
 
 
1892
<HR>
 
1893
<H2><A NAME="3.31">3.31. Build with GCC on Solaris 2.6 (SunOS 5.6) fails</A></H2>
 
1894
If you have upgraded Solaris 2.5 or 2.5.1 to Solaris 2.6,
 
1895
but you have not upgraded
 
1896
your GCC installation, the compile may fail, e.g. like this:
 
1897
<P>
 
1898
<PRE>
 
1899
 In file included from /usr/include/sys/stream.h:26,
 
1900
                  from /usr/include/netinet/in.h:38,
 
1901
                  from /usr/include/netdb.h:96,
 
1902
                  from ./socketmodule.c:121:
 
1903
 /usr/include/sys/model.h:32: #error "No DATAMODEL_NATIVE specified"
 
1904
</PRE>
 
1905
Solution: rebuild GCC for Solaris 2.6. 
 
1906
You might be able to simply re-run fixincludes, but
 
1907
people have had mixed success with doing that.
 
1908
<P>
 
1909
 
 
1910
<A HREF="faqw.py?req=edit&amp;file=faq03.031.htp">Edit this entry</A> /
 
1911
<A HREF="faqw.py?req=log&amp;file=faq03.031.htp">Log info</A>
 
1912
 
 
1913
/ Last changed on Wed Oct 21 11:18:46 1998 by
 
1914
<A HREF="mailto:guido@python.org">GvR</A>
 
1915
<P>
 
1916
 
 
1917
<HR>
 
1918
<H2><A NAME="3.32">3.32. Running &quot;make clean&quot; seems to leave problematic files that cause subsequent builds to fail.</A></H2>
 
1919
Use "make clobber" instead.  
 
1920
<P>
 
1921
Use "make clean" to reduce the size of the source/build directory
 
1922
after you're happy with your build and installation.
 
1923
If you have already tried to build python and you'd like to start 
 
1924
over, you should use "make clobber".  It does a "make clean" and also 
 
1925
removes files such as the partially built Python library from a previous build.
 
1926
<P>
 
1927
 
 
1928
<A HREF="faqw.py?req=edit&amp;file=faq03.032.htp">Edit this entry</A> /
 
1929
<A HREF="faqw.py?req=log&amp;file=faq03.032.htp">Log info</A>
 
1930
 
 
1931
/ Last changed on Thu Jun 24 20:39:26 1999 by
 
1932
<A HREF="mailto:tbryan@python.net">TAB</A>
 
1933
<P>
 
1934
 
 
1935
<HR>
 
1936
<H2><A NAME="3.33">3.33. Submitting bug reports and patches</A></H2>
 
1937
To report a bug or submit a patch, please use the relevant service
 
1938
from the Python project at SourceForge.
 
1939
<P>
 
1940
Bugs: <A HREF="http://sourceforge.net/tracker/?group_id=5470&amp;atid=105470">http://sourceforge.net/tracker/?group_id=5470&amp;atid=105470</A>
 
1941
<P>
 
1942
Patches: <A HREF="http://sourceforge.net/tracker/?group_id=5470&amp;atid=305470">http://sourceforge.net/tracker/?group_id=5470&amp;atid=305470</A>
 
1943
<P>
 
1944
If you have a SourceForge account, please log in before submitting your bug report; this will make it easier for us to contact you regarding your report in the event we have follow-up questions.  It will also enable SourceForge to send you update information as we act on your bug.  If you do not have a SourceForge account, please consider leaving your name and email address as part of the report.
 
1945
<P>
 
1946
 
 
1947
<A HREF="faqw.py?req=edit&amp;file=faq03.033.htp">Edit this entry</A> /
 
1948
<A HREF="faqw.py?req=log&amp;file=faq03.033.htp">Log info</A>
 
1949
 
 
1950
/ Last changed on Fri Apr 27 10:58:26 2001 by
 
1951
<A HREF="mailto:fdrake@acm.org">Fred Drake</A>
 
1952
<P>
 
1953
 
 
1954
<HR>
 
1955
<H2><A NAME="3.34">3.34. I can't load shared libraries under Python 1.5.2, Solaris 7, and gcc 2.95.2</A></H2>
 
1956
When trying to load shared libraries, you may see errors like:
 
1957
ImportError: ld.so.1: python: fatal: relocation error: file /usr/local/lib/python1.5/site-packages/Perp/util/du_SweepUtilc.so:
 
1958
<PRE>
 
1959
 symbol PyExc_RuntimeError: referenced symbol not found
 
1960
</PRE>
 
1961
<P>
 
1962
There is a problem with the configure script for Python 1.5.2
 
1963
under Solaris 7 with gcc 2.95 .  configure should set the make variable
 
1964
LINKFORSHARED=-Xlinker -export-dynamic
 
1965
<P>
 
1966
<P>
 
1967
in Modules/Makefile, 
 
1968
<P>
 
1969
Manually add this line to the Modules/Makefile.
 
1970
This builds a Python executable that can load shared library extensions (xxx.so) .
 
1971
<P>
 
1972
 
 
1973
<A HREF="faqw.py?req=edit&amp;file=faq03.034.htp">Edit this entry</A> /
 
1974
<A HREF="faqw.py?req=log&amp;file=faq03.034.htp">Log info</A>
 
1975
 
 
1976
/ Last changed on Mon Feb 19 10:37:05 2001 by
 
1977
<A HREF="mailto:guido@python.org">GvR</A>
 
1978
<P>
 
1979
 
 
1980
<HR>
 
1981
<H2><A NAME="3.35">3.35. In the regression test, test___all__ fails for the profile module. What's wrong?</A></H2>
 
1982
If you have been using the profile module, and have properly calibrated a copy of the module as described in the documentation for the profiler:
 
1983
<P>
 
1984
<A HREF="http://www.python.org/doc/current/lib/profile-calibration.html">http://www.python.org/doc/current/lib/profile-calibration.html</A>
 
1985
<P>
 
1986
then it is possible that the regression test "test___all__" will fail if you run the regression test manually rather than using "make test" in the Python source directory.  This will happen if you have set your PYTHONPATH environment variable to include the directory containing your calibrated profile module.  You have probably calibrated the profiler using an older version of the profile module which does not define the __all__ value, added to the module as of Python 2.1.
 
1987
<P>
 
1988
The problem can be fixed by removing the old calibrated version of the profile module and using the latest version to do a fresh calibration. In general, you will need to re-calibrate for each version of Python anyway, since the performance characteristics can change in subtle ways that impact profiling.
 
1989
<P>
 
1990
 
 
1991
<A HREF="faqw.py?req=edit&amp;file=faq03.035.htp">Edit this entry</A> /
 
1992
<A HREF="faqw.py?req=log&amp;file=faq03.035.htp">Log info</A>
 
1993
 
 
1994
/ Last changed on Fri Apr 27 10:44:10 2001 by
 
1995
<A HREF="mailto:fdrake@acm.org">Fred Drake</A>
 
1996
<P>
 
1997
 
 
1998
<HR>
 
1999
<H2><A NAME="3.36">3.36. relocations remain against allocatable but non-writable sections</A></H2>
 
2000
This linker error occurs on Solaris if you attempt to build an extension module which incorporates position-dependent (non-PIC) code. A common source of problems is that a static library (.a file), such as libreadline.a or libcrypto.a is linked with the extension module. The error specifically occurs when using gcc as the compiler, but /usr/ccs/bin/ld as the linker.
 
2001
<P>
 
2002
The following solutions and work-arounds are known:
 
2003
<P>
 
2004
1. Rebuild the libraries (libreadline, libcrypto) with -fPIC (-KPIC if using the system compiler). This is recommended; all object files in a shared library should be position-independent.
 
2005
<P>
 
2006
2. Statically link the extension module and its libraries into the Python interpreter, by editing Modules/Setup.
 
2007
<P>
 
2008
3. Use GNU ld instead of /usr/ccs/bin/ld; GNU ld will accept non-PIC code in shared libraries (and mark the section writable)
 
2009
<P>
 
2010
4. Pass -mimpure-text to GCC when linking the module. This will force gcc to not pass -z text to ld; in turn, ld will make all text sections writable.
 
2011
<P>
 
2012
Options 3 and 4 are not recommended, since the ability to share code across processes is lost.
 
2013
<P>
 
2014
 
 
2015
<A HREF="faqw.py?req=edit&amp;file=faq03.036.htp">Edit this entry</A> /
 
2016
<A HREF="faqw.py?req=log&amp;file=faq03.036.htp">Log info</A>
 
2017
 
 
2018
/ Last changed on Tue Jan 29 12:05:11 2002 by
 
2019
<A HREF="mailto:loewis@informatik.hu-berlin.de">Martin v. L�wis</A>
 
2020
<P>
 
2021
 
 
2022
<HR>
 
2023
<H1>4. Programming in Python</H1>
 
2024
 
 
2025
<HR>
 
2026
<H2><A NAME="4.1">4.1. Is there a source code level debugger with breakpoints, step, etc.?</A></H2>
 
2027
Yes.  
 
2028
<P>
 
2029
Module pdb is a rudimentary but adequate console-mode debugger for Python. It is part of the standard Python library, and is documented in the Library Reference Manual. (You can also write your own debugger by using the code for pdb as an example.)
 
2030
<P>
 
2031
The IDLE interactive development environment, which is part of the standard Python distribution (normally available in Tools/idle), includes a graphical debugger.  There is documentation for the IDLE debugger at <A HREF="http://www.python.org/idle/doc/idle2.html#Debugger">http://www.python.org/idle/doc/idle2.html#Debugger</A>
 
2032
<P>
 
2033
Pythonwin is a Python IDE that includes a GUI debugger based on bdb.  The Pythonwin debugger colors breakpoints and has quite a few cool features (including debugging non-Pythonwin programs).  A reference can be found at <A HREF="http://www.python.org/ftp/python/pythonwin/pwindex.html">http://www.python.org/ftp/python/pythonwin/pwindex.html</A>
 
2034
More recent versions of PythonWin are available as a part of the ActivePython distribution (see   <A HREF="http://www.activestate.com/Products/ActivePython/index.html">http://www.activestate.com/Products/ActivePython/index.html</A>).
 
2035
<P>
 
2036
Pydb is a version of the standard Python debugger pdb, modified for use with DDD (Data Display Debugger), a popular graphical debugger front end.  Pydb can be found at  <A HREF="http://packages.debian.org/unstable/devel/pydb.html">http://packages.debian.org/unstable/devel/pydb.html</A>
 
2037
and DDD can be found at <A HREF="http://www.gnu.org/software/ddd">http://www.gnu.org/software/ddd</A>/
 
2038
<P>
 
2039
There are a number of commmercial Python IDEs that include graphical debuggers.  They include:
 
2040
<P>
 
2041
<PRE>
 
2042
 * Wing IDE (<A HREF="http://wingide.com">http://wingide.com</A>/) 
 
2043
 * Komodo IDE (<A HREF="http://www.activestate.com/Products/Komodo">http://www.activestate.com/Products/Komodo</A>/)
 
2044
</PRE>
 
2045
<P>
 
2046
 
 
2047
<A HREF="faqw.py?req=edit&amp;file=faq04.001.htp">Edit this entry</A> /
 
2048
<A HREF="faqw.py?req=log&amp;file=faq04.001.htp">Log info</A>
 
2049
 
 
2050
/ Last changed on Tue Jan 28 01:43:41 2003 by
 
2051
<A HREF="mailto:steve@ferg.org">Stephen Ferg</A>
 
2052
<P>
 
2053
 
 
2054
<HR>
 
2055
<H2><A NAME="4.2">4.2. Can I create an object class with some methods implemented in C and others in Python (e.g. through inheritance)? (Also phrased as: Can I use a built-in type as base class?)</A></H2>
 
2056
In Python 2.2, you can inherit from builtin classes such as int, list, dict, etc.
 
2057
<P>
 
2058
In previous versions of Python, you can easily create a Python class which serves as a wrapper around a built-in object, e.g. (for dictionaries):
 
2059
<P>
 
2060
<PRE>
 
2061
        # A user-defined class behaving almost identical
 
2062
        # to a built-in dictionary.
 
2063
        class UserDict:
 
2064
                def __init__(self): self.data = {}
 
2065
                def __repr__(self): return repr(self.data)
 
2066
                def __cmp__(self, dict):
 
2067
                        if type(dict) == type(self.data):
 
2068
                                return cmp(self.data, dict)
 
2069
                        else:
 
2070
                                return cmp(self.data, dict.data)
 
2071
                def __len__(self): return len(self.data)
 
2072
                def __getitem__(self, key): return self.data[key]
 
2073
                def __setitem__(self, key, item): self.data[key] = item
 
2074
                def __delitem__(self, key): del self.data[key]
 
2075
                def keys(self): return self.data.keys()
 
2076
                def items(self): return self.data.items()
 
2077
                def values(self): return self.data.values()
 
2078
                def has_key(self, key): return self.data.has_key(key)
 
2079
</PRE>
 
2080
A2. See Jim Fulton's ExtensionClass for an example of a mechanism
 
2081
which allows you to have superclasses which you can inherit from in
 
2082
Python -- that way you can have some methods from a C superclass (call
 
2083
it a mixin) and some methods from either a Python superclass or your
 
2084
subclass.  ExtensionClass is distributed as a part of Zope (see
 
2085
<A HREF="http://www.zope.org">http://www.zope.org</A>), but will be phased out with Zope 3, since
 
2086
Zope 3 uses Python 2.2 or later which supports direct inheritance
 
2087
from built-in types.  Here's a link to the original paper about
 
2088
ExtensionClass:
 
2089
<A HREF="http://debian.acm.ndsu.nodak.edu/doc/python-extclass/ExtensionClass.html">http://debian.acm.ndsu.nodak.edu/doc/python-extclass/ExtensionClass.html</A>
 
2090
<P>
 
2091
A3. The Boost Python Library (BPL, <A HREF="http://www.boost.org/libs/python/doc/index.html">http://www.boost.org/libs/python/doc/index.html</A>)
 
2092
provides a way of doing this from C++ (i.e. you can inherit from an
 
2093
extension class written in C++ using the BPL).
 
2094
<P>
 
2095
 
 
2096
<A HREF="faqw.py?req=edit&amp;file=faq04.002.htp">Edit this entry</A> /
 
2097
<A HREF="faqw.py?req=log&amp;file=faq04.002.htp">Log info</A>
 
2098
 
 
2099
/ Last changed on Tue May 28 21:09:52 2002 by
 
2100
<A HREF="mailto:guido@python.org">GvR</A>
 
2101
<P>
 
2102
 
 
2103
<HR>
 
2104
<H2><A NAME="4.3">4.3. Is there a curses/termcap package for Python?</A></H2>
 
2105
The standard Python source distribution comes with a curses module in
 
2106
the Modules/ subdirectory, though it's not compiled by default (note
 
2107
that this is not available in the Windows distribution -- there is
 
2108
no curses module for Windows).
 
2109
<P>
 
2110
In Python versions before 2.0 the module only supported plain curses; 
 
2111
you couldn't use ncurses features like colors with it (though it would 
 
2112
link with ncurses).
 
2113
<P>
 
2114
In Python 2.0, the curses module has been greatly extended, starting 
 
2115
from Oliver Andrich's enhanced version, to provide many additional 
 
2116
functions from ncurses and SYSV curses, such as colour, alternative
 
2117
character set support, pads, and mouse support. This means the
 
2118
module is no longer compatible with operating systems that only
 
2119
have BSD curses, but there don't seem to be any currently
 
2120
maintained OSes that fall into this category.
 
2121
<P>
 
2122
 
 
2123
<A HREF="faqw.py?req=edit&amp;file=faq04.003.htp">Edit this entry</A> /
 
2124
<A HREF="faqw.py?req=log&amp;file=faq04.003.htp">Log info</A>
 
2125
 
 
2126
/ Last changed on Sun Jun 23 20:24:06 2002 by
 
2127
<A HREF="mailto:tim.one@comcast.net">Tim Peters</A>
 
2128
<P>
 
2129
 
 
2130
<HR>
 
2131
<H2><A NAME="4.4">4.4. Is there an equivalent to C's onexit() in Python?</A></H2>
 
2132
For Python 2.0: The new atexit module provides a register function that
 
2133
is similar to C's onexit. See the Library Reference for details. For
 
2134
2.0 you should <I>not</I> assign to sys.exitfunc!
 
2135
<P>
 
2136
For Python 1.5.2: You need to import sys and assign a function to 
 
2137
sys.exitfunc, it will be called when your program exits, is 
 
2138
killed by an unhandled exception, or (on UNIX) receives a 
 
2139
SIGHUP or SIGTERM signal.
 
2140
<P>
 
2141
 
 
2142
<A HREF="faqw.py?req=edit&amp;file=faq04.004.htp">Edit this entry</A> /
 
2143
<A HREF="faqw.py?req=log&amp;file=faq04.004.htp">Log info</A>
 
2144
 
 
2145
/ Last changed on Thu Dec 28 12:14:55 2000 by
 
2146
<A HREF="mailto:pbjorn@uswest.net">Bjorn Pettersen</A>
 
2147
<P>
 
2148
 
 
2149
<HR>
 
2150
<H2><A NAME="4.5">4.5. [deleted]</A></H2>
 
2151
[python used to lack nested scopes, it was explained here]
 
2152
<P>
 
2153
 
 
2154
<A HREF="faqw.py?req=edit&amp;file=faq04.005.htp">Edit this entry</A> /
 
2155
<A HREF="faqw.py?req=log&amp;file=faq04.005.htp">Log info</A>
 
2156
 
 
2157
/ Last changed on Thu Mar 21 05:18:22 2002 by
 
2158
<A HREF="mailto:erno-pyfaq@erno.iki.fi">Erno Kuusela</A>
 
2159
<P>
 
2160
 
 
2161
<HR>
 
2162
<H2><A NAME="4.6">4.6. How do I iterate over a sequence in reverse order?</A></H2>
 
2163
If it is a list, the fastest solution is
 
2164
<P>
 
2165
<PRE>
 
2166
        list.reverse()
 
2167
        try:
 
2168
                for x in list:
 
2169
                        "do something with x"
 
2170
        finally:
 
2171
                list.reverse()
 
2172
</PRE>
 
2173
This has the disadvantage that while you are in the loop, the list
 
2174
is temporarily reversed.  If you don't like this, you can make a copy.
 
2175
This appears expensive but is actually faster than other solutions:
 
2176
<P>
 
2177
<PRE>
 
2178
        rev = list[:]
 
2179
        rev.reverse()
 
2180
        for x in rev:
 
2181
                &lt;do something with x&gt;
 
2182
</PRE>
 
2183
If it's not a list, a more general but slower solution is:
 
2184
<P>
 
2185
<PRE>
 
2186
        for i in range(len(sequence)-1, -1, -1):
 
2187
                x = sequence[i]
 
2188
                &lt;do something with x&gt;
 
2189
</PRE>
 
2190
A more elegant solution, is to define a class which acts as a sequence
 
2191
and yields the elements in reverse order (solution due to Steve
 
2192
Majewski):
 
2193
<P>
 
2194
<PRE>
 
2195
        class Rev:
 
2196
                def __init__(self, seq):
 
2197
                        self.forw = seq
 
2198
                def __len__(self):
 
2199
                        return len(self.forw)
 
2200
                def __getitem__(self, i):
 
2201
                        return self.forw[-(i + 1)]
 
2202
</PRE>
 
2203
You can now simply write:
 
2204
<P>
 
2205
<PRE>
 
2206
        for x in Rev(list):
 
2207
                &lt;do something with x&gt;
 
2208
</PRE>
 
2209
Unfortunately, this solution is slowest of all, due to the method
 
2210
call overhead...
 
2211
<P>
 
2212
 
 
2213
<A HREF="faqw.py?req=edit&amp;file=faq04.006.htp">Edit this entry</A> /
 
2214
<A HREF="faqw.py?req=log&amp;file=faq04.006.htp">Log info</A>
 
2215
 
 
2216
/ Last changed on Sun May 25 21:10:50 1997 by
 
2217
<A HREF="mailto:guido@python.org">GvR</A>
 
2218
<P>
 
2219
 
 
2220
<HR>
 
2221
<H2><A NAME="4.7">4.7. My program is too slow. How do I speed it up?</A></H2>
 
2222
That's a tough one, in general.  There are many tricks to speed up
 
2223
Python code; I would consider rewriting parts in C only as a last
 
2224
resort.  One thing to notice is that function and (especially) method
 
2225
calls are rather expensive; if you have designed a purely OO interface
 
2226
with lots of tiny functions that don't do much more than get or set an
 
2227
instance variable or call another method, you may consider using a
 
2228
more direct way, e.g. directly accessing instance variables.  Also see
 
2229
the standard module "profile" (described in the Library Reference
 
2230
manual) which makes it possible to find out where
 
2231
your program is spending most of its time (if you have some patience
 
2232
-- the profiling itself can slow your program down by an order of
 
2233
magnitude).
 
2234
<P>
 
2235
Remember that many standard optimization heuristics you
 
2236
may know from other programming experience may well apply
 
2237
to Python.  For example it may be faster to send output to output
 
2238
devices using larger writes rather than smaller ones in order to
 
2239
avoid the overhead of kernel system calls.  Thus CGI scripts
 
2240
that write all output in "one shot" may be notably faster than
 
2241
those that write lots of small pieces of output.
 
2242
<P>
 
2243
Also, be sure to use "aggregate" operations where appropriate.
 
2244
For example the "slicing" feature allows programs to chop up
 
2245
lists and other sequence objects in a single tick of the interpreter
 
2246
mainloop using highly optimized C implementations.  Thus to 
 
2247
get the same effect as
 
2248
<P>
 
2249
<PRE>
 
2250
  L2 = []
 
2251
  for i in range[3]:
 
2252
       L2.append(L1[i])
 
2253
</PRE>
 
2254
it is much shorter and far faster to use
 
2255
<P>
 
2256
<PRE>
 
2257
  L2 = list(L1[:3]) # "list" is redundant if L1 is a list.
 
2258
</PRE>
 
2259
Note that the map() function, particularly used with
 
2260
builtin methods or builtin functions can be a convenient
 
2261
accelerator.  For example to pair the elements of two
 
2262
lists together:
 
2263
<P>
 
2264
<PRE>
 
2265
  >>> map(None, [1,2,3], [4,5,6])
 
2266
  [(1, 4), (2, 5), (3, 6)]
 
2267
</PRE>
 
2268
or to compute a number of sines:
 
2269
<P>
 
2270
<PRE>
 
2271
  >>> map( math.sin, (1,2,3,4))
 
2272
  [0.841470984808, 0.909297426826, 0.14112000806,   -0.756802495308]
 
2273
</PRE>
 
2274
The map operation completes very quickly in such cases.
 
2275
<P>
 
2276
Other examples of aggregate operations include the join and split
 
2277
methods of string objects.  For example if s1..s7 are large (10K+) strings then
 
2278
"".join([s1,s2,s3,s4,s5,s6,s7]) may be far faster than
 
2279
the more obvious s1+s2+s3+s4+s5+s6+s7, since the "summation"
 
2280
will compute many subexpressions, whereas join does all
 
2281
copying in one pass.  For manipulating strings also consider the 
 
2282
regular expression libraries and the "substitution" operations
 
2283
String % tuple and String % dictionary.  Also be sure to use
 
2284
the list.sort builtin method to do sorting, and see FAQ's 4.51
 
2285
and 4.59 for examples of moderately advanced usage -- list.sort beats
 
2286
other techniques for sorting in all but the most extreme
 
2287
circumstances.
 
2288
<P>
 
2289
There are many other aggregate operations
 
2290
available in the standard libraries and in contributed libraries
 
2291
and extensions.
 
2292
<P>
 
2293
Another common trick is to "push loops into functions or methods."
 
2294
For example suppose you have a program that runs slowly and you
 
2295
use the profiler (profile.run) to determine that a Python function ff
 
2296
is being called lots of times.  If you notice that ff 
 
2297
<P>
 
2298
<PRE>
 
2299
   def ff(x):
 
2300
       ...do something with x computing result...
 
2301
       return result
 
2302
</PRE>
 
2303
tends to be called in loops like (A)
 
2304
<P>
 
2305
<PRE>
 
2306
   list = map(ff, oldlist)
 
2307
</PRE>
 
2308
or (B)
 
2309
<P>
 
2310
<PRE>
 
2311
   for x in sequence:
 
2312
       value = ff(x)
 
2313
       ...do something with value...
 
2314
</PRE>
 
2315
then you can often eliminate function call overhead by rewriting
 
2316
ff to
 
2317
<P>
 
2318
<PRE>
 
2319
   def ffseq(seq):
 
2320
       resultseq = []
 
2321
       for x in seq:
 
2322
           ...do something with x computing result...
 
2323
           resultseq.append(result)
 
2324
       return resultseq
 
2325
</PRE>
 
2326
and rewrite (A) to
 
2327
<P>
 
2328
<PRE>
 
2329
    list = ffseq(oldlist)
 
2330
</PRE>
 
2331
and (B) to
 
2332
<P>
 
2333
<PRE>
 
2334
    for value in ffseq(sequence):
 
2335
        ...do something with value...
 
2336
</PRE>
 
2337
Other single calls ff(x) translate to ffseq([x])[0] with little
 
2338
penalty.  Of course this technique is not always appropriate
 
2339
and there are other variants, which you can figure out.
 
2340
<P>
 
2341
You can gain some performance by explicitly storing the results of
 
2342
a function or method lookup into a local variable.  A loop like
 
2343
<P>
 
2344
<PRE>
 
2345
    for key in token:
 
2346
        dict[key] = dict.get(key, 0) + 1
 
2347
</PRE>
 
2348
resolves dict.get every iteration.  If the method isn't going to
 
2349
change, a faster implementation is
 
2350
<P>
 
2351
<PRE>
 
2352
    dict_get = dict.get  # look up the method once
 
2353
    for key in token:
 
2354
        dict[key] = dict_get(key, 0) + 1
 
2355
</PRE>
 
2356
Default arguments can be used to determine values once, at
 
2357
compile time instead of at run time.  This can only be done for
 
2358
functions or objects which will not be changed during program
 
2359
execution, such as replacing
 
2360
<P>
 
2361
<PRE>
 
2362
    def degree_sin(deg):
 
2363
        return math.sin(deg * math.pi / 180.0)
 
2364
</PRE>
 
2365
with
 
2366
<P>
 
2367
<PRE>
 
2368
    def degree_sin(deg, factor = math.pi/180.0, sin = math.sin):
 
2369
        return sin(deg * factor)
 
2370
</PRE>
 
2371
Because this trick uses default arguments for terms which should
 
2372
not be changed, it should only be used when you are not concerned
 
2373
with presenting a possibly confusing API to your users.
 
2374
<P>
 
2375
<P>
 
2376
For an anecdote related to optimization, see
 
2377
<P>
 
2378
<PRE>
 
2379
        <A HREF="http://www.python.org/doc/essays/list2str.html">http://www.python.org/doc/essays/list2str.html</A>
 
2380
</PRE>
 
2381
<P>
 
2382
 
 
2383
<A HREF="faqw.py?req=edit&amp;file=faq04.007.htp">Edit this entry</A> /
 
2384
<A HREF="faqw.py?req=log&amp;file=faq04.007.htp">Log info</A>
 
2385
 
 
2386
/ Last changed on Mon Jun  3 01:03:54 2002 by
 
2387
<A HREF="mailto:neal@metaslash.com">Neal Norwitz</A>
 
2388
<P>
 
2389
 
 
2390
<HR>
 
2391
<H2><A NAME="4.8">4.8. When I have imported a module, then edit it, and import it again (into the same Python process), the changes don't seem to take place. What is going on?</A></H2>
 
2392
For reasons of efficiency as well as consistency, Python only reads
 
2393
the module file on the first time a module is imported.  (Otherwise a
 
2394
program consisting of many modules, each of which imports the same
 
2395
basic module, would read the basic module over and over again.)  To
 
2396
force rereading of a changed module, do this:
 
2397
<P>
 
2398
<PRE>
 
2399
        import modname
 
2400
        reload(modname)
 
2401
</PRE>
 
2402
Warning: this technique is not 100% fool-proof.  In particular,
 
2403
modules containing statements like
 
2404
<P>
 
2405
<PRE>
 
2406
        from modname import some_objects
 
2407
</PRE>
 
2408
will continue to work with the old version of the imported objects.
 
2409
<P>
 
2410
 
 
2411
<A HREF="faqw.py?req=edit&amp;file=faq04.008.htp">Edit this entry</A> /
 
2412
<A HREF="faqw.py?req=log&amp;file=faq04.008.htp">Log info</A>
 
2413
<P>
 
2414
 
 
2415
<HR>
 
2416
<H2><A NAME="4.9">4.9. How do I find the current module name?</A></H2>
 
2417
A module can find out its own module name by looking at the
 
2418
(predefined) global variable __name__.  If this has the value
 
2419
'__main__' you are running as a script.
 
2420
<P>
 
2421
 
 
2422
<A HREF="faqw.py?req=edit&amp;file=faq04.009.htp">Edit this entry</A> /
 
2423
<A HREF="faqw.py?req=log&amp;file=faq04.009.htp">Log info</A>
 
2424
<P>
 
2425
 
 
2426
<HR>
 
2427
<H2><A NAME="4.10">4.10. I have a module in which I want to execute some extra code when it is run as a script. How do I find out whether I am running as a script?</A></H2>
 
2428
See the previous question.  E.g. if you put the following on the
 
2429
last line of your module, main() is called only when your module is
 
2430
running as a script:
 
2431
<P>
 
2432
<PRE>
 
2433
        if __name__ == '__main__': main()
 
2434
</PRE>
 
2435
<P>
 
2436
 
 
2437
<A HREF="faqw.py?req=edit&amp;file=faq04.010.htp">Edit this entry</A> /
 
2438
<A HREF="faqw.py?req=log&amp;file=faq04.010.htp">Log info</A>
 
2439
<P>
 
2440
 
 
2441
<HR>
 
2442
<H2><A NAME="4.11">4.11. I try to run a program from the Demo directory but it fails with ImportError: No module named ...; what gives?</A></H2>
 
2443
This is probably an optional module (written in C!) which hasn't
 
2444
been configured on your system.  This especially happens with modules
 
2445
like "Tkinter", "stdwin", "gl", "Xt" or "Xm".  For Tkinter, STDWIN and
 
2446
many other modules, see Modules/Setup.in for info on how to add these
 
2447
modules to your Python, if it is possible at all.  Sometimes you will
 
2448
have to ftp and build another package first (e.g. Tcl and Tk for Tkinter).
 
2449
Sometimes the module only works on specific platforms (e.g. gl only works
 
2450
on SGI machines).
 
2451
<P>
 
2452
NOTE: if the complaint is about "Tkinter" (upper case T) and you have
 
2453
already configured module "tkinter" (lower case t), the solution is
 
2454
<I>not</I> to rename tkinter to Tkinter or vice versa.  There is probably
 
2455
something wrong with your module search path.  Check out the value of
 
2456
sys.path.
 
2457
<P>
 
2458
For X-related modules (Xt and Xm) you will have to do more work: they
 
2459
are currently not part of the standard Python distribution.  You will
 
2460
have to ftp the Extensions tar file, i.e.
 
2461
<A HREF="ftp://ftp.python.org/pub/python/src/X-extension.tar.gz">ftp://ftp.python.org/pub/python/src/X-extension.tar.gz</A> and follow
 
2462
the instructions there.
 
2463
<P>
 
2464
 
 
2465
<A HREF="faqw.py?req=edit&amp;file=faq04.011.htp">Edit this entry</A> /
 
2466
<A HREF="faqw.py?req=log&amp;file=faq04.011.htp">Log info</A>
 
2467
 
 
2468
/ Last changed on Wed Feb 12 21:31:08 2003 by
 
2469
<A HREF="mailto:kubieziel@gmx.de">Jens Kubieziel</A>
 
2470
<P>
 
2471
 
 
2472
<HR>
 
2473
<H2><A NAME="4.12">4.12. [deleted]</A></H2>
 
2474
[stdwin (long dead windowing library) entry deleted]
 
2475
<P>
 
2476
 
 
2477
<A HREF="faqw.py?req=edit&amp;file=faq04.012.htp">Edit this entry</A> /
 
2478
<A HREF="faqw.py?req=log&amp;file=faq04.012.htp">Log info</A>
 
2479
 
 
2480
/ Last changed on Thu Mar 21 08:30:13 2002 by
 
2481
<A HREF="mailto:erno-pyfaq@erno.iki.fi">Erno Kuusela</A>
 
2482
<P>
 
2483
 
 
2484
<HR>
 
2485
<H2><A NAME="4.13">4.13. What GUI toolkits exist for Python?</A></H2>
 
2486
Depending on what platform(s) you are aiming at, there are several.
 
2487
<P>
 
2488
Currently supported solutions:
 
2489
<P>
 
2490
Cross-platform:
 
2491
<P>
 
2492
Tk:
 
2493
<P>
 
2494
There's a neat object-oriented interface to the Tcl/Tk widget set,
 
2495
called Tkinter.  It is part of the standard Python distribution and
 
2496
well-supported -- all you need to do is build and install Tcl/Tk and
 
2497
enable the _tkinter module and the TKPATH definition in Modules/Setup
 
2498
when building Python.  This is probably the easiest to install and
 
2499
use, and the most complete widget set.  It is also very likely that in
 
2500
the future the standard Python GUI API will be based on or at least
 
2501
look very much like the Tkinter interface.  For more info about Tk,
 
2502
including pointers to the source, see the Tcl/Tk home page at
 
2503
<A HREF="http://www.scriptics.com">http://www.scriptics.com</A>.  Tcl/Tk is now fully
 
2504
portable to the Mac and Windows platforms (NT and 95 only); you need
 
2505
Python 1.4beta3 or later and Tk 4.1patch1 or later.
 
2506
<P>
 
2507
wxWindows:
 
2508
<P>
 
2509
There's an interface to wxWindows called wxPython.  wxWindows is a 
 
2510
portable GUI class library written in C++.  It supports GTK, Motif, 
 
2511
MS-Windows and Mac as targets.  Ports to other platforms are being 
 
2512
contemplated or have already had some work done on them.  wxWindows 
 
2513
preserves the look and feel of the underlying graphics toolkit, and 
 
2514
there is quite a rich widget set and collection of GDI classes.  
 
2515
See the wxWindows page at <A HREF="http://www.wxwindows.org">http://www.wxwindows.org</A>/ for more details.  
 
2516
wxPython is a python extension module that wraps many of the wxWindows 
 
2517
C++ classes, and is quickly gaining popularity amongst Python 
 
2518
developers.  You can get wxPython as part of the source or CVS 
 
2519
distribution of wxWindows, or directly from its home page at 
 
2520
<A HREF="http://alldunn.com/wxPython">http://alldunn.com/wxPython</A>/.
 
2521
<P>
 
2522
Gtk+:
 
2523
<P>
 
2524
PyGtk bindings for the Gtk+ Toolkit by James Henstridge exist; see <A HREF="ftp://ftp.daa.com.au/pub/james/python">ftp://ftp.daa.com.au/pub/james/python</A>/.  Note that there are two incompatible bindings.  If you are using Gtk+ 1.2.x you should get the 0.6.x PyGtk bindings from
 
2525
<P>
 
2526
<PRE>
 
2527
    <A HREF="ftp://ftp.gtk.org/pub/python/v1.2">ftp://ftp.gtk.org/pub/python/v1.2</A>
 
2528
</PRE>
 
2529
If you plan to use Gtk+ 2.0 with Python (highly recommended if you are just starting with Gtk), get the most recent distribution from
 
2530
<P>
 
2531
<PRE>
 
2532
    <A HREF="ftp://ftp.gtk.org/pub/python/v2.0">ftp://ftp.gtk.org/pub/python/v2.0</A>
 
2533
</PRE>
 
2534
If you are adventurous, you can also check out the source from the Gnome CVS repository.  Set your CVS directory to :pserver:<A HREF="mailto:anonymous@anoncvs.gnome.org">anonymous@anoncvs.gnome.org</A>:/cvs/gnome and check the gnome-python module out from the repository.
 
2535
<P>
 
2536
Other:
 
2537
<P>
 
2538
There are also bindings available for the Qt toolkit (PyQt), and for KDE (PyKDE); see <A HREF="http://www.thekompany.com/projects/pykde">http://www.thekompany.com/projects/pykde</A>/.
 
2539
<P>
 
2540
For OpenGL bindings, see <A HREF="http://starship.python.net/~da/PyOpenGL">http://starship.python.net/~da/PyOpenGL</A>.
 
2541
<P>
 
2542
Platform specific:
 
2543
<P>
 
2544
The Mac port has a rich and ever-growing set of modules that support
 
2545
the native Mac toolbox calls.  See the documentation that comes with
 
2546
the Mac port.  See <A HREF="ftp://ftp.python.org/pub/python/mac">ftp://ftp.python.org/pub/python/mac</A>.  Support
 
2547
by Jack Jansen <A HREF="mailto:jack@cwi.nl">jack@cwi.nl</A>.
 
2548
<P>
 
2549
Pythonwin by Mark Hammond (<A HREF="mailto:MHammond@skippinet.com.au">MHammond@skippinet.com.au</A>)
 
2550
includes an interface to the Microsoft Foundation
 
2551
Classes and a Python programming environment using it that's written
 
2552
mostly in Python.  See <A HREF="http://www.python.org/windows">http://www.python.org/windows</A>/.
 
2553
<P>
 
2554
There's an object-oriented GUI based on the Microsoft Foundation
 
2555
Classes model called WPY, supported by Jim Ahlstrom <A HREF="mailto:jim@interet.com">jim@interet.com</A>.
 
2556
Programs written in WPY run unchanged and with native look and feel on
 
2557
Windows NT/95, Windows 3.1 (using win32s), and on Unix (using Tk).
 
2558
Source and binaries for Windows and Linux are available in
 
2559
<A HREF="ftp://ftp.python.org/pub/python/wpy">ftp://ftp.python.org/pub/python/wpy</A>/.
 
2560
<P>
 
2561
Obsolete or minority solutions:
 
2562
<P>
 
2563
There's an interface to X11, including the Athena and Motif widget
 
2564
sets (and a few individual widgets, like Mosaic's HTML widget and
 
2565
SGI's GL widget) available from
 
2566
<A HREF="ftp://ftp.python.org/pub/python/src/X-extension.tar.gz">ftp://ftp.python.org/pub/python/src/X-extension.tar.gz</A>.
 
2567
Support by Sjoerd Mullender <A HREF="mailto:sjoerd@cwi.nl">sjoerd@cwi.nl</A>.
 
2568
<P>
 
2569
On top of the X11 interface there's the vpApp
 
2570
toolkit by Per Spilling, now also maintained by Sjoerd Mullender
 
2571
<A HREF="mailto:sjoerd@cwi.nl">sjoerd@cwi.nl</A>.  See <A HREF="ftp://ftp.cwi.nl/pub/sjoerd/vpApp.tar.gz">ftp://ftp.cwi.nl/pub/sjoerd/vpApp.tar.gz</A>.
 
2572
<P>
 
2573
For SGI IRIX only, there are unsupported interfaces to the complete
 
2574
GL (Graphics Library -- low level but very good 3D capabilities) as
 
2575
well as to FORMS (a buttons-and-sliders-etc package built on top of GL
 
2576
by Mark Overmars -- ftp'able from
 
2577
<A HREF="ftp://ftp.cs.ruu.nl/pub/SGI/FORMS">ftp://ftp.cs.ruu.nl/pub/SGI/FORMS</A>/).  This is probably also
 
2578
becoming obsolete, as OpenGL takes over (see above).
 
2579
<P>
 
2580
There's an interface to STDWIN, a platform-independent low-level
 
2581
windowing interface for Mac and X11.  This is totally unsupported and
 
2582
rapidly becoming obsolete.  The STDWIN sources are at
 
2583
<A HREF="ftp://ftp.cwi.nl/pub/stdwin">ftp://ftp.cwi.nl/pub/stdwin</A>/.
 
2584
<P>
 
2585
There is an interface to WAFE, a Tcl interface to the X11
 
2586
Motif and Athena widget sets.  WAFE is at
 
2587
<A HREF="http://www.wu-wien.ac.at/wafe/wafe.html">http://www.wu-wien.ac.at/wafe/wafe.html</A>.
 
2588
<P>
 
2589
 
 
2590
<A HREF="faqw.py?req=edit&amp;file=faq04.013.htp">Edit this entry</A> /
 
2591
<A HREF="faqw.py?req=log&amp;file=faq04.013.htp">Log info</A>
 
2592
 
 
2593
/ Last changed on Mon May 13 21:40:39 2002 by
 
2594
<A HREF="mailto:skip@pobox.com">Skip Montanaro</A>
 
2595
<P>
 
2596
 
 
2597
<HR>
 
2598
<H2><A NAME="4.14">4.14. Are there any interfaces to database packages in Python?</A></H2>
 
2599
Yes!  See the Database Topic Guide at 
 
2600
<A HREF="http://www.python.org/topics/database">http://www.python.org/topics/database</A>/ for details.
 
2601
<P>
 
2602
 
 
2603
<A HREF="faqw.py?req=edit&amp;file=faq04.014.htp">Edit this entry</A> /
 
2604
<A HREF="faqw.py?req=log&amp;file=faq04.014.htp">Log info</A>
 
2605
 
 
2606
/ Last changed on Tue Jan  4 20:12:19 2000 by
 
2607
<A HREF="mailto:bwarsaw@python.org">Barney Warplug</A>
 
2608
<P>
 
2609
 
 
2610
<HR>
 
2611
<H2><A NAME="4.15">4.15. Is it possible to write obfuscated one-liners in Python?</A></H2>
 
2612
Yes.  See the following three examples, due to Ulf Bartelt:
 
2613
<P>
 
2614
<PRE>
 
2615
        # Primes &lt; 1000
 
2616
        print filter(None,map(lambda y:y*reduce(lambda x,y:x*y!=0,
 
2617
        map(lambda x,y=y:y%x,range(2,int(pow(y,0.5)+1))),1),range(2,1000)))
 
2618
</PRE>
 
2619
<PRE>
 
2620
        # First 10 Fibonacci numbers
 
2621
        print map(lambda x,f=lambda x,f:(x&lt;=1) or (f(x-1,f)+f(x-2,f)): f(x,f),
 
2622
        range(10))
 
2623
</PRE>
 
2624
<PRE>
 
2625
        # Mandelbrot set
 
2626
        print (lambda Ru,Ro,Iu,Io,IM,Sx,Sy:reduce(lambda x,y:x+y,map(lambda y,
 
2627
        Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,Sy=Sy,L=lambda yc,Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,i=IM,
 
2628
        Sx=Sx,Sy=Sy:reduce(lambda x,y:x+y,map(lambda x,xc=Ru,yc=yc,Ru=Ru,Ro=Ro,
 
2629
        i=i,Sx=Sx,F=lambda xc,yc,x,y,k,f=lambda xc,yc,x,y,k,f:(k&lt;=0)or (x*x+y*y
 
2630
        >=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(
 
2631
        64+F(Ru+x*(Ro-Ru)/Sx,yc,0,0,i)),range(Sx))):L(Iu+y*(Io-Iu)/Sy),range(Sy
 
2632
        ))))(-2.1, 0.7, -1.2, 1.2, 30, 80, 24)
 
2633
        #    \___ ___/  \___ ___/  |   |   |__ lines on screen
 
2634
        #        V          V      |   |______ columns on screen
 
2635
        #        |          |      |__________ maximum of "iterations"
 
2636
        #        |          |_________________ range on y axis
 
2637
        #        |____________________________ range on x axis
 
2638
</PRE>
 
2639
Don't try this at home, kids!
 
2640
<P>
 
2641
 
 
2642
<A HREF="faqw.py?req=edit&amp;file=faq04.015.htp">Edit this entry</A> /
 
2643
<A HREF="faqw.py?req=log&amp;file=faq04.015.htp">Log info</A>
 
2644
 
 
2645
/ Last changed on Wed May 21 15:48:33 1997 by
 
2646
<A HREF="mailto:guido@python.org">GvR</A>
 
2647
<P>
 
2648
 
 
2649
<HR>
 
2650
<H2><A NAME="4.16">4.16. Is there an equivalent of C's &quot;?:&quot; ternary operator?</A></H2>
 
2651
Not directly.  In many cases you can mimic a?b:c with "a and b or
 
2652
c", but there's a flaw: if b is zero (or empty, or None -- anything
 
2653
that tests false) then c will be selected instead.  In many cases you
 
2654
can prove by looking at the code that this can't happen (e.g. because
 
2655
b is a constant or has a type that can never be false), but in general
 
2656
this can be a problem.
 
2657
<P>
 
2658
Tim Peters (who wishes it was Steve Majewski) suggested the following
 
2659
solution: (a and [b] or [c])[0].  Because [b] is a singleton list it
 
2660
is never false, so the wrong path is never taken; then applying [0] to
 
2661
the whole thing gets the b or c that you really wanted.  Ugly, but it
 
2662
gets you there in the rare cases where it is really inconvenient to
 
2663
rewrite your code using 'if'.
 
2664
<P>
 
2665
As a last resort it is possible to implement the "?:" operator as a function:
 
2666
<P>
 
2667
<PRE>
 
2668
    def q(cond,on_true,on_false):
 
2669
        from inspect import isfunction
 
2670
</PRE>
 
2671
<PRE>
 
2672
        if cond:
 
2673
            if not isfunction(on_true): return on_true
 
2674
            else: return apply(on_true)
 
2675
        else:
 
2676
            if not isfunction(on_false): return on_false 
 
2677
            else: return apply(on_false)
 
2678
</PRE>
 
2679
In most cases you'll pass b and c directly: q(a,b,c).  To avoid evaluating b
 
2680
or c when they shouldn't be, encapsulate them
 
2681
within a lambda function, e.g.: q(a,lambda: b, lambda: c).
 
2682
<P>
 
2683
<P>
 
2684
<P>
 
2685
It has been asked <I>why</I> Python has no if-then-else expression,
 
2686
since most language have one; it is a frequently requested feature.
 
2687
<P>
 
2688
There are several possible answers: just as many languages do
 
2689
just fine without one; it can easily lead to less readable code;
 
2690
no sufficiently "Pythonic" syntax has been discovered; a search
 
2691
of the standard library found remarkably few places where using an
 
2692
if-then-else expression would make the code more understandable.
 
2693
<P>
 
2694
Nevertheless, in an effort to decide once and for all whether
 
2695
an if-then-else expression should be added to the language,
 
2696
PEP 308 (<A HREF="http://www.python.org/peps/pep-0308.html">http://www.python.org/peps/pep-0308.html</A>) has been
 
2697
put forward, proposing a specific syntax.  The community can
 
2698
now vote on this issue.
 
2699
<P>
 
2700
 
 
2701
<A HREF="faqw.py?req=edit&amp;file=faq04.016.htp">Edit this entry</A> /
 
2702
<A HREF="faqw.py?req=log&amp;file=faq04.016.htp">Log info</A>
 
2703
 
 
2704
/ Last changed on Fri Feb  7 19:41:13 2003 by
 
2705
<A HREF="mailto:goodger@python.org">David Goodger</A>
 
2706
<P>
 
2707
 
 
2708
<HR>
 
2709
<H2><A NAME="4.17">4.17. My class defines __del__ but it is not called when I delete the object.</A></H2>
 
2710
There are several possible reasons for this.
 
2711
<P>
 
2712
The del statement does not necessarily call __del__ -- it simply
 
2713
decrements the object's reference count, and if this reaches zero
 
2714
__del__ is called.
 
2715
<P>
 
2716
If your data structures contain circular links (e.g. a tree where
 
2717
each child has a parent pointer and each parent has a list of
 
2718
children) the reference counts will never go back to zero.  You'll
 
2719
have to define an explicit close() method which removes those
 
2720
pointers.  Please don't ever call __del__ directly -- __del__ should
 
2721
call close() and close() should make sure that it can be called more
 
2722
than once for the same object.
 
2723
<P>
 
2724
If the object has ever been a local variable (or argument, which is
 
2725
really the same thing) to a function that caught an expression in an
 
2726
except clause, chances are that a reference to the object still exists
 
2727
in that function's stack frame as contained in the stack trace.
 
2728
Normally, deleting (better: assigning None to) sys.exc_traceback will
 
2729
take care of this.  If a stack was printed for an unhandled
 
2730
exception in an interactive interpreter, delete sys.last_traceback
 
2731
instead.
 
2732
<P>
 
2733
There is code that deletes all objects when the interpreter exits,
 
2734
but it is not called if your Python has been configured to support
 
2735
threads (because other threads may still be active).  You can define
 
2736
your own cleanup function using sys.exitfunc (see question 4.4).
 
2737
<P>
 
2738
Finally, if your __del__ method raises an exception, a warning message is printed to sys.stderr.
 
2739
<P>
 
2740
<P>
 
2741
Starting with Python 2.0, a garbage collector periodically reclaims the space used by most cycles with no external references. (See the "gc" module documentation for details.) There <I>are</I>, however, pathological cases where it can be expected to fail. Moreover, the garbage collector runs some time after the last reference to your data structure vanishes, so your __del__ method may be called at an inconvenient and random time. This is inconvenient if you're trying to reproduce a problem. Worse, the order in which object's __del__ methods are executed is arbitrary.
 
2742
<P>
 
2743
Another way to avoid cyclical references is to use the "weakref" module, which allows you to point to objects without incrementing their reference count. Tree data structures, for instance, should use weak references for their parent  and sibling pointers (if they need them!).
 
2744
<P>
 
2745
Question 6.14 is intended to explain the new garbage collection algorithm.
 
2746
<P>
 
2747
 
 
2748
<A HREF="faqw.py?req=edit&amp;file=faq04.017.htp">Edit this entry</A> /
 
2749
<A HREF="faqw.py?req=log&amp;file=faq04.017.htp">Log info</A>
 
2750
 
 
2751
/ Last changed on Mon Jun 10 15:27:28 2002 by
 
2752
<A HREF="mailto:smurf@noris.de">Matthias Urlichs</A>
 
2753
<P>
 
2754
 
 
2755
<HR>
 
2756
<H2><A NAME="4.18">4.18. How do I change the shell environment for programs called using os.popen() or os.system()? Changing os.environ doesn't work.</A></H2>
 
2757
You must be using either a version of python before 1.4, or on a
 
2758
(rare) system that doesn't have the putenv() library function.
 
2759
<P>
 
2760
Before Python 1.4, modifying the environment passed to subshells was
 
2761
left out of the interpreter because there seemed to be no
 
2762
well-established portable way to do it (in particular, some systems,
 
2763
have putenv(), others have setenv(), and some have none at all).  As
 
2764
of Python 1.4, almost all Unix systems <I>do</I> have putenv(), and so does
 
2765
the Win32 API, and thus the os module was modified so that changes to
 
2766
os.environ are trapped and the corresponding putenv() call is made.
 
2767
<P>
 
2768
 
 
2769
<A HREF="faqw.py?req=edit&amp;file=faq04.018.htp">Edit this entry</A> /
 
2770
<A HREF="faqw.py?req=log&amp;file=faq04.018.htp">Log info</A>
 
2771
<P>
 
2772
 
 
2773
<HR>
 
2774
<H2><A NAME="4.19">4.19. What is a class?</A></H2>
 
2775
A class is the particular object type created by executing
 
2776
a class statement.  Class objects are used as templates, to create
 
2777
instance objects, which embody both the data structure
 
2778
(attributes) and program routines (methods) specific to a datatype.
 
2779
<P>
 
2780
A class can be based on one or more other classes, called its base
 
2781
class(es). It then inherits the attributes and methods of its base classes. This allows an object model to be successively refined
 
2782
by inheritance.
 
2783
<P>
 
2784
The term "classic class" is used to refer to the original
 
2785
class implementation in Python. One problem with classic
 
2786
classes is their inability to use the built-in data types
 
2787
(such as list and dictionary) as base classes. Starting
 
2788
with Python 2.2 an attempt is in progress to unify user-defined
 
2789
classes and built-in types. It is now possible to declare classes
 
2790
that inherit from built-in types.
 
2791
<P>
 
2792
 
 
2793
<A HREF="faqw.py?req=edit&amp;file=faq04.019.htp">Edit this entry</A> /
 
2794
<A HREF="faqw.py?req=log&amp;file=faq04.019.htp">Log info</A>
 
2795
 
 
2796
/ Last changed on Mon May 27 01:31:21 2002 by
 
2797
<A HREF="mailto:sholden@holdenweb.com">Steve Holden</A>
 
2798
<P>
 
2799
 
 
2800
<HR>
 
2801
<H2><A NAME="4.20">4.20. What is a method?</A></H2>
 
2802
A method is a function that you normally call as
 
2803
x.name(arguments...) for some object x.  The term is used for methods
 
2804
of classes and class instances as well as for methods of built-in
 
2805
objects.  (The latter have a completely different implementation and
 
2806
only share the way their calls look in Python code.)  Methods of
 
2807
classes (and class instances) are defined as functions inside the
 
2808
class definition.
 
2809
<P>
 
2810
 
 
2811
<A HREF="faqw.py?req=edit&amp;file=faq04.020.htp">Edit this entry</A> /
 
2812
<A HREF="faqw.py?req=log&amp;file=faq04.020.htp">Log info</A>
 
2813
<P>
 
2814
 
 
2815
<HR>
 
2816
<H2><A NAME="4.21">4.21. What is self?</A></H2>
 
2817
Self is merely a conventional name for the first argument of a
 
2818
method -- i.e. a function defined inside a class definition.  A method
 
2819
defined as meth(self, a, b, c) should be called as x.meth(a, b, c) for
 
2820
some instance x of the class in which the definition occurs;
 
2821
the called method will think it is called as meth(x, a, b, c).
 
2822
<P>
 
2823
 
 
2824
<A HREF="faqw.py?req=edit&amp;file=faq04.021.htp">Edit this entry</A> /
 
2825
<A HREF="faqw.py?req=log&amp;file=faq04.021.htp">Log info</A>
 
2826
<P>
 
2827
 
 
2828
<HR>
 
2829
<H2><A NAME="4.22">4.22. What is an unbound method?</A></H2>
 
2830
An unbound method is a method defined in a class that is not yet
 
2831
bound to an instance.  You get an unbound method if you ask for a
 
2832
class attribute that happens to be a function. You get a bound method
 
2833
if you ask for an instance attribute.  A bound method knows which
 
2834
instance it belongs to and calling it supplies the instance automatically;
 
2835
an unbound method only knows which class it wants for its first
 
2836
argument (a derived class is also OK).  Calling an unbound method
 
2837
doesn't "magically" derive the first argument from the context -- you
 
2838
have to provide it explicitly.
 
2839
<P>
 
2840
Trivia note regarding bound methods:  each reference to a bound
 
2841
method of a particular object creates a bound method object.  If you
 
2842
have two such references (a = inst.meth; b = inst.meth), they will
 
2843
compare equal (a == b) but are not the same (a is not b).
 
2844
<P>
 
2845
 
 
2846
<A HREF="faqw.py?req=edit&amp;file=faq04.022.htp">Edit this entry</A> /
 
2847
<A HREF="faqw.py?req=log&amp;file=faq04.022.htp">Log info</A>
 
2848
 
 
2849
/ Last changed on Wed May  6 18:07:25 1998 by
 
2850
<A HREF="mailto:clarence@avtel.com">Clarence Gardner</A>
 
2851
<P>
 
2852
 
 
2853
<HR>
 
2854
<H2><A NAME="4.23">4.23. How do I call a method defined in a base class from a derived class that overrides it?</A></H2>
 
2855
If your class definition starts with "class Derived(Base): ..."
 
2856
then you can call method meth defined in Base (or one of Base's base
 
2857
classes) as Base.meth(self, arguments...).  Here, Base.meth is an
 
2858
unbound method (see previous question).
 
2859
<P>
 
2860
 
 
2861
<A HREF="faqw.py?req=edit&amp;file=faq04.023.htp">Edit this entry</A> /
 
2862
<A HREF="faqw.py?req=log&amp;file=faq04.023.htp">Log info</A>
 
2863
<P>
 
2864
 
 
2865
<HR>
 
2866
<H2><A NAME="4.24">4.24. How do I call a method from a base class without using the name of the base class?</A></H2>
 
2867
DON'T DO THIS.  REALLY.  I MEAN IT.  It appears that you could call
 
2868
self.__class__.__bases__[0].meth(self, arguments...) but this fails when
 
2869
a doubly-derived method is derived from your class: for its instances,
 
2870
self.__class__.__bases__[0] is your class, not its base class -- so
 
2871
(assuming you are doing this from within Derived.meth) you would start
 
2872
a recursive call.
 
2873
<P>
 
2874
Often when you want to do this you are forgetting that classes
 
2875
are first class in Python.  You can "point to" the class you want
 
2876
to delegate an operation to either at the instance or at the
 
2877
subclass level.  For example if you want to use a "glorp"
 
2878
operation of a superclass you can point to the right superclass
 
2879
to use.
 
2880
<P>
 
2881
<PRE>
 
2882
  class subclass(superclass1, superclass2, superclass3):
 
2883
      delegate_glorp = superclass2
 
2884
      ...
 
2885
      def glorp(self, arg1, arg2):
 
2886
            ... subclass specific stuff ...
 
2887
            self.delegate_glorp.glorp(self, arg1, arg2)
 
2888
       ...
 
2889
</PRE>
 
2890
<PRE>
 
2891
  class subsubclass(subclass):
 
2892
       delegate_glorp = superclass3
 
2893
       ...
 
2894
</PRE>
 
2895
Note, however that setting delegate_glorp to subclass in
 
2896
subsubclass would cause an infinite recursion on subclass.delegate_glorp.  Careful!  Maybe you are getting too fancy for your own good.  Consider simplifying the design (?).
 
2897
<P>
 
2898
 
 
2899
<A HREF="faqw.py?req=edit&amp;file=faq04.024.htp">Edit this entry</A> /
 
2900
<A HREF="faqw.py?req=log&amp;file=faq04.024.htp">Log info</A>
 
2901
 
 
2902
/ Last changed on Mon Jul 28 13:58:22 1997 by
 
2903
<A HREF="mailto:aaron_watters@msn.com">aaron watters</A>
 
2904
<P>
 
2905
 
 
2906
<HR>
 
2907
<H2><A NAME="4.25">4.25. How can I organize my code to make it easier to change the base class?</A></H2>
 
2908
You could define an alias for the base class, assign the real base
 
2909
class to it before your class definition, and use the alias throughout
 
2910
your class.  Then all you have to change is the value assigned to the
 
2911
alias.  Incidentally, this trick is also handy if you want to decide
 
2912
dynamically (e.g. depending on availability of resources) which base
 
2913
class to use.  Example:
 
2914
<P>
 
2915
<PRE>
 
2916
        BaseAlias = &lt;real base class&gt;
 
2917
        class Derived(BaseAlias):
 
2918
                def meth(self):
 
2919
                        BaseAlias.meth(self)
 
2920
                        ...
 
2921
</PRE>
 
2922
<P>
 
2923
 
 
2924
<A HREF="faqw.py?req=edit&amp;file=faq04.025.htp">Edit this entry</A> /
 
2925
<A HREF="faqw.py?req=log&amp;file=faq04.025.htp">Log info</A>
 
2926
 
 
2927
/ Last changed on Wed May 21 15:49:57 1997 by
 
2928
<A HREF="mailto:guido@python.org">GvR</A>
 
2929
<P>
 
2930
 
 
2931
<HR>
 
2932
<H2><A NAME="4.26">4.26. How can I find the methods or attributes of an object?</A></H2>
 
2933
This depends on the object type.
 
2934
<P>
 
2935
For an instance x of a user-defined class, instance attributes are
 
2936
found in the dictionary x.__dict__, and methods and attributes defined
 
2937
by its class are found in x.__class__.__bases__[i].__dict__ (for i in
 
2938
range(len(x.__class__.__bases__))).  You'll have to walk the tree of
 
2939
base classes to find <I>all</I> class methods and attributes.
 
2940
<P>
 
2941
Many, but not all built-in types define a list of their method names
 
2942
in x.__methods__, and if they have data attributes, their names may be
 
2943
found in x.__members__.  However this is only a convention.
 
2944
<P>
 
2945
For more information, read the source of the standard (but
 
2946
undocumented) module newdir.
 
2947
<P>
 
2948
 
 
2949
<A HREF="faqw.py?req=edit&amp;file=faq04.026.htp">Edit this entry</A> /
 
2950
<A HREF="faqw.py?req=log&amp;file=faq04.026.htp">Log info</A>
 
2951
<P>
 
2952
 
 
2953
<HR>
 
2954
<H2><A NAME="4.27">4.27. I can't seem to use os.read() on a pipe created with os.popen().</A></H2>
 
2955
os.read() is a low-level function which takes a file descriptor (a
 
2956
small integer).  os.popen() creates a high-level file object -- the
 
2957
same type used for sys.std{in,out,err} and returned by the builtin
 
2958
open() function.  Thus, to read n bytes from a pipe p created with
 
2959
os.popen(), you need to use p.read(n).
 
2960
<P>
 
2961
 
 
2962
<A HREF="faqw.py?req=edit&amp;file=faq04.027.htp">Edit this entry</A> /
 
2963
<A HREF="faqw.py?req=log&amp;file=faq04.027.htp">Log info</A>
 
2964
<P>
 
2965
 
 
2966
<HR>
 
2967
<H2><A NAME="4.28">4.28. How can I create a stand-alone binary from a Python script?</A></H2>
 
2968
Even though there are Python compilers being developed,
 
2969
you probably don't need a <I>real</I> compiler, if all you want
 
2970
is a stand-alone program. There are three solutions to that. 
 
2971
<P>
 
2972
One is to use the freeze tool, which is included in the Python
 
2973
source tree as Tools/freeze. It converts Python byte
 
2974
code to C arrays.  Using a C compiler, you can embed all
 
2975
your modules into a new program, which is then linked
 
2976
with the standard Python modules. 
 
2977
<P>
 
2978
It works by scanning your source recursively for import statements
 
2979
(in both forms) and looking for the modules in the standard Python path
 
2980
as well as in the source directory (for built-in modules).  It then
 
2981
1 the modules written in Python to C code (array initializers
 
2982
that can be turned into code objects using the marshal module) and
 
2983
creates a custom-made config file that only contains those built-in
 
2984
modules which are actually used in the program.  It then compiles the
 
2985
generated C code and links it with the rest of the Python interpreter
 
2986
to form a self-contained binary which acts exactly like your script.
 
2987
<P>
 
2988
(Hint: the freeze program only works if your script's filename ends in
 
2989
".py".)
 
2990
<P>
 
2991
There are several utilities which may be helpful.  The first is Gordon McMillan's installer at
 
2992
<P>
 
2993
<PRE>
 
2994
    <A HREF="http://www.mcmillan-inc.com/install1.html">http://www.mcmillan-inc.com/install1.html</A>
 
2995
</PRE>
 
2996
which works on Windows, Linux and at least some forms of Unix.
 
2997
<P>
 
2998
Another is Thomas Heller's py2exe (Windows only) at
 
2999
<P>
 
3000
<PRE>
 
3001
    <A HREF="http://starship.python.net/crew/theller/py2exe">http://starship.python.net/crew/theller/py2exe</A>/
 
3002
</PRE>
 
3003
A third is Christian Tismer's SQFREEZE
 
3004
(<A HREF="http://starship.python.net/crew/pirx">http://starship.python.net/crew/pirx</A>/) which appends the byte code
 
3005
to a specially-prepared Python interpreter, which
 
3006
will find the byte code in executable. 
 
3007
<P>
 
3008
A fourth is Fredrik Lundh's Squeeze
 
3009
(<A HREF="http://www.pythonware.com/products/python/squeeze">http://www.pythonware.com/products/python/squeeze</A>/).
 
3010
<P>
 
3011
 
 
3012
<A HREF="faqw.py?req=edit&amp;file=faq04.028.htp">Edit this entry</A> /
 
3013
<A HREF="faqw.py?req=log&amp;file=faq04.028.htp">Log info</A>
 
3014
 
 
3015
/ Last changed on Wed Jun 19 14:01:30 2002 by
 
3016
<A HREF="mailto:gmcm@hypernet.com">Gordon McMillan</A>
 
3017
<P>
 
3018
 
 
3019
<HR>
 
3020
<H2><A NAME="4.29">4.29. What WWW tools are there for Python?</A></H2>
 
3021
See the chapters titled "Internet Protocols and Support" and
 
3022
"Internet Data Handling" in the Library Reference
 
3023
Manual. Python is full of good things which will help you build server-side and client-side web systems.
 
3024
<P>
 
3025
A summary of available frameworks is maintained by Paul Boddie at
 
3026
<P>
 
3027
<PRE>
 
3028
    <A HREF="http://thor.prohosting.com/~pboddie/Python/web_modules.html">http://thor.prohosting.com/~pboddie/Python/web_modules.html</A>
 
3029
</PRE>
 
3030
Cameron Laird maintains a useful set of pages about Python web technologies at
 
3031
<P>
 
3032
<PRE>
 
3033
   <A HREF="http://starbase.neosoft.com/~claird/comp.lang.python/web_python.html">http://starbase.neosoft.com/~claird/comp.lang.python/web_python.html</A>/
 
3034
</PRE>
 
3035
There was a web browser written in Python, called Grail --
 
3036
see <A HREF="http://sourceforge.net/project/grail">http://sourceforge.net/project/grail</A>/. This project has been terminated; <A HREF="http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/grail/grail/README">http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/grail/grail/README</A> gives more details.
 
3037
<P>
 
3038
 
 
3039
<A HREF="faqw.py?req=edit&amp;file=faq04.029.htp">Edit this entry</A> /
 
3040
<A HREF="faqw.py?req=log&amp;file=faq04.029.htp">Log info</A>
 
3041
 
 
3042
/ Last changed on Mon Nov 11 22:48:25 2002 by
 
3043
<A HREF="mailto:guido@python.org">GvR</A>
 
3044
<P>
 
3045
 
 
3046
<HR>
 
3047
<H2><A NAME="4.30">4.30. How do I run a subprocess with pipes connected to both input and output?</A></H2>
 
3048
Use the standard popen2 module.  For example:
 
3049
<P>
 
3050
<PRE>
 
3051
        import popen2
 
3052
        fromchild, tochild = popen2.popen2("command")
 
3053
        tochild.write("input\n")
 
3054
        tochild.flush()
 
3055
        output = fromchild.readline()
 
3056
</PRE>
 
3057
Warning: in general, it is unwise to
 
3058
do this, because you can easily cause a deadlock where your
 
3059
process is blocked waiting for output from the child, while the child
 
3060
is blocked waiting for input from you.  This can be caused
 
3061
because the parent expects the child to output more text than it does,
 
3062
or it can be caused by data being stuck in stdio buffers due to lack
 
3063
of flushing.  The Python parent can of course explicitly flush the data
 
3064
it sends to the child before it reads any output, but if the child is
 
3065
a naive C program it can easily have been written to never explicitly
 
3066
flush its output, even if it is interactive, since flushing is
 
3067
normally automatic.
 
3068
<P>
 
3069
Note that a deadlock is also possible if you use popen3 to read
 
3070
stdout and stderr. If one of the two is too large for the internal
 
3071
buffer (increasing the buffersize does not help) and you read()
 
3072
the other one first, there is a deadlock, too.
 
3073
<P>
 
3074
Note on a bug in popen2: unless your program calls wait()
 
3075
or waitpid(), finished child processes are never removed,
 
3076
and eventually calls to popen2 will fail because of a limit on
 
3077
the number of child processes.  Calling os.waitpid with the
 
3078
os.WNOHANG option can prevent this; a good place to insert such
 
3079
a call would be before calling popen2 again.
 
3080
<P>
 
3081
Another way to produce a deadlock: Call a wait() and there is
 
3082
still more output from the program than what fits into the 
 
3083
internal buffers.
 
3084
<P>
 
3085
In many cases, all you really need is to run some data through a
 
3086
command and get the result back.  Unless the data is infinite in size,
 
3087
the easiest (and often the most efficient!) way to do this is to write
 
3088
it to a temporary file and run the command with that temporary file as
 
3089
input.  The standard module tempfile exports a function mktemp() which
 
3090
generates unique temporary file names.
 
3091
<P>
 
3092
<PRE>
 
3093
 import tempfile
 
3094
 import os
 
3095
 class Popen3:
 
3096
    """
 
3097
    This is a deadlock-save version of popen, that returns
 
3098
    an object with errorlevel, out (a string) and err (a string).
 
3099
    (capturestderr may not work under windows.)
 
3100
    Example: print Popen3('grep spam','\n\nhere spam\n\n').out
 
3101
    """
 
3102
    def __init__(self,command,input=None,capturestderr=None):
 
3103
        outfile=tempfile.mktemp()
 
3104
        command="( %s ) > %s" % (command,outfile)
 
3105
        if input:
 
3106
            infile=tempfile.mktemp()
 
3107
            open(infile,"w").write(input)
 
3108
            command=command+" &lt;"+infile
 
3109
        if capturestderr:
 
3110
            errfile=tempfile.mktemp()
 
3111
            command=command+" 2>"+errfile
 
3112
        self.errorlevel=os.system(command) >> 8
 
3113
        self.out=open(outfile,"r").read()
 
3114
        os.remove(outfile)
 
3115
        if input:
 
3116
            os.remove(infile)
 
3117
        if capturestderr:
 
3118
            self.err=open(errfile,"r").read()
 
3119
            os.remove(errfile)
 
3120
</PRE>
 
3121
Note that many interactive programs (e.g. vi) don't work well with
 
3122
pipes substituted for standard input and output.  You will have to use
 
3123
pseudo ttys ("ptys") instead of pipes.  There is some undocumented
 
3124
code to use these in the library module pty.py -- I'm afraid you're on
 
3125
your own here.
 
3126
<P>
 
3127
A different answer is a Python interface to Don Libes' "expect"
 
3128
library.  A Python extension that interfaces to expect is called "expy"
 
3129
and available from
 
3130
<A HREF="http://expectpy.sourceforge.net">http://expectpy.sourceforge.net</A>/.
 
3131
<P>
 
3132
A pure Python solution that works like expect is pexpect of Noah Spurrier.
 
3133
A beta version is available from
 
3134
<A HREF="http://pexpect.sourceforge.net">http://pexpect.sourceforge.net</A>/
 
3135
<P>
 
3136
 
 
3137
<A HREF="faqw.py?req=edit&amp;file=faq04.030.htp">Edit this entry</A> /
 
3138
<A HREF="faqw.py?req=log&amp;file=faq04.030.htp">Log info</A>
 
3139
 
 
3140
/ Last changed on Tue Sep  3 16:31:31 2002 by
 
3141
<A HREF="mailto:polzin@gmx.de">Tobias Polzin</A>
 
3142
<P>
 
3143
 
 
3144
<HR>
 
3145
<H2><A NAME="4.31">4.31. How do I call a function if I have the arguments in a tuple?</A></H2>
 
3146
Use the built-in function apply().  For instance,
 
3147
<P>
 
3148
<PRE>
 
3149
    func(1, 2, 3)
 
3150
</PRE>
 
3151
is equivalent to
 
3152
<P>
 
3153
<PRE>
 
3154
    args = (1, 2, 3)
 
3155
    apply(func, args)
 
3156
</PRE>
 
3157
Note that func(args) is not the same -- it calls func() with exactly
 
3158
one argument, the tuple args, instead of three arguments, the integers
 
3159
1, 2 and 3.
 
3160
<P>
 
3161
In Python 2.0, you can also use extended call syntax:
 
3162
<P>
 
3163
f(*args) is equivalent to apply(f, args)
 
3164
<P>
 
3165
 
 
3166
<A HREF="faqw.py?req=edit&amp;file=faq04.031.htp">Edit this entry</A> /
 
3167
<A HREF="faqw.py?req=log&amp;file=faq04.031.htp">Log info</A>
 
3168
 
 
3169
/ Last changed on Tue Jan  2 03:42:50 2001 by
 
3170
<A HREF="mailto:moshez@zadka.site.co.il">Moshe Zadka</A>
 
3171
<P>
 
3172
 
 
3173
<HR>
 
3174
<H2><A NAME="4.32">4.32. How do I enable font-lock-mode for Python in Emacs?</A></H2>
 
3175
If you are using XEmacs 19.14 or later, any XEmacs 20, FSF Emacs 19.34
 
3176
or any Emacs 20, font-lock should work automatically for you if you
 
3177
are using the latest python-mode.el.
 
3178
<P>
 
3179
If you are using an older version of XEmacs or Emacs you will need 
 
3180
to put this in your .emacs file:
 
3181
<P>
 
3182
<PRE>
 
3183
        (defun my-python-mode-hook ()
 
3184
          (setq font-lock-keywords python-font-lock-keywords)
 
3185
          (font-lock-mode 1))
 
3186
        (add-hook 'python-mode-hook 'my-python-mode-hook)
 
3187
</PRE>
 
3188
<P>
 
3189
 
 
3190
<A HREF="faqw.py?req=edit&amp;file=faq04.032.htp">Edit this entry</A> /
 
3191
<A HREF="faqw.py?req=log&amp;file=faq04.032.htp">Log info</A>
 
3192
 
 
3193
/ Last changed on Mon Apr  6 16:18:46 1998 by
 
3194
<A HREF="mailto:bwarsaw@python.org">Barry Warsaw</A>
 
3195
<P>
 
3196
 
 
3197
<HR>
 
3198
<H2><A NAME="4.33">4.33. Is there a scanf() or sscanf() equivalent?</A></H2>
 
3199
Not as such.
 
3200
<P>
 
3201
For simple input parsing, the easiest approach is usually to split
 
3202
the line into whitespace-delimited words using string.split(), and to
 
3203
convert decimal strings to numeric values using int(),
 
3204
long() or float().  (Python's int() is 32-bit and its
 
3205
long() is arbitrary precision.)  string.split supports an optional
 
3206
"sep" parameter which is useful if the line uses something other
 
3207
than whitespace as a delimiter.
 
3208
<P>
 
3209
For more complicated input parsing, regular expressions (see module re)
 
3210
are better suited and more powerful than C's sscanf().
 
3211
<P>
 
3212
There's a contributed module that emulates sscanf(), by Steve Clift;
 
3213
see contrib/Misc/sscanfmodule.c of the ftp site:
 
3214
<P>
 
3215
<PRE>
 
3216
    <A HREF="http://www.python.org/ftp/python/contrib-09-Dec-1999/Misc">http://www.python.org/ftp/python/contrib-09-Dec-1999/Misc</A>/
 
3217
</PRE>
 
3218
<P>
 
3219
 
 
3220
<A HREF="faqw.py?req=edit&amp;file=faq04.033.htp">Edit this entry</A> /
 
3221
<A HREF="faqw.py?req=log&amp;file=faq04.033.htp">Log info</A>
 
3222
 
 
3223
/ Last changed on Mon Jun  3 01:07:51 2002 by
 
3224
<A HREF="mailto:neal@metaslash.com">Neal Norwitz</A>
 
3225
<P>
 
3226
 
 
3227
<HR>
 
3228
<H2><A NAME="4.34">4.34. Can I have Tk events handled while waiting for I/O?</A></H2>
 
3229
Yes, and you don't even need threads!  But you'll have to
 
3230
restructure your I/O code a bit.  Tk has the equivalent of Xt's
 
3231
XtAddInput() call, which allows you to register a callback function
 
3232
which will be called from the Tk mainloop when I/O is possible on a
 
3233
file descriptor.  Here's what you need:
 
3234
<P>
 
3235
<PRE>
 
3236
        from Tkinter import tkinter
 
3237
        tkinter.createfilehandler(file, mask, callback)
 
3238
</PRE>
 
3239
The file may be a Python file or socket object (actually, anything
 
3240
with a fileno() method), or an integer file descriptor.  The mask is
 
3241
one of the constants tkinter.READABLE or tkinter.WRITABLE.  The
 
3242
callback is called as follows:
 
3243
<P>
 
3244
<PRE>
 
3245
        callback(file, mask)
 
3246
</PRE>
 
3247
You must unregister the callback when you're done, using
 
3248
<P>
 
3249
<PRE>
 
3250
        tkinter.deletefilehandler(file)
 
3251
</PRE>
 
3252
Note: since you don't know *how many bytes* are available for reading,
 
3253
you can't use the Python file object's read or readline methods, since
 
3254
these will insist on reading a predefined number of bytes.  For
 
3255
sockets, the recv() or recvfrom() methods will work fine; for other
 
3256
files, use os.read(file.fileno(), maxbytecount).
 
3257
<P>
 
3258
 
 
3259
<A HREF="faqw.py?req=edit&amp;file=faq04.034.htp">Edit this entry</A> /
 
3260
<A HREF="faqw.py?req=log&amp;file=faq04.034.htp">Log info</A>
 
3261
<P>
 
3262
 
 
3263
<HR>
 
3264
<H2><A NAME="4.35">4.35. How do I write a function with output parameters (call by reference)?</A></H2>
 
3265
[Mark Lutz] The thing to remember is that arguments are passed by
 
3266
assignment in Python.  Since assignment just creates references to
 
3267
objects, there's no alias between an argument name in the caller and
 
3268
callee, and so no call-by-reference per se.  But you can simulate it
 
3269
in a number of ways:
 
3270
<P>
 
3271
1) By using global variables; but you probably shouldn't :-)
 
3272
<P>
 
3273
2) By passing a mutable (changeable in-place) object:
 
3274
<P>
 
3275
<PRE>
 
3276
      def func1(a):
 
3277
          a[0] = 'new-value'     # 'a' references a mutable list
 
3278
          a[1] = a[1] + 1        # changes a shared object
 
3279
</PRE>
 
3280
<PRE>
 
3281
      args = ['old-value', 99]
 
3282
      func1(args)
 
3283
      print args[0], args[1]     # output: new-value 100
 
3284
</PRE>
 
3285
3) By returning a tuple, holding the final values of arguments:
 
3286
<P>
 
3287
<PRE>
 
3288
      def func2(a, b):
 
3289
          a = 'new-value'        # a and b are local names
 
3290
          b = b + 1              # assigned to new objects
 
3291
          return a, b            # return new values
 
3292
</PRE>
 
3293
<PRE>
 
3294
      x, y = 'old-value', 99
 
3295
      x, y = func2(x, y)
 
3296
      print x, y                 # output: new-value 100
 
3297
</PRE>
 
3298
4) And other ideas that fall-out from Python's object model. For instance, it might be clearer to pass in a mutable dictionary:
 
3299
<P>
 
3300
<PRE>
 
3301
      def func3(args):
 
3302
          args['a'] = 'new-value'     # args is a mutable dictionary
 
3303
          args['b'] = args['b'] + 1   # change it in-place
 
3304
</PRE>
 
3305
<PRE>
 
3306
      args = {'a':' old-value', 'b': 99}
 
3307
      func3(args)
 
3308
      print args['a'], args['b']
 
3309
</PRE>
 
3310
5) Or bundle-up values in a class instance:
 
3311
<P>
 
3312
<PRE>
 
3313
      class callByRef:
 
3314
          def __init__(self, **args):
 
3315
              for (key, value) in args.items():
 
3316
                  setattr(self, key, value)
 
3317
</PRE>
 
3318
<PRE>
 
3319
      def func4(args):
 
3320
          args.a = 'new-value'        # args is a mutable callByRef
 
3321
          args.b = args.b + 1         # change object in-place
 
3322
</PRE>
 
3323
<PRE>
 
3324
      args = callByRef(a='old-value', b=99)
 
3325
      func4(args)
 
3326
      print args.a, args.b
 
3327
</PRE>
 
3328
<PRE>
 
3329
   But there's probably no good reason to get this complicated :-).
 
3330
</PRE>
 
3331
[Python's author favors solution 3 in most cases.]
 
3332
<P>
 
3333
 
 
3334
<A HREF="faqw.py?req=edit&amp;file=faq04.035.htp">Edit this entry</A> /
 
3335
<A HREF="faqw.py?req=log&amp;file=faq04.035.htp">Log info</A>
 
3336
 
 
3337
/ Last changed on Sun Jun  8 23:49:46 1997 by
 
3338
<A HREF="mailto:david_ascher@brown.edu">David Ascher</A>
 
3339
<P>
 
3340
 
 
3341
<HR>
 
3342
<H2><A NAME="4.36">4.36. Please explain the rules for local and global variables in Python.</A></H2>
 
3343
[Ken Manheimer] In Python, procedure variables are implicitly
 
3344
global, unless they are assigned anywhere within the block.
 
3345
In that case
 
3346
they are implicitly local, and you need to explicitly declare them as
 
3347
'global'.
 
3348
<P>
 
3349
Though a bit surprising at first, a moment's consideration explains
 
3350
this.  On one hand, requirement of 'global' for assigned vars provides
 
3351
a bar against unintended side-effects.  On the other hand, if global
 
3352
were required for all global references, you'd be using global all the
 
3353
time.  Eg, you'd have to declare as global every reference to a
 
3354
builtin function, or to a component of an imported module.  This
 
3355
clutter would defeat the usefulness of the 'global' declaration for
 
3356
identifying side-effects.
 
3357
<P>
 
3358
 
 
3359
<A HREF="faqw.py?req=edit&amp;file=faq04.036.htp">Edit this entry</A> /
 
3360
<A HREF="faqw.py?req=log&amp;file=faq04.036.htp">Log info</A>
 
3361
 
 
3362
/ Last changed on Fri Aug 28 09:53:27 1998 by
 
3363
<A HREF="mailto:guido@python.org">GvR</A>
 
3364
<P>
 
3365
 
 
3366
<HR>
 
3367
<H2><A NAME="4.37">4.37. How can I have modules that mutually import each other?</A></H2>
 
3368
Suppose you have the following modules:
 
3369
<P>
 
3370
foo.py:
 
3371
<P>
 
3372
<PRE>
 
3373
        from bar import bar_var
 
3374
        foo_var=1
 
3375
</PRE>
 
3376
bar.py:
 
3377
<P>
 
3378
<PRE>
 
3379
        from foo import foo_var
 
3380
        bar_var=2
 
3381
</PRE>
 
3382
The problem is that the above is processed by the interpreter thus:
 
3383
<P>
 
3384
<PRE>
 
3385
        main imports foo
 
3386
        Empty globals for foo are created
 
3387
        foo is compiled and starts executing
 
3388
        foo imports bar
 
3389
        Empty globals for bar are created
 
3390
        bar is compiled and starts executing
 
3391
        bar imports foo (which is a no-op since there already is a module named foo)
 
3392
        bar.foo_var = foo.foo_var
 
3393
        ...
 
3394
</PRE>
 
3395
The last step fails, because Python isn't done with interpreting foo yet and the global symbol dict for foo is still empty.
 
3396
<P>
 
3397
The same thing happens when you use "import foo", and then try to access "foo.one" in global code.
 
3398
<P>
 
3399
<P>
 
3400
There are (at least) three possible workarounds for this problem. 
 
3401
<P>
 
3402
Guido van Rossum recommends to avoid all uses of "from &lt;module&gt; import ..." (so everything from an imported module is referenced as &lt;module&gt;.&lt;name&gt;) and to place all code inside functions. Initializations of global variables and class variables should use constants or built-in functions only.
 
3403
<P>
 
3404
<P>
 
3405
Jim Roskind suggests the following order in each module:
 
3406
<P>
 
3407
<PRE>
 
3408
 exports (globals, functions, and classes that don't need imported base classes)
 
3409
 import statements
 
3410
 active code (including globals that are initialized from imported values).
 
3411
</PRE>
 
3412
Python's author doesn't like this approach much because the imports
 
3413
appear in a strange place, but has to admit that it works.
 
3414
<P>
 
3415
<P>
 
3416
<P>
 
3417
Matthias Urlichs recommends to restructure your code so that the recursive import is not necessary in the first place.
 
3418
<P>
 
3419
<P>
 
3420
These solutions are not mutually exclusive.
 
3421
<P>
 
3422
 
 
3423
<A HREF="faqw.py?req=edit&amp;file=faq04.037.htp">Edit this entry</A> /
 
3424
<A HREF="faqw.py?req=log&amp;file=faq04.037.htp">Log info</A>
 
3425
 
 
3426
/ Last changed on Mon Jun  3 06:52:51 2002 by
 
3427
<A HREF="mailto:smurf@noris.de">Matthias Urlichs</A>
 
3428
<P>
 
3429
 
 
3430
<HR>
 
3431
<H2><A NAME="4.38">4.38. How do I copy an object in Python?</A></H2>
 
3432
Try copy.copy() or copy.deepcopy() for the general case. Not all objects can be copied, but most can.
 
3433
<P>
 
3434
Dictionaries have a copy method. Sequences can be copied by slicing:
 
3435
<PRE>
 
3436
 new_l = l[:]
 
3437
</PRE>
 
3438
<P>
 
3439
 
 
3440
<A HREF="faqw.py?req=edit&amp;file=faq04.038.htp">Edit this entry</A> /
 
3441
<A HREF="faqw.py?req=log&amp;file=faq04.038.htp">Log info</A>
 
3442
 
 
3443
/ Last changed on Thu Mar 21 05:40:26 2002 by
 
3444
<A HREF="mailto:erno-pyfaq@erno.iki.fi">Erno Kuusela</A>
 
3445
<P>
 
3446
 
 
3447
<HR>
 
3448
<H2><A NAME="4.39">4.39. How to implement persistent objects in Python? (Persistent == automatically saved to and restored from disk.)</A></H2>
 
3449
The library module "pickle" now solves this in a very general way
 
3450
(though you still can't store things like open files, sockets or
 
3451
windows), and the library module "shelve" uses pickle and (g)dbm to
 
3452
create persistent mappings containing arbitrary Python objects.
 
3453
For possibly better performance also look for the latest version
 
3454
of the relatively recent cPickle module.
 
3455
<P>
 
3456
A more awkward way of doing things is to use pickle's little sister,
 
3457
marshal.  The marshal module provides very fast ways to store
 
3458
noncircular basic Python types to files and strings, and back again.
 
3459
Although marshal does not do fancy things like store instances or
 
3460
handle shared references properly, it does run extremely fast.  For
 
3461
example loading a half megabyte of data may take less than a
 
3462
third of a second (on some machines).  This often beats doing
 
3463
something more complex and general such as using gdbm with
 
3464
pickle/shelve.
 
3465
<P>
 
3466
 
 
3467
<A HREF="faqw.py?req=edit&amp;file=faq04.039.htp">Edit this entry</A> /
 
3468
<A HREF="faqw.py?req=log&amp;file=faq04.039.htp">Log info</A>
 
3469
 
 
3470
/ Last changed on Sun Jun  8 22:59:00 1997 by
 
3471
<A HREF="mailto:david_ascher@brown.edu">David Ascher</A>
 
3472
<P>
 
3473
 
 
3474
<HR>
 
3475
<H2><A NAME="4.40">4.40. I try to use __spam and I get an error about _SomeClassName__spam.</A></H2>
 
3476
Variables with double leading underscore are "mangled" to provide a
 
3477
simple but effective way to define class private variables.  See the
 
3478
chapter "New in Release 1.4" in the Python Tutorial.
 
3479
<P>
 
3480
 
 
3481
<A HREF="faqw.py?req=edit&amp;file=faq04.040.htp">Edit this entry</A> /
 
3482
<A HREF="faqw.py?req=log&amp;file=faq04.040.htp">Log info</A>
 
3483
<P>
 
3484
 
 
3485
<HR>
 
3486
<H2><A NAME="4.41">4.41. How do I delete a file? And other file questions.</A></H2>
 
3487
Use os.remove(filename) or os.unlink(filename); for documentation,
 
3488
see the posix section of the library manual.  They are the same,
 
3489
unlink() is simply the Unix name for this function.  In earlier
 
3490
versions of Python, only os.unlink() was available.
 
3491
<P>
 
3492
To remove a directory, use os.rmdir(); use os.mkdir() to create one.
 
3493
<P>
 
3494
To rename a file, use os.rename().
 
3495
<P>
 
3496
To truncate a file, open it using f = open(filename, "r+"), and use
 
3497
f.truncate(offset); offset defaults to the current seek position.
 
3498
(The "r+" mode opens the file for reading and writing.)
 
3499
There's also os.ftruncate(fd, offset) for files opened with os.open()
 
3500
-- for advanced Unix hacks only.
 
3501
<P>
 
3502
The shutil module also contains a number of functions to work on files
 
3503
including copyfile, copytree, and rmtree amongst others.
 
3504
<P>
 
3505
 
 
3506
<A HREF="faqw.py?req=edit&amp;file=faq04.041.htp">Edit this entry</A> /
 
3507
<A HREF="faqw.py?req=log&amp;file=faq04.041.htp">Log info</A>
 
3508
 
 
3509
/ Last changed on Thu Dec 28 12:30:01 2000 by
 
3510
<A HREF="mailto:pbjorn@uswest.net">Bjorn Pettersen</A>
 
3511
<P>
 
3512
 
 
3513
<HR>
 
3514
<H2><A NAME="4.42">4.42. How to modify urllib or httplib to support HTTP/1.1?</A></H2>
 
3515
Recent versions of Python (2.0 and onwards) support HTTP/1.1 natively.
 
3516
<P>
 
3517
 
 
3518
<A HREF="faqw.py?req=edit&amp;file=faq04.042.htp">Edit this entry</A> /
 
3519
<A HREF="faqw.py?req=log&amp;file=faq04.042.htp">Log info</A>
 
3520
 
 
3521
/ Last changed on Tue Jan  2 02:56:56 2001 by
 
3522
<A HREF="mailto:moshez@zadka.site.co.il">Moshe Zadka</A>
 
3523
<P>
 
3524
 
 
3525
<HR>
 
3526
<H2><A NAME="4.43">4.43. Unexplicable syntax errors in compile() or exec.</A></H2>
 
3527
When a statement suite (as opposed to an expression) is compiled by
 
3528
compile(), exec or execfile(), it <I>must</I> end in a newline.  In some
 
3529
cases, when the source ends in an indented block it appears that at
 
3530
least two newlines are required.
 
3531
<P>
 
3532
 
 
3533
<A HREF="faqw.py?req=edit&amp;file=faq04.043.htp">Edit this entry</A> /
 
3534
<A HREF="faqw.py?req=log&amp;file=faq04.043.htp">Log info</A>
 
3535
<P>
 
3536
 
 
3537
<HR>
 
3538
<H2><A NAME="4.44">4.44. How do I convert a string to a number?</A></H2>
 
3539
For integers, use the built-in int() function, e.g. int('144') == 144.  Similarly, long() converts from string to long integer, e.g. long('144') == 144L; and float() to floating-point, e.g. float('144') == 144.0.
 
3540
<P>
 
3541
Note that these are restricted to decimal interpretation, so 
 
3542
that int('0144') == 144 and int('0x144') raises ValueError. For Python
 
3543
2.0 int takes the base to convert from as a second optional argument, so
 
3544
int('0x144', 16) == 324.
 
3545
<P>
 
3546
For greater flexibility, or before Python 1.5, import the module 
 
3547
string and use the string.atoi() function for integers, 
 
3548
string.atol() for long integers, or string.atof() for 
 
3549
floating-point.  E.g., 
 
3550
string.atoi('100', 16) == string.atoi('0x100', 0) == 256.  
 
3551
See the library reference manual section for the string module for 
 
3552
more details.
 
3553
<P>
 
3554
While you could use the built-in function eval() instead of
 
3555
any of those, this is not recommended, because someone could pass you
 
3556
a Python expression that might have unwanted side effects (like
 
3557
reformatting your disk). It also has the effect of interpreting numbers
 
3558
as Python expressions, so that e.g. eval('09') gives a syntax error
 
3559
since Python regards numbers starting with '0' as octal (base 8).
 
3560
<P>
 
3561
 
 
3562
<A HREF="faqw.py?req=edit&amp;file=faq04.044.htp">Edit this entry</A> /
 
3563
<A HREF="faqw.py?req=log&amp;file=faq04.044.htp">Log info</A>
 
3564
 
 
3565
/ Last changed on Thu Dec 28 12:37:34 2000 by
 
3566
<A HREF="mailto:pbjorn@uswest.net">Bjorn Pettersen</A>
 
3567
<P>
 
3568
 
 
3569
<HR>
 
3570
<H2><A NAME="4.45">4.45. How do I convert a number to a string?</A></H2>
 
3571
To convert, e.g., the number 144 to the string '144', use the
 
3572
built-in function repr() or the backquote notation (these are
 
3573
equivalent).  If you want a hexadecimal or octal representation, use
 
3574
the built-in functions hex() or oct(), respectively.  For fancy
 
3575
formatting, use the % operator on strings, just like C printf formats,
 
3576
e.g. "%04d" % 144 yields '0144' and "%.3f" % (1/3.0) yields '0.333'.
 
3577
See the library reference manual for details.
 
3578
<P>
 
3579
 
 
3580
<A HREF="faqw.py?req=edit&amp;file=faq04.045.htp">Edit this entry</A> /
 
3581
<A HREF="faqw.py?req=log&amp;file=faq04.045.htp">Log info</A>
 
3582
<P>
 
3583
 
 
3584
<HR>
 
3585
<H2><A NAME="4.46">4.46. How do I copy a file?</A></H2>
 
3586
There's the shutil module which contains a copyfile()
 
3587
function that implements a copy loop;
 
3588
it isn't good enough for the Macintosh, though:
 
3589
it doesn't copy the resource fork and Finder info.
 
3590
<P>
 
3591
 
 
3592
<A HREF="faqw.py?req=edit&amp;file=faq04.046.htp">Edit this entry</A> /
 
3593
<A HREF="faqw.py?req=log&amp;file=faq04.046.htp">Log info</A>
 
3594
 
 
3595
/ Last changed on Tue Jan  2 02:59:40 2001 by
 
3596
<A HREF="mailto:moshez@zadka.site.co.il">Moshe Zadka</A>
 
3597
<P>
 
3598
 
 
3599
<HR>
 
3600
<H2><A NAME="4.47">4.47. How do I check if an object is an instance of a given class or of a subclass of it?</A></H2>
 
3601
If you are developing the classes from scratch it might be better to
 
3602
program in a more proper object-oriented style -- instead of doing a different
 
3603
thing based on class membership, why not use a method and define the
 
3604
method differently in different classes?
 
3605
<P>
 
3606
However, there are some legitimate situations
 
3607
where you need to test for class membership.
 
3608
<P>
 
3609
In Python 1.5, you can use the built-in function isinstance(obj, cls).
 
3610
<P>
 
3611
The following approaches can be used with earlier Python versions:
 
3612
<P>
 
3613
An unobvious method is to raise the object
 
3614
as an exception and to try to catch the exception with the class you're
 
3615
testing for:
 
3616
<P>
 
3617
<PRE>
 
3618
        def is_instance_of(the_instance, the_class):
 
3619
            try:
 
3620
                raise the_instance
 
3621
            except the_class:
 
3622
                return 1
 
3623
            except:
 
3624
                return 0
 
3625
</PRE>
 
3626
This technique can be used to distinguish "subclassness"
 
3627
from a collection of classes as well
 
3628
<P>
 
3629
<PRE>
 
3630
                try:
 
3631
                              raise the_instance
 
3632
                except Audible:
 
3633
                              the_instance.play(largo)
 
3634
                except Visual:
 
3635
                              the_instance.display(gaudy)
 
3636
                except Olfactory:
 
3637
                              sniff(the_instance)
 
3638
                except:
 
3639
                              raise ValueError, "dunno what to do with this!"
 
3640
</PRE>
 
3641
This uses the fact that exception catching tests for class or subclass
 
3642
membership.
 
3643
<P>
 
3644
A different approach is to test for the presence of a class attribute that
 
3645
is presumably unique for the given class.  For instance:
 
3646
<P>
 
3647
<PRE>
 
3648
        class MyClass:
 
3649
            ThisIsMyClass = 1
 
3650
            ...
 
3651
</PRE>
 
3652
<PRE>
 
3653
        def is_a_MyClass(the_instance):
 
3654
            return hasattr(the_instance, 'ThisIsMyClass')
 
3655
</PRE>
 
3656
This version is easier to inline, and probably faster (inlined it
 
3657
is definitely faster).  The disadvantage is that someone else could cheat:
 
3658
<P>
 
3659
<PRE>
 
3660
        class IntruderClass:
 
3661
            ThisIsMyClass = 1    # Masquerade as MyClass
 
3662
            ...
 
3663
</PRE>
 
3664
but this may be seen as a feature (anyway, there are plenty of other ways
 
3665
to cheat in Python).  Another disadvantage is that the class must be
 
3666
prepared for the membership test.  If you do not "control the
 
3667
source code" for the class it may not be advisable to modify the
 
3668
class to support testability.
 
3669
<P>
 
3670
 
 
3671
<A HREF="faqw.py?req=edit&amp;file=faq04.047.htp">Edit this entry</A> /
 
3672
<A HREF="faqw.py?req=log&amp;file=faq04.047.htp">Log info</A>
 
3673
 
 
3674
/ Last changed on Fri Jan  2 15:16:04 1998 by
 
3675
<A HREF="mailto:guido@python.org">GvR</A>
 
3676
<P>
 
3677
 
 
3678
<HR>
 
3679
<H2><A NAME="4.48">4.48. What is delegation?</A></H2>
 
3680
Delegation refers to an object oriented technique Python programmers
 
3681
may implement with particular ease.  Consider the following:
 
3682
<P>
 
3683
<PRE>
 
3684
  from string import upper
 
3685
</PRE>
 
3686
<PRE>
 
3687
  class UpperOut:
 
3688
        def __init__(self, outfile):
 
3689
              self.__outfile = outfile
 
3690
        def write(self, str):
 
3691
              self.__outfile.write( upper(str) )
 
3692
        def __getattr__(self, name):
 
3693
              return getattr(self.__outfile, name)
 
3694
</PRE>
 
3695
Here the UpperOut class redefines the write method
 
3696
to convert the argument string to upper case before
 
3697
calling the underlying self.__outfile.write method, but
 
3698
all other methods are delegated to the underlying 
 
3699
self.__outfile object.  The delegation is accomplished
 
3700
via the "magic" __getattr__ method.  Please see the
 
3701
language reference for more information on the use
 
3702
of this method.
 
3703
<P>
 
3704
Note that for more general cases delegation can
 
3705
get trickier. Particularly when attributes must be set
 
3706
as well as gotten the class must define a __settattr__
 
3707
method too, and it must do so carefully.
 
3708
<P>
 
3709
The basic implementation of __setattr__ is roughly
 
3710
equivalent to the following:
 
3711
<P>
 
3712
<PRE>
 
3713
   class X:
 
3714
        ...
 
3715
        def __setattr__(self, name, value):
 
3716
             self.__dict__[name] = value
 
3717
        ...
 
3718
</PRE>
 
3719
Most __setattr__ implementations must modify
 
3720
self.__dict__ to store local state for self without
 
3721
causing an infinite recursion.
 
3722
<P>
 
3723
 
 
3724
<A HREF="faqw.py?req=edit&amp;file=faq04.048.htp">Edit this entry</A> /
 
3725
<A HREF="faqw.py?req=log&amp;file=faq04.048.htp">Log info</A>
 
3726
 
 
3727
/ Last changed on Wed Aug 13 07:11:24 1997 by
 
3728
<A HREF="mailto:aaron_watters@msn.com">aaron watters</A>
 
3729
<P>
 
3730
 
 
3731
<HR>
 
3732
<H2><A NAME="4.49">4.49. How do I test a Python program or component.</A></H2>
 
3733
We presume for the purposes of this question you are interested
 
3734
in standalone testing, rather than testing your components inside
 
3735
a testing framework. The best-known testing framework for Python
 
3736
is the PyUnit module, maintained at
 
3737
<P>
 
3738
<PRE>
 
3739
    <A HREF="http://pyunit.sourceforge.net">http://pyunit.sourceforge.net</A>/
 
3740
</PRE>
 
3741
For standalone testing, it helps to write the program so that
 
3742
it may be easily tested by using good modular design.
 
3743
In particular your program
 
3744
should have almost all functionality encapsulated in either functions
 
3745
or class methods -- and this sometimes has the surprising and
 
3746
delightful effect of making the program run faster (because
 
3747
local variable accesses are faster than global accesses).
 
3748
Furthermore the program should avoid depending on mutating
 
3749
global variables, since this makes testing much more difficult to do.
 
3750
<P>
 
3751
The "global main logic" of your program may be as simple
 
3752
as
 
3753
<P>
 
3754
<PRE>
 
3755
  if __name__=="__main__":
 
3756
       main_logic()
 
3757
</PRE>
 
3758
at the bottom of the main module of your program.
 
3759
<P>
 
3760
Once your program is organized as a tractable collection
 
3761
of functions and class behaviours you should write test
 
3762
functions that exercise the behaviours.  A test suite 
 
3763
can be associated with each module which automates
 
3764
a sequence of tests.  This sounds like a lot of work, but
 
3765
since Python is so terse and flexible it's surprisingly easy.
 
3766
You can make coding much more pleasant and fun by
 
3767
writing your test functions in parallel with the "production
 
3768
code", since this makes it easy to find bugs and even
 
3769
design flaws earlier.
 
3770
<P>
 
3771
"Support modules" that are not intended to be the main
 
3772
module of a program may include a "test script interpretation"
 
3773
which invokes a self test of the module.
 
3774
<P>
 
3775
<PRE>
 
3776
   if __name__ == "__main__":
 
3777
      self_test()
 
3778
</PRE>
 
3779
Even programs that interact with complex external
 
3780
interfaces may be tested when the external interfaces are
 
3781
unavailable by using "fake" interfaces implemented in
 
3782
Python.  For an example of a "fake" interface, the following
 
3783
class defines (part of) a "fake" file interface:
 
3784
<P>
 
3785
<PRE>
 
3786
 import string
 
3787
 testdata = "just a random sequence of characters"
 
3788
</PRE>
 
3789
<PRE>
 
3790
 class FakeInputFile:
 
3791
   data = testdata
 
3792
   position = 0
 
3793
   closed = 0
 
3794
</PRE>
 
3795
<PRE>
 
3796
   def read(self, n=None):
 
3797
       self.testclosed()
 
3798
       p = self.position
 
3799
       if n is None:
 
3800
          result= self.data[p:]
 
3801
       else:
 
3802
          result= self.data[p: p+n]
 
3803
       self.position = p + len(result)
 
3804
       return result
 
3805
</PRE>
 
3806
<PRE>
 
3807
   def seek(self, n, m=0):
 
3808
       self.testclosed()
 
3809
       last = len(self.data)
 
3810
       p = self.position
 
3811
       if m==0: 
 
3812
          final=n
 
3813
       elif m==1:
 
3814
          final=n+p
 
3815
       elif m==2:
 
3816
          final=len(self.data)+n
 
3817
       else:
 
3818
          raise ValueError, "bad m"
 
3819
       if final&lt;0:
 
3820
          raise IOError, "negative seek"
 
3821
       self.position = final
 
3822
</PRE>
 
3823
<PRE>
 
3824
   def isatty(self):
 
3825
       return 0
 
3826
</PRE>
 
3827
<PRE>
 
3828
   def tell(self):
 
3829
       return self.position
 
3830
</PRE>
 
3831
<PRE>
 
3832
   def close(self):
 
3833
       self.closed = 1
 
3834
</PRE>
 
3835
<PRE>
 
3836
   def testclosed(self):
 
3837
       if self.closed:
 
3838
          raise IOError, "file closed"
 
3839
</PRE>
 
3840
Try f=FakeInputFile() and test out its operations.
 
3841
<P>
 
3842
 
 
3843
<A HREF="faqw.py?req=edit&amp;file=faq04.049.htp">Edit this entry</A> /
 
3844
<A HREF="faqw.py?req=log&amp;file=faq04.049.htp">Log info</A>
 
3845
 
 
3846
/ Last changed on Mon Jun  3 01:12:10 2002 by
 
3847
<A HREF="mailto:neal@metaslash.com">Neal Norwitz</A>
 
3848
<P>
 
3849
 
 
3850
<HR>
 
3851
<H2><A NAME="4.50">4.50. My multidimensional list (array) is broken! What gives?</A></H2>
 
3852
You probably tried to make a multidimensional array like this.
 
3853
<P>
 
3854
<PRE>
 
3855
   A = [[None] * 2] * 3
 
3856
</PRE>
 
3857
This makes a list containing 3 references to the same list of length
 
3858
two.  Changes to one row will show in all rows, which is probably not
 
3859
what you want.  The following works much better:
 
3860
<P>
 
3861
<PRE>
 
3862
   A = [None]*3
 
3863
   for i in range(3):
 
3864
        A[i] = [None] * 2
 
3865
</PRE>
 
3866
This generates a list containing 3 different lists of length two.
 
3867
<P>
 
3868
If you feel weird, you can also do it in the following way:
 
3869
<P>
 
3870
<PRE>
 
3871
   w, h = 2, 3
 
3872
   A = map(lambda i,w=w: [None] * w, range(h))
 
3873
</PRE>
 
3874
For Python 2.0 the above can be spelled using a list comprehension:
 
3875
<P>
 
3876
<PRE>
 
3877
   w,h = 2,3
 
3878
   A = [ [None]*w for i in range(h) ]
 
3879
</PRE>
 
3880
<P>
 
3881
 
 
3882
<A HREF="faqw.py?req=edit&amp;file=faq04.050.htp">Edit this entry</A> /
 
3883
<A HREF="faqw.py?req=log&amp;file=faq04.050.htp">Log info</A>
 
3884
 
 
3885
/ Last changed on Thu Dec 28 12:18:35 2000 by
 
3886
<A HREF="mailto:pbjorn@uswest.net">Bjorn Pettersen</A>
 
3887
<P>
 
3888
 
 
3889
<HR>
 
3890
<H2><A NAME="4.51">4.51. I want to do a complicated sort: can you do a Schwartzian Transform in Python?</A></H2>
 
3891
Yes, and in Python you only have to write it once:
 
3892
<P>
 
3893
<PRE>
 
3894
 def st(List, Metric):
 
3895
     def pairing(element, M = Metric):
 
3896
           return (M(element), element)
 
3897
     paired = map(pairing, List)
 
3898
     paired.sort()
 
3899
     return map(stripit, paired)
 
3900
</PRE>
 
3901
<PRE>
 
3902
 def stripit(pair):
 
3903
     return pair[1]
 
3904
</PRE>
 
3905
This technique, attributed to Randal Schwartz, sorts the elements
 
3906
of a list by a metric which maps each element to its "sort value".
 
3907
For example, if L is a list of string then
 
3908
<P>
 
3909
<PRE>
 
3910
   import string
 
3911
   Usorted = st(L, string.upper)
 
3912
</PRE>
 
3913
<PRE>
 
3914
   def intfield(s):
 
3915
         return string.atoi( string.strip(s[10:15] ) )
 
3916
</PRE>
 
3917
<PRE>
 
3918
   Isorted = st(L, intfield)
 
3919
</PRE>
 
3920
Usorted gives the elements of L sorted as if they were upper
 
3921
case, and Isorted gives the elements of L sorted by the integer
 
3922
values that appear in the string slices starting at position 10
 
3923
and ending at position 15. In Python 2.0 this can be done more
 
3924
naturally with list comprehensions:
 
3925
<P>
 
3926
<PRE>
 
3927
  tmp1 = [ (x.upper(), x) for x in L ] # Schwartzian transform
 
3928
  tmp1.sort()
 
3929
  Usorted = [ x[1] for x in tmp1 ]
 
3930
</PRE>
 
3931
<PRE>
 
3932
  tmp2 = [ (int(s[10:15]), s) for s in L ] # Schwartzian transform
 
3933
  tmp2.sort()
 
3934
  Isorted = [ x[1] for x in tmp2 ]
 
3935
</PRE>
 
3936
<P>
 
3937
Note that Isorted may also be computed by
 
3938
<P>
 
3939
<PRE>
 
3940
   def Icmp(s1, s2):
 
3941
         return cmp( intfield(s1), intfield(s2) )
 
3942
</PRE>
 
3943
<PRE>
 
3944
   Isorted = L[:]
 
3945
   Isorted.sort(Icmp)
 
3946
</PRE>
 
3947
but since this method computes intfield many times for each
 
3948
element of L, it is slower than the Schwartzian Transform.
 
3949
<P>
 
3950
 
 
3951
<A HREF="faqw.py?req=edit&amp;file=faq04.051.htp">Edit this entry</A> /
 
3952
<A HREF="faqw.py?req=log&amp;file=faq04.051.htp">Log info</A>
 
3953
 
 
3954
/ Last changed on Sat Jun  1 19:18:46 2002 by
 
3955
<A HREF="mailto:neal@metaslash.com">Neal Norwitz</A>
 
3956
<P>
 
3957
 
 
3958
<HR>
 
3959
<H2><A NAME="4.52">4.52. How to convert between tuples and lists?</A></H2>
 
3960
The function tuple(seq) converts any sequence into a tuple with
 
3961
the same items in the same order.
 
3962
For example, tuple([1, 2, 3]) yields (1, 2, 3) and tuple('abc')
 
3963
yields ('a', 'b', 'c').  If the argument is
 
3964
a tuple, it does not make a copy but returns the same object, so
 
3965
it is cheap to call tuple() when you aren't sure that an object
 
3966
is already a tuple.
 
3967
<P>
 
3968
The function list(seq) converts any sequence into a list with
 
3969
the same items in the same order.
 
3970
For example, list((1, 2, 3)) yields [1, 2, 3] and list('abc')
 
3971
yields ['a', 'b', 'c'].  If the argument is a list,
 
3972
it makes a copy just like seq[:] would.
 
3973
<P>
 
3974
 
 
3975
<A HREF="faqw.py?req=edit&amp;file=faq04.052.htp">Edit this entry</A> /
 
3976
<A HREF="faqw.py?req=log&amp;file=faq04.052.htp">Log info</A>
 
3977
 
 
3978
/ Last changed on Sun Jun 14 14:18:53 1998 by
 
3979
<A HREF="mailto:tim_one@email.msn.com">Tim Peters</A>
 
3980
<P>
 
3981
 
 
3982
<HR>
 
3983
<H2><A NAME="4.53">4.53. Files retrieved with urllib contain leading garbage that looks like email headers.</A></H2>
 
3984
<I>Extremely</I> old  versions of Python supplied libraries which
 
3985
did not support HTTP/1.1; the vanilla httplib in Python 1.4
 
3986
only recognized HTTP/1.0.  In Python 2.0 full HTTP/1.1 support is included.
 
3987
<P>
 
3988
 
 
3989
<A HREF="faqw.py?req=edit&amp;file=faq04.053.htp">Edit this entry</A> /
 
3990
<A HREF="faqw.py?req=log&amp;file=faq04.053.htp">Log info</A>
 
3991
 
 
3992
/ Last changed on Mon Jan  8 17:26:18 2001 by
 
3993
<A HREF="mailto:sholden@holdenweb.com">Steve Holden</A>
 
3994
<P>
 
3995
 
 
3996
<HR>
 
3997
<H2><A NAME="4.54">4.54. How do I get a list of all instances of a given class?</A></H2>
 
3998
Python does not keep track of all instances of a class (or of a
 
3999
built-in type).
 
4000
<P>
 
4001
You can program the class's constructor to keep track of all
 
4002
instances, but unless you're very clever, this has the disadvantage
 
4003
that the instances never get deleted,because your list of all
 
4004
instances keeps a reference to them.
 
4005
<P>
 
4006
(The trick is to regularly inspect the reference counts of the
 
4007
instances you've retained, and if the reference count is below a
 
4008
certain level, remove it from the list.  Determining that level is
 
4009
tricky -- it's definitely larger than 1.)
 
4010
<P>
 
4011
 
 
4012
<A HREF="faqw.py?req=edit&amp;file=faq04.054.htp">Edit this entry</A> /
 
4013
<A HREF="faqw.py?req=log&amp;file=faq04.054.htp">Log info</A>
 
4014
 
 
4015
/ Last changed on Tue May 27 23:52:16 1997 by
 
4016
<A HREF="mailto:guido@cnri.reston.va.us">GvR</A>
 
4017
<P>
 
4018
 
 
4019
<HR>
 
4020
<H2><A NAME="4.55">4.55. A regular expression fails with regex.error: match failure.</A></H2>
 
4021
This is usually caused by too much backtracking; the regular
 
4022
expression engine has a fixed size stack which holds at most 4000
 
4023
backtrack points.  Every character matched by e.g. ".*" accounts for a
 
4024
backtrack point, so even a simple search like
 
4025
<P>
 
4026
<PRE>
 
4027
  regex.match('.*x',"x"*5000)
 
4028
</PRE>
 
4029
will fail.
 
4030
<P>
 
4031
This is fixed in the re module introduced with 
 
4032
Python 1.5; consult the Library Reference section on re for more information.
 
4033
<P>
 
4034
 
 
4035
<A HREF="faqw.py?req=edit&amp;file=faq04.055.htp">Edit this entry</A> /
 
4036
<A HREF="faqw.py?req=log&amp;file=faq04.055.htp">Log info</A>
 
4037
 
 
4038
/ Last changed on Thu Jul 30 12:35:49 1998 by
 
4039
<A HREF="mailto:akuchlin@cnri.reston.va.us">A.M. Kuchling</A>
 
4040
<P>
 
4041
 
 
4042
<HR>
 
4043
<H2><A NAME="4.56">4.56. I can't get signal handlers to work.</A></H2>
 
4044
The most common problem is that the signal handler is declared
 
4045
with the wrong argument list.  It is called as
 
4046
<P>
 
4047
<PRE>
 
4048
        handler(signum, frame)
 
4049
</PRE>
 
4050
so it should be declared with two arguments:
 
4051
<P>
 
4052
<PRE>
 
4053
        def handler(signum, frame):
 
4054
                ...
 
4055
</PRE>
 
4056
<P>
 
4057
 
 
4058
<A HREF="faqw.py?req=edit&amp;file=faq04.056.htp">Edit this entry</A> /
 
4059
<A HREF="faqw.py?req=log&amp;file=faq04.056.htp">Log info</A>
 
4060
 
 
4061
/ Last changed on Wed May 28 09:29:08 1997 by
 
4062
<A HREF="mailto:guido@cnri.reston.va.us">GvR</A>
 
4063
<P>
 
4064
 
 
4065
<HR>
 
4066
<H2><A NAME="4.57">4.57. I can't use a global variable in a function? Help!</A></H2>
 
4067
Did you do something like this?
 
4068
<P>
 
4069
<PRE>
 
4070
   x = 1 # make a global
 
4071
</PRE>
 
4072
<PRE>
 
4073
   def f():
 
4074
         print x # try to print the global
 
4075
         ...
 
4076
         for j in range(100):
 
4077
              if q>3:
 
4078
                 x=4
 
4079
</PRE>
 
4080
Any variable assigned in a function is local to that function.
 
4081
unless it is specifically declared global. Since a value is bound
 
4082
to x as the last statement of the function body, the compiler
 
4083
assumes that x is local. Consequently the "print x"
 
4084
attempts to print an uninitialized local variable and will
 
4085
trigger a NameError.
 
4086
<P>
 
4087
In such cases the solution is to insert an explicit global
 
4088
declaration at the start of the function, making it
 
4089
<P>
 
4090
<P>
 
4091
<PRE>
 
4092
   def f():
 
4093
         global x
 
4094
         print x # try to print the global
 
4095
         ...
 
4096
         for j in range(100):
 
4097
              if q>3:
 
4098
                 x=4
 
4099
</PRE>
 
4100
<P>
 
4101
In this case, all references to x are interpreted as references
 
4102
to the x from the module namespace.
 
4103
<P>
 
4104
 
 
4105
<A HREF="faqw.py?req=edit&amp;file=faq04.057.htp">Edit this entry</A> /
 
4106
<A HREF="faqw.py?req=log&amp;file=faq04.057.htp">Log info</A>
 
4107
 
 
4108
/ Last changed on Mon Feb 12 15:52:12 2001 by
 
4109
<A HREF="mailto:sholden@holdenweb.com">Steve Holden</A>
 
4110
<P>
 
4111
 
 
4112
<HR>
 
4113
<H2><A NAME="4.58">4.58. What's a negative index? Why doesn't list.insert() use them?</A></H2>
 
4114
Python sequences are indexed with positive numbers and
 
4115
negative numbers.  For positive numbers 0 is the first index
 
4116
1 is the second index and so forth.  For negative indices -1
 
4117
is the last index and -2 is the pentultimate (next to last) index
 
4118
and so forth.  Think of seq[-n] as the same as seq[len(seq)-n].
 
4119
<P>
 
4120
Using negative indices can be very convenient.  For example
 
4121
if the string Line ends in a newline then Line[:-1] is all of Line except
 
4122
the newline.
 
4123
<P>
 
4124
Sadly the list builtin method L.insert does not observe negative
 
4125
indices.  This feature could be considered a mistake but since
 
4126
existing programs depend on this feature it may stay around 
 
4127
forever.  L.insert for negative indices inserts at the start of the
 
4128
list.  To get "proper" negative index behaviour use L[n:n] = [x]
 
4129
in place of the insert method.
 
4130
<P>
 
4131
 
 
4132
<A HREF="faqw.py?req=edit&amp;file=faq04.058.htp">Edit this entry</A> /
 
4133
<A HREF="faqw.py?req=log&amp;file=faq04.058.htp">Log info</A>
 
4134
 
 
4135
/ Last changed on Wed Aug 13 07:03:18 1997 by
 
4136
<A HREF="mailto:aaron_watters@msn.com">aaron watters</A>
 
4137
<P>
 
4138
 
 
4139
<HR>
 
4140
<H2><A NAME="4.59">4.59. How can I sort one list by values from another list?</A></H2>
 
4141
You can sort lists of  tuples.
 
4142
<P>
 
4143
<PRE>
 
4144
  >>> list1 = ["what", "I'm", "sorting", "by"]
 
4145
  >>> list2 = ["something", "else", "to", "sort"]
 
4146
  >>> pairs = map(None, list1, list2)
 
4147
  >>> pairs
 
4148
  [('what', 'something'), ("I'm", 'else'), ('sorting', 'to'), ('by', 'sort')]
 
4149
  >>> pairs.sort()
 
4150
  >>> pairs
 
4151
  [("I'm", 'else'), ('by', 'sort'), ('sorting', 'to'), ('what', 'something')]
 
4152
  >>> result = pairs[:]
 
4153
  >>> for i in xrange(len(result)): result[i] = result[i][1]
 
4154
  ...
 
4155
  >>> result
 
4156
  ['else', 'sort', 'to', 'something']
 
4157
</PRE>
 
4158
And if you didn't understand the question, please see the
 
4159
example above ;c).  Note that "I'm" sorts before "by" because
 
4160
uppercase "I" comes before lowercase "b" in the ascii order.
 
4161
Also see 4.51.
 
4162
<P>
 
4163
In Python 2.0 this can be done like:
 
4164
<P>
 
4165
<PRE>
 
4166
 >>> list1 = ["what", "I'm", "sorting", "by"]
 
4167
 >>> list2 = ["something", "else", "to", "sort"]
 
4168
 >>> pairs = zip(list1, list2)
 
4169
 >>> pairs
 
4170
 [('what', 'something'), ("I'm", 'else'), ('sorting', 'to'), ('by', 'sort')]
 
4171
 >>> pairs.sort()
 
4172
 >>> result = [ x[1] for x in pairs ]
 
4173
 >>> result
 
4174
 ['else', 'sort', 'to', 'something']
 
4175
</PRE>
 
4176
[Followup]
 
4177
<P>
 
4178
Someone asked, why not this for the last steps:
 
4179
<P>
 
4180
<PRE>
 
4181
  result = []
 
4182
  for p in pairs: result.append(p[1])
 
4183
</PRE>
 
4184
This is much more legible.  However, a quick test shows that
 
4185
it is almost twice as slow for long lists.  Why?  First of all,
 
4186
the append() operation has to reallocate memory, and while it
 
4187
uses some tricks to avoid doing that each time, it still has
 
4188
to do it occasionally, and apparently that costs quite a bit.
 
4189
Second, the expression "result.append" requires an extra
 
4190
attribute lookup.  The attribute lookup could be done away
 
4191
with by rewriting as follows:
 
4192
<P>
 
4193
<PRE>
 
4194
  result = []
 
4195
  append = result.append
 
4196
  for p in pairs: append(p[1])
 
4197
</PRE>
 
4198
which gains back some speed, but is still considerably slower
 
4199
than the original solution, and hardly less convoluted.
 
4200
<P>
 
4201
 
 
4202
<A HREF="faqw.py?req=edit&amp;file=faq04.059.htp">Edit this entry</A> /
 
4203
<A HREF="faqw.py?req=log&amp;file=faq04.059.htp">Log info</A>
 
4204
 
 
4205
/ Last changed on Thu Dec 28 12:56:35 2000 by
 
4206
<A HREF="mailto:pbjorn@uswest.net">Bjorn Pettersen</A>
 
4207
<P>
 
4208
 
 
4209
<HR>
 
4210
<H2><A NAME="4.60">4.60. Why doesn't dir() work on builtin types like files and lists?</A></H2>
 
4211
It does starting with Python 1.5.
 
4212
<P>
 
4213
Using 1.4, you can find out which methods a given object supports
 
4214
by looking at its __methods__ attribute:
 
4215
<P>
 
4216
<PRE>
 
4217
    >>> List = []
 
4218
    >>> List.__methods__
 
4219
    ['append', 'count', 'index', 'insert', 'remove', 'reverse', 'sort']
 
4220
</PRE>
 
4221
<P>
 
4222
 
 
4223
<A HREF="faqw.py?req=edit&amp;file=faq04.060.htp">Edit this entry</A> /
 
4224
<A HREF="faqw.py?req=log&amp;file=faq04.060.htp">Log info</A>
 
4225
 
 
4226
/ Last changed on Thu Sep 16 14:56:42 1999 by
 
4227
<A HREF="mailto:skip@mojam.com">Skip Montanaro</A>
 
4228
<P>
 
4229
 
 
4230
<HR>
 
4231
<H2><A NAME="4.61">4.61. How can I mimic CGI form submission (METHOD=POST)?</A></H2>
 
4232
I would like to retrieve web pages that are the result of POSTing a
 
4233
form. Is there existing code that would let me do this easily?
 
4234
<P>
 
4235
Yes. Here's a simple example that uses httplib.  
 
4236
<P>
 
4237
<PRE>
 
4238
    #!/usr/local/bin/python
 
4239
</PRE>
 
4240
<PRE>
 
4241
    import httplib, sys, time
 
4242
</PRE>
 
4243
<PRE>
 
4244
    ### build the query string
 
4245
    qs = "First=Josephine&amp;MI=Q&amp;Last=Public"
 
4246
</PRE>
 
4247
<PRE>
 
4248
    ### connect and send the server a path
 
4249
    httpobj = httplib.HTTP('www.some-server.out-there', 80)
 
4250
    httpobj.putrequest('POST', '/cgi-bin/some-cgi-script')
 
4251
    ### now generate the rest of the HTTP headers...
 
4252
    httpobj.putheader('Accept', '*/*')
 
4253
    httpobj.putheader('Connection', 'Keep-Alive')
 
4254
    httpobj.putheader('Content-type', 'application/x-www-form-urlencoded')
 
4255
    httpobj.putheader('Content-length', '%d' % len(qs))
 
4256
    httpobj.endheaders()
 
4257
    httpobj.send(qs)
 
4258
    ### find out what the server said in response...
 
4259
    reply, msg, hdrs = httpobj.getreply()
 
4260
    if reply != 200:
 
4261
        sys.stdout.write(httpobj.getfile().read())
 
4262
</PRE>
 
4263
Note that in general for "url encoded posts" (the default) query strings must be "quoted" to, for example, change equals signs and spaces to an encoded form when they occur in name or value.  Use urllib.quote to perform this quoting.  For example to send name="Guy Steele, Jr.":
 
4264
<P>
 
4265
<PRE>
 
4266
   >>> from urllib import quote
 
4267
   >>> x = quote("Guy Steele, Jr.")
 
4268
   >>> x
 
4269
   'Guy%20Steele,%20Jr.'
 
4270
   >>> query_string = "name="+x
 
4271
   >>> query_string
 
4272
   'name=Guy%20Steele,%20Jr.'
 
4273
</PRE>
 
4274
<P>
 
4275
 
 
4276
<A HREF="faqw.py?req=edit&amp;file=faq04.061.htp">Edit this entry</A> /
 
4277
<A HREF="faqw.py?req=log&amp;file=faq04.061.htp">Log info</A>
 
4278
 
 
4279
/ Last changed on Mon Jun 21 03:47:07 1999 by
 
4280
<A HREF="mailto:tbryan@python.net">TAB</A>
 
4281
<P>
 
4282
 
 
4283
<HR>
 
4284
<H2><A NAME="4.62">4.62. If my program crashes with a bsddb (or anydbm) database open, it gets corrupted. How come?</A></H2>
 
4285
Databases opened for write access with the bsddb module (and often by
 
4286
the anydbm module, since it will preferentially use bsddb) must
 
4287
explicitly be closed using the close method of the database.  The
 
4288
underlying libdb package caches database contents which need to be
 
4289
converted to on-disk form and written, unlike regular open files which
 
4290
already have the on-disk bits in the kernel's write buffer, where they
 
4291
can just be dumped by the kernel with the program exits.
 
4292
<P>
 
4293
If you have initialized a new bsddb database but not written anything to
 
4294
it before the program crashes, you will often wind up with a zero-length
 
4295
file and encounter an exception the next time the file is opened.
 
4296
<P>
 
4297
 
 
4298
<A HREF="faqw.py?req=edit&amp;file=faq04.062.htp">Edit this entry</A> /
 
4299
<A HREF="faqw.py?req=log&amp;file=faq04.062.htp">Log info</A>
 
4300
 
 
4301
/ Last changed on Mon Jun  3 01:15:01 2002 by
 
4302
<A HREF="mailto:neal@metaslash.com">Neal Norwitz</A>
 
4303
<P>
 
4304
 
 
4305
<HR>
 
4306
<H2><A NAME="4.63">4.63. How do I make a Python script executable on Unix?</A></H2>
 
4307
You need to do two things: the script file's mode must be executable
 
4308
(include the 'x' bit), and the first line must begin with #!
 
4309
followed by the pathname for the Python interpreter.
 
4310
<P>
 
4311
The first is done by executing 'chmod +x scriptfile' or perhaps
 
4312
'chmod 755 scriptfile'.
 
4313
<P>
 
4314
The second can be done in a number of way.  The most straightforward
 
4315
way is to write
 
4316
<P>
 
4317
<PRE>
 
4318
  #!/usr/local/bin/python
 
4319
</PRE>
 
4320
as the very first line of your file - or whatever the pathname is
 
4321
where the python interpreter is installed on your platform.
 
4322
<P>
 
4323
If you would like the script to be independent of where the python
 
4324
interpreter lives, you can use the "env" program.  On almost all
 
4325
platforms, the following will work, assuming the python interpreter
 
4326
is in a directory on the user's $PATH:
 
4327
<P>
 
4328
<PRE>
 
4329
  #! /usr/bin/env python
 
4330
</PRE>
 
4331
Note -- *don't* do this for CGI scripts.  The $PATH variable for
 
4332
CGI scripts is often very minimal, so you need to use the actual
 
4333
absolute pathname of the interpreter.
 
4334
<P>
 
4335
Occasionally, a user's environment is so full that the /usr/bin/env
 
4336
program fails; or there's no env program at all.
 
4337
In that case, you can try the following hack (due to Alex Rezinsky):
 
4338
<P>
 
4339
<PRE>
 
4340
  #! /bin/sh
 
4341
  """:"
 
4342
  exec python $0 ${1+"$@"}
 
4343
  """
 
4344
</PRE>
 
4345
The disadvantage is that this defines the script's __doc__ string.
 
4346
However, you can fix that by adding
 
4347
<P>
 
4348
<PRE>
 
4349
  __doc__ = """...Whatever..."""
 
4350
</PRE>
 
4351
<P>
 
4352
 
 
4353
<A HREF="faqw.py?req=edit&amp;file=faq04.063.htp">Edit this entry</A> /
 
4354
<A HREF="faqw.py?req=log&amp;file=faq04.063.htp">Log info</A>
 
4355
 
 
4356
/ Last changed on Mon Jan 15 09:19:16 2001 by
 
4357
<A HREF="mailto:neal@metaslash.com">Neal Norwitz</A>
 
4358
<P>
 
4359
 
 
4360
<HR>
 
4361
<H2><A NAME="4.64">4.64. How do you remove duplicates from a list?</A></H2>
 
4362
See the Python Cookbook for a long discussion of many cool ways:
 
4363
<P>
 
4364
<PRE>
 
4365
    <A HREF="http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52560">http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52560</A>
 
4366
</PRE>
 
4367
Generally, if you don't mind reordering the List
 
4368
<P>
 
4369
<PRE>
 
4370
   if List:
 
4371
      List.sort()
 
4372
      last = List[-1]
 
4373
      for i in range(len(List)-2, -1, -1):
 
4374
          if last==List[i]: del List[i]
 
4375
          else: last=List[i]
 
4376
</PRE>
 
4377
If all elements of the list may be used as
 
4378
dictionary keys (ie, they are all hashable)
 
4379
this is often faster
 
4380
<P>
 
4381
<PRE>
 
4382
   d = {}
 
4383
   for x in List: d[x]=x
 
4384
   List = d.values()
 
4385
</PRE>
 
4386
Also, for extremely large lists you might
 
4387
consider more optimal alternatives to the first one.
 
4388
The second one is pretty good whenever it can
 
4389
be used.
 
4390
<P>
 
4391
 
 
4392
<A HREF="faqw.py?req=edit&amp;file=faq04.064.htp">Edit this entry</A> /
 
4393
<A HREF="faqw.py?req=log&amp;file=faq04.064.htp">Log info</A>
 
4394
 
 
4395
/ Last changed on Fri May 24 21:56:33 2002 by
 
4396
<A HREF="mailto:tim.one@comcast.net">Tim Peters</A>
 
4397
<P>
 
4398
 
 
4399
<HR>
 
4400
<H2><A NAME="4.65">4.65. Are there any known year 2000 problems in Python?</A></H2>
 
4401
I am not aware of year 2000 deficiencies in Python 1.5.  Python does
 
4402
very few date calculations and for what it does, it relies on the C
 
4403
library functions.  Python generally represent times either as seconds
 
4404
since 1970 or as a tuple (year, month, day, ...) where the year is
 
4405
expressed with four digits, which makes Y2K bugs unlikely.  So as long
 
4406
as your C library is okay, Python should be okay.  Of course, I cannot
 
4407
vouch for <I>your</I> Python code!
 
4408
<P>
 
4409
Given the nature of freely available software, I have to add that this statement is not
 
4410
legally binding.  The Python copyright notice contains the following
 
4411
disclaimer:
 
4412
<P>
 
4413
<PRE>
 
4414
  STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
 
4415
  REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
 
4416
  MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
 
4417
  CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
 
4418
  DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 
4419
  PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 
4420
  TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 
4421
  PERFORMANCE OF THIS SOFTWARE.
 
4422
</PRE>
 
4423
The good news is that <I>if</I> you encounter a problem, you have full
 
4424
source available to track it down and fix it!
 
4425
<P>
 
4426
 
 
4427
<A HREF="faqw.py?req=edit&amp;file=faq04.065.htp">Edit this entry</A> /
 
4428
<A HREF="faqw.py?req=log&amp;file=faq04.065.htp">Log info</A>
 
4429
 
 
4430
/ Last changed on Fri Apr 10 14:59:31 1998 by
 
4431
<A HREF="mailto:guido@python.org">GvR</A>
 
4432
<P>
 
4433
 
 
4434
<HR>
 
4435
<H2><A NAME="4.66">4.66. I want a version of map that applies a method to a sequence of objects! Help!</A></H2>
 
4436
Get fancy!
 
4437
<P>
 
4438
<PRE>
 
4439
  def method_map(objects, method, arguments):
 
4440
       """method_map([a,b], "flog", (1,2)) gives [a.flog(1,2), b.flog(1,2)]"""
 
4441
       nobjects = len(objects)
 
4442
       methods = map(getattr, objects, [method]*nobjects)
 
4443
       return map(apply, methods, [arguments]*nobjects)
 
4444
</PRE>
 
4445
It's generally a good idea to get to know the mysteries of map and apply
 
4446
and getattr and the other dynamic features of Python.
 
4447
<P>
 
4448
 
 
4449
<A HREF="faqw.py?req=edit&amp;file=faq04.066.htp">Edit this entry</A> /
 
4450
<A HREF="faqw.py?req=log&amp;file=faq04.066.htp">Log info</A>
 
4451
 
 
4452
/ Last changed on Mon Jan  5 14:21:14 1998 by
 
4453
<A HREF="mailto:aaron_watters@msn.com">Aaron Watters</A>
 
4454
<P>
 
4455
 
 
4456
<HR>
 
4457
<H2><A NAME="4.67">4.67. How do I generate random numbers in Python?</A></H2>
 
4458
The standard library module "random" implements a random number
 
4459
generator.  Usage is simple:
 
4460
<P>
 
4461
<PRE>
 
4462
    import random
 
4463
</PRE>
 
4464
<PRE>
 
4465
    random.random()
 
4466
</PRE>
 
4467
This returns a random floating point number in the range [0, 1).
 
4468
<P>
 
4469
There are also many other specialized generators in this module, such
 
4470
as
 
4471
<P>
 
4472
<PRE>
 
4473
    randrange(a, b) chooses an integer in the range [a, b)
 
4474
    uniform(a, b) chooses a floating point number in the range [a, b)
 
4475
    normalvariate(mean, sdev) sample from normal (Gaussian) distribution
 
4476
</PRE>
 
4477
Some higher-level functions operate on sequences directly, such as
 
4478
<P>
 
4479
<PRE>
 
4480
    choice(S) chooses random element from a given sequence
 
4481
    shuffle(L) shuffles a list in-place, i.e. permutes it randomly
 
4482
</PRE>
 
4483
There's also a class, Random, which you can instantiate
 
4484
to create independent multiple random number generators.
 
4485
<P>
 
4486
All this is documented in the library reference manual.  Note that
 
4487
the module "whrandom" is obsolete.
 
4488
<P>
 
4489
 
 
4490
<A HREF="faqw.py?req=edit&amp;file=faq04.067.htp">Edit this entry</A> /
 
4491
<A HREF="faqw.py?req=log&amp;file=faq04.067.htp">Log info</A>
 
4492
 
 
4493
/ Last changed on Mon Jun  3 01:16:51 2002 by
 
4494
<A HREF="mailto:neal@metaslash.com">Neal Norwitz</A>
 
4495
<P>
 
4496
 
 
4497
<HR>
 
4498
<H2><A NAME="4.68">4.68. How do I access the serial (RS232) port?</A></H2>
 
4499
There's a Windows serial communication module (for communication
 
4500
over RS 232 serial ports) at
 
4501
<P>
 
4502
<PRE>
 
4503
  <A HREF="ftp://ftp.python.org/pub/python/contrib/sio-151.zip">ftp://ftp.python.org/pub/python/contrib/sio-151.zip</A>
 
4504
  <A HREF="http://www.python.org/ftp/python/contrib/sio-151.zip">http://www.python.org/ftp/python/contrib/sio-151.zip</A>
 
4505
</PRE>
 
4506
For DOS, try Hans Nowak's Python-DX, which supports this, at:
 
4507
<P>
 
4508
<PRE>
 
4509
  <A HREF="http://www.cuci.nl/~hnowak">http://www.cuci.nl/~hnowak</A>/
 
4510
</PRE>
 
4511
For Unix, see a usenet post by Mitch Chapman:
 
4512
<P>
 
4513
<PRE>
 
4514
  <A HREF="http://groups.google.com/groups?selm=34A04430.CF9@ohioee.com">http://groups.google.com/groups?selm=34A04430.CF9@ohioee.com</A>
 
4515
</PRE>
 
4516
For Win32, POSIX(Linux, BSD, *), Jython, Chris':
 
4517
<P>
 
4518
<PRE>
 
4519
  <A HREF="http://pyserial.sourceforge.net">http://pyserial.sourceforge.net</A>
 
4520
</PRE>
 
4521
<P>
 
4522
 
 
4523
<A HREF="faqw.py?req=edit&amp;file=faq04.068.htp">Edit this entry</A> /
 
4524
<A HREF="faqw.py?req=log&amp;file=faq04.068.htp">Log info</A>
 
4525
 
 
4526
/ Last changed on Tue Jul  2 21:11:07 2002 by
 
4527
<A HREF="mailto:cliechti@gmx.net">Chris Liechti</A>
 
4528
<P>
 
4529
 
 
4530
<HR>
 
4531
<H2><A NAME="4.69">4.69. Images on Tk-Buttons don't work in Py15?</A></H2>
 
4532
They <I>do</I> work, but you must keep your own <I>reference</I> to the image 
 
4533
object now. More verbosely, you must make sure that, say, a global 
 
4534
variable or a class attribute refers to the object.
 
4535
<P>
 
4536
Quoting Fredrik Lundh from the mailinglist:
 
4537
<P>
 
4538
<PRE>
 
4539
  Well, the Tk button widget keeps a reference to the internal
 
4540
  photoimage object, but Tkinter does not.  So when the last
 
4541
  Python reference goes away, Tkinter tells Tk to release the
 
4542
  photoimage.  But since the image is in use by a widget, Tk
 
4543
  doesn't destroy it.  Not completely.  It just blanks the image,
 
4544
  making it completely transparent...
 
4545
</PRE>
 
4546
<PRE>
 
4547
  And yes, there was a bug in the keyword argument handling
 
4548
  in 1.4 that kept an extra reference around in some cases.  And
 
4549
  when Guido fixed that bug in 1.5, he broke quite a few Tkinter
 
4550
  programs...
 
4551
</PRE>
 
4552
<P>
 
4553
 
 
4554
<A HREF="faqw.py?req=edit&amp;file=faq04.069.htp">Edit this entry</A> /
 
4555
<A HREF="faqw.py?req=log&amp;file=faq04.069.htp">Log info</A>
 
4556
 
 
4557
/ Last changed on Tue Feb  3 11:31:03 1998 by
 
4558
<A HREF="mailto:cjr@euronet.nl">Case Roole</A>
 
4559
<P>
 
4560
 
 
4561
<HR>
 
4562
<H2><A NAME="4.70">4.70. Where is the math.py (socket.py, regex.py, etc.) source file?</A></H2>
 
4563
If you can't find a source file for a module it may be a builtin
 
4564
or dynamically loaded module implemented in C, C++ or other
 
4565
compiled language.  In this case you may not have the source
 
4566
file or it may be something like mathmodule.c, somewhere in
 
4567
a C source directory (not on the Python Path).
 
4568
<P>
 
4569
Fredrik Lundh (<A HREF="mailto:fredrik@pythonware.com">fredrik@pythonware.com</A>) explains (on the python-list):
 
4570
<P>
 
4571
There are (at least) three kinds of modules in Python:
 
4572
1) modules written in Python (.py);
 
4573
2) modules written in C and dynamically loaded (.dll, .pyd, .so, .sl, etc);
 
4574
3) modules written in C and linked with the interpreter; to get a list
 
4575
of these, type:
 
4576
<P>
 
4577
<PRE>
 
4578
    import sys
 
4579
    print sys.builtin_module_names
 
4580
</PRE>
 
4581
<P>
 
4582
 
 
4583
<A HREF="faqw.py?req=edit&amp;file=faq04.070.htp">Edit this entry</A> /
 
4584
<A HREF="faqw.py?req=log&amp;file=faq04.070.htp">Log info</A>
 
4585
 
 
4586
/ Last changed on Tue Feb  3 13:55:33 1998 by
 
4587
<A HREF="mailto:aaron_watters@msn.com">Aaron Watters</A>
 
4588
<P>
 
4589
 
 
4590
<HR>
 
4591
<H2><A NAME="4.71">4.71. How do I send mail from a Python script?</A></H2>
 
4592
The standard library module smtplib does this.
 
4593
Here's a very simple interactive mail
 
4594
sender that uses it.  This method will work on any host that
 
4595
supports an SMTP listener.
 
4596
<P>
 
4597
<PRE>
 
4598
    import sys, smtplib
 
4599
</PRE>
 
4600
<PRE>
 
4601
    fromaddr = raw_input("From: ")
 
4602
    toaddrs  = raw_input("To: ").split(',')
 
4603
    print "Enter message, end with ^D:"
 
4604
    msg = ''
 
4605
    while 1:
 
4606
        line = sys.stdin.readline()
 
4607
        if not line:
 
4608
            break
 
4609
        msg = msg + line
 
4610
</PRE>
 
4611
<PRE>
 
4612
    # The actual mail send
 
4613
    server = smtplib.SMTP('localhost')
 
4614
    server.sendmail(fromaddr, toaddrs, msg)
 
4615
    server.quit()
 
4616
</PRE>
 
4617
If the local host doesn't have an SMTP listener, you need to find one. The simple method is to ask the user. Alternately, you can use the DNS system to find the mail gateway(s) responsible for the source address.
 
4618
<P>
 
4619
A Unix-only alternative uses sendmail.  The location of the
 
4620
sendmail program varies between systems; sometimes it is
 
4621
/usr/lib/sendmail, sometime /usr/sbin/sendmail.  The sendmail manual
 
4622
page will help you out.  Here's some sample code:
 
4623
<P>
 
4624
<PRE>
 
4625
  SENDMAIL = "/usr/sbin/sendmail" # sendmail location
 
4626
  import os
 
4627
  p = os.popen("%s -t -i" % SENDMAIL, "w")
 
4628
  p.write("To: <A HREF="mailto:cary@ratatosk.org">cary@ratatosk.org</A>\n")
 
4629
  p.write("Subject: test\n")
 
4630
  p.write("\n") # blank line separating headers from body
 
4631
  p.write("Some text\n")
 
4632
  p.write("some more text\n")
 
4633
  sts = p.close()
 
4634
  if sts != 0:
 
4635
      print "Sendmail exit status", sts
 
4636
</PRE>
 
4637
<P>
 
4638
 
 
4639
<A HREF="faqw.py?req=edit&amp;file=faq04.071.htp">Edit this entry</A> /
 
4640
<A HREF="faqw.py?req=log&amp;file=faq04.071.htp">Log info</A>
 
4641
 
 
4642
/ Last changed on Mon Jun  3 07:05:12 2002 by
 
4643
<A HREF="mailto:smurf@noris.de">Matthias Urlichs</A>
 
4644
<P>
 
4645
 
 
4646
<HR>
 
4647
<H2><A NAME="4.72">4.72. How do I avoid blocking in connect() of a socket?</A></H2>
 
4648
The select module is widely known to help with asynchronous
 
4649
I/O on sockets once they are connected.  However, it is less
 
4650
than common knowledge how to avoid blocking on the initial
 
4651
connect() call.  Jeremy Hylton has the following advice (slightly
 
4652
edited):
 
4653
<P>
 
4654
To prevent the TCP connect from blocking, you can set the socket to 
 
4655
non-blocking mode.  Then when you do the connect(), you will either 
 
4656
connect immediately (unlikely) or get an exception that contains the 
 
4657
errno.  errno.EINPROGRESS indicates that the connection is in 
 
4658
progress, but hasn't finished yet.  Different OSes will return 
 
4659
different errnos, so you're going to have to check.  I can tell you 
 
4660
that different versions of Solaris return different errno values. 
 
4661
<P>
 
4662
In Python 1.5 and later, you can use connect_ex() to avoid
 
4663
creating an exception.  It will just return the errno value. 
 
4664
<P>
 
4665
To poll, you can call connect_ex() again later -- 0 or errno.EISCONN 
 
4666
indicate that you're connected -- or you can pass this socket to 
 
4667
select (checking to see if it is writeable).
 
4668
<P>
 
4669
 
 
4670
<A HREF="faqw.py?req=edit&amp;file=faq04.072.htp">Edit this entry</A> /
 
4671
<A HREF="faqw.py?req=log&amp;file=faq04.072.htp">Log info</A>
 
4672
 
 
4673
/ Last changed on Tue Feb 24 21:30:45 1998 by
 
4674
<A HREF="mailto:guido@python.org">GvR</A>
 
4675
<P>
 
4676
 
 
4677
<HR>
 
4678
<H2><A NAME="4.73">4.73. How do I specify hexadecimal and octal integers?</A></H2>
 
4679
To specify an octal digit, precede the octal value with a zero.  For example,
 
4680
to set the variable "a" to the octal value "10" (8 in decimal), type:
 
4681
<P>
 
4682
<PRE>
 
4683
    >>> a = 010
 
4684
</PRE>
 
4685
To verify that this works, you can type "a" and hit enter while in the
 
4686
interpreter, which will cause Python to spit out the current value of "a"
 
4687
in decimal:
 
4688
<P>
 
4689
<PRE>
 
4690
    >>> a
 
4691
    8
 
4692
</PRE>
 
4693
Hexadecimal is just as easy.  Simply precede the hexadecimal number with a
 
4694
zero, and then a lower or uppercase "x".  Hexadecimal digits can be specified
 
4695
in lower or uppercase.  For example, in the Python interpreter:
 
4696
<P>
 
4697
<PRE>
 
4698
    >>> a = 0xa5
 
4699
    >>> a
 
4700
    165
 
4701
    >>> b = 0XB2
 
4702
    >>> b
 
4703
    178
 
4704
</PRE>
 
4705
<P>
 
4706
 
 
4707
<A HREF="faqw.py?req=edit&amp;file=faq04.073.htp">Edit this entry</A> /
 
4708
<A HREF="faqw.py?req=log&amp;file=faq04.073.htp">Log info</A>
 
4709
 
 
4710
/ Last changed on Tue Mar  3 12:53:16 1998 by
 
4711
<A HREF="mailto:guido@python.org">GvR</A>
 
4712
<P>
 
4713
 
 
4714
<HR>
 
4715
<H2><A NAME="4.74">4.74. How to get a single keypress at a time?</A></H2>
 
4716
For Windows, see question 8.2.  Here is an answer for Unix (see also 4.94).
 
4717
<P>
 
4718
There are several solutions; some involve using curses, which is a
 
4719
pretty big thing to learn.  Here's a solution without curses, due
 
4720
to Andrew Kuchling (adapted from code to do a PGP-style
 
4721
randomness pool):
 
4722
<P>
 
4723
<PRE>
 
4724
        import termios, sys, os
 
4725
        fd = sys.stdin.fileno()
 
4726
        old = termios.tcgetattr(fd)
 
4727
        new = termios.tcgetattr(fd)
 
4728
        new[3] = new[3] &amp; ~termios.ICANON &amp; ~termios.ECHO
 
4729
        new[6][termios.VMIN] = 1
 
4730
        new[6][termios.VTIME] = 0
 
4731
        termios.tcsetattr(fd, termios.TCSANOW, new)
 
4732
        s = ''    # We'll save the characters typed and add them to the pool.
 
4733
        try:
 
4734
            while 1:
 
4735
                c = os.read(fd, 1)
 
4736
                print "Got character", `c`
 
4737
                s = s+c
 
4738
        finally:
 
4739
            termios.tcsetattr(fd, termios.TCSAFLUSH, old)
 
4740
</PRE>
 
4741
You need the termios module for any of this to work, and I've only
 
4742
tried it on Linux, though it should work elsewhere.  It turns off
 
4743
stdin's echoing and disables canonical mode, and then reads a
 
4744
character at a time from stdin, noting the time after each keystroke.
 
4745
<P>
 
4746
 
 
4747
<A HREF="faqw.py?req=edit&amp;file=faq04.074.htp">Edit this entry</A> /
 
4748
<A HREF="faqw.py?req=log&amp;file=faq04.074.htp">Log info</A>
 
4749
 
 
4750
/ Last changed on Thu Oct 24 00:36:56 2002 by
 
4751
<A HREF="mailto:cliechti@gmx.net">chris</A>
 
4752
<P>
 
4753
 
 
4754
<HR>
 
4755
<H2><A NAME="4.75">4.75. How can I overload constructors (or methods) in Python?</A></H2>
 
4756
(This actually applies to all methods, but somehow the question
 
4757
usually comes up first in the context of constructors.)
 
4758
<P>
 
4759
Where in C++ you'd write
 
4760
<P>
 
4761
<PRE>
 
4762
    class C {
 
4763
        C() { cout &lt;&lt; "No arguments\n"; }
 
4764
        C(int i) { cout &lt;&lt; "Argument is " &lt;&lt; i &lt;&lt; "\n"; }
 
4765
    }
 
4766
</PRE>
 
4767
in Python you have to write a single constructor that catches all
 
4768
cases using default arguments.  For example:
 
4769
<P>
 
4770
<PRE>
 
4771
    class C:
 
4772
        def __init__(self, i=None):
 
4773
            if i is None:
 
4774
                print "No arguments"
 
4775
            else:
 
4776
                print "Argument is", i
 
4777
</PRE>
 
4778
This is not entirely equivalent, but close enough in practice.
 
4779
<P>
 
4780
You could also try a variable-length argument list, e.g.
 
4781
<P>
 
4782
<PRE>
 
4783
        def __init__(self, *args):
 
4784
            ....
 
4785
</PRE>
 
4786
The same approach works for all method definitions.
 
4787
<P>
 
4788
 
 
4789
<A HREF="faqw.py?req=edit&amp;file=faq04.075.htp">Edit this entry</A> /
 
4790
<A HREF="faqw.py?req=log&amp;file=faq04.075.htp">Log info</A>
 
4791
 
 
4792
/ Last changed on Mon Apr 20 11:55:55 1998 by
 
4793
<A HREF="mailto:guido@python.org">GvR</A>
 
4794
<P>
 
4795
 
 
4796
<HR>
 
4797
<H2><A NAME="4.76">4.76. How do I pass keyword arguments from one method to another?</A></H2>
 
4798
Use apply.  For example:
 
4799
<P>
 
4800
<PRE>
 
4801
    class Account:
 
4802
        def __init__(self, **kw):
 
4803
            self.accountType = kw.get('accountType')
 
4804
            self.balance = kw.get('balance')
 
4805
</PRE>
 
4806
<PRE>
 
4807
    class CheckingAccount(Account):
 
4808
        def __init__(self, **kw):
 
4809
            kw['accountType'] = 'checking'
 
4810
            apply(Account.__init__, (self,), kw)
 
4811
</PRE>
 
4812
<PRE>
 
4813
    myAccount = CheckingAccount(balance=100.00)
 
4814
</PRE>
 
4815
In Python 2.0 you can call it directly using the new ** syntax:
 
4816
<P>
 
4817
<PRE>
 
4818
    class CheckingAccount(Account):
 
4819
        def __init__(self, **kw):
 
4820
            kw['accountType'] = 'checking'
 
4821
            Account.__init__(self, **kw)
 
4822
</PRE>
 
4823
or more generally:
 
4824
<P>
 
4825
<PRE>
 
4826
 >>> def f(x, *y, **z):
 
4827
 ...  print x,y,z
 
4828
 ...
 
4829
 >>> Y = [1,2,3]
 
4830
 >>> Z = {'foo':3,'bar':None}
 
4831
 >>> f('hello', *Y, **Z)
 
4832
 hello (1, 2, 3) {'foo': 3, 'bar': None}
 
4833
</PRE>
 
4834
<P>
 
4835
 
 
4836
<A HREF="faqw.py?req=edit&amp;file=faq04.076.htp">Edit this entry</A> /
 
4837
<A HREF="faqw.py?req=log&amp;file=faq04.076.htp">Log info</A>
 
4838
 
 
4839
/ Last changed on Thu Dec 28 13:04:01 2000 by
 
4840
<A HREF="mailto:pbjorn@uswest.net">Bjorn Pettersen</A>
 
4841
<P>
 
4842
 
 
4843
<HR>
 
4844
<H2><A NAME="4.77">4.77. What module should I use to help with generating HTML?</A></H2>
 
4845
Check out HTMLgen written by Robin Friedrich. It's a class library
 
4846
of objects corresponding to all the HTML 3.2 markup tags. It's used
 
4847
when you are writing in Python and wish to synthesize HTML pages for
 
4848
generating a web or for CGI forms, etc. 
 
4849
<P>
 
4850
It can be found in the FTP contrib area on python.org or on the 
 
4851
Starship. Use the search engines there to locate the latest version.
 
4852
<P>
 
4853
It might also be useful to consider DocumentTemplate, which offers clear 
 
4854
separation between Python code and HTML code.  DocumentTemplate is part 
 
4855
of the Bobo objects publishing system (http:/www.digicool.com/releases) 
 
4856
but can be used independantly of course!
 
4857
<P>
 
4858
 
 
4859
<A HREF="faqw.py?req=edit&amp;file=faq04.077.htp">Edit this entry</A> /
 
4860
<A HREF="faqw.py?req=log&amp;file=faq04.077.htp">Log info</A>
 
4861
 
 
4862
/ Last changed on Fri Aug 28 09:54:58 1998 by
 
4863
<A HREF="mailto:guido@python.org">GvR</A>
 
4864
<P>
 
4865
 
 
4866
<HR>
 
4867
<H2><A NAME="4.78">4.78. How do I create documentation from doc strings?</A></H2>
 
4868
Use gendoc, by Daniel Larson.  See
 
4869
<P>
 
4870
<A HREF="http://starship.python.net/crew/danilo">http://starship.python.net/crew/danilo</A>/
 
4871
<P>
 
4872
It can create HTML from the doc strings in your Python source code.
 
4873
<P>
 
4874
 
 
4875
<A HREF="faqw.py?req=edit&amp;file=faq04.078.htp">Edit this entry</A> /
 
4876
<A HREF="faqw.py?req=log&amp;file=faq04.078.htp">Log info</A>
 
4877
 
 
4878
/ Last changed on Mon Oct  7 17:15:51 2002 by
 
4879
<A HREF="mailto:phil@dspfactory.com">Phil Rittenhouse</A>
 
4880
<P>
 
4881
 
 
4882
<HR>
 
4883
<H2><A NAME="4.79">4.79. How do I read (or write) binary data?</A></H2>
 
4884
For complex data formats, it's best to use
 
4885
use the struct module.  It's documented in the library reference.
 
4886
It allows you to take a string read from a file containing binary
 
4887
data (usually numbers) and convert it to Python objects; and vice
 
4888
versa.
 
4889
<P>
 
4890
For example, the following code reads two 2-byte integers
 
4891
and one 4-byte integer in big-endian format from a file:
 
4892
<P>
 
4893
<PRE>
 
4894
  import struct
 
4895
</PRE>
 
4896
<PRE>
 
4897
  f = open(filename, "rb")  # Open in binary mode for portability
 
4898
  s = f.read(8)
 
4899
  x, y, z = struct.unpack(">hhl", s)
 
4900
</PRE>
 
4901
The '>' in the format string forces bin-endian data; the letter
 
4902
'h' reads one "short integer" (2 bytes), and 'l' reads one
 
4903
"long integer" (4 bytes) from the string.
 
4904
<P>
 
4905
For data that is more regular (e.g. a homogeneous list of ints or
 
4906
floats), you can also use the array module, also documented
 
4907
in the library reference.
 
4908
<P>
 
4909
 
 
4910
<A HREF="faqw.py?req=edit&amp;file=faq04.079.htp">Edit this entry</A> /
 
4911
<A HREF="faqw.py?req=log&amp;file=faq04.079.htp">Log info</A>
 
4912
 
 
4913
/ Last changed on Wed Oct  7 09:16:45 1998 by
 
4914
<A HREF="mailto:guido@python.org">GvR</A>
 
4915
<P>
 
4916
 
 
4917
<HR>
 
4918
<H2><A NAME="4.80">4.80. I can't get key bindings to work in Tkinter</A></H2>
 
4919
An oft-heard complaint is that event handlers bound to events
 
4920
with the bind() method don't get handled even when the appropriate
 
4921
key is pressed.
 
4922
<P>
 
4923
The most common cause is that the widget to which the binding applies
 
4924
doesn't have "keyboard focus".  Check out the Tk documentation
 
4925
for the focus command.  Usually a widget is given the keyboard
 
4926
focus by clicking in it (but not for labels; see the taketocus
 
4927
option).
 
4928
<P>
 
4929
 
 
4930
<A HREF="faqw.py?req=edit&amp;file=faq04.080.htp">Edit this entry</A> /
 
4931
<A HREF="faqw.py?req=log&amp;file=faq04.080.htp">Log info</A>
 
4932
 
 
4933
/ Last changed on Fri Jun 12 09:37:33 1998 by
 
4934
<A HREF="mailto:guido@python.org">GvR</A>
 
4935
<P>
 
4936
 
 
4937
<HR>
 
4938
<H2><A NAME="4.81">4.81. &quot;import crypt&quot; fails</A></H2>
 
4939
[Unix]
 
4940
<P>
 
4941
Starting with Python 1.5, the crypt module is disabled by default.
 
4942
In order to enable it, you must go into the Python source tree and
 
4943
edit the file Modules/Setup to enable it (remove a '#' sign in
 
4944
front of the line starting with '#crypt').  Then rebuild.
 
4945
You may also have to add the string '-lcrypt' to that same line.
 
4946
<P>
 
4947
 
 
4948
<A HREF="faqw.py?req=edit&amp;file=faq04.081.htp">Edit this entry</A> /
 
4949
<A HREF="faqw.py?req=log&amp;file=faq04.081.htp">Log info</A>
 
4950
 
 
4951
/ Last changed on Wed Aug  5 08:57:09 1998 by
 
4952
<A HREF="mailto:guido@python.org">GvR</A>
 
4953
<P>
 
4954
 
 
4955
<HR>
 
4956
<H2><A NAME="4.82">4.82. Are there coding standards or a style guide for Python programs?</A></H2>
 
4957
Yes, Guido has written the "Python Style Guide".  See
 
4958
<A HREF="http://www.python.org/doc/essays/styleguide.html">http://www.python.org/doc/essays/styleguide.html</A>
 
4959
<P>
 
4960
 
 
4961
<A HREF="faqw.py?req=edit&amp;file=faq04.082.htp">Edit this entry</A> /
 
4962
<A HREF="faqw.py?req=log&amp;file=faq04.082.htp">Log info</A>
 
4963
 
 
4964
/ Last changed on Tue Sep 29 09:50:27 1998 by
 
4965
<A HREF="mailto:vanandel@ucar.edu">Joseph VanAndel</A>
 
4966
<P>
 
4967
 
 
4968
<HR>
 
4969
<H2><A NAME="4.83">4.83. How do I freeze Tkinter applications?</A></H2>
 
4970
Freeze is a tool to create stand-alone applications (see 4.28).
 
4971
<P>
 
4972
When freezing Tkinter applications, the applications will not be
 
4973
truly stand-alone, as the application will still need the tcl and
 
4974
tk libraries.
 
4975
<P>
 
4976
One solution is to ship the application with the tcl and tk libraries,
 
4977
and point to them at run-time using the TCL_LIBRARY and TK_LIBRARY
 
4978
environment variables.
 
4979
<P>
 
4980
To get truly stand-alone applications, the Tcl scripts that form
 
4981
the library have to be integrated into the application as well. One
 
4982
tool supporting that is SAM (stand-alone modules), which is part
 
4983
of the Tix distribution (<A HREF="http://tix.mne.com">http://tix.mne.com</A>). Build Tix with SAM 
 
4984
enabled, perform the appropriate call to Tclsam_init etc inside 
 
4985
Python's Modules/tkappinit.c, and link with libtclsam
 
4986
and libtksam (you might include the Tix libraries as well).
 
4987
<P>
 
4988
 
 
4989
<A HREF="faqw.py?req=edit&amp;file=faq04.083.htp">Edit this entry</A> /
 
4990
<A HREF="faqw.py?req=log&amp;file=faq04.083.htp">Log info</A>
 
4991
 
 
4992
/ Last changed on Wed Jan 20 17:35:01 1999 by
 
4993
<A HREF="mailto:loewis@informatik.hu-berlin.de">Martin v. L�wis</A>
 
4994
<P>
 
4995
 
 
4996
<HR>
 
4997
<H2><A NAME="4.84">4.84. How do I create static class data and static class methods?</A></H2>
 
4998
[Tim Peters, <A HREF="mailto:tim_one@email.msn.com">tim_one@email.msn.com</A>]
 
4999
<P>
 
5000
Static data (in the sense of C++ or Java) is easy; static methods (again in the sense of C++ or Java) are not supported directly.
 
5001
<P>
 
5002
STATIC DATA
 
5003
<P>
 
5004
For example,
 
5005
<P>
 
5006
<PRE>
 
5007
    class C:
 
5008
        count = 0   # number of times C.__init__ called
 
5009
</PRE>
 
5010
<PRE>
 
5011
        def __init__(self):
 
5012
            C.count = C.count + 1
 
5013
</PRE>
 
5014
<PRE>
 
5015
        def getcount(self):
 
5016
            return C.count  # or return self.count
 
5017
</PRE>
 
5018
c.count also refers to C.count for any c such that isinstance(c, C) holds, unless overridden by c itself or by some class on the base-class search path from c.__class__ back to C.
 
5019
<P>
 
5020
Caution:  within a method of C,
 
5021
<P>
 
5022
<PRE>
 
5023
    self.count = 42
 
5024
</PRE>
 
5025
creates a new and unrelated instance vrbl named "count" in self's own dict.  So rebinding of a class-static data name needs the
 
5026
<P>
 
5027
<PRE>
 
5028
    C.count = 314
 
5029
</PRE>
 
5030
form whether inside a method or not.
 
5031
<P>
 
5032
<P>
 
5033
STATIC METHODS
 
5034
<P>
 
5035
Static methods (as opposed to static data) are unnatural in Python, because
 
5036
<P>
 
5037
<PRE>
 
5038
    C.getcount
 
5039
</PRE>
 
5040
returns an unbound method object, which can't be invoked without supplying an instance of C as the first argument.
 
5041
<P>
 
5042
The intended way to get the effect of a static method is via a module-level function:
 
5043
<P>
 
5044
<PRE>
 
5045
    def getcount():
 
5046
        return C.count
 
5047
</PRE>
 
5048
If your code is structured so as to define one class (or tightly related class hierarchy) per module, this supplies the desired encapsulation.
 
5049
<P>
 
5050
Several tortured schemes for faking static methods can be found by searching DejaNews.  Most people feel such cures are worse than the disease.  Perhaps the least obnoxious is due to Pekka Pessi (mailto:<A HREF="mailto:ppessi@hut.fi">ppessi@hut.fi</A>):
 
5051
<P>
 
5052
<PRE>
 
5053
    # helper class to disguise function objects
 
5054
    class _static:
 
5055
        def __init__(self, f):
 
5056
            self.__call__ = f
 
5057
</PRE>
 
5058
<PRE>
 
5059
    class C:
 
5060
        count = 0
 
5061
</PRE>
 
5062
<PRE>
 
5063
        def __init__(self):
 
5064
            C.count = C.count + 1
 
5065
</PRE>
 
5066
<PRE>
 
5067
        def getcount():
 
5068
            return C.count
 
5069
        getcount = _static(getcount)
 
5070
</PRE>
 
5071
<PRE>
 
5072
        def sum(x, y):
 
5073
            return x + y
 
5074
        sum = _static(sum)
 
5075
</PRE>
 
5076
<PRE>
 
5077
    C(); C()
 
5078
    c = C()
 
5079
    print C.getcount()  # prints 3
 
5080
    print c.getcount()  # prints 3
 
5081
    print C.sum(27, 15) # prints 42
 
5082
</PRE>
 
5083
<P>
 
5084
 
 
5085
<A HREF="faqw.py?req=edit&amp;file=faq04.084.htp">Edit this entry</A> /
 
5086
<A HREF="faqw.py?req=log&amp;file=faq04.084.htp">Log info</A>
 
5087
 
 
5088
/ Last changed on Thu Jan 21 21:35:38 1999 by
 
5089
<A HREF="mailto:tim_one@email.msn.com">Tim Peters</A>
 
5090
<P>
 
5091
 
 
5092
<HR>
 
5093
<H2><A NAME="4.85">4.85. __import__('x.y.z') returns &lt;module 'x'&gt;; how do I get z?</A></H2>
 
5094
Try
 
5095
<P>
 
5096
<PRE>
 
5097
   __import__('x.y.z').y.z
 
5098
</PRE>
 
5099
For more realistic situations, you may have to do something like
 
5100
<P>
 
5101
<PRE>
 
5102
   m = __import__(s)
 
5103
   for i in string.split(s, ".")[1:]:
 
5104
       m = getattr(m, i)
 
5105
</PRE>
 
5106
<P>
 
5107
 
 
5108
<A HREF="faqw.py?req=edit&amp;file=faq04.085.htp">Edit this entry</A> /
 
5109
<A HREF="faqw.py?req=log&amp;file=faq04.085.htp">Log info</A>
 
5110
 
 
5111
/ Last changed on Thu Jan 28 11:01:43 1999 by
 
5112
<A HREF="mailto:guido@python.org">GvR</A>
 
5113
<P>
 
5114
 
 
5115
<HR>
 
5116
<H2><A NAME="4.86">4.86. Basic thread wisdom</A></H2>
 
5117
Please note that there is no way to take advantage of
 
5118
multiprocessor hardware using the Python thread model. The interpreter
 
5119
uses a global interpreter lock (GIL),
 
5120
which does not allow multiple threads to be concurrently active.
 
5121
<P>
 
5122
If you write a simple test program like this:
 
5123
<P>
 
5124
<PRE>
 
5125
  import thread
 
5126
  def run(name, n):
 
5127
      for i in range(n): print name, i
 
5128
  for i in range(10):
 
5129
      thread.start_new(run, (i, 100))
 
5130
</PRE>
 
5131
none of the threads seem to run!  The reason is that as soon as
 
5132
the main thread exits, all threads are killed.
 
5133
<P>
 
5134
A simple fix is to add a sleep to the end of the program,
 
5135
sufficiently long for all threads to finish:
 
5136
<P>
 
5137
<PRE>
 
5138
  import thread, time
 
5139
  def run(name, n):
 
5140
      for i in range(n): print name, i
 
5141
  for i in range(10):
 
5142
      thread.start_new(run, (i, 100))
 
5143
  time.sleep(10) # &lt;----------------------------!
 
5144
</PRE>
 
5145
But now (on many platforms) the threads don't run in parallel,
 
5146
but appear to run sequentially, one at a time!  The reason is
 
5147
that the OS thread scheduler doesn't start a new thread until
 
5148
the previous thread is blocked.
 
5149
<P>
 
5150
A simple fix is to add a tiny sleep to the start of the run
 
5151
function:
 
5152
<P>
 
5153
<PRE>
 
5154
  import thread, time
 
5155
  def run(name, n):
 
5156
      time.sleep(0.001) # &lt;---------------------!
 
5157
      for i in range(n): print name, i
 
5158
  for i in range(10):
 
5159
      thread.start_new(run, (i, 100))
 
5160
  time.sleep(10)
 
5161
</PRE>
 
5162
Some more hints:
 
5163
<P>
 
5164
Instead of using a time.sleep() call at the end, it's
 
5165
better to use some kind of semaphore mechanism.  One idea is to
 
5166
use a the Queue module to create a queue object, let each thread
 
5167
append a token to the queue when it finishes, and let the main
 
5168
thread read as many tokens from the queue as there are threads.
 
5169
<P>
 
5170
Use the threading module instead of the thread module.  It's part
 
5171
of Python since version 1.5.1.  It takes care of all these details,
 
5172
and has many other nice features too!
 
5173
<P>
 
5174
 
 
5175
<A HREF="faqw.py?req=edit&amp;file=faq04.086.htp">Edit this entry</A> /
 
5176
<A HREF="faqw.py?req=log&amp;file=faq04.086.htp">Log info</A>
 
5177
 
 
5178
/ Last changed on Fri Feb  7 16:21:55 2003 by
 
5179
<A HREF="mailto:guido@python.org">GvR</A>
 
5180
<P>
 
5181
 
 
5182
<HR>
 
5183
<H2><A NAME="4.87">4.87. Why doesn't closing sys.stdout (stdin, stderr) really close it?</A></H2>
 
5184
Python file objects are a high-level layer of abstraction on top of C streams, which in turn are a medium-level layer of abstraction on top of (among other things) low-level C file descriptors.
 
5185
<P>
 
5186
For most file objects f you create in Python via the builtin "open" function, f.close() marks the Python file object as being closed from Python's point of view, and also arranges to close the underlying C stream.  This happens automatically too, in f's destructor, when f becomes garbage.
 
5187
<P>
 
5188
But stdin, stdout and stderr are treated specially by Python, because of the special status also given to them by C:  doing
 
5189
<P>
 
5190
<PRE>
 
5191
    sys.stdout.close() # ditto for stdin and stderr
 
5192
</PRE>
 
5193
marks the Python-level file object as being closed, but does <I>not</I> close the associated C stream (provided sys.stdout is still bound to its default value, which is the stream C also calls "stdout").
 
5194
<P>
 
5195
To close the underlying C stream for one of these three, you should first be sure that's what you really want to do (e.g., you may confuse the heck out of extension modules trying to do I/O).  If it is, use os.close:
 
5196
<P>
 
5197
<PRE>
 
5198
    os.close(0)   # close C's stdin stream
 
5199
    os.close(1)   # close C's stdout stream
 
5200
    os.close(2)   # close C's stderr stream
 
5201
</PRE>
 
5202
<P>
 
5203
 
 
5204
<A HREF="faqw.py?req=edit&amp;file=faq04.087.htp">Edit this entry</A> /
 
5205
<A HREF="faqw.py?req=log&amp;file=faq04.087.htp">Log info</A>
 
5206
 
 
5207
/ Last changed on Sat Apr 17 02:22:35 1999 by
 
5208
<A HREF="mailto:tim_one@email.msn.com">Tim Peters</A>
 
5209
<P>
 
5210
 
 
5211
<HR>
 
5212
<H2><A NAME="4.88">4.88. What kinds of global value mutation are thread-safe?</A></H2>
 
5213
[adapted from c.l.py responses by Gordon McMillan &amp; GvR]
 
5214
<P>
 
5215
A global interpreter lock (GIL) is used internally to ensure that only one thread runs in the Python VM at a time.  In general, Python offers to switch among threads only between bytecode instructions (how frequently it offers to switch can be set via sys.setcheckinterval).  Each bytecode instruction-- and all the C implementation code reached from it --is therefore atomic.
 
5216
<P>
 
5217
In theory, this means an exact accounting requires an exact understanding of the PVM bytecode implementation.  In practice, it means that operations on shared vrbls of builtin data types (ints, lists, dicts, etc) that "look atomic" really are.
 
5218
<P>
 
5219
For example, these are atomic (L, L1, L2 are lists, D, D1, D2 are dicts, x, y
 
5220
are objects, i, j are ints):
 
5221
<P>
 
5222
<PRE>
 
5223
    L.append(x)
 
5224
    L1.extend(L2)
 
5225
    x = L[i]
 
5226
    x = L.pop()
 
5227
    L1[i:j] = L2
 
5228
    L.sort()
 
5229
    x = y
 
5230
    x.field = y
 
5231
    D[x] = y
 
5232
    D1.update(D2)
 
5233
    D.keys()
 
5234
</PRE>
 
5235
These aren't:
 
5236
<P>
 
5237
<PRE>
 
5238
    i = i+1
 
5239
    L.append(L[-1])
 
5240
    L[i] = L[j]
 
5241
    D[x] = D[x] + 1
 
5242
</PRE>
 
5243
Note: operations that replace other objects may invoke those other objects' __del__ method when their reference count reaches zero, and that can affect things.  This is especially true for the mass updates to dictionaries and lists.  When in doubt, use a mutex!
 
5244
<P>
 
5245
 
 
5246
<A HREF="faqw.py?req=edit&amp;file=faq04.088.htp">Edit this entry</A> /
 
5247
<A HREF="faqw.py?req=log&amp;file=faq04.088.htp">Log info</A>
 
5248
 
 
5249
/ Last changed on Fri Feb  7 16:21:03 2003 by
 
5250
<A HREF="mailto:guido@python.org">GvR</A>
 
5251
<P>
 
5252
 
 
5253
<HR>
 
5254
<H2><A NAME="4.89">4.89. How do I modify a string in place?</A></H2>
 
5255
Strings are immutable (see question 6.2) so you cannot modify a string
 
5256
directly.  If you need an object with this ability, try converting the
 
5257
string to a list or take a look at the array module.
 
5258
<P>
 
5259
<PRE>
 
5260
    >>> s = "Hello, world"
 
5261
    >>> a = list(s)
 
5262
    >>> print a
 
5263
    ['H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd']
 
5264
    >>> a[7:] = list("there!")
 
5265
    >>> import string
 
5266
    >>> print string.join(a, '')
 
5267
    'Hello, there!'
 
5268
</PRE>
 
5269
<PRE>
 
5270
    >>> import array
 
5271
    >>> a = array.array('c', s)
 
5272
    >>> print a
 
5273
    array('c', 'Hello, world')
 
5274
    >>> a[0] = 'y' ; print a
 
5275
    array('c', 'yello world')
 
5276
    >>> a.tostring()
 
5277
    'yello, world'
 
5278
</PRE>
 
5279
<P>
 
5280
 
 
5281
<A HREF="faqw.py?req=edit&amp;file=faq04.089.htp">Edit this entry</A> /
 
5282
<A HREF="faqw.py?req=log&amp;file=faq04.089.htp">Log info</A>
 
5283
 
 
5284
/ Last changed on Tue May 18 01:22:47 1999 by
 
5285
<A HREF="mailto:dalke@bioreason.com">Andrew Dalke</A>
 
5286
<P>
 
5287
 
 
5288
<HR>
 
5289
<H2><A NAME="4.90">4.90. How to pass on keyword/optional parameters/arguments</A></H2>
 
5290
Q: How can I pass on optional or keyword parameters from one function to another?
 
5291
<P>
 
5292
<PRE>
 
5293
        def f1(a, *b, **c):
 
5294
                ...
 
5295
</PRE>
 
5296
A: In Python 2.0 and above:
 
5297
<P>
 
5298
<PRE>
 
5299
        def f2(x, *y, **z):
 
5300
                ...
 
5301
                z['width']='14.3c'
 
5302
                ...
 
5303
                f1(x, *y, **z)
 
5304
</PRE>
 
5305
<PRE>
 
5306
   Note: y can be any sequence (e.g., list or tuple) and z must be a dict.
 
5307
</PRE>
 
5308
<P>
 
5309
A: For versions prior to 2.0, use 'apply', like:
 
5310
<P>
 
5311
<PRE>
 
5312
        def f2(x, *y, **z):
 
5313
                ...
 
5314
                z['width']='14.3c'
 
5315
                ...
 
5316
                apply(f1, (x,)+y, z)
 
5317
</PRE>
 
5318
<P>
 
5319
 
 
5320
<A HREF="faqw.py?req=edit&amp;file=faq04.090.htp">Edit this entry</A> /
 
5321
<A HREF="faqw.py?req=log&amp;file=faq04.090.htp">Log info</A>
 
5322
 
 
5323
/ Last changed on Mon Jun  3 07:20:56 2002 by
 
5324
<A HREF="mailto:smurf@noris.de">Matthias Urlichs</A>
 
5325
<P>
 
5326
 
 
5327
<HR>
 
5328
<H2><A NAME="4.91">4.91. How can I get a dictionary to display its keys in a consistent order?</A></H2>
 
5329
In general, dictionaries store their keys in an unpredictable order,
 
5330
so the display order of a dictionary's elements will be similarly
 
5331
unpredictable.
 
5332
(See 
 
5333
<a href="http://www.python.org/doc/FAQ.html#6.12">Question 6.12</a>
 
5334
to understand why this is so.)
 
5335
<P>
 
5336
This can be frustrating if you want to save a printable version to a 
 
5337
file, make some changes and then compare it with some other printed
 
5338
dictionary.  If you have such needs you can subclass UserDict.UserDict
 
5339
to create a SortedDict class that prints itself in a predictable order.
 
5340
Here's one simpleminded implementation of such a class:
 
5341
<P>
 
5342
<PRE>
 
5343
  import UserDict, string
 
5344
</PRE>
 
5345
<PRE>
 
5346
  class SortedDict(UserDict.UserDict):
 
5347
    def __repr__(self):
 
5348
      result = []
 
5349
      append = result.append
 
5350
      keys = self.data.keys()
 
5351
      keys.sort()
 
5352
      for k in keys:
 
5353
        append("%s: %s" % (`k`, `self.data[k]`))
 
5354
      return "{%s}" % string.join(result, ", ")
 
5355
</PRE>
 
5356
<PRE>
 
5357
    ___str__ = __repr__
 
5358
</PRE>
 
5359
<P>
 
5360
This will work for many common situations you might encounter, though
 
5361
it's far from a perfect solution. (It won't have any effect on the
 
5362
pprint module and does not transparently handle values that are or
 
5363
contain dictionaries.
 
5364
<P>
 
5365
 
 
5366
<A HREF="faqw.py?req=edit&amp;file=faq04.091.htp">Edit this entry</A> /
 
5367
<A HREF="faqw.py?req=log&amp;file=faq04.091.htp">Log info</A>
 
5368
 
 
5369
/ Last changed on Thu Sep 16 17:31:06 1999 by
 
5370
<A HREF="mailto:skip@mojam.com">Skip Montanaro</A>
 
5371
<P>
 
5372
 
 
5373
<HR>
 
5374
<H2><A NAME="4.92">4.92. Is there a Python tutorial?</A></H2>
 
5375
Yes.  See question 1.20 at 
 
5376
<A HREF="http://www.python.org/doc/FAQ.html#1.20">http://www.python.org/doc/FAQ.html#1.20</A>
 
5377
<P>
 
5378
 
 
5379
<A HREF="faqw.py?req=edit&amp;file=faq04.092.htp">Edit this entry</A> /
 
5380
<A HREF="faqw.py?req=log&amp;file=faq04.092.htp">Log info</A>
 
5381
 
 
5382
/ Last changed on Sat Dec  4 16:04:00 1999 by
 
5383
<A HREF="mailto:tbryan@python.net">TAB</A>
 
5384
<P>
 
5385
 
 
5386
<HR>
 
5387
<H2><A NAME="4.93">4.93. Deleted</A></H2>
 
5388
See 4.28
 
5389
<P>
 
5390
 
 
5391
<A HREF="faqw.py?req=edit&amp;file=faq04.093.htp">Edit this entry</A> /
 
5392
<A HREF="faqw.py?req=log&amp;file=faq04.093.htp">Log info</A>
 
5393
 
 
5394
/ Last changed on Tue May 28 20:40:37 2002 by
 
5395
<A HREF="mailto:guido@python.org">GvR</A>
 
5396
<P>
 
5397
 
 
5398
<HR>
 
5399
<H2><A NAME="4.94">4.94. How do I get a single keypress without blocking?</A></H2>
 
5400
There are several solutions; some involve using curses, which is a
 
5401
pretty big thing to learn.  Here's a solution without curses. (see also 4.74, for Windows, see question 8.2)
 
5402
<P>
 
5403
<PRE>
 
5404
  import termios, fcntl, sys, os
 
5405
  fd = sys.stdin.fileno()
 
5406
</PRE>
 
5407
<PRE>
 
5408
  oldterm = termios.tcgetattr(fd)
 
5409
  newattr = termios.tcgetattr(fd)
 
5410
  newattr[3] = newattr[3] &amp; ~termios.ICANON &amp; ~termios.ECHO
 
5411
  termios.tcsetattr(fd, termios.TCSANOW, newattr)
 
5412
</PRE>
 
5413
<PRE>
 
5414
  oldflags = fcntl.fcntl(fd, fcntl.F_GETFL)
 
5415
  fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK)
 
5416
</PRE>
 
5417
<PRE>
 
5418
  try:
 
5419
      while 1:
 
5420
          try:
 
5421
              c = sys.stdin.read(1)
 
5422
              print "Got character", `c`
 
5423
          except IOError: pass
 
5424
  finally:
 
5425
      termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm)
 
5426
      fcntl.fcntl(fd, fcntl.F_SETFL, oldflags)
 
5427
</PRE>
 
5428
<P>
 
5429
You need the termios and the fcntl module for any of this to work, 
 
5430
and I've only tried it on Linux, though it should work elsewhere. 
 
5431
<P>
 
5432
In this code, characters are read and printed one at a time.
 
5433
<P>
 
5434
termios.tcsetattr() turns off stdin's echoing and disables canonical
 
5435
mode.  fcntl.fnctl() is used to obtain stdin's file descriptor flags
 
5436
and modify them for non-blocking mode.  Since reading stdin when it is
 
5437
empty results in an IOError, this error is caught and ignored.
 
5438
<P>
 
5439
 
 
5440
<A HREF="faqw.py?req=edit&amp;file=faq04.094.htp">Edit this entry</A> /
 
5441
<A HREF="faqw.py?req=log&amp;file=faq04.094.htp">Log info</A>
 
5442
 
 
5443
/ Last changed on Thu Oct 24 00:39:06 2002 by
 
5444
<A HREF="mailto:cliechti@gmx.net">chris</A>
 
5445
<P>
 
5446
 
 
5447
<HR>
 
5448
<H2><A NAME="4.95">4.95. Is there an equivalent to Perl chomp()? (Remove trailing newline from string)</A></H2>
 
5449
There are two partial substitutes. If you want to remove all trailing
 
5450
whitespace, use the method string.rstrip(). Otherwise, if there is only
 
5451
one line in the string, use string.splitlines()[0].
 
5452
<P>
 
5453
<PRE>
 
5454
 -----------------------------------------------------------------------
 
5455
</PRE>
 
5456
<PRE>
 
5457
 rstrip() is too greedy, it strips all trailing white spaces.
 
5458
 splitlines() takes ControlM as line boundary.
 
5459
 Consider these strings as input:
 
5460
   "python python    \r\n"
 
5461
   "python\rpython\r\n"
 
5462
   "python python   \r\r\r\n"
 
5463
 The results from rstrip()/splitlines() are perhaps not what we want.
 
5464
</PRE>
 
5465
<PRE>
 
5466
 It seems re can perform this task.
 
5467
</PRE>
 
5468
<P>
 
5469
<PRE>
 
5470
 #!/usr/bin/python 
 
5471
 # requires python2                                                             
 
5472
</PRE>
 
5473
<PRE>
 
5474
 import re, os, StringIO
 
5475
</PRE>
 
5476
<PRE>
 
5477
 lines=StringIO.StringIO(
 
5478
   "The Python Programming Language\r\n"
 
5479
   "The Python Programming Language \r \r \r\r\n"
 
5480
   "The\rProgramming\rLanguage\r\n"
 
5481
   "The\rProgramming\rLanguage\r\r\r\r\n"
 
5482
   "The\r\rProgramming\r\rLanguage\r\r\r\r\n"
 
5483
 )
 
5484
</PRE>
 
5485
<PRE>
 
5486
 ln=re.compile("(?:[\r]?\n|\r)$") # dos:\r\n, unix:\n, mac:\r, others: unknown
 
5487
 # os.linesep does not work if someone ftps(in binary mode) a dos/mac text file
 
5488
 # to your unix box
 
5489
 #ln=re.compile(os.linesep + "$")
 
5490
</PRE>
 
5491
<PRE>
 
5492
 while 1:
 
5493
   s=lines.readline()
 
5494
   if not s: break
 
5495
   print "1.(%s)" % `s.rstrip()`
 
5496
   print "2.(%s)" % `ln.sub( "", s, 1)`
 
5497
   print "3.(%s)" % `s.splitlines()[0]`
 
5498
   print "4.(%s)" % `s.splitlines()`
 
5499
   print
 
5500
</PRE>
 
5501
<PRE>
 
5502
 lines.close()
 
5503
</PRE>
 
5504
<P>
 
5505
 
 
5506
<A HREF="faqw.py?req=edit&amp;file=faq04.095.htp">Edit this entry</A> /
 
5507
<A HREF="faqw.py?req=log&amp;file=faq04.095.htp">Log info</A>
 
5508
 
 
5509
/ Last changed on Wed Aug  8 09:51:34 2001 by
 
5510
<A HREF="mailto:serpent@ms5.hinet.net">Crystal</A>
 
5511
<P>
 
5512
 
 
5513
<HR>
 
5514
<H2><A NAME="4.96">4.96. Why is join() a string method when I'm really joining the elements of a (list, tuple, sequence)?</A></H2>
 
5515
Strings became much more like other standard types starting in release 1.6, when methods were added which give the same functionality that has always been available using the functions of the string module.  These new methods have been widely accepted, but the one which appears to make (some) programmers feel uncomfortable is:
 
5516
<P>
 
5517
<PRE>
 
5518
    ", ".join(['1', '2', '4', '8', '16'])
 
5519
</PRE>
 
5520
which gives the result
 
5521
<P>
 
5522
<PRE>
 
5523
    "1, 2, 4, 8, 16"
 
5524
</PRE>
 
5525
There are two usual arguments against this usage.
 
5526
<P>
 
5527
The first runs along the lines of: "It looks really ugly using a method of a string literal (string constant)", to which the answer is that it might, but a string literal is just a fixed value. If the methods are to be allowed on names bound to strings there is no logical reason to make them unavailable on literals. Get over it!
 
5528
<P>
 
5529
The second objection is typically cast as: "I am really telling a sequence to join its members together with a string constant". Sadly, you aren't. For some reason there seems to be much less difficulty with having split() as a string method, since in that case it is easy to see that
 
5530
<P>
 
5531
<PRE>
 
5532
    "1, 2, 4, 8, 16".split(", ")
 
5533
</PRE>
 
5534
is an instruction to a string literal to return the substrings delimited by the given separator (or, by default, arbitrary runs of white space). In this case a Unicode string returns a list of Unicode strings, an ASCII string returns a list of ASCII strings, and everyone is happy.
 
5535
<P>
 
5536
join() is a string method because in using it you are telling the separator string to iterate over an arbitrary sequence, forming string representations of each of the elements, and inserting itself between the elements' representations.  This method can be used with any argument which obeys the rules for sequence objects, inluding any new classes you might define yourself.
 
5537
<P>
 
5538
Because this is a string method it can work for Unicode strings as well as plain ASCII strings. If join() were a method of the sequence types then the sequence types would have to decide which type of string to return depending on the type of the separator.
 
5539
<P>
 
5540
If none of these arguments persuade you, then for the moment you can continue to use the join() function from the string module, which allows you to write
 
5541
<P>
 
5542
<PRE>
 
5543
    string.join(['1', '2', '4', '8', '16'], ", ")
 
5544
</PRE>
 
5545
You will just have to try and forget that the string module actually uses the syntax you are compaining about to implement the syntax you prefer!
 
5546
<P>
 
5547
 
 
5548
<A HREF="faqw.py?req=edit&amp;file=faq04.096.htp">Edit this entry</A> /
 
5549
<A HREF="faqw.py?req=log&amp;file=faq04.096.htp">Log info</A>
 
5550
 
 
5551
/ Last changed on Fri Aug  2 15:51:58 2002 by
 
5552
<A HREF="mailto:sholden@holdenweb.com">Steve Holden</A>
 
5553
<P>
 
5554
 
 
5555
<HR>
 
5556
<H2><A NAME="4.97">4.97. How can my code discover the name of an object?</A></H2>
 
5557
Generally speaking, it can't, because objects don't really have names. The assignment statement does not store the assigned value in the name but a reference to it. Essentially, assignment creates a binding of a name to a value. The same is true of <I>def</I> and <I>class</I> statements, but in that case the value is a callable. Consider the following code:
 
5558
<P>
 
5559
<PRE>
 
5560
    class A:
 
5561
        pass
 
5562
</PRE>
 
5563
<PRE>
 
5564
    B = A
 
5565
</PRE>
 
5566
<PRE>
 
5567
    a = B()
 
5568
    b = a
 
5569
    print b
 
5570
    &lt;__main__.A instance at 016D07CC&gt;
 
5571
    print a
 
5572
    &lt;__main__.A instance at 016D07CC&gt;
 
5573
</PRE>
 
5574
<P>
 
5575
Arguably the class has a name: even though it is bound to two names and invoked through the name B the created instance is still reported as an instance of class A. However, it is impossible to say whether the instance's name is a or b, since both names are bound to the same value.
 
5576
<P>
 
5577
Generally speaking it should not be necessary for your code to "know the names" of particular values. Unless you are deliberately writing introspective programs, this is usually an indication that a change of approach might be beneficial.
 
5578
<P>
 
5579
 
 
5580
<A HREF="faqw.py?req=edit&amp;file=faq04.097.htp">Edit this entry</A> /
 
5581
<A HREF="faqw.py?req=log&amp;file=faq04.097.htp">Log info</A>
 
5582
 
 
5583
/ Last changed on Thu Mar  8 03:53:39 2001 by
 
5584
<A HREF="mailto:sholden@holdenweb.com">Steve Holden</A>
 
5585
<P>
 
5586
 
 
5587
<HR>
 
5588
<H2><A NAME="4.98">4.98. Why are floating point calculations so inaccurate?</A></H2>
 
5589
The development version of the Python Tutorial now contains an Appendix with more info:
 
5590
<PRE>
 
5591
    <A HREF="http://www.python.org/doc/current/tut/node14.html">http://www.python.org/doc/current/tut/node14.html</A>
 
5592
</PRE>
 
5593
People are often very surprised by results like this:
 
5594
<P>
 
5595
<PRE>
 
5596
 >>> 1.2-1.0
 
5597
 0.199999999999999996
 
5598
</PRE>
 
5599
And think it is a bug in Python. It's not. It's a problem caused by
 
5600
the internal representation of a floating point number. A floating point
 
5601
number is stored as a fixed number of binary digits.
 
5602
<P>
 
5603
In decimal math, there are many numbers that can't be represented
 
5604
with a fixed number of decimal digits, i.e.
 
5605
1/3 = 0.3333333333.......
 
5606
<P>
 
5607
In the binary case, 1/2 = 0.1, 1/4 = 0.01, 1/8 = 0.001, etc. There are 
 
5608
a lot of numbers that can't be represented. The digits are cut off at
 
5609
some point.
 
5610
<P>
 
5611
Since Python 1.6, a floating point's repr() function prints as many
 
5612
digits are necessary to make eval(repr(f)) == f true for any float f.
 
5613
The str() function prints the more sensible number that was probably
 
5614
intended:
 
5615
<P>
 
5616
<PRE>
 
5617
 >>> 0.2
 
5618
 0.20000000000000001
 
5619
 >>> print 0.2
 
5620
 0.2
 
5621
</PRE>
 
5622
Again, this has nothing to do with Python, but with the way the
 
5623
underlying C platform handles floating points, and ultimately with
 
5624
the inaccuracy you'll always have when writing down numbers of fixed
 
5625
number of digit strings.
 
5626
<P>
 
5627
One of the consequences of this is that it is dangerous to compare 
 
5628
the result of some computation to a float with == ! 
 
5629
Tiny inaccuracies may mean that == fails.
 
5630
<P>
 
5631
Instead try something like this:
 
5632
<P>
 
5633
<PRE>
 
5634
 epsilon = 0.0000000000001 # Tiny allowed error
 
5635
 expected_result = 0.4
 
5636
</PRE>
 
5637
<PRE>
 
5638
 if expected_result-epsilon &lt;= computation() &lt;= expected_result+epsilon:
 
5639
    ...
 
5640
</PRE>
 
5641
<P>
 
5642
 
 
5643
<A HREF="faqw.py?req=edit&amp;file=faq04.098.htp">Edit this entry</A> /
 
5644
<A HREF="faqw.py?req=log&amp;file=faq04.098.htp">Log info</A>
 
5645
 
 
5646
/ Last changed on Mon Apr  1 22:18:47 2002 by
 
5647
<A HREF="mailto:fdrake@acm.org">Fred Drake</A>
 
5648
<P>
 
5649
 
 
5650
<HR>
 
5651
<H2><A NAME="4.99">4.99. I tried to open Berkeley DB file, but bsddb produces bsddb.error: (22, 'Invalid argument'). Help! How can I restore my data?</A></H2>
 
5652
Don't panic! Your data are probably intact. The most frequent cause
 
5653
for the error is that you tried to open an earlier Berkeley DB file
 
5654
with a later version of the Berkeley DB library.
 
5655
<P>
 
5656
Many Linux systems now have all three versions of Berkeley DB
 
5657
available.  If you are migrating from version 1 to a newer version use
 
5658
db_dump185 to dump a plain text version of the database.
 
5659
If you are migrating from version 2 to version 3 use db2_dump to create
 
5660
a plain text version of the database.  In either case, use db_load to
 
5661
create a new native database for the latest version installed on your
 
5662
computer.  If you have version 3 of Berkeley DB installed, you should
 
5663
be able to use db2_load to create a native version 2 database.
 
5664
<P>
 
5665
You should probably move away from Berkeley DB version 1 files because
 
5666
the hash file code contains known bugs that can corrupt your data.
 
5667
<P>
 
5668
 
 
5669
<A HREF="faqw.py?req=edit&amp;file=faq04.099.htp">Edit this entry</A> /
 
5670
<A HREF="faqw.py?req=log&amp;file=faq04.099.htp">Log info</A>
 
5671
 
 
5672
/ Last changed on Wed Aug 29 16:04:29 2001 by
 
5673
<A HREF="mailto:skip@pobox.com">Skip Montanaro</A>
 
5674
<P>
 
5675
 
 
5676
<HR>
 
5677
<H2><A NAME="4.100">4.100. What are the &quot;best practices&quot; for using import in a module?</A></H2>
 
5678
First, the standard modules are great.  Use them!  The standard Python library is large and varied.  Using modules can save you time and effort and will reduce maintainenance cost of your code.  (Other programs are dedicated to supporting and fixing bugs in the standard Python modules.  Coworkers may also be familiar with themodules that you use, reducing the amount of time it takes them to understand your code.)
 
5679
<P>
 
5680
The rest of this answer is largely a matter of personal preference, but here's what some newsgroup posters said (thanks to all who responded)
 
5681
<P>
 
5682
In general, don't use 
 
5683
<PRE>
 
5684
 from modulename import *
 
5685
</PRE>
 
5686
Doing so clutters the importer's namespace.  Some avoid this idiom even with the few modules that were designed to be imported in this manner.  (Modules designed in this manner include Tkinter, thread, and wxPython.)
 
5687
<P>
 
5688
Import modules at the top of a file, one module per line.  Doing so makes it clear what other modules your code requires and avoids questions of whether the module name is in scope.  Using one import per line makes it easy to add and delete module imports.
 
5689
<P>
 
5690
Move imports into a local scope (such as at the top of a function definition) if there are a lot of imports, and you're trying to avoid the cost (lots of initialization time) of many imports.  This technique is especially helpful if many of the imports are unnecessary depending on how the program executes.  You may also want to move imports into a function if the modules are only ever used in that function.  Note that loading a module the first time may be expensive (because of the one time initialization of the module) but that loading a module multiple times is virtually free (a couple of dictionary lookups).  Even if the module name has gone out of scope, the module is probably available in sys.modules.  Thus, there isn't really anything wrong with putting no imports at the module level (if they aren't needed) and putting all of the imports at the function level.  
 
5691
<P>
 
5692
It is sometimes necessary to move imports to a function or class to avoid problems with circular imports.  Gordon says:
 
5693
<PRE>
 
5694
 Circular imports are fine where both modules use the "import &lt;module&gt;"
 
5695
 form of import. They fail when the 2nd module wants to grab a name
 
5696
 out of the first ("from module import name") and the import is at
 
5697
 the top level. That's because names in the 1st are not yet available,
 
5698
 (the first module is busy importing the 2nd).  
 
5699
</PRE>
 
5700
In this case, if the 2nd module is only used in one function, then the  import can easily be moved into that function.  By the time the import is called, the first module will have finished initializing, and the second module can do its import.
 
5701
<P>
 
5702
It may also be necessary to move imports out of the top level of code 
 
5703
if some of the modules are platform-specific.  In that case, it may not even be possible to import all of the modules at the top of the file.  In this case, importing the correct modules in the corresponding platform-specific code is a good option.   
 
5704
<P>
 
5705
If only instances of a specific class uses a module, then it is reasonable to import the module in the class's __init__ method and then assign the module to an instance variable so that the module is always available (via that instance variable) during the life of the object.  Note that to delay an import until the class is instantiated, the import must be inside a method.  Putting the import inside the class but outside of any method still causes the import to occur when the module is initialized.
 
5706
<P>
 
5707
 
 
5708
<A HREF="faqw.py?req=edit&amp;file=faq04.100.htp">Edit this entry</A> /
 
5709
<A HREF="faqw.py?req=log&amp;file=faq04.100.htp">Log info</A>
 
5710
 
 
5711
/ Last changed on Sat Aug  4 04:44:47 2001 by
 
5712
<A HREF="mailto:tbryan@python.net">TAB</A>
 
5713
<P>
 
5714
 
 
5715
<HR>
 
5716
<H2><A NAME="4.101">4.101. Is there a tool to help find bugs or perform static analysis?</A></H2>
 
5717
Yes.  PyChecker is a static analysis tool for finding bugs 
 
5718
in Python source code as well as warning about code complexity 
 
5719
and style.
 
5720
<P>
 
5721
You can get PyChecker from:  <A HREF="http://pychecker.sf.net">http://pychecker.sf.net</A>.
 
5722
<P>
 
5723
 
 
5724
<A HREF="faqw.py?req=edit&amp;file=faq04.101.htp">Edit this entry</A> /
 
5725
<A HREF="faqw.py?req=log&amp;file=faq04.101.htp">Log info</A>
 
5726
 
 
5727
/ Last changed on Fri Aug 10 15:42:11 2001 by
 
5728
<A HREF="mailto:neal@metaslash.com">Neal</A>
 
5729
<P>
 
5730
 
 
5731
<HR>
 
5732
<H2><A NAME="4.102">4.102. UnicodeError: ASCII [decoding,encoding] error: ordinal not in range(128)</A></H2>
 
5733
This error indicates that your Python installation can handle
 
5734
only 7-bit ASCII strings.  There are a couple ways to fix or
 
5735
workaround the problem.
 
5736
<P>
 
5737
If your programs must handle data in arbitary character set encodings, the environment the application runs in will generally identify the encoding of the data it is handing you.  You need to convert the input to Unicode data using that encoding.  For instance, a program that handles email or web input will typically find character set encoding information in Content-Type headers.  This can then be used to properly convert input data to Unicode. Assuming the string referred to by "value" is encoded as UTF-8:
 
5738
<P>
 
5739
<PRE>
 
5740
    value = unicode(value, "utf-8")
 
5741
</PRE>
 
5742
will return a Unicode object.  If the data is not correctly encoded as UTF-8, the above call will raise a UnicodeError.
 
5743
<P>
 
5744
If you only want strings coverted to Unicode which have non-ASCII data, you can try converting them first assuming an ASCII encoding, and then generate Unicode objects if that fails:
 
5745
<P>
 
5746
<PRE>
 
5747
    try:
 
5748
        x = unicode(value, "ascii")
 
5749
    except UnicodeError:
 
5750
        value = unicode(value, "utf-8")
 
5751
    else:
 
5752
        # value was valid ASCII data
 
5753
        pass
 
5754
</PRE>
 
5755
<P>
 
5756
If you normally use a character set encoding other than US-ASCII and only need to handle data in that encoding, the simplest way to fix the problem may be simply to set the encoding in sitecustomize.py. The following code is just a modified version of the encoding setup code from site.py with the relevant lines uncommented.
 
5757
<P>
 
5758
<PRE>
 
5759
    # Set the string encoding used by the Unicode implementation.
 
5760
    # The default is 'ascii'
 
5761
    encoding = "ascii" # &lt;= CHANGE THIS if you wish
 
5762
</PRE>
 
5763
<PRE>
 
5764
    # Enable to support locale aware default string encodings.
 
5765
    import locale
 
5766
    loc = locale.getdefaultlocale()
 
5767
    if loc[1]:
 
5768
        encoding = loc[1]
 
5769
    if encoding != "ascii":
 
5770
        import sys
 
5771
        sys.setdefaultencoding(encoding)
 
5772
</PRE>
 
5773
<P>
 
5774
Also note that on Windows, there is an encoding known as "mbcs", which uses an encoding specific to your current locale.  In many cases, and particularly when working with COM, this may be an appropriate default encoding to use.
 
5775
<P>
 
5776
 
 
5777
<A HREF="faqw.py?req=edit&amp;file=faq04.102.htp">Edit this entry</A> /
 
5778
<A HREF="faqw.py?req=log&amp;file=faq04.102.htp">Log info</A>
 
5779
 
 
5780
/ Last changed on Sat Apr 13 04:45:41 2002 by
 
5781
<A HREF="mailto:skip@pobox.com">Skip Montanaro</A>
 
5782
<P>
 
5783
 
 
5784
<HR>
 
5785
<H2><A NAME="4.103">4.103. Using strings to call functions/methods</A></H2>
 
5786
There are various techniques:
 
5787
<P>
 
5788
* Use a dictionary pre-loaded with strings and functions.  The primary
 
5789
advantage of this technique is that the strings do not need to match the
 
5790
names of the functions.  This is also the primary technique used to
 
5791
emulate a case construct:
 
5792
<P>
 
5793
<PRE>
 
5794
    def a():
 
5795
        pass
 
5796
</PRE>
 
5797
<PRE>
 
5798
    def b():
 
5799
        pass
 
5800
</PRE>
 
5801
<PRE>
 
5802
    dispatch = {'go': a, 'stop': b}  # Note lack of parens for funcs
 
5803
</PRE>
 
5804
<PRE>
 
5805
    dispatch[get_input()]()  # Note trailing parens to call function
 
5806
</PRE>
 
5807
* Use the built-in function getattr():
 
5808
<P>
 
5809
<PRE>
 
5810
    import foo
 
5811
    getattr(foo, 'bar')()
 
5812
</PRE>
 
5813
Note that getattr() works on any object, including classes, class
 
5814
instances, modules, and so on.
 
5815
<P>
 
5816
This is used in several places in the standard library, like
 
5817
this:
 
5818
<P>
 
5819
<PRE>
 
5820
    class Foo:
 
5821
        def do_foo(self):
 
5822
            ...
 
5823
</PRE>
 
5824
<PRE>
 
5825
        def do_bar(self):
 
5826
            ...
 
5827
</PRE>
 
5828
<PRE>
 
5829
     f = getattr(foo_instance, 'do_' + opname)
 
5830
     f()
 
5831
</PRE>
 
5832
<P>
 
5833
* Use locals() or eval() to resolve the function name:
 
5834
<P>
 
5835
def myFunc():
 
5836
<PRE>
 
5837
    print "hello"
 
5838
</PRE>
 
5839
fname = "myFunc"
 
5840
<P>
 
5841
f = locals()[fname]
 
5842
f()
 
5843
<P>
 
5844
f = eval(fname)
 
5845
f()
 
5846
<P>
 
5847
Note: Using eval() can be dangerous. If you don't have absolute control
 
5848
over the contents of the string, all sorts of things could happen...
 
5849
<P>
 
5850
 
 
5851
<A HREF="faqw.py?req=edit&amp;file=faq04.103.htp">Edit this entry</A> /
 
5852
<A HREF="faqw.py?req=log&amp;file=faq04.103.htp">Log info</A>
 
5853
 
 
5854
/ Last changed on Thu Mar 21 08:14:58 2002 by
 
5855
<A HREF="mailto:erno-pyfaq@erno.iki.fi">Erno Kuusela</A>
 
5856
<P>
 
5857
 
 
5858
<HR>
 
5859
<H2><A NAME="4.104">4.104. How fast are exceptions?</A></H2>
 
5860
A try/except block is extremely efficient.  Actually executing an
 
5861
exception is expensive.  In older versions of Python (prior to 2.0), it
 
5862
was common to code this idiom:
 
5863
<P>
 
5864
<PRE>
 
5865
    try:
 
5866
        value = dict[key]
 
5867
    except KeyError:
 
5868
        dict[key] = getvalue(key)
 
5869
        value = dict[key]
 
5870
</PRE>
 
5871
This idiom only made sense when you expected the dict to have the key
 
5872
95% of the time or more; other times, you coded it like this:
 
5873
<P>
 
5874
<PRE>
 
5875
    if dict.has_key(key):
 
5876
        value = dict[key]
 
5877
    else:
 
5878
        dict[key] = getvalue(key)
 
5879
        value = dict[key]
 
5880
</PRE>
 
5881
In Python 2.0 and higher, of course, you can code this as
 
5882
<P>
 
5883
<PRE>
 
5884
    value = dict.setdefault(key, getvalue(key))
 
5885
</PRE>
 
5886
However this evaluates getvalue(key) always, regardless of whether it's needed or not.  So if it's slow or has a side effect you should use one of the above variants.
 
5887
<P>
 
5888
 
 
5889
<A HREF="faqw.py?req=edit&amp;file=faq04.104.htp">Edit this entry</A> /
 
5890
<A HREF="faqw.py?req=log&amp;file=faq04.104.htp">Log info</A>
 
5891
 
 
5892
/ Last changed on Mon Dec  9 10:12:30 2002 by
 
5893
<A HREF="mailto:yeti@physics.muni.cz">Yeti</A>
 
5894
<P>
 
5895
 
 
5896
<HR>
 
5897
<H2><A NAME="4.105">4.105. Sharing global variables across modules</A></H2>
 
5898
The canonical way to share information across modules within a single
 
5899
program is to create a special module (often called config or cfg).
 
5900
Just import the config module in all modules of your application; the
 
5901
module then becomes available as a global name.  Because there is only
 
5902
one instance of each module, any changes made to the module object get
 
5903
reflected everywhere.  For example:
 
5904
<P>
 
5905
config.py:
 
5906
<P>
 
5907
<PRE>
 
5908
    pass
 
5909
</PRE>
 
5910
mod.py:
 
5911
<P>
 
5912
<PRE>
 
5913
    import config
 
5914
    config.x = 1
 
5915
</PRE>
 
5916
main.py:
 
5917
<P>
 
5918
<PRE>
 
5919
    import config
 
5920
    import mod
 
5921
    print config.x
 
5922
</PRE>
 
5923
Note that using a module is also the basis for implementing the
 
5924
Singleton design pattern, for the same reason.
 
5925
<P>
 
5926
 
 
5927
<A HREF="faqw.py?req=edit&amp;file=faq04.105.htp">Edit this entry</A> /
 
5928
<A HREF="faqw.py?req=log&amp;file=faq04.105.htp">Log info</A>
 
5929
 
 
5930
/ Last changed on Tue Apr 23 23:07:19 2002 by
 
5931
<A HREF="mailto:aahz@pythoncraft.com">Aahz</A>
 
5932
<P>
 
5933
 
 
5934
<HR>
 
5935
<H2><A NAME="4.106">4.106. Why is cPickle so slow?</A></H2>
 
5936
Use the binary option.  We'd like to make that the default, but it would
 
5937
break backward compatibility:
 
5938
<P>
 
5939
<PRE>
 
5940
    largeString = 'z' * (100 * 1024)
 
5941
    myPickle = cPickle.dumps(largeString, 1)
 
5942
</PRE>
 
5943
<P>
 
5944
 
 
5945
<A HREF="faqw.py?req=edit&amp;file=faq04.106.htp">Edit this entry</A> /
 
5946
<A HREF="faqw.py?req=log&amp;file=faq04.106.htp">Log info</A>
 
5947
 
 
5948
/ Last changed on Thu Aug 22 19:54:25 2002 by
 
5949
<A HREF="mailto:aahz@pythoncraft.com">Aahz</A>
 
5950
<P>
 
5951
 
 
5952
<HR>
 
5953
<H2><A NAME="4.107">4.107. When importing module XXX, why do I get &quot;undefined symbol: PyUnicodeUCS2_...&quot; ?</A></H2>
 
5954
You are using a version of Python that uses a 4-byte representation for
 
5955
Unicode characters, but the extension module you are importing (possibly
 
5956
indirectly) was compiled using a Python that uses a 2-byte representation
 
5957
for Unicode characters (the default).
 
5958
<P>
 
5959
If instead the name of the undefined symbol starts with PyUnicodeUCS4_,
 
5960
the problem is the same by the relationship is reversed:  Python was
 
5961
built using 2-byte Unicode characters, and the extension module was
 
5962
compiled using a Python with 4-byte Unicode characters.
 
5963
<P>
 
5964
This can easily occur when using pre-built extension packages.  RedHat
 
5965
Linux 7.x, in particular, provides a "python2" binary that is compiled
 
5966
with 4-byte Unicode.  This only causes the link failure if the extension
 
5967
uses any of the PyUnicode_*() functions.  It is also a problem if if an
 
5968
extension uses any of the Unicode-related format specifiers for
 
5969
Py_BuildValue (or similar) or parameter-specifications for
 
5970
PyArg_ParseTuple().
 
5971
<P>
 
5972
You can check the size of the Unicode character a Python interpreter is
 
5973
using by checking the value of sys.maxunicode:
 
5974
<P>
 
5975
<PRE>
 
5976
  >>> import sys
 
5977
  >>> if sys.maxunicode > 65535:
 
5978
  ...     print 'UCS4 build'
 
5979
  ... else:
 
5980
  ...     print 'UCS2 build'
 
5981
</PRE>
 
5982
The only way to solve this problem is to use extension modules compiled
 
5983
with a Python binary built using the same size for Unicode characters.
 
5984
<P>
 
5985
 
 
5986
<A HREF="faqw.py?req=edit&amp;file=faq04.107.htp">Edit this entry</A> /
 
5987
<A HREF="faqw.py?req=log&amp;file=faq04.107.htp">Log info</A>
 
5988
 
 
5989
/ Last changed on Tue Aug 27 15:00:17 2002 by
 
5990
<A HREF="mailto:fdrake@acm.org">Fred Drake</A>
 
5991
<P>
 
5992
 
 
5993
<HR>
 
5994
<H2><A NAME="4.108">4.108. How do I create a .pyc file?</A></H2>
 
5995
QUESTION:
 
5996
<P>
 
5997
I have a module and I wish to generate a .pyc file.  
 
5998
How do I do it? Everything I read says that generation of a .pyc file is 
 
5999
"automatic", but I'm not getting anywhere.  
 
6000
<P>
 
6001
<P>
 
6002
ANSWER: 
 
6003
<P>
 
6004
When a module is imported for the first time (or when the source is more
 
6005
recent than the current compiled file) a .pyc file containing the compiled code should be created in the
 
6006
same directory as the .py file.
 
6007
<P>
 
6008
One reason that a .pyc file may not be created is permissions problems with the directory. This can happen, for example, if you develop as one user but run as another, such as if you are testing with a web server.
 
6009
<P>
 
6010
However, in most cases, that's not the problem.
 
6011
<P>
 
6012
Creation of a .pyc file is "automatic" if you are importing a module and Python has the
 
6013
ability (permissions, free space, etc...) to write the compiled module
 
6014
back to the directory. But note that running Python on a top level script is not considered an
 
6015
import and so no .pyc will be created automatically.  For example, if you have a top-level module abc.py that imports another module xyz.py, when you run abc, xyz.pyc will be created since xyz is imported, but no abc.pyc file will be created since abc isn't imported.
 
6016
<P>
 
6017
If you need to create abc.pyc -- that is, to create a .pyc file for a
 
6018
module that is not imported -- you can.  (Look up
 
6019
the py_compile and compileall modules in the Library Reference.) 
 
6020
<P>
 
6021
You can manually compile any module using the "py_compile" module.  One
 
6022
way is to use the compile() function in that module interactively:
 
6023
<P>
 
6024
<PRE>
 
6025
    >>> import py_compile
 
6026
    >>> py_compile.compile('abc.py')
 
6027
</PRE>
 
6028
This will write the .pyc to the same location as abc.py (or you
 
6029
can override that with the optional parameter cfile).
 
6030
<P>
 
6031
You can also automatically compile all files in a directory or
 
6032
directories using the "compileall" module, which can also be run
 
6033
straight from the command line.
 
6034
<P>
 
6035
You can do it from the shell (or DOS) prompt by entering:
 
6036
<PRE>
 
6037
       python compile.py abc.py
 
6038
</PRE>
 
6039
or 
 
6040
<PRE>
 
6041
       python compile.py *
 
6042
</PRE>
 
6043
Or you can write a script to do it on a list of filenames that you enter.
 
6044
<P>
 
6045
<PRE>
 
6046
     import sys
 
6047
     from py_compile import compile
 
6048
</PRE>
 
6049
<PRE>
 
6050
     if len(sys.argv) &lt;= 1:
 
6051
        sys.exit(1)
 
6052
</PRE>
 
6053
<PRE>
 
6054
     for file in sys.argv[1:]:
 
6055
        compile(file)
 
6056
</PRE>
 
6057
ACKNOWLEDGMENTS:
 
6058
<P>
 
6059
Steve Holden, David Bolen, Rich Somerfield,  Oleg Broytmann, Steve Ferg
 
6060
<P>
 
6061
 
 
6062
<A HREF="faqw.py?req=edit&amp;file=faq04.108.htp">Edit this entry</A> /
 
6063
<A HREF="faqw.py?req=log&amp;file=faq04.108.htp">Log info</A>
 
6064
 
 
6065
/ Last changed on Wed Feb 12 15:58:25 2003 by
 
6066
<A HREF="mailto:steve@ferg.org">Stephen Ferg</A>
 
6067
<P>
 
6068
 
 
6069
<HR>
 
6070
<H1>5. Extending Python</H1>
 
6071
 
 
6072
<HR>
 
6073
<H2><A NAME="5.1">5.1. Can I create my own functions in C?</A></H2>
 
6074
Yes, you can create built-in modules containing functions,
 
6075
variables, exceptions and even new types in C.  This is explained in
 
6076
the document "Extending and Embedding the Python Interpreter" (<A HREF="http://www.python.org/doc/current/ext/ext.html">http://www.python.org/doc/current/ext/ext.html</A>).  Also read the chapter
 
6077
on dynamic loading.
 
6078
<P>
 
6079
There's more information on this in each of the Python books:
 
6080
Programming Python, Internet Programming with Python, and Das Python-Buch
 
6081
(in German).
 
6082
<P>
 
6083
 
 
6084
<A HREF="faqw.py?req=edit&amp;file=faq05.001.htp">Edit this entry</A> /
 
6085
<A HREF="faqw.py?req=log&amp;file=faq05.001.htp">Log info</A>
 
6086
 
 
6087
/ Last changed on Mon Dec 10 05:18:57 2001 by
 
6088
<A HREF="mailto:fdrake@acm.org">Fred L. Drake, Jr.</A>
 
6089
<P>
 
6090
 
 
6091
<HR>
 
6092
<H2><A NAME="5.2">5.2. Can I create my own functions in C++?</A></H2>
 
6093
Yes, using the C-compatibility features found in C++.  Basically
 
6094
you place extern "C" { ... } around the Python include files and put
 
6095
extern "C" before each function that is going to be called by the
 
6096
Python interpreter.  Global or static C++ objects with constructors
 
6097
are probably not a good idea.
 
6098
<P>
 
6099
 
 
6100
<A HREF="faqw.py?req=edit&amp;file=faq05.002.htp">Edit this entry</A> /
 
6101
<A HREF="faqw.py?req=log&amp;file=faq05.002.htp">Log info</A>
 
6102
<P>
 
6103
 
 
6104
<HR>
 
6105
<H2><A NAME="5.3">5.3. How can I execute arbitrary Python statements from C?</A></H2>
 
6106
The highest-level function to do this is PyRun_SimpleString() which takes
 
6107
a single string argument which is executed in the context of module
 
6108
__main__ and returns 0 for success and -1 when an exception occurred
 
6109
(including SyntaxError).  If you want more control, use PyRun_String();
 
6110
see the source for PyRun_SimpleString() in Python/pythonrun.c.
 
6111
<P>
 
6112
 
 
6113
<A HREF="faqw.py?req=edit&amp;file=faq05.003.htp">Edit this entry</A> /
 
6114
<A HREF="faqw.py?req=log&amp;file=faq05.003.htp">Log info</A>
 
6115
 
 
6116
/ Last changed on Fri May 23 20:08:14 1997 by
 
6117
<A HREF="mailto:billtut@microsoft.com">Bill Tutt</A>
 
6118
<P>
 
6119
 
 
6120
<HR>
 
6121
<H2><A NAME="5.4">5.4. How can I evaluate an arbitrary Python expression from C?</A></H2>
 
6122
Call the function PyRun_String() from the previous question with the
 
6123
start symbol eval_input (Py_eval_input starting with 1.5a1); it 
 
6124
parses an expression, evaluates it and returns its value.
 
6125
<P>
 
6126
 
 
6127
<A HREF="faqw.py?req=edit&amp;file=faq05.004.htp">Edit this entry</A> /
 
6128
<A HREF="faqw.py?req=log&amp;file=faq05.004.htp">Log info</A>
 
6129
 
 
6130
/ Last changed on Wed May 21 22:23:18 1997 by
 
6131
<A HREF="mailto:david_ascher@brown.edu">David Ascher</A>
 
6132
<P>
 
6133
 
 
6134
<HR>
 
6135
<H2><A NAME="5.5">5.5. How do I extract C values from a Python object?</A></H2>
 
6136
That depends on the object's type.  If it's a tuple,
 
6137
PyTupleSize(o) returns its length and PyTuple_GetItem(o, i)
 
6138
returns its i'th item; similar for lists with PyListSize(o)
 
6139
and PyList_GetItem(o, i).  For strings, PyString_Size(o) returns
 
6140
its length and PyString_AsString(o) a pointer to its value
 
6141
(note that Python strings may contain null bytes so strlen()
 
6142
is not safe).  To test which type an object is, first make sure
 
6143
it isn't NULL, and then use PyString_Check(o), PyTuple_Check(o),
 
6144
PyList_Check(o), etc.
 
6145
<P>
 
6146
There is also a high-level API to Python objects which is
 
6147
provided by the so-called 'abstract' interface -- read
 
6148
Include/abstract.h for further details.  It allows for example 
 
6149
interfacing with any kind of Python sequence (e.g. lists and tuples)
 
6150
using calls like PySequence_Length(), PySequence_GetItem(), etc.)
 
6151
as well as many other useful protocols.
 
6152
<P>
 
6153
 
 
6154
<A HREF="faqw.py?req=edit&amp;file=faq05.005.htp">Edit this entry</A> /
 
6155
<A HREF="faqw.py?req=log&amp;file=faq05.005.htp">Log info</A>
 
6156
 
 
6157
/ Last changed on Wed May 21 22:34:20 1997 by
 
6158
<A HREF="mailto:david_ascher@brown.edu">David Ascher</A>
 
6159
<P>
 
6160
 
 
6161
<HR>
 
6162
<H2><A NAME="5.6">5.6. How do I use Py_BuildValue() to create a tuple of arbitrary length?</A></H2>
 
6163
You can't.  Use t = PyTuple_New(n) instead, and fill it with
 
6164
objects using PyTuple_SetItem(t, i, o) -- note that this "eats" a
 
6165
reference count of o.  Similar for lists with PyList_New(n) and
 
6166
PyList_SetItem(l, i, o).  Note that you <I>must</I> set all the tuple items to
 
6167
some value before you pass the tuple to Python code --
 
6168
PyTuple_New(n) initializes them to NULL, which isn't a valid Python
 
6169
value.
 
6170
<P>
 
6171
 
 
6172
<A HREF="faqw.py?req=edit&amp;file=faq05.006.htp">Edit this entry</A> /
 
6173
<A HREF="faqw.py?req=log&amp;file=faq05.006.htp">Log info</A>
 
6174
 
 
6175
/ Last changed on Thu Jul 31 18:15:29 1997 by
 
6176
<A HREF="mailto:guido@python.org">Guido van Rossum</A>
 
6177
<P>
 
6178
 
 
6179
<HR>
 
6180
<H2><A NAME="5.7">5.7. How do I call an object's method from C?</A></H2>
 
6181
The PyObject_CallMethod() function can be used to call an arbitrary
 
6182
method of an object.  The parameters are the object, the name of the
 
6183
method to call, a format string like that used with Py_BuildValue(), and the argument values:
 
6184
<P>
 
6185
<PRE>
 
6186
    PyObject *
 
6187
    PyObject_CallMethod(PyObject *object, char *method_name,
 
6188
                        char *arg_format, ...);
 
6189
</PRE>
 
6190
This works for any object that has methods -- whether built-in or
 
6191
user-defined.  You are responsible for eventually DECREF'ing the
 
6192
return value.
 
6193
<P>
 
6194
To call, e.g., a file object's "seek" method with arguments 10, 0
 
6195
(assuming the file object pointer is "f"):
 
6196
<P>
 
6197
<PRE>
 
6198
        res = PyObject_CallMethod(f, "seek", "(ii)", 10, 0);
 
6199
        if (res == NULL) {
 
6200
                ... an exception occurred ...
 
6201
        }
 
6202
        else {
 
6203
                Py_DECREF(res);
 
6204
        }
 
6205
</PRE>
 
6206
Note that since PyObject_CallObject() <I>always</I> wants a tuple for the
 
6207
argument list, to call a function without arguments, pass "()" for the
 
6208
format, and to call a function with one argument, surround the argument
 
6209
in parentheses, e.g. "(i)".
 
6210
<P>
 
6211
 
 
6212
<A HREF="faqw.py?req=edit&amp;file=faq05.007.htp">Edit this entry</A> /
 
6213
<A HREF="faqw.py?req=log&amp;file=faq05.007.htp">Log info</A>
 
6214
 
 
6215
/ Last changed on Thu Jun  6 16:15:46 2002 by
 
6216
<A HREF="mailto:neal@metaslash.com">Neal Norwitz</A>
 
6217
<P>
 
6218
 
 
6219
<HR>
 
6220
<H2><A NAME="5.8">5.8. How do I catch the output from PyErr_Print() (or anything that prints to stdout/stderr)?</A></H2>
 
6221
(Due to Mark Hammond):
 
6222
<P>
 
6223
In Python code, define an object that supports the "write()" method.
 
6224
Redirect sys.stdout and sys.stderr to this object.
 
6225
Call print_error, or just allow the standard traceback mechanism to
 
6226
work. Then, the output will go wherever your write() method sends it.
 
6227
<P>
 
6228
The easiest way to do this is to use the StringIO class in the standard
 
6229
library.
 
6230
<P>
 
6231
Sample code and use for catching stdout:
 
6232
<PRE>
 
6233
        >>> class StdoutCatcher:
 
6234
        ...  def __init__(self):
 
6235
        ...   self.data = ''
 
6236
        ...  def write(self, stuff):
 
6237
        ...   self.data = self.data + stuff
 
6238
        ...  
 
6239
        >>> import sys
 
6240
        >>> sys.stdout = StdoutCatcher()
 
6241
        >>> print 'foo'
 
6242
        >>> print 'hello world!'
 
6243
        >>> sys.stderr.write(sys.stdout.data)
 
6244
        foo
 
6245
        hello world!
 
6246
</PRE>
 
6247
<P>
 
6248
 
 
6249
<A HREF="faqw.py?req=edit&amp;file=faq05.008.htp">Edit this entry</A> /
 
6250
<A HREF="faqw.py?req=log&amp;file=faq05.008.htp">Log info</A>
 
6251
 
 
6252
/ Last changed on Wed Dec 16 18:34:25 1998 by
 
6253
<A HREF="mailto:richard@bofh.asn.au">Richard Jones</A>
 
6254
<P>
 
6255
 
 
6256
<HR>
 
6257
<H2><A NAME="5.9">5.9. How do I access a module written in Python from C?</A></H2>
 
6258
You can get a pointer to the module object as follows:
 
6259
<P>
 
6260
<PRE>
 
6261
        module = PyImport_ImportModule("&lt;modulename&gt;");
 
6262
</PRE>
 
6263
If the module hasn't been imported yet (i.e. it is not yet present in
 
6264
sys.modules), this initializes the module; otherwise it simply returns
 
6265
the value of sys.modules["&lt;modulename&gt;"].  Note that it doesn't enter
 
6266
the module into any namespace -- it only ensures it has been
 
6267
initialized and is stored in sys.modules.
 
6268
<P>
 
6269
You can then access the module's attributes (i.e. any name defined in
 
6270
the module) as follows:
 
6271
<P>
 
6272
<PRE>
 
6273
        attr = PyObject_GetAttrString(module, "&lt;attrname&gt;");
 
6274
</PRE>
 
6275
Calling PyObject_SetAttrString(), to assign to variables in the module, also works.
 
6276
<P>
 
6277
 
 
6278
<A HREF="faqw.py?req=edit&amp;file=faq05.009.htp">Edit this entry</A> /
 
6279
<A HREF="faqw.py?req=log&amp;file=faq05.009.htp">Log info</A>
 
6280
 
 
6281
/ Last changed on Wed May 21 22:56:40 1997 by
 
6282
<A HREF="mailto:david_ascher@brown.edu">david ascher</A>
 
6283
<P>
 
6284
 
 
6285
<HR>
 
6286
<H2><A NAME="5.10">5.10. How do I interface to C++ objects from Python?</A></H2>
 
6287
Depending on your requirements, there are many approaches.  To do
 
6288
this manually, begin by reading the "Extending and Embedding" document
 
6289
(Doc/ext.tex, see also <A HREF="http://www.python.org/doc">http://www.python.org/doc</A>/).  Realize
 
6290
that for the Python run-time system, there isn't a whole lot of
 
6291
difference between C and C++ -- so the strategy to build a new Python
 
6292
type around a C structure (pointer) type will also work for C++
 
6293
objects.
 
6294
<P>
 
6295
A useful automated approach (which also works for C) is SWIG:
 
6296
<A HREF="http://www.swig.org">http://www.swig.org</A>/.
 
6297
<P>
 
6298
 
 
6299
<A HREF="faqw.py?req=edit&amp;file=faq05.010.htp">Edit this entry</A> /
 
6300
<A HREF="faqw.py?req=log&amp;file=faq05.010.htp">Log info</A>
 
6301
 
 
6302
/ Last changed on Fri Oct 15 05:14:01 1999 by
 
6303
<A HREF="mailto:sjoerd.mullender@oratrix.nl">Sjoerd Mullender</A>
 
6304
<P>
 
6305
 
 
6306
<HR>
 
6307
<H2><A NAME="5.11">5.11. mSQLmodule (or other old module) won't build with Python 1.5 (or later)</A></H2>
 
6308
Since python-1.4 "Python.h" will have the file includes needed in an 
 
6309
extension module.
 
6310
Backward compatibility is dropped after version 1.4 and therefore 
 
6311
mSQLmodule.c will not build as "allobjects.h" cannot be found. 
 
6312
The following change in mSQLmodule.c is harmless when building it with
 
6313
1.4 and necessary when doing so for later python versions:
 
6314
<P>
 
6315
Remove lines:
 
6316
<P>
 
6317
<PRE>
 
6318
        #include "allobjects.h"
 
6319
        #include "modsupport.h"
 
6320
</PRE>
 
6321
And insert instead:
 
6322
<P>
 
6323
<PRE>
 
6324
        #include "Python.h"
 
6325
</PRE>
 
6326
You may also need to add
 
6327
<P>
 
6328
<PRE>
 
6329
                #include "rename2.h"
 
6330
</PRE>
 
6331
if the module uses "old names".
 
6332
<P>
 
6333
This may happen with other ancient python modules as well,
 
6334
and the same fix applies.
 
6335
<P>
 
6336
 
 
6337
<A HREF="faqw.py?req=edit&amp;file=faq05.011.htp">Edit this entry</A> /
 
6338
<A HREF="faqw.py?req=log&amp;file=faq05.011.htp">Log info</A>
 
6339
 
 
6340
/ Last changed on Sun Dec 21 02:03:35 1997 by
 
6341
<A HREF="mailto:guido@python.org">GvR</A>
 
6342
<P>
 
6343
 
 
6344
<HR>
 
6345
<H2><A NAME="5.12">5.12. I added a module using the Setup file and the make fails! Huh?</A></H2>
 
6346
Setup must end in a newline, if there is no newline there it gets
 
6347
very sad.  Aside from this possibility, maybe you have other
 
6348
non-Python-specific linkage problems.
 
6349
<P>
 
6350
 
 
6351
<A HREF="faqw.py?req=edit&amp;file=faq05.012.htp">Edit this entry</A> /
 
6352
<A HREF="faqw.py?req=log&amp;file=faq05.012.htp">Log info</A>
 
6353
 
 
6354
/ Last changed on Tue Jun 24 15:54:01 1997 by
 
6355
<A HREF="mailto:aaron_watters@msn.com">aaron watters</A>
 
6356
<P>
 
6357
 
 
6358
<HR>
 
6359
<H2><A NAME="5.13">5.13. I want to compile a Python module on my Red Hat Linux system, but some files are missing.</A></H2>
 
6360
Red Hat's RPM for Python doesn't include the 
 
6361
/usr/lib/python1.x/config/ directory, which contains various files required 
 
6362
for compiling Python extensions.
 
6363
Install the python-devel RPM to get the necessary files.
 
6364
<P>
 
6365
 
 
6366
<A HREF="faqw.py?req=edit&amp;file=faq05.013.htp">Edit this entry</A> /
 
6367
<A HREF="faqw.py?req=log&amp;file=faq05.013.htp">Log info</A>
 
6368
 
 
6369
/ Last changed on Tue Jan 26 13:44:04 1999 by
 
6370
<A HREF="mailto:akuchling@acm.org">A.M. Kuchling</A>
 
6371
<P>
 
6372
 
 
6373
<HR>
 
6374
<H2><A NAME="5.14">5.14. What does &quot;SystemError: _PyImport_FixupExtension: module yourmodule not loaded&quot; mean?</A></H2>
 
6375
This means that you have created an extension module named "yourmodule", but your module init function does not initialize with that name.
 
6376
<P>
 
6377
Every module init function will have a line similar to:
 
6378
<P>
 
6379
<PRE>
 
6380
  module = Py_InitModule("yourmodule", yourmodule_functions);
 
6381
</PRE>
 
6382
If the string passed to this function is not the same name as your extenion module, the SystemError will be raised.
 
6383
<P>
 
6384
 
 
6385
<A HREF="faqw.py?req=edit&amp;file=faq05.014.htp">Edit this entry</A> /
 
6386
<A HREF="faqw.py?req=log&amp;file=faq05.014.htp">Log info</A>
 
6387
 
 
6388
/ Last changed on Thu Mar 25 07:16:08 1999 by
 
6389
<A HREF="mailto:mhammond@skippinet.com.au">Mark Hammond</A>
 
6390
<P>
 
6391
 
 
6392
<HR>
 
6393
<H2><A NAME="5.15">5.15. How to tell &quot;incomplete input&quot; from &quot;invalid input&quot;?</A></H2>
 
6394
Sometimes you want to emulate the Python interactive interpreter's
 
6395
behavior, where it gives you a continuation prompt when the input
 
6396
is incomplete (e.g. you typed the start of an "if" statement
 
6397
or you didn't close your parentheses or triple string quotes),
 
6398
but it gives you a syntax error message immediately when the input
 
6399
is invalid.
 
6400
<P>
 
6401
In Python you can use the codeop module, which approximates the
 
6402
parser's behavior sufficiently.  IDLE uses this, for example.
 
6403
<P>
 
6404
The easiest way to do it in C is to call PyRun_InteractiveLoop()
 
6405
(in a separate thread maybe) and let the Python interpreter handle
 
6406
the input for you. You can also set the PyOS_ReadlineFunctionPointer
 
6407
to point at your custom input function. See Modules/readline.c and
 
6408
Parser/myreadline.c for more hints.
 
6409
<P>
 
6410
However sometimes you have to run the embedded Python interpreter
 
6411
in the same thread as your rest application and you can't allow the
 
6412
PyRun_InteractiveLoop() to stop while waiting for user input.
 
6413
The one solution then is to call PyParser_ParseString()
 
6414
and test for e.error equal to E_EOF (then the input is incomplete).
 
6415
Sample code fragment, untested, inspired by code from  Alex Farber:
 
6416
<P>
 
6417
<PRE>
 
6418
  #include &lt;Python.h&gt;
 
6419
  #include &lt;node.h&gt;
 
6420
  #include &lt;errcode.h&gt;
 
6421
  #include &lt;grammar.h&gt;
 
6422
  #include &lt;parsetok.h&gt;
 
6423
  #include &lt;compile.h&gt;
 
6424
</PRE>
 
6425
<PRE>
 
6426
  int testcomplete(char *code)
 
6427
    /* code should end in \n */
 
6428
    /* return -1 for error, 0 for incomplete, 1 for complete */
 
6429
  {
 
6430
    node *n;
 
6431
    perrdetail e;
 
6432
</PRE>
 
6433
<PRE>
 
6434
    n = PyParser_ParseString(code, &amp;_PyParser_Grammar,
 
6435
                             Py_file_input, &amp;e);
 
6436
    if (n == NULL) {
 
6437
      if (e.error == E_EOF) 
 
6438
        return 0;
 
6439
      return -1;
 
6440
    }
 
6441
</PRE>
 
6442
<PRE>
 
6443
    PyNode_Free(n);
 
6444
    return 1;
 
6445
  }
 
6446
</PRE>
 
6447
Another solution is trying to compile the received string with 
 
6448
Py_CompileString(). If it compiles fine - try to execute the returned 
 
6449
code object by calling PyEval_EvalCode(). Otherwise save the input for 
 
6450
later. If the compilation fails, find out if it's an error or just 
 
6451
more input is required - by extracting the message string from the 
 
6452
exception tuple and comparing it to the "unexpected EOF while parsing".
 
6453
Here is a complete example using the GNU readline library (you may
 
6454
want to ignore SIGINT while calling readline()):
 
6455
<P>
 
6456
<PRE>
 
6457
  #include &lt;stdio.h&gt;
 
6458
  #include &lt;readline.h&gt;
 
6459
</PRE>
 
6460
<PRE>
 
6461
  #include &lt;Python.h&gt;
 
6462
  #include &lt;object.h&gt;
 
6463
  #include &lt;compile.h&gt;
 
6464
  #include &lt;eval.h&gt;
 
6465
</PRE>
 
6466
<PRE>
 
6467
  int main (int argc, char* argv[])
 
6468
  {
 
6469
    int i, j, done = 0;                          /* lengths of line, code */
 
6470
    char ps1[] = ">>> ";
 
6471
    char ps2[] = "... ";
 
6472
    char *prompt = ps1;
 
6473
    char *msg, *line, *code = NULL;
 
6474
    PyObject *src, *glb, *loc;
 
6475
    PyObject *exc, *val, *trb, *obj, *dum;
 
6476
</PRE>
 
6477
<PRE>
 
6478
    Py_Initialize ();
 
6479
    loc = PyDict_New ();
 
6480
    glb = PyDict_New ();
 
6481
    PyDict_SetItemString (glb, "__builtins__", PyEval_GetBuiltins ());
 
6482
</PRE>
 
6483
<PRE>
 
6484
    while (!done)
 
6485
    {
 
6486
      line = readline (prompt);
 
6487
</PRE>
 
6488
<PRE>
 
6489
      if (NULL == line)                          /* CTRL-D pressed */
 
6490
      {
 
6491
        done = 1;
 
6492
      }
 
6493
      else
 
6494
      {
 
6495
        i = strlen (line);
 
6496
</PRE>
 
6497
<PRE>
 
6498
        if (i > 0)
 
6499
          add_history (line);                    /* save non-empty lines */
 
6500
</PRE>
 
6501
<PRE>
 
6502
        if (NULL == code)                        /* nothing in code yet */
 
6503
          j = 0;
 
6504
        else
 
6505
          j = strlen (code);
 
6506
</PRE>
 
6507
<PRE>
 
6508
        code = realloc (code, i + j + 2);
 
6509
        if (NULL == code)                        /* out of memory */
 
6510
          exit (1);
 
6511
</PRE>
 
6512
<PRE>
 
6513
        if (0 == j)                              /* code was empty, so */
 
6514
          code[0] = '\0';                        /* keep strncat happy */
 
6515
</PRE>
 
6516
<PRE>
 
6517
        strncat (code, line, i);                 /* append line to code */
 
6518
        code[i + j] = '\n';                      /* append '\n' to code */
 
6519
        code[i + j + 1] = '\0';
 
6520
</PRE>
 
6521
<PRE>
 
6522
        src = Py_CompileString (code, "&lt;stdin&gt;", Py_single_input);       
 
6523
</PRE>
 
6524
<PRE>
 
6525
        if (NULL != src)                         /* compiled just fine - */
 
6526
        {
 
6527
          if (ps1  == prompt ||                  /* "&gt;&gt;&gt; " or */
 
6528
              '\n' == code[i + j - 1])           /* "... " and double '\n' */
 
6529
          {                                               /* so execute it */
 
6530
            dum = PyEval_EvalCode ((PyCodeObject *)src, glb, loc);
 
6531
            Py_XDECREF (dum);
 
6532
            Py_XDECREF (src);
 
6533
            free (code);
 
6534
            code = NULL;
 
6535
            if (PyErr_Occurred ())
 
6536
              PyErr_Print ();
 
6537
            prompt = ps1;
 
6538
          }
 
6539
        }                                        /* syntax error or E_EOF? */
 
6540
        else if (PyErr_ExceptionMatches (PyExc_SyntaxError))           
 
6541
        {
 
6542
          PyErr_Fetch (&amp;exc, &amp;val, &amp;trb);        /* clears exception! */
 
6543
</PRE>
 
6544
<PRE>
 
6545
          if (PyArg_ParseTuple (val, "sO", &amp;msg, &amp;obj) &amp;&amp;
 
6546
              !strcmp (msg, "unexpected EOF while parsing")) /* E_EOF */
 
6547
          {
 
6548
            Py_XDECREF (exc);
 
6549
            Py_XDECREF (val);
 
6550
            Py_XDECREF (trb);
 
6551
            prompt = ps2;
 
6552
          }
 
6553
          else                                   /* some other syntax error */
 
6554
          {
 
6555
            PyErr_Restore (exc, val, trb);
 
6556
            PyErr_Print ();
 
6557
            free (code);
 
6558
            code = NULL;
 
6559
            prompt = ps1;
 
6560
          }
 
6561
        }
 
6562
        else                                     /* some non-syntax error */
 
6563
        {
 
6564
          PyErr_Print ();
 
6565
          free (code);
 
6566
          code = NULL;
 
6567
          prompt = ps1;
 
6568
        }
 
6569
</PRE>
 
6570
<PRE>
 
6571
        free (line);
 
6572
      }
 
6573
    }
 
6574
</PRE>
 
6575
<PRE>
 
6576
    Py_XDECREF(glb);
 
6577
    Py_XDECREF(loc);
 
6578
    Py_Finalize();
 
6579
    exit(0);
 
6580
  }
 
6581
</PRE>
 
6582
<P>
 
6583
 
 
6584
<A HREF="faqw.py?req=edit&amp;file=faq05.015.htp">Edit this entry</A> /
 
6585
<A HREF="faqw.py?req=log&amp;file=faq05.015.htp">Log info</A>
 
6586
 
 
6587
/ Last changed on Wed Mar 15 09:47:24 2000 by
 
6588
<A HREF="mailto:farber@cpan.org">Alex Farber</A>
 
6589
<P>
 
6590
 
 
6591
<HR>
 
6592
<H2><A NAME="5.16">5.16. How do I debug an extension?</A></H2>
 
6593
When using gdb with dynamically loaded extensions, you can't set a 
 
6594
breakpoint in your extension until your extension is loaded.
 
6595
<P>
 
6596
In your .gdbinit file (or interactively), add the command
 
6597
<P>
 
6598
br _PyImport_LoadDynamicModule
 
6599
<P>
 
6600
<P>
 
6601
$ gdb /local/bin/python
 
6602
<P>
 
6603
gdb) run myscript.py
 
6604
<P>
 
6605
gdb) continue # repeat until your extension is loaded
 
6606
<P>
 
6607
gdb) finish   # so that your extension is loaded
 
6608
<P>
 
6609
gdb) br myfunction.c:50
 
6610
<P>
 
6611
gdb) continue
 
6612
<P>
 
6613
 
 
6614
<A HREF="faqw.py?req=edit&amp;file=faq05.016.htp">Edit this entry</A> /
 
6615
<A HREF="faqw.py?req=log&amp;file=faq05.016.htp">Log info</A>
 
6616
 
 
6617
/ Last changed on Fri Oct 20 11:10:32 2000 by
 
6618
<A HREF="mailto:vanandel@ucar.edu">Joe VanAndel</A>
 
6619
<P>
 
6620
 
 
6621
<HR>
 
6622
<H2><A NAME="5.17">5.17. How do I find undefined Linux g++ symbols, __builtin_new or __pure_virtural</A></H2>
 
6623
To dynamically load g++ extension modules, you must recompile python, relink python using g++ (change LINKCC in the python Modules Makefile), and link your extension module using g++ (e.g., "g++ -shared -o mymodule.so mymodule.o").
 
6624
<P>
 
6625
 
 
6626
<A HREF="faqw.py?req=edit&amp;file=faq05.017.htp">Edit this entry</A> /
 
6627
<A HREF="faqw.py?req=log&amp;file=faq05.017.htp">Log info</A>
 
6628
 
 
6629
/ Last changed on Sun Jan 14 18:03:51 2001 by
 
6630
<A HREF="mailto:dbo@angryduck.com">douglas orr</A>
 
6631
<P>
 
6632
 
 
6633
<HR>
 
6634
<H2><A NAME="5.18">5.18. How do I define and create objects corresponding to built-in/extension types</A></H2>
 
6635
Usually you would like to be able to inherit from a Python type when
 
6636
you ask this question. The bottom line for Python 2.2 is: types and classes are miscible. You build instances by calling classes, and you can build subclasses to your heart's desire.
 
6637
<P>
 
6638
You need to be careful when instantiating immutable types like integers or strings. See <A HREF="http://www.amk.ca/python/2.2">http://www.amk.ca/python/2.2</A>/, section 2, for details.
 
6639
<P>
 
6640
Prior to version 2.2, Python (like Java) insisted that there are first-class and second-class objects (the former are types, the latter classes), and never the twain shall meet.
 
6641
<P>
 
6642
The library has, however, done a good job of providing class wrappers for the more commonly desired objects (see UserDict, UserList and UserString for examples), and more are always welcome if you happen to be in the mood to write code. These wrappers still exist in Python 2.2.
 
6643
<P>
 
6644
 
 
6645
<A HREF="faqw.py?req=edit&amp;file=faq05.018.htp">Edit this entry</A> /
 
6646
<A HREF="faqw.py?req=log&amp;file=faq05.018.htp">Log info</A>
 
6647
 
 
6648
/ Last changed on Mon Jun 10 15:14:07 2002 by
 
6649
<A HREF="mailto:smurf@noris.de">Matthias Urlichs</A>
 
6650
<P>
 
6651
 
 
6652
<HR>
 
6653
<H1>6. Python's design</H1>
 
6654
 
 
6655
<HR>
 
6656
<H2><A NAME="6.1">6.1. Why isn't there a switch or case statement in Python?</A></H2>
 
6657
You can do this easily enough with a sequence of
 
6658
if... elif... elif... else.  There have been some proposals for switch
 
6659
statement syntax, but there is no consensus (yet) on whether and how
 
6660
to do range tests.
 
6661
<P>
 
6662
 
 
6663
<A HREF="faqw.py?req=edit&amp;file=faq06.001.htp">Edit this entry</A> /
 
6664
<A HREF="faqw.py?req=log&amp;file=faq06.001.htp">Log info</A>
 
6665
<P>
 
6666
 
 
6667
<HR>
 
6668
<H2><A NAME="6.2">6.2. Why does Python use indentation for grouping of statements?</A></H2>
 
6669
Basically I believe that using indentation for grouping is
 
6670
extremely elegant and contributes a lot to the clarity of the average
 
6671
Python program.  Most people learn to love this feature after a while.
 
6672
Some arguments for it:
 
6673
<P>
 
6674
Since there are no begin/end brackets there cannot be a disagreement
 
6675
between grouping perceived by the parser and the human reader.  I
 
6676
remember long ago seeing a C fragment like this:
 
6677
<P>
 
6678
<PRE>
 
6679
        if (x &lt;= y)
 
6680
                x++;
 
6681
                y--;
 
6682
        z++;
 
6683
</PRE>
 
6684
and staring a long time at it wondering why y was being decremented
 
6685
even for x > y...  (And I wasn't a C newbie then either.)
 
6686
<P>
 
6687
Since there are no begin/end brackets, Python is much less prone to
 
6688
coding-style conflicts.  In C there are loads of different ways to
 
6689
place the braces (including the choice whether to place braces around
 
6690
single statements in certain cases, for consistency).  If you're used
 
6691
to reading (and writing) code that uses one style, you will feel at
 
6692
least slightly uneasy when reading (or being required to write)
 
6693
another style.
 
6694
Many coding styles place begin/end brackets on a line by themself.
 
6695
This makes programs considerably longer and wastes valuable screen
 
6696
space, making it harder to get a good overview over a program.
 
6697
Ideally, a function should fit on one basic tty screen (say, 20
 
6698
lines).  20 lines of Python are worth a LOT more than 20 lines of C.
 
6699
This is not solely due to the lack of begin/end brackets (the lack of
 
6700
declarations also helps, and the powerful operations of course), but
 
6701
it certainly helps!
 
6702
<P>
 
6703
 
 
6704
<A HREF="faqw.py?req=edit&amp;file=faq06.002.htp">Edit this entry</A> /
 
6705
<A HREF="faqw.py?req=log&amp;file=faq06.002.htp">Log info</A>
 
6706
 
 
6707
/ Last changed on Wed May 21 16:00:15 1997 by
 
6708
<A HREF="mailto:guido@python.org">GvR</A>
 
6709
<P>
 
6710
 
 
6711
<HR>
 
6712
<H2><A NAME="6.3">6.3. Why are Python strings immutable?</A></H2>
 
6713
There are two advantages.  One is performance: knowing that a
 
6714
string is immutable makes it easy to lay it out at construction time
 
6715
-- fixed and unchanging storage requirements.  (This is also one of
 
6716
the reasons for the distinction between tuples and lists.)  The
 
6717
other is that strings in Python are considered as "elemental" as
 
6718
numbers.  No amount of activity will change the value 8 to anything
 
6719
else, and in Python, no amount of activity will change the string
 
6720
"eight" to anything else.  (Adapted from Jim Roskind)
 
6721
<P>
 
6722
 
 
6723
<A HREF="faqw.py?req=edit&amp;file=faq06.003.htp">Edit this entry</A> /
 
6724
<A HREF="faqw.py?req=log&amp;file=faq06.003.htp">Log info</A>
 
6725
<P>
 
6726
 
 
6727
<HR>
 
6728
<H2><A NAME="6.4">6.4. Delete</A></H2>
 
6729
<P>
 
6730
<P>
 
6731
 
 
6732
<A HREF="faqw.py?req=edit&amp;file=faq06.004.htp">Edit this entry</A> /
 
6733
<A HREF="faqw.py?req=log&amp;file=faq06.004.htp">Log info</A>
 
6734
 
 
6735
/ Last changed on Tue Jan  2 03:05:25 2001 by
 
6736
<A HREF="mailto:moshez@zadka.site.co.il">Moshe Zadka</A>
 
6737
<P>
 
6738
 
 
6739
<HR>
 
6740
<H2><A NAME="6.5">6.5. Why does Python use methods for some functionality (e.g. list.index()) but functions for other (e.g. len(list))?</A></H2>
 
6741
The major reason is history. Functions were used for those
 
6742
operations that were generic for a group of types and which
 
6743
were intended to work even for objects that didn't have
 
6744
methods at all (e.g. numbers before type/class unification
 
6745
began, or tuples).
 
6746
<P>
 
6747
It is also convenient to have a function that can readily be applied
 
6748
to an amorphous collection of objects when you use the functional features of Python (map(), apply() et al).
 
6749
<P>
 
6750
In fact, implementing len(), max(), min() as a built-in function is
 
6751
actually less code than implementing them as methods for each type.
 
6752
One can quibble about individual cases but it's a part of Python,
 
6753
and it's too late to change such things fundamentally now. The
 
6754
functions have to remain to avoid massive code breakage.
 
6755
<P>
 
6756
Note that for string operations Python has moved from external functions
 
6757
(the string module) to methods.  However, len() is still a function.
 
6758
<P>
 
6759
 
 
6760
<A HREF="faqw.py?req=edit&amp;file=faq06.005.htp">Edit this entry</A> /
 
6761
<A HREF="faqw.py?req=log&amp;file=faq06.005.htp">Log info</A>
 
6762
 
 
6763
/ Last changed on Thu May 30 14:08:58 2002 by
 
6764
<A HREF="mailto:sholden@holdenweb.com">Steve Holden</A>
 
6765
<P>
 
6766
 
 
6767
<HR>
 
6768
<H2><A NAME="6.6">6.6. Why can't I derive a class from built-in types (e.g. lists or files)?</A></H2>
 
6769
As of Python 2.2, you can derive from built-in types.  For previous versions, the answer is:
 
6770
<P>
 
6771
This is caused by the relatively late addition of (user-defined)
 
6772
classes to the language -- the implementation framework doesn't easily
 
6773
allow it.  See the answer to question 4.2 for a work-around.  This
 
6774
<I>may</I> be fixed in the (distant) future.
 
6775
<P>
 
6776
 
 
6777
<A HREF="faqw.py?req=edit&amp;file=faq06.006.htp">Edit this entry</A> /
 
6778
<A HREF="faqw.py?req=log&amp;file=faq06.006.htp">Log info</A>
 
6779
 
 
6780
/ Last changed on Thu May 23 02:53:22 2002 by
 
6781
<A HREF="mailto:neal@metaslash.com">Neal Norwitz</A>
 
6782
<P>
 
6783
 
 
6784
<HR>
 
6785
<H2><A NAME="6.7">6.7. Why must 'self' be declared and used explicitly in method definitions and calls?</A></H2>
 
6786
So, is your current programming language C++ or Java? :-)
 
6787
When classes were added to Python, this was (again) the simplest way of
 
6788
implementing methods without too many changes to the interpreter.  The
 
6789
idea was borrowed from Modula-3.  It turns out to be very useful, for
 
6790
a variety of reasons.
 
6791
<P>
 
6792
First, it makes it more obvious that you are using a method or
 
6793
instance attribute instead of a local variable.  Reading "self.x" or
 
6794
"self.meth()" makes it absolutely clear that an instance variable or
 
6795
method is used even if you don't know the class definition by heart.
 
6796
In C++, you can sort of tell by the lack of a local variable
 
6797
declaration (assuming globals are rare or easily recognizable) -- but
 
6798
in Python, there are no local variable declarations, so you'd have to
 
6799
look up the class definition to be sure.
 
6800
<P>
 
6801
Second, it means that no special syntax is necessary if you want to
 
6802
explicitly reference or call the method from a particular class.  In
 
6803
C++, if you want to use a method from base class that is overridden in
 
6804
a derived class, you have to use the :: operator -- in Python you can
 
6805
write baseclass.methodname(self, &lt;argument list&gt;).  This is
 
6806
particularly useful for __init__() methods, and in general in cases
 
6807
where a derived class method wants to extend the base class method of
 
6808
the same name and thus has to call the base class method somehow.
 
6809
<P>
 
6810
Lastly, for instance variables, it solves a syntactic problem with
 
6811
assignment: since local variables in Python are (by definition!) those
 
6812
variables to which a value assigned in a function body (and that
 
6813
aren't explicitly declared global), there has to be some way to tell
 
6814
the interpreter that an assignment was meant to assign to an instance
 
6815
variable instead of to a local variable, and it should preferably be
 
6816
syntactic (for efficiency reasons).  C++ does this through
 
6817
declarations, but Python doesn't have declarations and it would be a
 
6818
pity having to introduce them just for this purpose.  Using the
 
6819
explicit "self.var" solves this nicely.  Similarly, for using instance
 
6820
variables, having to write "self.var" means that references to
 
6821
unqualified names inside a method don't have to search the instance's
 
6822
directories.
 
6823
<P>
 
6824
 
 
6825
<A HREF="faqw.py?req=edit&amp;file=faq06.007.htp">Edit this entry</A> /
 
6826
<A HREF="faqw.py?req=log&amp;file=faq06.007.htp">Log info</A>
 
6827
 
 
6828
/ Last changed on Fri Jan 12 08:01:50 2001 by
 
6829
<A HREF="mailto:sholden@holdenweb.com">Steve Holden</A>
 
6830
<P>
 
6831
 
 
6832
<HR>
 
6833
<H2><A NAME="6.8">6.8. Can't you emulate threads in the interpreter instead of relying on an OS-specific thread implementation?</A></H2>
 
6834
Answer 1: Unfortunately, the interpreter pushes at least one C stack
 
6835
frame for each Python stack frame.  Also, extensions can call back into
 
6836
Python at almost random moments.  Therefore a complete threads
 
6837
implementation requires thread support for C.
 
6838
<P>
 
6839
Answer 2: Fortunately, there is Stackless Python, which has a completely redesigned interpreter loop that avoids the C stack. It's still experimental but looks very promising. Although it is binary compatible with standard Python, it's still unclear whether Stackless will make it into the core -- maybe it's just too revolutionary. Stackless Python currently lives here: <A HREF="http://www.stackless.com">http://www.stackless.com</A>. A microthread implementation that uses it can be found here: <A HREF="http://world.std.com/~wware/uthread.html">http://world.std.com/~wware/uthread.html</A>.
 
6840
<P>
 
6841
 
 
6842
<A HREF="faqw.py?req=edit&amp;file=faq06.008.htp">Edit this entry</A> /
 
6843
<A HREF="faqw.py?req=log&amp;file=faq06.008.htp">Log info</A>
 
6844
 
 
6845
/ Last changed on Sat Apr 15 08:18:16 2000 by
 
6846
<A HREF="mailto:just@letterror.com">Just van Rossum</A>
 
6847
<P>
 
6848
 
 
6849
<HR>
 
6850
<H2><A NAME="6.9">6.9. Why can't lambda forms contain statements?</A></H2>
 
6851
Python lambda forms cannot contain statements because Python's
 
6852
syntactic framework can't handle statements nested inside expressions.
 
6853
<P>
 
6854
However, in Python, this is not a serious problem.  Unlike lambda
 
6855
forms in other languages, where they add functionality, Python lambdas
 
6856
are only a shorthand notation if you're too lazy to define a function.
 
6857
<P>
 
6858
Functions are already first class objects in Python, and can be
 
6859
declared in a local scope.  Therefore the only advantage of using a
 
6860
lambda form instead of a locally-defined function is that you don't need to invent a name for the function -- but that's just a local variable to which the function object (which is exactly the same type of object that a lambda form yields) is assigned!
 
6861
<P>
 
6862
 
 
6863
<A HREF="faqw.py?req=edit&amp;file=faq06.009.htp">Edit this entry</A> /
 
6864
<A HREF="faqw.py?req=log&amp;file=faq06.009.htp">Log info</A>
 
6865
 
 
6866
/ Last changed on Sun Jun 14 14:15:17 1998 by
 
6867
<A HREF="mailto:tim_one@email.msn.com">Tim Peters</A>
 
6868
<P>
 
6869
 
 
6870
<HR>
 
6871
<H2><A NAME="6.10">6.10. [deleted]</A></H2>
 
6872
[lambda vs non-nested scopes used to be here]
 
6873
<P>
 
6874
 
 
6875
<A HREF="faqw.py?req=edit&amp;file=faq06.010.htp">Edit this entry</A> /
 
6876
<A HREF="faqw.py?req=log&amp;file=faq06.010.htp">Log info</A>
 
6877
 
 
6878
/ Last changed on Thu Mar 21 05:20:56 2002 by
 
6879
<A HREF="mailto:erno-pyfaq@erno.iki.fi">Erno Kuusela</A>
 
6880
<P>
 
6881
 
 
6882
<HR>
 
6883
<H2><A NAME="6.11">6.11. [deleted]</A></H2>
 
6884
[recursive functions vs non-nested scopes used to be here]
 
6885
<P>
 
6886
 
 
6887
<A HREF="faqw.py?req=edit&amp;file=faq06.011.htp">Edit this entry</A> /
 
6888
<A HREF="faqw.py?req=log&amp;file=faq06.011.htp">Log info</A>
 
6889
 
 
6890
/ Last changed on Thu Mar 21 05:22:04 2002 by
 
6891
<A HREF="mailto:erno-pyfaq@erno.iki.fi">Erno Kuusela</A>
 
6892
<P>
 
6893
 
 
6894
<HR>
 
6895
<H2><A NAME="6.12">6.12. Why is there no more efficient way of iterating over a dictionary than first constructing the list of keys()?</A></H2>
 
6896
As of Python 2.2, you can now iterate over a dictionary directly,
 
6897
using the new implied dictionary iterator:
 
6898
<P>
 
6899
<PRE>
 
6900
    for k in d: ...
 
6901
</PRE>
 
6902
There are also methods returning iterators over the values and items:
 
6903
<P>
 
6904
<PRE>
 
6905
    for k in d.iterkeys(): # same as above
 
6906
    for v in d.itervalues(): # iterate over values
 
6907
    for k, v in d.iteritems(): # iterate over items
 
6908
</PRE>
 
6909
All these require that you do not modify the dictionary during the loop.
 
6910
<P>
 
6911
For previous Python versions, the following defense should do:
 
6912
<P>
 
6913
Have you tried it?  I bet it's fast enough for your purposes!  In
 
6914
most cases such a list takes only a few percent of the space occupied
 
6915
by the dictionary.  Apart from the fixed header,
 
6916
the list needs only 4 bytes (the size of a pointer) per
 
6917
key.  A dictionary uses 12 bytes per key plus between 30 and 70
 
6918
percent hash table overhead, plus the space for the keys and values.
 
6919
By necessity, all keys are distinct objects, and a string object (the most
 
6920
common key type) costs at least 20 bytes plus the length of the
 
6921
string.  Add to that the values contained in the dictionary, and you
 
6922
see that 4 bytes more per item really isn't that much more memory...
 
6923
<P>
 
6924
A call to dict.keys() makes one fast scan over the dictionary
 
6925
(internally, the iteration function does exist) copying the pointers
 
6926
to the key objects into a pre-allocated list object of the right size.
 
6927
The iteration time isn't lost (since you'll have to iterate anyway --
 
6928
unless in the majority of cases your loop terminates very prematurely
 
6929
(which I doubt since you're getting the keys in random order).
 
6930
<P>
 
6931
I don't expose the dictionary iteration operation to Python
 
6932
programmers because the dictionary shouldn't be modified during the
 
6933
entire iteration -- if it is, there's a small chance that the
 
6934
dictionary is reorganized because the hash table becomes too full, and
 
6935
then the iteration may miss some items and see others twice.  Exactly
 
6936
because this only occurs rarely, it would lead to hidden bugs in
 
6937
programs: it's easy never to have it happen during test runs if you
 
6938
only insert or delete a few items per iteration -- but your users will 
 
6939
surely hit upon it sooner or later.
 
6940
<P>
 
6941
 
 
6942
<A HREF="faqw.py?req=edit&amp;file=faq06.012.htp">Edit this entry</A> /
 
6943
<A HREF="faqw.py?req=log&amp;file=faq06.012.htp">Log info</A>
 
6944
 
 
6945
/ Last changed on Fri May 24 21:24:08 2002 by
 
6946
<A HREF="mailto:guido@python.org">GvR</A>
 
6947
<P>
 
6948
 
 
6949
<HR>
 
6950
<H2><A NAME="6.13">6.13. Can Python be compiled to machine code, C or some other language?</A></H2>
 
6951
Not easily.  Python's high level data types, dynamic typing of
 
6952
objects and run-time invocation of the interpreter (using eval() or
 
6953
exec) together mean that a "compiled" Python program would probably
 
6954
consist mostly of calls into the Python run-time system, even for
 
6955
seemingly simple operations like "x+1".
 
6956
<P>
 
6957
Several projects described in the Python newsgroup or at past
 
6958
Python conferences have shown that this approach is feasible,
 
6959
although the speedups reached so far are only modest (e.g. 2x).
 
6960
JPython uses the same strategy for compiling to Java bytecode.
 
6961
(Jim Hugunin has demonstrated that in combination with whole-program
 
6962
analysis, speedups of 1000x are feasible for small demo programs.
 
6963
See the website for the 1997 Python conference.)
 
6964
<P>
 
6965
Internally, Python source code is always translated into a "virtual
 
6966
machine code" or "byte code" representation before it is interpreted
 
6967
(by the "Python virtual machine" or "bytecode interpreter").  In order
 
6968
to avoid the overhead of parsing and translating modules that rarely
 
6969
change over and over again, this byte code is written on a file whose
 
6970
name ends in ".pyc" whenever a module is parsed (from a file whose
 
6971
name ends in ".py").  When the corresponding .py file is changed, it
 
6972
is parsed and translated again and the .pyc file is rewritten.
 
6973
<P>
 
6974
There is no performance difference once the .pyc file has been loaded
 
6975
(the bytecode read from the .pyc file is exactly the same as the bytecode
 
6976
created by direct translation).  The only difference is that loading
 
6977
code from a .pyc file is faster than parsing and translating a .py
 
6978
file, so the presence of precompiled .pyc files will generally improve
 
6979
start-up time of Python scripts.  If desired, the Lib/compileall.py
 
6980
module/script can be used to force creation of valid .pyc files for a
 
6981
given set of modules.
 
6982
<P>
 
6983
Note that the main script executed by Python, even if its filename
 
6984
ends in .py, is not compiled to a .pyc file.  It is compiled to
 
6985
bytecode, but the bytecode is not saved to a file.
 
6986
<P>
 
6987
If you are looking for a way to translate Python programs in order to
 
6988
distribute them in binary form, without the need to distribute the
 
6989
interpreter and library as well, have a look at the freeze.py script
 
6990
in the Tools/freeze directory.  This creates a single binary file
 
6991
incorporating your program, the Python interpreter, and those parts of
 
6992
the Python library that are needed by your program.  Of course, the
 
6993
resulting binary will only run on the same type of platform as that
 
6994
used to create it.
 
6995
<P>
 
6996
Newsflash: there are now several programs that do this, to some extent.
 
6997
Look for Psyco, Pyrex, PyInline, Py2Cmod, and Weave.
 
6998
<P>
 
6999
 
 
7000
<A HREF="faqw.py?req=edit&amp;file=faq06.013.htp">Edit this entry</A> /
 
7001
<A HREF="faqw.py?req=log&amp;file=faq06.013.htp">Log info</A>
 
7002
 
 
7003
/ Last changed on Fri May 24 21:26:19 2002 by
 
7004
<A HREF="mailto:guido@python.org">GvR</A>
 
7005
<P>
 
7006
 
 
7007
<HR>
 
7008
<H2><A NAME="6.14">6.14. How does Python manage memory?</A></H2>
 
7009
The details of Python memory management depend on the implementation.
 
7010
The standard Python implementation (the C implementation) uses reference
 
7011
counting and another mechanism to collect reference cycles.
 
7012
<P>
 
7013
Jython relies on the Java runtime; so it uses
 
7014
the JVM's garbage collector.  This difference can cause some subtle
 
7015
porting problems if your Python code depends on the behavior of
 
7016
the reference counting implementation.
 
7017
<P>
 
7018
The reference cycle collector was added in CPython 2.0. It
 
7019
periodically executes a cycle detection algorithm which looks for inaccessible cycles and deletes the objects involved. A new gc module provides functions to perform a garbage collection, obtain debugging statistics, and tuning the collector's parameters. 
 
7020
<P>
 
7021
The detection of cycles can be disabled when Python is compiled, if you can't afford even a tiny speed penalty or suspect that the cycle collection is buggy, by specifying the "--without-cycle-gc" switch when running the configure script. 
 
7022
<P>
 
7023
Sometimes objects get stuck in "tracebacks" temporarily and hence are not deallocated when you might expect.  Clear the tracebacks via
 
7024
<P>
 
7025
<PRE>
 
7026
       import sys
 
7027
       sys.exc_traceback = sys.last_traceback = None
 
7028
</PRE>
 
7029
Tracebacks are used for reporting errors and implementing debuggers and related things.  They contain a portion of the program state extracted during the handling of an exception (usually the most recent exception).
 
7030
<P>
 
7031
In the absence of circularities and modulo tracebacks, Python programs need not explicitly manage memory.
 
7032
<P>
 
7033
Why python doesn't use a more traditional garbage collection
 
7034
scheme? For one thing, unless this were
 
7035
added to C as a standard feature, it's a portability pain in the ass.
 
7036
And yes, I know about the Xerox library.  It has bits of assembler
 
7037
code for <I>most</I> <I>common</I> platforms.  Not for all.  And although it is
 
7038
mostly transparent, it isn't completely transparent (when I once
 
7039
linked Python with it, it dumped core).
 
7040
<P>
 
7041
Traditional GC also becomes a problem when Python gets embedded into
 
7042
other applications.  While in a stand-alone Python it may be fine to
 
7043
replace the standard malloc() and free() with versions provided by the
 
7044
GC library, an application embedding Python may want to have its <I>own</I>
 
7045
substitute for malloc() and free(), and may not want Python's.  Right
 
7046
now, Python works with anything that implements malloc() and free()
 
7047
properly.
 
7048
<P>
 
7049
In Jython, the following code (which is 
 
7050
fine in C Python) will probably run out of file descriptors long before
 
7051
it runs out of memory:
 
7052
<P>
 
7053
<PRE>
 
7054
        for file in &lt;very long list of files&gt;:
 
7055
                f = open(file)
 
7056
                c = f.read(1)
 
7057
</PRE>
 
7058
Using the current reference counting and destructor scheme, each new
 
7059
assignment to f closes the previous file.  Using GC, this is not
 
7060
guaranteed.  Sure, you can think of ways to fix this.  But it's not
 
7061
off-the-shelf technology.  If you want to write code that will
 
7062
work with any Python implementation, you should explicitly close
 
7063
the file; this will work regardless of GC:
 
7064
<P>
 
7065
<PRE>
 
7066
       for file in &lt;very long list of files&gt;:
 
7067
                f = open(file)
 
7068
                c = f.read(1)
 
7069
                f.close()
 
7070
</PRE>
 
7071
<P>
 
7072
 
 
7073
<A HREF="faqw.py?req=edit&amp;file=faq06.014.htp">Edit this entry</A> /
 
7074
<A HREF="faqw.py?req=log&amp;file=faq06.014.htp">Log info</A>
 
7075
 
 
7076
/ Last changed on Thu Mar 21 05:35:38 2002 by
 
7077
<A HREF="mailto:erno-pyfaq@erno.iki.fi">Erno Kuusela</A>
 
7078
<P>
 
7079
 
 
7080
<HR>
 
7081
<H2><A NAME="6.15">6.15. Why are there separate tuple and list data types?</A></H2>
 
7082
This is done so that tuples can be immutable while lists are mutable.
 
7083
<P>
 
7084
Immutable tuples are useful in situations where you need to pass a few
 
7085
items to a function and don't want the function to modify the tuple;
 
7086
for example,
 
7087
<P>
 
7088
<PRE>
 
7089
        point1 = (120, 140)
 
7090
        point2 = (200, 300)
 
7091
        record(point1, point2)
 
7092
        draw(point1, point2)
 
7093
</PRE>
 
7094
You don't want to have to think about what would happen if record()
 
7095
changed the coordinates -- it can't, because the tuples are immutable.
 
7096
<P>
 
7097
On the other hand, when creating large lists dynamically, it is
 
7098
absolutely crucial that they are mutable -- adding elements to a tuple
 
7099
one by one requires using the concatenation operator, which makes it
 
7100
quadratic in time.
 
7101
<P>
 
7102
As a general guideline, use tuples like you would use structs in C or
 
7103
records in Pascal, use lists like (variable length) arrays.
 
7104
<P>
 
7105
 
 
7106
<A HREF="faqw.py?req=edit&amp;file=faq06.015.htp">Edit this entry</A> /
 
7107
<A HREF="faqw.py?req=log&amp;file=faq06.015.htp">Log info</A>
 
7108
 
 
7109
/ Last changed on Fri May 23 15:26:03 1997 by
 
7110
<A HREF="mailto:guido@python.org">GvR</A>
 
7111
<P>
 
7112
 
 
7113
<HR>
 
7114
<H2><A NAME="6.16">6.16. How are lists implemented?</A></H2>
 
7115
Despite what a Lisper might think, Python's lists are really
 
7116
variable-length arrays.  The implementation uses a contiguous
 
7117
array of references to other objects, and keeps a pointer
 
7118
to this array (as well as its length) in a list head structure.
 
7119
<P>
 
7120
This makes indexing a list (a[i]) an operation whose cost is
 
7121
independent of the size of the list or the value of the index.
 
7122
<P>
 
7123
When items are appended or inserted, the array of references is resized.
 
7124
Some cleverness is applied to improve the performance of appending
 
7125
items repeatedly; when the array must be grown, some extra space
 
7126
is allocated so the next few times don't require an actual resize.
 
7127
<P>
 
7128
 
 
7129
<A HREF="faqw.py?req=edit&amp;file=faq06.016.htp">Edit this entry</A> /
 
7130
<A HREF="faqw.py?req=log&amp;file=faq06.016.htp">Log info</A>
 
7131
 
 
7132
/ Last changed on Fri May 23 15:32:24 1997 by
 
7133
<A HREF="mailto:guido@python.org">GvR</A>
 
7134
<P>
 
7135
 
 
7136
<HR>
 
7137
<H2><A NAME="6.17">6.17. How are dictionaries implemented?</A></H2>
 
7138
Python's dictionaries are implemented as resizable hash tables.
 
7139
<P>
 
7140
Compared to B-trees, this gives better performance for lookup
 
7141
(the most common operation by far) under most circumstances,
 
7142
and the implementation is simpler.
 
7143
<P>
 
7144
 
 
7145
<A HREF="faqw.py?req=edit&amp;file=faq06.017.htp">Edit this entry</A> /
 
7146
<A HREF="faqw.py?req=log&amp;file=faq06.017.htp">Log info</A>
 
7147
 
 
7148
/ Last changed on Fri May 23 23:51:14 1997 by
 
7149
<A HREF="mailto:Vladimir.Marangozov@inrialpes.fr">Vladimir Marangozov</A>
 
7150
<P>
 
7151
 
 
7152
<HR>
 
7153
<H2><A NAME="6.18">6.18. Why must dictionary keys be immutable?</A></H2>
 
7154
The hash table implementation of dictionaries uses a hash value
 
7155
calculated from the key value to find the key.  If the key were
 
7156
a mutable object, its value could change, and thus its hash could
 
7157
change.  But since whoever changes the key object can't tell that
 
7158
is incorporated in a dictionary, it can't move the entry around in
 
7159
the dictionary.  Then, when you try to look up the same object
 
7160
in the dictionary, it won't be found, since its hash value is different;
 
7161
and if you try to look up the old value, it won't be found either,
 
7162
since the value of the object found in that hash bin differs.
 
7163
<P>
 
7164
If you think you need to have a dictionary indexed with a list,
 
7165
try to use a tuple instead.  The function tuple(l) creates a tuple
 
7166
with the same entries as the list l.
 
7167
<P>
 
7168
Some unacceptable solutions that have been proposed:
 
7169
<P>
 
7170
- Hash lists by their address (object ID).  This doesn't work because
 
7171
if you construct a new list with the same value it won't be found;
 
7172
e.g.,
 
7173
<P>
 
7174
<PRE>
 
7175
  d = {[1,2]: '12'}
 
7176
  print d[[1,2]]
 
7177
</PRE>
 
7178
will raise a KeyError exception because the id of the [1,2] used
 
7179
in the second line differs from that in the first line.
 
7180
In other words, dictionary keys should be compared using '==', not using 'is'.
 
7181
<P>
 
7182
- Make a copy when using a list as a key.  This doesn't work because
 
7183
the list (being a mutable object) could contain a reference to itself,
 
7184
and then the copying code would run into an infinite loop.
 
7185
<P>
 
7186
- Allow lists as keys but tell the user not to modify them.  This would
 
7187
allow a class of hard-to-track bugs in programs that I'd rather not see;
 
7188
it invalidates an important invariant of dictionaries (every value in
 
7189
d.keys() is usable as a key of the dictionary).
 
7190
<P>
 
7191
- Mark lists as read-only once they are used as a dictionary key.
 
7192
The problem is that it's not just the top-level object that could change
 
7193
its value; you could use a tuple containing a list as a key.  Entering
 
7194
anything as a key into a dictionary would require marking all objects
 
7195
reachable from there as read-only -- and again, self-referential objects
 
7196
could cause an infinite loop again (and again and again).
 
7197
<P>
 
7198
There is a trick to get around this if you need to, but
 
7199
use it at your own risk:  You
 
7200
can wrap a mutable structure inside a class instance which
 
7201
has both a __cmp__ and a __hash__ method.  
 
7202
<P>
 
7203
<PRE>
 
7204
   class listwrapper:
 
7205
        def __init__(self, the_list):
 
7206
              self.the_list = the_list
 
7207
        def __cmp__(self, other):
 
7208
              return self.the_list == other.the_list
 
7209
        def __hash__(self):
 
7210
              l = self.the_list
 
7211
              result = 98767 - len(l)*555
 
7212
              for i in range(len(l)):
 
7213
                   try:
 
7214
                        result = result + (hash(l[i]) % 9999999) * 1001 + i
 
7215
                   except:
 
7216
                        result = (result % 7777777) + i * 333
 
7217
              return result
 
7218
</PRE>
 
7219
Note that the hash computation is complicated by the
 
7220
possibility that some members of the list may be unhashable
 
7221
and also by the possibility of arithmetic overflow.
 
7222
<P>
 
7223
You must make
 
7224
sure that the hash value for all such wrapper objects that reside in a
 
7225
dictionary (or other hash based structure), remain fixed while
 
7226
the object is in the dictionary (or other structure).
 
7227
<P>
 
7228
Furthermore it must always be the case that if
 
7229
o1 == o2 (ie o1.__cmp__(o2)==0) then hash(o1)==hash(o2)
 
7230
(ie, o1.__hash__() == o2.__hash__()), regardless of whether
 
7231
the object is in a dictionary or not.
 
7232
If you fail to meet these restrictions dictionaries and other
 
7233
hash based structures may misbehave!
 
7234
<P>
 
7235
In the case of listwrapper above whenever the wrapper
 
7236
object is in a dictionary the wrapped list must not change
 
7237
to avoid anomalies.  Don't do this unless you are prepared
 
7238
to think hard about the requirements and the consequences
 
7239
of not meeting them correctly.  You've been warned!
 
7240
<P>
 
7241
 
 
7242
<A HREF="faqw.py?req=edit&amp;file=faq06.018.htp">Edit this entry</A> /
 
7243
<A HREF="faqw.py?req=log&amp;file=faq06.018.htp">Log info</A>
 
7244
 
 
7245
/ Last changed on Thu Jul 10 10:08:40 1997 by
 
7246
<A HREF="mailto:aaron_watters@msn.com">aaron watters</A>
 
7247
<P>
 
7248
 
 
7249
<HR>
 
7250
<H2><A NAME="6.19">6.19. How the heck do you make an array in Python?</A></H2>
 
7251
["this", 1, "is", "an", "array"]
 
7252
<P>
 
7253
Lists are arrays in the C or Pascal sense of the word (see question
 
7254
6.16).  The array module also provides methods for creating arrays
 
7255
of fixed types with compact representations (but they are slower to
 
7256
index than lists).  Also note that the Numerics extensions and others
 
7257
define array-like structures with various characteristics as well.
 
7258
<P>
 
7259
To get Lisp-like lists, emulate cons cells
 
7260
<P>
 
7261
<PRE>
 
7262
    lisp_list = ("like",  ("this",  ("example", None) ) )
 
7263
</PRE>
 
7264
using tuples (or lists, if you want mutability).  Here the analogue
 
7265
of lisp car is lisp_list[0] and the analogue of cdr is lisp_list[1].
 
7266
Only do this if you're sure you really need to (it's usually a lot
 
7267
slower than using Python lists).
 
7268
<P>
 
7269
Think of Python lists as mutable heterogeneous arrays of
 
7270
Python objects (say that 10 times fast :) ).
 
7271
<P>
 
7272
 
 
7273
<A HREF="faqw.py?req=edit&amp;file=faq06.019.htp">Edit this entry</A> /
 
7274
<A HREF="faqw.py?req=log&amp;file=faq06.019.htp">Log info</A>
 
7275
 
 
7276
/ Last changed on Wed Aug 13 07:08:27 1997 by
 
7277
<A HREF="mailto:aaron_watters@msn.com">aaron watters</A>
 
7278
<P>
 
7279
 
 
7280
<HR>
 
7281
<H2><A NAME="6.20">6.20. Why doesn't list.sort() return the sorted list?</A></H2>
 
7282
In situations where performance matters, making a copy of the list
 
7283
just to sort it would be wasteful.  Therefore, list.sort() sorts
 
7284
the list in place.  In order to remind you of that fact, it does
 
7285
not return the sorted list.  This way, you won't be fooled into
 
7286
accidentally overwriting a list when you need a sorted copy but also
 
7287
need to keep the unsorted version around.
 
7288
<P>
 
7289
As a result, here's the idiom to iterate over the keys of a dictionary
 
7290
in sorted order:
 
7291
<P>
 
7292
<PRE>
 
7293
        keys = dict.keys()
 
7294
        keys.sort()
 
7295
        for key in keys:
 
7296
                ...do whatever with dict[key]...
 
7297
</PRE>
 
7298
<P>
 
7299
 
 
7300
<A HREF="faqw.py?req=edit&amp;file=faq06.020.htp">Edit this entry</A> /
 
7301
<A HREF="faqw.py?req=log&amp;file=faq06.020.htp">Log info</A>
 
7302
 
 
7303
/ Last changed on Thu Dec  2 17:01:52 1999 by
 
7304
<A HREF="mailto:fdrake@acm.org">Fred L. Drake, Jr.</A>
 
7305
<P>
 
7306
 
 
7307
<HR>
 
7308
<H2><A NAME="6.21">6.21. How do you specify and enforce an interface spec in Python?</A></H2>
 
7309
An interfaces specification for a module as provided
 
7310
by languages such as C++ and java describes the prototypes
 
7311
for the methods and functions of the module.  Many feel
 
7312
that compile time enforcement of interface specifications
 
7313
help aid in the construction of large programs.  Python
 
7314
does not support interface specifications directly, but many
 
7315
of their advantages can be obtained by an appropriate
 
7316
test discipline for components, which can often be very
 
7317
easily accomplished in Python.  There is also a tool, PyChecker,
 
7318
which can be used to find problems due to subclassing.
 
7319
<P>
 
7320
A good test suite for a module can at
 
7321
once provide a regression test and serve as a module interface
 
7322
specification (even better since it also gives example usage).  Look to
 
7323
many of the standard libraries which often have a "script
 
7324
interpretation" which provides a simple "self test."  Even
 
7325
modules which use complex external interfaces can often
 
7326
be tested in isolation using trivial "stub" emulations of the
 
7327
external interface.
 
7328
<P>
 
7329
An appropriate testing discipline (if enforced) can help
 
7330
build large complex applications in Python as well as having interface
 
7331
specifications would do (or better).  Of course Python allows you 
 
7332
to get sloppy and not do it.  Also you might want to design
 
7333
your code with an eye to make it easily tested.
 
7334
<P>
 
7335
 
 
7336
<A HREF="faqw.py?req=edit&amp;file=faq06.021.htp">Edit this entry</A> /
 
7337
<A HREF="faqw.py?req=log&amp;file=faq06.021.htp">Log info</A>
 
7338
 
 
7339
/ Last changed on Thu May 23 03:05:29 2002 by
 
7340
<A HREF="mailto:neal@metaslash.com">Neal Norwitz</A>
 
7341
<P>
 
7342
 
 
7343
<HR>
 
7344
<H2><A NAME="6.22">6.22. Why do all classes have the same type? Why do instances all have the same type?</A></H2>
 
7345
The Pythonic use of the word "type" is quite different from
 
7346
common usage in much of the rest of the programming language
 
7347
world.  A "type" in Python is a description for an object's operations
 
7348
as implemented in C.  All classes have the same operations
 
7349
implemented in C which sometimes "call back" to differing program
 
7350
fragments implemented in Python, and hence all classes have the
 
7351
same type.  Similarly at the C level all class instances have the
 
7352
same C implementation, and hence all instances have the same
 
7353
type.
 
7354
<P>
 
7355
Remember that in Python usage "type" refers to a C implementation
 
7356
of an object.  To distinguish among instances of different classes
 
7357
use Instance.__class__, and also look to 4.47.  Sorry for the
 
7358
terminological confusion, but at this point in Python's development
 
7359
nothing can be done!
 
7360
<P>
 
7361
 
 
7362
<A HREF="faqw.py?req=edit&amp;file=faq06.022.htp">Edit this entry</A> /
 
7363
<A HREF="faqw.py?req=log&amp;file=faq06.022.htp">Log info</A>
 
7364
 
 
7365
/ Last changed on Tue Jul  1 12:35:47 1997 by
 
7366
<A HREF="mailto:aaron_watters@msn.com">aaron watters</A>
 
7367
<P>
 
7368
 
 
7369
<HR>
 
7370
<H2><A NAME="6.23">6.23. Why isn't all memory freed when Python exits?</A></H2>
 
7371
Objects referenced from Python module global name spaces are
 
7372
not always deallocated when Python exits.
 
7373
<P>
 
7374
This may happen if there are circular references (see question
 
7375
4.17).  There are also certain bits of memory that are allocated
 
7376
by the C library that are impossible to free (e.g. a tool
 
7377
like Purify will complain about these).
 
7378
<P>
 
7379
But in general, Python 1.5 and beyond
 
7380
(in contrast with earlier versions) is quite agressive about
 
7381
cleaning up memory on exit.
 
7382
<P>
 
7383
If you want to force Python to delete certain things on deallocation
 
7384
use the sys.exitfunc hook to force those deletions.  For example
 
7385
if you are debugging an extension module using a memory analysis
 
7386
tool and you wish to make Python deallocate almost everything
 
7387
you might use an exitfunc like this one:
 
7388
<P>
 
7389
<PRE>
 
7390
  import sys
 
7391
</PRE>
 
7392
<PRE>
 
7393
  def my_exitfunc():
 
7394
       print "cleaning up"
 
7395
       import sys
 
7396
       # do order dependant deletions here
 
7397
       ...
 
7398
       # now delete everything else in arbitrary order
 
7399
       for x in sys.modules.values():
 
7400
            d = x.__dict__
 
7401
            for name in d.keys():
 
7402
                 del d[name]
 
7403
</PRE>
 
7404
<PRE>
 
7405
  sys.exitfunc = my_exitfunc
 
7406
</PRE>
 
7407
Other exitfuncs can be less drastic, of course.
 
7408
<P>
 
7409
(In fact, this one just does what Python now already does itself;
 
7410
but the example of using sys.exitfunc to force cleanups is still
 
7411
useful.)
 
7412
<P>
 
7413
 
 
7414
<A HREF="faqw.py?req=edit&amp;file=faq06.023.htp">Edit this entry</A> /
 
7415
<A HREF="faqw.py?req=log&amp;file=faq06.023.htp">Log info</A>
 
7416
 
 
7417
/ Last changed on Tue Sep 29 09:46:26 1998 by
 
7418
<A HREF="mailto:guido@python.org">GvR</A>
 
7419
<P>
 
7420
 
 
7421
<HR>
 
7422
<H2><A NAME="6.24">6.24. Why no class methods or mutable class variables?</A></H2>
 
7423
The notation
 
7424
<P>
 
7425
<PRE>
 
7426
    instance.attribute(arg1, arg2)
 
7427
</PRE>
 
7428
usually translates to the equivalent of
 
7429
<P>
 
7430
<PRE>
 
7431
    Class.attribute(instance, arg1, arg2)
 
7432
</PRE>
 
7433
where Class is a (super)class of instance.  Similarly
 
7434
<P>
 
7435
<PRE>
 
7436
    instance.attribute = value
 
7437
</PRE>
 
7438
sets an attribute of an instance (overriding any attribute of a class
 
7439
that instance inherits).  
 
7440
<P>
 
7441
Sometimes programmers want to have
 
7442
different behaviours -- they want a method which does not bind
 
7443
to the instance and a class attribute which changes in place.
 
7444
Python does not preclude these behaviours, but you have to
 
7445
adopt a convention to implement them.  One way to accomplish
 
7446
this is to use "list wrappers" and global functions.
 
7447
<P>
 
7448
<PRE>
 
7449
   def C_hello():
 
7450
         print "hello"
 
7451
</PRE>
 
7452
<PRE>
 
7453
   class C:
 
7454
        hello = [C_hello]
 
7455
        counter = [0]
 
7456
</PRE>
 
7457
<PRE>
 
7458
    I = C()
 
7459
</PRE>
 
7460
Here I.hello[0]() acts very much like a "class method" and
 
7461
I.counter[0] = 2 alters C.counter (and doesn't override it).
 
7462
If you don't understand why you'd ever want to do this, that's
 
7463
because you are pure of mind, and you probably never will
 
7464
want to do it!  This is dangerous trickery, not recommended
 
7465
when avoidable.  (Inspired by Tim Peter's discussion.)
 
7466
<P>
 
7467
In Python 2.2, you can do this using the new built-in operations
 
7468
classmethod and staticmethod.
 
7469
See <A HREF="http://www.python.org/2.2/descrintro.html#staticmethods">http://www.python.org/2.2/descrintro.html#staticmethods</A>
 
7470
<P>
 
7471
 
 
7472
<A HREF="faqw.py?req=edit&amp;file=faq06.024.htp">Edit this entry</A> /
 
7473
<A HREF="faqw.py?req=log&amp;file=faq06.024.htp">Log info</A>
 
7474
 
 
7475
/ Last changed on Tue Sep 11 15:59:37 2001 by
 
7476
<A HREF="mailto:guido@python.org">GvR</A>
 
7477
<P>
 
7478
 
 
7479
<HR>
 
7480
<H2><A NAME="6.25">6.25. Why are default values sometimes shared between objects?</A></H2>
 
7481
It is often expected that a function CALL creates new objects for default
 
7482
values. This is not what happens. Default values are created when the
 
7483
function is DEFINED, that is, there is only one such object that all 
 
7484
functions refer to. If that object is changed, subsequent calls to the 
 
7485
function will refer to this changed object. By definition, immutable objects
 
7486
(like numbers, strings, tuples, None) are safe from change. Changes to mutable 
 
7487
objects (like dictionaries, lists, class instances) is what causes the
 
7488
confusion.
 
7489
<P>
 
7490
Because of this feature it is good programming practice not to use mutable
 
7491
objects as default values, but to introduce them in the function.
 
7492
Don't write:
 
7493
<P>
 
7494
<PRE>
 
7495
        def foo(dict={}):  # XXX shared reference to one dict for all calls
 
7496
            ...
 
7497
</PRE>
 
7498
but:
 
7499
<PRE>
 
7500
        def foo(dict=None):
 
7501
                if dict is None:
 
7502
                        dict = {} # create a new dict for local namespace
 
7503
</PRE>
 
7504
See page 182 of "Internet Programming with Python" for one discussion
 
7505
of this feature. Or see the top of page 144 or bottom of page 277 in 
 
7506
"Programming Python" for another discussion.
 
7507
<P>
 
7508
 
 
7509
<A HREF="faqw.py?req=edit&amp;file=faq06.025.htp">Edit this entry</A> /
 
7510
<A HREF="faqw.py?req=log&amp;file=faq06.025.htp">Log info</A>
 
7511
 
 
7512
/ Last changed on Sat Aug 16 07:03:35 1997 by
 
7513
<A HREF="mailto:cjr@bound.xs4all.nl">Case Roole</A>
 
7514
<P>
 
7515
 
 
7516
<HR>
 
7517
<H2><A NAME="6.26">6.26. Why no goto?</A></H2>
 
7518
Actually, you can use exceptions to provide a "structured goto"
 
7519
that even works across function calls.  Many feel that exceptions
 
7520
can conveniently emulate all reasonable uses of the "go" or "goto"
 
7521
constructs of C, Fortran, and other languages.  For example:
 
7522
<P>
 
7523
<PRE>
 
7524
   class label: pass # declare a label
 
7525
   try:
 
7526
        ...
 
7527
        if (condition): raise label() # goto label
 
7528
        ...
 
7529
   except label: # where to goto
 
7530
        pass
 
7531
   ...
 
7532
</PRE>
 
7533
This doesn't allow you to jump into the middle of a loop, but
 
7534
that's usually considered an abuse of goto anyway.  Use sparingly.
 
7535
<P>
 
7536
 
 
7537
<A HREF="faqw.py?req=edit&amp;file=faq06.026.htp">Edit this entry</A> /
 
7538
<A HREF="faqw.py?req=log&amp;file=faq06.026.htp">Log info</A>
 
7539
 
 
7540
/ Last changed on Wed Sep 10 07:16:44 1997 by
 
7541
<A HREF="mailto:aaron_watters@msn.com">aaron watters</A>
 
7542
<P>
 
7543
 
 
7544
<HR>
 
7545
<H2><A NAME="6.27">6.27. How do you make a higher order function in Python?</A></H2>
 
7546
You have two choices: you can use default arguments and override
 
7547
them or you can use "callable objects."  For example suppose you
 
7548
wanted to define linear(a,b) which returns a function f where f(x)
 
7549
computes the value a*x+b.  Using default arguments:
 
7550
<P>
 
7551
<PRE>
 
7552
     def linear(a,b):
 
7553
         def result(x, a=a, b=b):
 
7554
             return a*x + b
 
7555
         return result
 
7556
</PRE>
 
7557
Or using callable objects:
 
7558
<P>
 
7559
<PRE>
 
7560
     class linear:
 
7561
        def __init__(self, a, b):
 
7562
            self.a, self.b = a,b
 
7563
        def __call__(self, x):
 
7564
            return self.a * x + self.b
 
7565
</PRE>
 
7566
In both cases:
 
7567
<P>
 
7568
<PRE>
 
7569
     taxes = linear(0.3,2)
 
7570
</PRE>
 
7571
gives a callable object where taxes(10e6) == 0.3 * 10e6 + 2.
 
7572
<P>
 
7573
The defaults strategy has the disadvantage that the default arguments
 
7574
could be accidentally or maliciously overridden.  The callable objects
 
7575
approach has the disadvantage that it is a bit slower and a bit
 
7576
longer.  Note however that a collection of callables can share
 
7577
their signature via inheritance.  EG
 
7578
<P>
 
7579
<PRE>
 
7580
      class exponential(linear):
 
7581
         # __init__ inherited
 
7582
         def __call__(self, x):
 
7583
             return self.a * (x ** self.b)
 
7584
</PRE>
 
7585
On comp.lang.python, <A HREF="mailto:zenin@bawdycaste.org">zenin@bawdycaste.org</A> points out that
 
7586
an object can encapsulate state for several methods in order
 
7587
to emulate the "closure" concept from functional programming
 
7588
languages, for example:
 
7589
<P>
 
7590
<PRE>
 
7591
    class counter:
 
7592
        value = 0
 
7593
        def set(self, x): self.value = x
 
7594
        def up(self): self.value=self.value+1
 
7595
        def down(self): self.value=self.value-1
 
7596
</PRE>
 
7597
<PRE>
 
7598
    count = counter()
 
7599
    inc, dec, reset = count.up, count.down, count.set
 
7600
</PRE>
 
7601
Here inc, dec and reset act like "functions which share the
 
7602
same closure containing the variable count.value" (if you
 
7603
like that way of thinking).
 
7604
<P>
 
7605
 
 
7606
<A HREF="faqw.py?req=edit&amp;file=faq06.027.htp">Edit this entry</A> /
 
7607
<A HREF="faqw.py?req=log&amp;file=faq06.027.htp">Log info</A>
 
7608
 
 
7609
/ Last changed on Fri Sep 25 08:38:35 1998 by
 
7610
<A HREF="mailto:arw@pythonpros.com">Aaron Watters</A>
 
7611
<P>
 
7612
 
 
7613
<HR>
 
7614
<H2><A NAME="6.28">6.28. Why do I get a SyntaxError for a 'continue' inside a 'try'?</A></H2>
 
7615
This is an implementation limitation,
 
7616
caused by the extremely simple-minded
 
7617
way Python generates bytecode.  The try block pushes something on the
 
7618
"block stack" which the continue would have to pop off again.  The
 
7619
current code generator doesn't have the data structures around so that 
 
7620
'continue' can generate the right code.
 
7621
<P>
 
7622
Note that JPython doesn't have this restriction!
 
7623
<P>
 
7624
 
 
7625
<A HREF="faqw.py?req=edit&amp;file=faq06.028.htp">Edit this entry</A> /
 
7626
<A HREF="faqw.py?req=log&amp;file=faq06.028.htp">Log info</A>
 
7627
 
 
7628
/ Last changed on Fri May 22 15:01:07 1998 by
 
7629
<A HREF="mailto:guido@python.org">GvR</A>
 
7630
<P>
 
7631
 
 
7632
<HR>
 
7633
<H2><A NAME="6.29">6.29. Why can't raw strings (r-strings) end with a backslash?</A></H2>
 
7634
More precisely, they can't end with an odd number of backslashes:
 
7635
the unpaired backslash at the end escapes the closing quote character,
 
7636
leaving an unterminated string.
 
7637
<P>
 
7638
Raw strings were designed to ease creating input for processors (chiefly
 
7639
regular expression engines) that want to do their own backslash escape processing. Such processors consider an unmatched trailing backslash to be an error anyway, so raw strings disallow that.  In return, they allow you to pass on the string quote character by escaping it with a backslash.  These rules work well when r-strings are used for their intended purpose.
 
7640
<P>
 
7641
If you're trying to build Windows pathnames, note that all Windows system calls accept forward slashes too:
 
7642
<P>
 
7643
<PRE>
 
7644
    f = open("/mydir/file.txt") # works fine!
 
7645
</PRE>
 
7646
If you're trying to build a pathname for a DOS command, try e.g. one of
 
7647
<P>
 
7648
<PRE>
 
7649
    dir = r"\this\is\my\dos\dir" "\\"
 
7650
    dir = r"\this\is\my\dos\dir\ "[:-1]
 
7651
    dir = "\\this\\is\\my\\dos\\dir\\"
 
7652
</PRE>
 
7653
<P>
 
7654
 
 
7655
<A HREF="faqw.py?req=edit&amp;file=faq06.029.htp">Edit this entry</A> /
 
7656
<A HREF="faqw.py?req=log&amp;file=faq06.029.htp">Log info</A>
 
7657
 
 
7658
/ Last changed on Mon Jul 13 20:50:20 1998 by
 
7659
<A HREF="mailto:tim_one@email.msn.com">Tim Peters</A>
 
7660
<P>
 
7661
 
 
7662
<HR>
 
7663
<H2><A NAME="6.30">6.30. Why can't I use an assignment in an expression?</A></H2>
 
7664
Many people used to C or Perl complain that they want to be able to
 
7665
use e.g. this C idiom:
 
7666
<P>
 
7667
<PRE>
 
7668
    while (line = readline(f)) {
 
7669
        ...do something with line...
 
7670
    }
 
7671
</PRE>
 
7672
where in Python you're forced to write this:
 
7673
<P>
 
7674
<PRE>
 
7675
    while 1:
 
7676
        line = f.readline()
 
7677
        if not line:
 
7678
            break
 
7679
        ...do something with line...
 
7680
</PRE>
 
7681
This issue comes up in the Python newsgroup with alarming frequency
 
7682
-- search Deja News for past messages about assignment expression.
 
7683
The reason for not allowing assignment in Python expressions
 
7684
is a common, hard-to-find bug in those other languages,
 
7685
caused by this construct:
 
7686
<P>
 
7687
<PRE>
 
7688
    if (x = 0) {
 
7689
        ...error handling...
 
7690
    }
 
7691
    else {
 
7692
        ...code that only works for nonzero x...
 
7693
    }
 
7694
</PRE>
 
7695
Many alternatives have been proposed.  Most are hacks that save some
 
7696
typing but use arbitrary or cryptic syntax or keywords,
 
7697
and fail the simple criterion that I use for language change proposals:
 
7698
it should intuitively suggest the proper meaning to a human reader
 
7699
who has not yet been introduced with the construct.
 
7700
<P>
 
7701
The earliest time something can be done about this will be with
 
7702
Python 2.0 -- if it is decided that it is worth fixing.
 
7703
An interesting phenomenon is that most experienced Python programmers
 
7704
recognize the "while 1" idiom and don't seem to be missing the
 
7705
assignment in expression construct much; it's only the newcomers
 
7706
who express a strong desire to add this to the language.
 
7707
<P>
 
7708
One fairly elegant solution would be to introduce a new operator
 
7709
for assignment in expressions spelled ":=" -- this avoids the "="
 
7710
instead of "==" problem.  It would have the same precedence
 
7711
as comparison operators but the parser would flag combination with
 
7712
other comparisons (without disambiguating parentheses) as an error.
 
7713
<P>
 
7714
Finally -- there's an alternative way of spelling this that seems
 
7715
attractive but is generally less robust than the "while 1" solution:
 
7716
<P>
 
7717
<PRE>
 
7718
    line = f.readline()
 
7719
    while line:
 
7720
        ...do something with line...
 
7721
        line = f.readline()
 
7722
</PRE>
 
7723
The problem with this is that if you change your mind about exactly
 
7724
how you get the next line (e.g. you want to change it into
 
7725
sys.stdin.readline()) you have to remember to change two places
 
7726
in your program -- the second one hidden at the bottom of the loop.
 
7727
<P>
 
7728
 
 
7729
<A HREF="faqw.py?req=edit&amp;file=faq06.030.htp">Edit this entry</A> /
 
7730
<A HREF="faqw.py?req=log&amp;file=faq06.030.htp">Log info</A>
 
7731
 
 
7732
/ Last changed on Tue May 18 00:57:41 1999 by
 
7733
<A HREF="mailto:dalke@bioreason.com">Andrew Dalke</A>
 
7734
<P>
 
7735
 
 
7736
<HR>
 
7737
<H2><A NAME="6.31">6.31. Why doesn't Python have a &quot;with&quot; statement like some other languages?</A></H2>
 
7738
Basically, because such a construct would be terribly ambiguous. Thanks to Carlos Ribeiro for the following remarks:
 
7739
<P>
 
7740
Some languages, such as Object Pascal, Delphi, and C++, use static types. So it is possible to know, in an unambiguous way, what member is being assigned in a "with" clause. This is the main point - the compiler <I>always</I>  knows the scope of every variable at compile time.
 
7741
<P>
 
7742
Python uses dynamic types. It is impossible to know in advance which 
 
7743
attribute will be referenced at runtime. Member attributes may be added or removed from objects on the fly. This would make it impossible to know, from a simple reading, what attribute is being referenced - a local one, a global one, or a member attribute.
 
7744
<P>
 
7745
For instance, take the following snippet (it is incomplete btw, just to 
 
7746
give you the idea):
 
7747
<P>
 
7748
<PRE>
 
7749
   def with_is_broken(a):
 
7750
      with a:
 
7751
         print x
 
7752
</PRE>
 
7753
The snippet assumes that "a" must have a member attribute called "x". 
 
7754
However, there is nothing in Python that guarantees that. What should 
 
7755
happen if "a" is, let us say, an integer? And if I have a global variable named "x", will it end up being used inside the with block? As you see, the dynamic nature of Python makes such choices much harder.
 
7756
<P>
 
7757
The primary benefit of "with" and similar language features (reduction of code volume) can, however, easily be achieved in Python by assignment. Instead of:
 
7758
<P>
 
7759
<PRE>
 
7760
    function(args).dict[index][index].a = 21
 
7761
    function(args).dict[index][index].b = 42
 
7762
    function(args).dict[index][index].c = 63
 
7763
</PRE>
 
7764
would become:
 
7765
<P>
 
7766
<PRE>
 
7767
    ref = function(args).dict[index][index]
 
7768
    ref.a = 21
 
7769
    ref.b = 42
 
7770
    ref.c = 63
 
7771
</PRE>
 
7772
This also has the happy side-effect of increasing execution speed, since name bindings are resolved at run-time in Python, and the second method only needs to perform the resolution once. If the referenced object does not have a, b and c attributes, of course, the end result is still a run-time exception.
 
7773
<P>
 
7774
 
 
7775
<A HREF="faqw.py?req=edit&amp;file=faq06.031.htp">Edit this entry</A> /
 
7776
<A HREF="faqw.py?req=log&amp;file=faq06.031.htp">Log info</A>
 
7777
 
 
7778
/ Last changed on Fri Jan 11 14:32:58 2002 by
 
7779
<A HREF="mailto:sholden@holdenweb.com">Steve Holden</A>
 
7780
<P>
 
7781
 
 
7782
<HR>
 
7783
<H2><A NAME="6.32">6.32. Why are colons required for if/while/def/class?</A></H2>
 
7784
The colon is required primarily to enhance readability (one of the
 
7785
results of the experimental ABC language).  Consider this:
 
7786
<P>
 
7787
<PRE>
 
7788
    if a==b
 
7789
        print a
 
7790
</PRE>
 
7791
versus
 
7792
<P>
 
7793
<PRE>
 
7794
    if a==b:
 
7795
        print a
 
7796
</PRE>
 
7797
Notice how the second one is slightly easier to read.  Notice further how
 
7798
a colon sets off the example in the second line of this FAQ answer; it's
 
7799
a standard usage in English.  Finally, the colon makes it easier for
 
7800
editors with syntax highlighting.
 
7801
<P>
 
7802
 
 
7803
<A HREF="faqw.py?req=edit&amp;file=faq06.032.htp">Edit this entry</A> /
 
7804
<A HREF="faqw.py?req=log&amp;file=faq06.032.htp">Log info</A>
 
7805
 
 
7806
/ Last changed on Mon Jun  3 07:22:57 2002 by
 
7807
<A HREF="mailto:smurf@noris.de">Matthias Urlichs</A>
 
7808
<P>
 
7809
 
 
7810
<HR>
 
7811
<H2><A NAME="6.33">6.33. Can't we get rid of the Global Interpreter Lock?</A></H2>
 
7812
The Global Interpreter Lock (GIL) is often seen as a hindrance to
 
7813
Python's deployment on high-end multiprocessor server machines,
 
7814
because a multi-threaded Python program effectively only uses
 
7815
one CPU, due to the insistence that (almost) all Python code
 
7816
can only run while the GIL is held.
 
7817
<P>
 
7818
Back in the days of Python 1.5, Greg Stein actually implemented
 
7819
a comprehensive patch set ("free threading")
 
7820
that removed the GIL, replacing it with
 
7821
fine-grained locking.  Unfortunately, even on Windows (where locks
 
7822
are very efficient) this ran ordinary Python code about twice as
 
7823
slow as the interpreter using the GIL.  On Linux the performance
 
7824
loss was even worse (pthread locks aren't as efficient).
 
7825
<P>
 
7826
Since then, the idea of getting rid of the GIL has occasionally
 
7827
come up but nobody has found a way to deal with the expected slowdown;
 
7828
Greg's free threading patch set has not been kept up-to-date for
 
7829
later Python versions.
 
7830
<P>
 
7831
This doesn't mean that you can't make good use of Python on
 
7832
multi-CPU machines!  You just have to be creative with dividing
 
7833
the work up between multiple <I>processes</I> rather than multiple
 
7834
<I>threads</I>.
 
7835
<P>
 
7836
<P>
 
7837
It has been suggested that the GIL should be a per-interpreter-state
 
7838
lock rather than truly global; interpreters then wouldn't be able
 
7839
to share objects.  Unfortunately, this isn't likely to happen either.
 
7840
<P>
 
7841
It would be a tremendous amount of work, because many object
 
7842
implementations currently have global state.  E.g. small ints and
 
7843
small strings are cached; these caches would have to be moved to the
 
7844
interpreter state.  Other object types have their own free list; these
 
7845
free lists would have to be moved to the interpreter state.  And so
 
7846
on.
 
7847
<P>
 
7848
And I doubt that it can even be done in finite time, because the same
 
7849
problem exists for 3rd party extensions.  It is likely that 3rd party
 
7850
extensions are being written at a faster rate than you can convert
 
7851
them to store all their global state in the interpreter state.
 
7852
<P>
 
7853
And finally, once you have multiple interpreters not sharing any
 
7854
state, what have you gained over running each interpreter
 
7855
in a separate process?
 
7856
<P>
 
7857
 
 
7858
<A HREF="faqw.py?req=edit&amp;file=faq06.033.htp">Edit this entry</A> /
 
7859
<A HREF="faqw.py?req=log&amp;file=faq06.033.htp">Log info</A>
 
7860
 
 
7861
/ Last changed on Fri Feb  7 16:34:01 2003 by
 
7862
<A HREF="mailto:guido@python.org">GvR</A>
 
7863
<P>
 
7864
 
 
7865
<HR>
 
7866
<H1>7. Using Python on non-UNIX platforms</H1>
 
7867
 
 
7868
<HR>
 
7869
<H2><A NAME="7.1">7.1. Is there a Mac version of Python?</A></H2>
 
7870
Yes, it is maintained by Jack Jansen.  See Jack's MacPython Page:
 
7871
<P>
 
7872
<PRE>
 
7873
  <A HREF="http://www.cwi.nl/~jack/macpython.html">http://www.cwi.nl/~jack/macpython.html</A>
 
7874
</PRE>
 
7875
<P>
 
7876
 
 
7877
<A HREF="faqw.py?req=edit&amp;file=faq07.001.htp">Edit this entry</A> /
 
7878
<A HREF="faqw.py?req=log&amp;file=faq07.001.htp">Log info</A>
 
7879
 
 
7880
/ Last changed on Fri May  4 09:33:42 2001 by
 
7881
<A HREF="mailto:guido@python.org">GvR</A>
 
7882
<P>
 
7883
 
 
7884
<HR>
 
7885
<H2><A NAME="7.2">7.2. Are there DOS and Windows versions of Python?</A></H2>
 
7886
Yes.  The core windows binaries are available from <A HREF="http://www.python.org/windows">http://www.python.org/windows</A>/. There is a plethora of Windows extensions available, including a large number of not-always-compatible GUI toolkits.  The core binaries include the standard Tkinter GUI extension.
 
7887
<P>
 
7888
Most windows extensions can be found (or referenced) at <A HREF="http://www.python.org/windows">http://www.python.org/windows</A>/ 
 
7889
<P>
 
7890
Windows 3.1/DOS support seems to have dropped off recently.  You may need to settle for an old version of Python one these platforms.  One such port is WPY
 
7891
<P>
 
7892
WPY: Ports to DOS, Windows 3.1(1), Windows 95, Windows NT and OS/2.
 
7893
Also contains a GUI package that offers portability between Windows 
 
7894
(not DOS) and Unix, and native look and feel on both.
 
7895
<A HREF="ftp://ftp.python.org/pub/python/wpy">ftp://ftp.python.org/pub/python/wpy</A>/.
 
7896
<P>
 
7897
 
 
7898
<A HREF="faqw.py?req=edit&amp;file=faq07.002.htp">Edit this entry</A> /
 
7899
<A HREF="faqw.py?req=log&amp;file=faq07.002.htp">Log info</A>
 
7900
 
 
7901
/ Last changed on Tue Jun  2 20:21:57 1998 by
 
7902
<A HREF="mailto:MHammond@skippinet.com.au">Mark Hammond</A>
 
7903
<P>
 
7904
 
 
7905
<HR>
 
7906
<H2><A NAME="7.3">7.3. Is there an OS/2 version of Python?</A></H2>
 
7907
Yes, see <A HREF="http://www.python.org/download/download_os2.html">http://www.python.org/download/download_os2.html</A>.
 
7908
<P>
 
7909
 
 
7910
<A HREF="faqw.py?req=edit&amp;file=faq07.003.htp">Edit this entry</A> /
 
7911
<A HREF="faqw.py?req=log&amp;file=faq07.003.htp">Log info</A>
 
7912
 
 
7913
/ Last changed on Tue Sep  7 11:33:16 1999 by
 
7914
<A HREF="mailto:guido@python.org">GvR</A>
 
7915
<P>
 
7916
 
 
7917
<HR>
 
7918
<H2><A NAME="7.4">7.4. Is there a VMS version of Python?</A></H2>
 
7919
Jean-Fran�ois Pi�ronne has ported 2.1.3 to OpenVMS.  It can be found at
 
7920
&lt;<A HREF="http://vmspython.dyndns.org">http://vmspython.dyndns.org</A>/&gt;.
 
7921
<P>
 
7922
 
 
7923
<A HREF="faqw.py?req=edit&amp;file=faq07.004.htp">Edit this entry</A> /
 
7924
<A HREF="faqw.py?req=log&amp;file=faq07.004.htp">Log info</A>
 
7925
 
 
7926
/ Last changed on Thu Sep 19 15:40:38 2002 by
 
7927
<A HREF="mailto:skip@pobox.com">Skip Montanaro</A>
 
7928
<P>
 
7929
 
 
7930
<HR>
 
7931
<H2><A NAME="7.5">7.5. What about IBM mainframes, or other non-UNIX platforms?</A></H2>
 
7932
I haven't heard about these, except I remember hearing about an
 
7933
OS/9 port and a port to Vxworks (both operating systems for embedded
 
7934
systems).  If you're interested in any of this, go directly to the
 
7935
newsgroup and ask there, you may find exactly what you need.  For
 
7936
example, a port to MPE/iX 5.0 on HP3000 computers was just announced,
 
7937
see <A HREF="http://www.allegro.com/software">http://www.allegro.com/software</A>/.
 
7938
<P>
 
7939
On the IBM mainframe side, for Z/OS there's a port of python 1.4 that goes with their open-unix package, formely OpenEdition MVS, (<A HREF="http://www-1.ibm.com/servers/eserver/zseries/zos/unix/python.html">http://www-1.ibm.com/servers/eserver/zseries/zos/unix/python.html</A>). On a side note, there's also a java vm ported - so, in theory, jython could run too.
 
7940
<P>
 
7941
 
 
7942
<A HREF="faqw.py?req=edit&amp;file=faq07.005.htp">Edit this entry</A> /
 
7943
<A HREF="faqw.py?req=log&amp;file=faq07.005.htp">Log info</A>
 
7944
 
 
7945
/ Last changed on Mon Nov 18 03:18:39 2002 by
 
7946
<A HREF="mailto:bjessen@uol.com.br">Bruno Jessen</A>
 
7947
<P>
 
7948
 
 
7949
<HR>
 
7950
<H2><A NAME="7.6">7.6. Where are the source or Makefiles for the non-UNIX versions?</A></H2>
 
7951
The standard sources can (almost) be used.  Additional sources can
 
7952
be found in the platform-specific subdirectories of the distribution.
 
7953
<P>
 
7954
 
 
7955
<A HREF="faqw.py?req=edit&amp;file=faq07.006.htp">Edit this entry</A> /
 
7956
<A HREF="faqw.py?req=log&amp;file=faq07.006.htp">Log info</A>
 
7957
<P>
 
7958
 
 
7959
<HR>
 
7960
<H2><A NAME="7.7">7.7. What is the status and support for the non-UNIX versions?</A></H2>
 
7961
I don't have access to most of these platforms, so in general I am
 
7962
dependent on material submitted by volunteers.  However I strive to
 
7963
integrate all changes needed to get it to compile on a particular
 
7964
platform back into the standard sources, so porting of the next
 
7965
version to the various non-UNIX platforms should be easy.
 
7966
(Note that Linux is classified as a UNIX platform here. :-)
 
7967
<P>
 
7968
Some specific platforms:
 
7969
<P>
 
7970
Windows: all versions (95, 98, ME, NT, 2000, XP) are supported,
 
7971
all python.org releases come with a Windows installer.
 
7972
<P>
 
7973
MacOS: Jack Jansen does an admirable job of keeping the Mac version
 
7974
up to date (both MacOS X and older versions);
 
7975
see <A HREF="http://www.cwi.nl/~jack/macpython.html">http://www.cwi.nl/~jack/macpython.html</A>
 
7976
<P>
 
7977
For all supported platforms, see <A HREF="http://www.python.org/download">http://www.python.org/download</A>/
 
7978
(follow the link to "Other platforms" for less common platforms)
 
7979
<P>
 
7980
 
 
7981
<A HREF="faqw.py?req=edit&amp;file=faq07.007.htp">Edit this entry</A> /
 
7982
<A HREF="faqw.py?req=log&amp;file=faq07.007.htp">Log info</A>
 
7983
 
 
7984
/ Last changed on Fri May 24 21:34:24 2002 by
 
7985
<A HREF="mailto:guido@python.org">GvR</A>
 
7986
<P>
 
7987
 
 
7988
<HR>
 
7989
<H2><A NAME="7.8">7.8. I have a PC version but it appears to be only a binary. Where's the library?</A></H2>
 
7990
If you are running any version of Windows, then you have the wrong distribution.  The FAQ lists current Windows versions.  Notably, Pythonwin and wpy provide fully functional installations.
 
7991
<P>
 
7992
But if you are sure you have the only distribution with a hope of working on
 
7993
your system, then...
 
7994
<P>
 
7995
You still need to copy the files from the distribution directory
 
7996
"python/Lib" to your system.  If you don't have the full distribution,
 
7997
you can get the file lib&lt;version&gt;.tar.gz from most ftp sites carrying
 
7998
Python; this is a subset of the distribution containing just those
 
7999
files, e.g.  <A HREF="ftp://ftp.python.org/pub/python/src/lib1.4.tar.gz">ftp://ftp.python.org/pub/python/src/lib1.4.tar.gz</A>.
 
8000
<P>
 
8001
Once you have installed the library, you need to point sys.path to it.
 
8002
Assuming the library is in C:\misc\python\lib, the following commands
 
8003
will point your Python interpreter to it (note the doubled backslashes
 
8004
-- you can also use single forward slashes instead):
 
8005
<P>
 
8006
<PRE>
 
8007
        >>> import sys
 
8008
        >>> sys.path.insert(0, 'C:\\misc\\python\\lib')
 
8009
        >>>
 
8010
</PRE>
 
8011
For a more permanent effect, set the environment variable PYTHONPATH,
 
8012
as follows (talking to a DOS prompt):
 
8013
<P>
 
8014
<PRE>
 
8015
        C> SET PYTHONPATH=C:\misc\python\lib
 
8016
</PRE>
 
8017
<P>
 
8018
 
 
8019
<A HREF="faqw.py?req=edit&amp;file=faq07.008.htp">Edit this entry</A> /
 
8020
<A HREF="faqw.py?req=log&amp;file=faq07.008.htp">Log info</A>
 
8021
 
 
8022
/ Last changed on Fri May 23 16:28:27 1997 by
 
8023
<A HREF="mailto:klm@python.org">Ken Manheimer</A>
 
8024
<P>
 
8025
 
 
8026
<HR>
 
8027
<H2><A NAME="7.9">7.9. Where's the documentation for the Mac or PC version?</A></H2>
 
8028
The documentation for the Unix version also applies to the Mac and
 
8029
PC versions.  Where applicable, differences are indicated in the text.
 
8030
<P>
 
8031
 
 
8032
<A HREF="faqw.py?req=edit&amp;file=faq07.009.htp">Edit this entry</A> /
 
8033
<A HREF="faqw.py?req=log&amp;file=faq07.009.htp">Log info</A>
 
8034
<P>
 
8035
 
 
8036
<HR>
 
8037
<H2><A NAME="7.10">7.10. How do I create a Python program file on the Mac or PC?</A></H2>
 
8038
Use an external editor.  On the Mac, BBEdit seems to be a popular
 
8039
no-frills text editor.  I work like this: start the interpreter; edit
 
8040
a module file using BBedit; import and test it in the interpreter;
 
8041
edit again in BBedit; then use the built-in function reload() to
 
8042
re-read the imported module; etc. In the 1.4 distribution 
 
8043
you will find a BBEdit extension that makes life a little easier: 
 
8044
it can tell the interpreter to execute the current window. 
 
8045
See :Mac:Tools:BBPy:README. 
 
8046
<P>
 
8047
Regarding the same question for the PC, Kurt Wm. Hemr writes: "While
 
8048
anyone with a pulse could certainly figure out how to do the same on
 
8049
MS-Windows, I would recommend the NotGNU Emacs clone for MS-Windows.
 
8050
Not only can you easily resave and "reload()" from Python after making
 
8051
changes, but since WinNot auto-copies to the clipboard any text you
 
8052
select, you can simply select the entire procedure (function) which
 
8053
you changed in WinNot, switch to QWPython, and shift-ins to reenter
 
8054
the changed program unit."
 
8055
<P>
 
8056
If you're using Windows95 or Windows NT, you should also know about
 
8057
PythonWin, which provides a GUI framework, with an mouse-driven
 
8058
editor, an object browser, and a GUI-based debugger.  See
 
8059
<PRE>
 
8060
       <A HREF="http://www.python.org/ftp/python/pythonwin">http://www.python.org/ftp/python/pythonwin</A>/
 
8061
</PRE>
 
8062
for details.
 
8063
<P>
 
8064
 
 
8065
<A HREF="faqw.py?req=edit&amp;file=faq07.010.htp">Edit this entry</A> /
 
8066
<A HREF="faqw.py?req=log&amp;file=faq07.010.htp">Log info</A>
 
8067
 
 
8068
/ Last changed on Sun May 25 10:04:25 1997 by
 
8069
<A HREF="mailto:guido@python.org">GvR</A>
 
8070
<P>
 
8071
 
 
8072
<HR>
 
8073
<H2><A NAME="7.11">7.11. How can I use Tkinter on Windows 95/NT?</A></H2>
 
8074
Starting from Python 1.5, it's very easy -- just download and install
 
8075
Python and Tcl/Tk and you're in business.  See 
 
8076
<P>
 
8077
<PRE>
 
8078
  <A HREF="http://www.python.org/download/download_windows.html">http://www.python.org/download/download_windows.html</A>
 
8079
</PRE>
 
8080
One warning: don't attempt to use Tkinter from PythonWin
 
8081
(Mark Hammond's IDE).  Use it from the command line interface
 
8082
(python.exe) or the windowless interpreter (pythonw.exe).
 
8083
<P>
 
8084
 
 
8085
<A HREF="faqw.py?req=edit&amp;file=faq07.011.htp">Edit this entry</A> /
 
8086
<A HREF="faqw.py?req=log&amp;file=faq07.011.htp">Log info</A>
 
8087
 
 
8088
/ Last changed on Fri Jun 12 09:32:48 1998 by
 
8089
<A HREF="mailto:guido@python.org">GvR</A>
 
8090
<P>
 
8091
 
 
8092
<HR>
 
8093
<H2><A NAME="7.12">7.12. cgi.py (or other CGI programming) doesn't work sometimes on NT or win95!</A></H2>
 
8094
Be sure you have the latest python.exe, that you are using
 
8095
python.exe rather than a GUI version of python and that you
 
8096
have configured the server to execute
 
8097
<P>
 
8098
<PRE>
 
8099
     "...\python.exe -u ..."
 
8100
</PRE>
 
8101
for the cgi execution.  The -u (unbuffered) option on NT and
 
8102
win95 prevents the interpreter from altering newlines in the
 
8103
standard input and output.  Without it post/multipart requests
 
8104
will seem to have the wrong length and binary (eg, GIF)
 
8105
responses may get garbled (resulting in, eg, a "broken image").
 
8106
<P>
 
8107
 
 
8108
<A HREF="faqw.py?req=edit&amp;file=faq07.012.htp">Edit this entry</A> /
 
8109
<A HREF="faqw.py?req=log&amp;file=faq07.012.htp">Log info</A>
 
8110
 
 
8111
/ Last changed on Wed Jul 30 10:48:02 1997 by
 
8112
<A HREF="mailto:aaron_watters@msn.com">aaron watters</A>
 
8113
<P>
 
8114
 
 
8115
<HR>
 
8116
<H2><A NAME="7.13">7.13. Why doesn't os.popen() work in PythonWin on NT?</A></H2>
 
8117
The reason that os.popen() doesn't work from within PythonWin is due to a bug in Microsoft's C Runtime Library (CRT). The CRT assumes you  have a Win32 console attached to the process.
 
8118
<P>
 
8119
You should use the win32pipe module's popen() instead which doesn't depend on having an attached Win32 console.
 
8120
<P>
 
8121
Example:
 
8122
<PRE>
 
8123
 import win32pipe
 
8124
 f = win32pipe.popen('dir /c c:\\')
 
8125
 print f.readlines()
 
8126
 f.close()
 
8127
</PRE>
 
8128
<P>
 
8129
 
 
8130
<A HREF="faqw.py?req=edit&amp;file=faq07.013.htp">Edit this entry</A> /
 
8131
<A HREF="faqw.py?req=log&amp;file=faq07.013.htp">Log info</A>
 
8132
 
 
8133
/ Last changed on Thu Jul 31 15:34:09 1997 by
 
8134
<A HREF="mailto:billtut@microsoft.com">Bill Tutt</A>
 
8135
<P>
 
8136
 
 
8137
<HR>
 
8138
<H2><A NAME="7.14">7.14. How do I use different functionality on different platforms with the same program?</A></H2>
 
8139
Remember that Python is extremely dynamic and that you
 
8140
can use this dynamism to configure a program at run-time to
 
8141
use available functionality on different platforms.  For example
 
8142
you can test the sys.platform and import different modules based
 
8143
on its value.
 
8144
<P>
 
8145
<PRE>
 
8146
   import sys
 
8147
   if sys.platform == "win32":
 
8148
      import win32pipe
 
8149
      popen = win32pipe.popen
 
8150
   else:
 
8151
      import os
 
8152
      popen = os.popen
 
8153
</PRE>
 
8154
(See FAQ 7.13 for an explanation of why you might want to
 
8155
do something like this.)  Also you can try to import a module
 
8156
and use a fallback if the import fails:
 
8157
<P>
 
8158
<PRE>
 
8159
    try:
 
8160
         import really_fast_implementation
 
8161
         choice = really_fast_implementation
 
8162
    except ImportError:
 
8163
         import slower_implementation
 
8164
         choice = slower_implementation
 
8165
</PRE>
 
8166
<P>
 
8167
 
 
8168
<A HREF="faqw.py?req=edit&amp;file=faq07.014.htp">Edit this entry</A> /
 
8169
<A HREF="faqw.py?req=log&amp;file=faq07.014.htp">Log info</A>
 
8170
 
 
8171
/ Last changed on Wed Aug 13 07:39:06 1997 by
 
8172
<A HREF="mailto:aaron_watters@msn.com">aaron watters</A>
 
8173
<P>
 
8174
 
 
8175
<HR>
 
8176
<H2><A NAME="7.15">7.15. Is there an Amiga version of Python?</A></H2>
 
8177
Yes. See the AmigaPython homepage at <A HREF="http://www.bigfoot.com/~irmen/python.html">http://www.bigfoot.com/~irmen/python.html</A>.
 
8178
<P>
 
8179
 
 
8180
<A HREF="faqw.py?req=edit&amp;file=faq07.015.htp">Edit this entry</A> /
 
8181
<A HREF="faqw.py?req=log&amp;file=faq07.015.htp">Log info</A>
 
8182
 
 
8183
/ Last changed on Mon Dec 14 06:53:32 1998 by
 
8184
<A HREF="mailto:irmen@bigfoot.com">Irmen de Jong</A>
 
8185
<P>
 
8186
 
 
8187
<HR>
 
8188
<H2><A NAME="7.16">7.16. Why doesn't os.popen()/win32pipe.popen() work on Win9x?</A></H2>
 
8189
There is a bug in Win9x that prevents os.popen/win32pipe.popen* from working. The good news is there is a way to work around this problem.
 
8190
The Microsoft Knowledge Base article that you need to lookup is: Q150956. You will find links to the knowledge base at:
 
8191
<A HREF="http://www.microsoft.com/kb">http://www.microsoft.com/kb</A>.
 
8192
<P>
 
8193
 
 
8194
<A HREF="faqw.py?req=edit&amp;file=faq07.016.htp">Edit this entry</A> /
 
8195
<A HREF="faqw.py?req=log&amp;file=faq07.016.htp">Log info</A>
 
8196
 
 
8197
/ Last changed on Fri Jun 25 10:45:38 1999 by
 
8198
<A HREF="mailto:billtut@microsoft.com">Bill Tutt</A>
 
8199
<P>
 
8200
 
 
8201
<HR>
 
8202
<H1>8. Python on Windows</H1>
 
8203
 
 
8204
<HR>
 
8205
<H2><A NAME="8.1">8.1. Using Python for CGI on Microsoft Windows</A></H2>
 
8206
** Setting up the Microsoft IIS Server/Peer Server
 
8207
<P>
 
8208
On the Microsoft IIS 
 
8209
server or on the Win95 MS Personal Web Server
 
8210
you set up python in the same way that you 
 
8211
would set up any other scripting engine. 
 
8212
<P>
 
8213
Run regedt32 and go to: 
 
8214
<P>
 
8215
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W3SVC\Parameters\ScriptMap 
 
8216
<P>
 
8217
and enter the following line (making any specific changes that your system may need) 
 
8218
<P>
 
8219
.py :REG_SZ: c:\&lt;path to python&gt;\python.exe -u %s %s 
 
8220
<P>
 
8221
This line will allow you to call your script with a simple reference like:
 
8222
http://yourserver/scripts/yourscript.py 
 
8223
provided "scripts" is an "executable" directory for your server (which
 
8224
it usually is by default).
 
8225
The "-u" flag specifies unbuffered and binary mode for stdin - needed when working with binary data
 
8226
<P>
 
8227
In addition, it is recommended by people who would know that using ".py" may 
 
8228
not be a good idea for the file extensions when used in this context
 
8229
(you might want to reserve *.py for support modules and use *.cgi or *.cgp
 
8230
for "main program" scripts).
 
8231
However, that issue is beyond this Windows FAQ entry.
 
8232
<P>
 
8233
<P>
 
8234
** Apache configuration
 
8235
<P>
 
8236
In the Apache configuration file httpd.conf, add the following line at 
 
8237
the end of the file:
 
8238
<P>
 
8239
ScriptInterpreterSource Registry
 
8240
<P>
 
8241
Then, give your Python CGI-scripts the extension .py and put them in the cgi-bin directory.
 
8242
<P>
 
8243
<P>
 
8244
** Netscape Servers:
 
8245
Information on this topic exists at:
 
8246
<A HREF="http://home.netscape.com/comprod/server_central/support/fasttrack_man/programs.htm#1010870">http://home.netscape.com/comprod/server_central/support/fasttrack_man/programs.htm#1010870</A>
 
8247
<P>
 
8248
 
 
8249
<A HREF="faqw.py?req=edit&amp;file=faq08.001.htp">Edit this entry</A> /
 
8250
<A HREF="faqw.py?req=log&amp;file=faq08.001.htp">Log info</A>
 
8251
 
 
8252
/ Last changed on Wed Mar 27 12:25:54 2002 by
 
8253
<A HREF="mailto:gerhard@bigfoot.de">Gerhard H�ring</A>
 
8254
<P>
 
8255
 
 
8256
<HR>
 
8257
<H2><A NAME="8.2">8.2. How to check for a keypress without blocking?</A></H2>
 
8258
Use the msvcrt module.  This is a standard Windows-specific extensions
 
8259
in Python 1.5 and beyond.  It defines a function kbhit() which checks
 
8260
whether a keyboard hit is present; also getch() which gets one
 
8261
character without echo.  Plus a few other goodies.
 
8262
<P>
 
8263
(Search for "keypress" to find an answer for Unix as well.)
 
8264
<P>
 
8265
 
 
8266
<A HREF="faqw.py?req=edit&amp;file=faq08.002.htp">Edit this entry</A> /
 
8267
<A HREF="faqw.py?req=log&amp;file=faq08.002.htp">Log info</A>
 
8268
 
 
8269
/ Last changed on Mon Mar 30 16:21:46 1998 by
 
8270
<A HREF="mailto:guido@python.org">GvR</A>
 
8271
<P>
 
8272
 
 
8273
<HR>
 
8274
<H2><A NAME="8.3">8.3. $PYTHONPATH</A></H2>
 
8275
In MS-DOS derived environments, a unix variable such as $PYTHONPATH is 
 
8276
set as PYTHONPATH, without the dollar sign.  PYTHONPATH is useful for
 
8277
specifying the location of library files.
 
8278
<P>
 
8279
 
 
8280
<A HREF="faqw.py?req=edit&amp;file=faq08.003.htp">Edit this entry</A> /
 
8281
<A HREF="faqw.py?req=log&amp;file=faq08.003.htp">Log info</A>
 
8282
 
 
8283
/ Last changed on Thu Jun 11 00:41:26 1998 by
 
8284
<A HREF="mailto:guido@python.org">Gvr</A>
 
8285
<P>
 
8286
 
 
8287
<HR>
 
8288
<H2><A NAME="8.4">8.4. dedent syntax errors</A></H2>
 
8289
The FAQ does not recommend using tabs, and Guido's Python Style Guide recommends 4 spaces for distributed Python code; this is also the Emacs python-mode default; see
 
8290
<P>
 
8291
<PRE>
 
8292
    <A HREF="http://www.python.org/doc/essays/styleguide.html">http://www.python.org/doc/essays/styleguide.html</A>
 
8293
</PRE>
 
8294
Under any editor mixing tabs and spaces is a bad idea.  MSVC is no different in this respect, and is easily configured to use spaces:  Take Tools -> Options -> Tabs, and for file type "Default" set "Tab size" and "Indent size" to 4, and select the "Insert spaces" radio button.
 
8295
<P>
 
8296
If you suspect mixed tabs and spaces are causing problems in leading whitespace, run Python with the -t switch or, run Tools/Scripts/tabnanny.py to check a directory tree in batch mode.
 
8297
<P>
 
8298
 
 
8299
<A HREF="faqw.py?req=edit&amp;file=faq08.004.htp">Edit this entry</A> /
 
8300
<A HREF="faqw.py?req=log&amp;file=faq08.004.htp">Log info</A>
 
8301
 
 
8302
/ Last changed on Mon Feb 12 15:04:14 2001 by
 
8303
<A HREF="mailto:sholden@holdenweb.com">Steve Holden</A>
 
8304
<P>
 
8305
 
 
8306
<HR>
 
8307
<H2><A NAME="8.5">8.5. How do I emulate os.kill() in Windows?</A></H2>
 
8308
Use win32api:
 
8309
<P>
 
8310
<PRE>
 
8311
    def kill(pid):
 
8312
        """kill function for Win32"""
 
8313
        import win32api
 
8314
        handle = win32api.OpenProcess(1, 0, pid)
 
8315
        return (0 != win32api.TerminateProcess(handle, 0))
 
8316
</PRE>
 
8317
<P>
 
8318
 
 
8319
<A HREF="faqw.py?req=edit&amp;file=faq08.005.htp">Edit this entry</A> /
 
8320
<A HREF="faqw.py?req=log&amp;file=faq08.005.htp">Log info</A>
 
8321
 
 
8322
/ Last changed on Sat Aug  8 18:55:06 1998 by
 
8323
<A HREF="mailto:jeffbauer@bigfoot.com">Jeff Bauer</A>
 
8324
<P>
 
8325
 
 
8326
<HR>
 
8327
<H2><A NAME="8.6">8.6. Why does os.path.isdir() fail on NT shared directories?</A></H2>
 
8328
The solution appears to be always append the "\\" on 
 
8329
the end of shared drives.
 
8330
<P>
 
8331
<PRE>
 
8332
  >>> import os
 
8333
  >>> os.path.isdir( '\\\\rorschach\\public')
 
8334
  0
 
8335
  >>> os.path.isdir( '\\\\rorschach\\public\\')
 
8336
  1
 
8337
</PRE>
 
8338
[Blake Winton responds:]
 
8339
I've had the same problem doing "Start >> Run" and then a
 
8340
directory on a shared drive.  If I use "\\rorschach\public", 
 
8341
it will fail, but if I use "\\rorschach\public\", it will 
 
8342
work.  For that matter, os.stat() does the same thing (well, 
 
8343
it gives an error for "\\\\rorschach\\public", but you get 
 
8344
the idea)...
 
8345
<P>
 
8346
I've got a theory about why this happens, but it's only 
 
8347
a theory.  NT knows the difference between shared directories, 
 
8348
and regular directories.  "\\rorschach\public" isn't a 
 
8349
directory, it's _really_ an IPC abstraction.  This is sort 
 
8350
of lended credence to by the fact that when you're mapping 
 
8351
a network drive, you can't map "\\rorschach\public\utils", 
 
8352
but only "\\rorschach\public".
 
8353
<P>
 
8354
[Clarification by <A HREF="mailto:funkster@midwinter.com">funkster@midwinter.com</A>]
 
8355
It's not actually a Python 
 
8356
question, as Python is working just fine; it's clearing up something 
 
8357
a bit muddled about Windows networked drives.
 
8358
<P>
 
8359
It helps to think of share points as being like drive letters.
 
8360
Example:
 
8361
<PRE>
 
8362
        k: is not a directory
 
8363
        k:\ is a directory
 
8364
        k:\media is a directory
 
8365
        k:\media\ is not a directory
 
8366
</PRE>
 
8367
The same rules apply if you substitute "k:" with "\\conky\foo":
 
8368
<PRE>
 
8369
        \\conky\foo  is not a directory
 
8370
        \\conky\foo\ is a directory
 
8371
        \\conky\foo\media is a directory
 
8372
        \\conky\foo\media\ is not a directory
 
8373
</PRE>
 
8374
<P>
 
8375
 
 
8376
<A HREF="faqw.py?req=edit&amp;file=faq08.006.htp">Edit this entry</A> /
 
8377
<A HREF="faqw.py?req=log&amp;file=faq08.006.htp">Log info</A>
 
8378
 
 
8379
/ Last changed on Sun Jan 31 08:44:48 1999 by
 
8380
<A HREF="mailto:guido@python.org">GvR</A>
 
8381
<P>
 
8382
 
 
8383
<HR>
 
8384
<H2><A NAME="8.7">8.7. PyRun_SimpleFile() crashes on Windows but not on Unix</A></H2>
 
8385
I've seen a number of reports of PyRun_SimpleFile() failing
 
8386
in a Windows port of an application embedding Python that worked
 
8387
fine on Unix.  PyRun_SimpleString() works fine on both platforms.
 
8388
<P>
 
8389
I think this happens because the application was compiled with a 
 
8390
different set of compiler flags than Python15.DLL.  It seems that some 
 
8391
compiler flags affect the standard I/O library in such a way that 
 
8392
using different flags makes calls fail.  You need to set it for 
 
8393
the non-debug multi-threaded DLL (/MD on the command line, or can be set via MSVC under Project Settings-&gt;C++/Code Generation then the "Use rum-time library" dropdown.)
 
8394
<P>
 
8395
Also note that you can not mix-and-match Debug and Release versions.  If you wish to use the Debug Multithreaded DLL, then your module _must_ have an "_d" appended to the base name.
 
8396
<P>
 
8397
 
 
8398
<A HREF="faqw.py?req=edit&amp;file=faq08.007.htp">Edit this entry</A> /
 
8399
<A HREF="faqw.py?req=log&amp;file=faq08.007.htp">Log info</A>
 
8400
 
 
8401
/ Last changed on Wed Nov 17 17:37:07 1999 by
 
8402
<A HREF="mailto:mhammond@skippinet.com.au">Mark Hammond</A>
 
8403
<P>
 
8404
 
 
8405
<HR>
 
8406
<H2><A NAME="8.8">8.8. Import of _tkinter fails on Windows 95/98</A></H2>
 
8407
Sometimes, the import of _tkinter fails on Windows 95 or 98,
 
8408
complaining with a message like the following:
 
8409
<P>
 
8410
<PRE>
 
8411
  ImportError: DLL load failed: One of the library files needed
 
8412
  to run this application cannot be found.
 
8413
</PRE>
 
8414
It could be that you haven't installed Tcl/Tk, but if you did
 
8415
install Tcl/Tk, and the Wish application works correctly,
 
8416
the problem may be that its installer didn't
 
8417
manage to edit the autoexec.bat file correctly.  It tries to add a
 
8418
statement that changes the PATH environment variable to include
 
8419
the Tcl/Tk 'bin' subdirectory, but sometimes this edit doesn't
 
8420
quite work.  Opening it with notepad usually reveals what the
 
8421
problem is.
 
8422
<P>
 
8423
(One additional hint, noted by David Szafranski: you can't use
 
8424
long filenames here; e.g. use C:\PROGRA~1\Tcl\bin instead of
 
8425
C:\Program Files\Tcl\bin.)
 
8426
<P>
 
8427
 
 
8428
<A HREF="faqw.py?req=edit&amp;file=faq08.008.htp">Edit this entry</A> /
 
8429
<A HREF="faqw.py?req=log&amp;file=faq08.008.htp">Log info</A>
 
8430
 
 
8431
/ Last changed on Wed Dec  2 22:32:41 1998 by
 
8432
<A HREF="mailto:guido@python.org">GvR</A>
 
8433
<P>
 
8434
 
 
8435
<HR>
 
8436
<H2><A NAME="8.9">8.9. Can't extract the downloaded documentation on Windows</A></H2>
 
8437
Sometimes, when you download the documentation package to a Windows
 
8438
machine using a web browser, the file extension of the saved file
 
8439
ends up being .EXE.  This is a mistake; the extension should be .TGZ.
 
8440
<P>
 
8441
Simply rename the downloaded file to have the .TGZ extension, and
 
8442
WinZip will be able to handle it.  (If your copy of WinZip doesn't,
 
8443
get a newer one from <A HREF="http://www.winzip.com">http://www.winzip.com</A>.)
 
8444
<P>
 
8445
 
 
8446
<A HREF="faqw.py?req=edit&amp;file=faq08.009.htp">Edit this entry</A> /
 
8447
<A HREF="faqw.py?req=log&amp;file=faq08.009.htp">Log info</A>
 
8448
 
 
8449
/ Last changed on Sat Nov 21 13:41:35 1998 by
 
8450
<A HREF="mailto:guido@python.org">GvR</A>
 
8451
<P>
 
8452
 
 
8453
<HR>
 
8454
<H2><A NAME="8.10">8.10. Can't get Py_RunSimpleFile() to work.</A></H2>
 
8455
This is very sensitive to the compiler vendor, version and (perhaps)
 
8456
even options.  If the FILE* structure in your embedding program isn't
 
8457
the same as is assumed by the Python interpreter it won't work.
 
8458
<P>
 
8459
The Python 1.5.* DLLs (python15.dll) are all compiled
 
8460
with MS VC++ 5.0 and with multithreading-DLL options (/MD, I think).
 
8461
<P>
 
8462
If you can't change compilers or flags, try using Py_RunSimpleString().
 
8463
A trick to get it to run an arbitrary file is to construct a call to
 
8464
execfile() with the name of your file as argument.
 
8465
<P>
 
8466
 
 
8467
<A HREF="faqw.py?req=edit&amp;file=faq08.010.htp">Edit this entry</A> /
 
8468
<A HREF="faqw.py?req=log&amp;file=faq08.010.htp">Log info</A>
 
8469
 
 
8470
/ Last changed on Wed Jan 13 10:58:14 1999 by
 
8471
<A HREF="mailto:guido@python.org">GvR</A>
 
8472
<P>
 
8473
 
 
8474
<HR>
 
8475
<H2><A NAME="8.11">8.11. Where is Freeze for Windows?</A></H2>
 
8476
("Freeze" is a program that allows you to ship a Python program
 
8477
as a single stand-alone executable file.  It is <I>not</I> a compiler,
 
8478
your programs don't run any faster, but they are more easily
 
8479
distributable (to platforms with the same OS and CPU).  Read the
 
8480
README file of the freeze program for more disclaimers.)
 
8481
<P>
 
8482
You can use freeze on Windows, but you must download the source
 
8483
tree (see <A HREF="http://www.python.org/download/download_source.html">http://www.python.org/download/download_source.html</A>).
 
8484
This is recommended for Python 1.5.2 (and betas thereof) only;
 
8485
older versions don't quite work.
 
8486
<P>
 
8487
You need the Microsoft VC++ 5.0 compiler (maybe it works with
 
8488
6.0 too).  You probably need to build Python -- the project files
 
8489
are all in the PCbuild directory.
 
8490
<P>
 
8491
The freeze program is in the Tools\freeze subdirectory of the source
 
8492
tree.
 
8493
<P>
 
8494
 
 
8495
<A HREF="faqw.py?req=edit&amp;file=faq08.011.htp">Edit this entry</A> /
 
8496
<A HREF="faqw.py?req=log&amp;file=faq08.011.htp">Log info</A>
 
8497
 
 
8498
/ Last changed on Wed Feb 17 18:47:24 1999 by
 
8499
<A HREF="mailto:guido@python.org">GvR</A>
 
8500
<P>
 
8501
 
 
8502
<HR>
 
8503
<H2><A NAME="8.12">8.12. Is a *.pyd file the same as a DLL?</A></H2>
 
8504
Yes, .pyd files are dll's.  But there are a few differences.  If you
 
8505
have a DLL named foo.pyd, then it must have a function initfoo().  You
 
8506
can then write Python "import foo", and Python will search for foo.pyd
 
8507
(as well as foo.py, foo.pyc) and if it finds it, will attempt to call
 
8508
initfoo() to initialize it.  You do not link your .exe with foo.lib,
 
8509
as that would cause Windows to require the DLL to be present.
 
8510
<P>
 
8511
Note that the search path for foo.pyd is PYTHONPATH, not the same as
 
8512
the path that Windows uses to search for foo.dll.  Also, foo.pyd need
 
8513
not be present to run your program, whereas if you linked your program
 
8514
with a dll, the dll is required.  Of course, foo.pyd is required if
 
8515
you want to say "import foo".  In a dll, linkage is declared in the
 
8516
source code with __declspec(dllexport).  In a .pyd, linkage is defined
 
8517
in a list of available functions.
 
8518
<P>
 
8519
 
 
8520
<A HREF="faqw.py?req=edit&amp;file=faq08.012.htp">Edit this entry</A> /
 
8521
<A HREF="faqw.py?req=log&amp;file=faq08.012.htp">Log info</A>
 
8522
 
 
8523
/ Last changed on Tue Nov 23 02:40:08 1999 by
 
8524
<A HREF="mailto:jquinn+psa@cs.oberlin.edu">Jameson Quinn</A>
 
8525
<P>
 
8526
 
 
8527
<HR>
 
8528
<H2><A NAME="8.13">8.13. Missing cw3215mt.dll (or missing cw3215.dll)</A></H2>
 
8529
Sometimes, when using Tkinter on Windows, you get an error that
 
8530
cw3215mt.dll or cw3215.dll is missing.
 
8531
<P>
 
8532
Cause: you have an old Tcl/Tk DLL built with cygwin in your path
 
8533
(probably C:\Windows).  You must use the Tcl/Tk DLLs from the
 
8534
standard Tcl/Tk installation (Python 1.5.2 comes with one).
 
8535
<P>
 
8536
 
 
8537
<A HREF="faqw.py?req=edit&amp;file=faq08.013.htp">Edit this entry</A> /
 
8538
<A HREF="faqw.py?req=log&amp;file=faq08.013.htp">Log info</A>
 
8539
 
 
8540
/ Last changed on Fri Jun 11 00:54:13 1999 by
 
8541
<A HREF="mailto:guido@python.org">GvR</A>
 
8542
<P>
 
8543
 
 
8544
<HR>
 
8545
<H2><A NAME="8.14">8.14. How to make python scripts executable:</A></H2>
 
8546
[Blake Coverett]
 
8547
<P>
 
8548
Win2K:
 
8549
<P>
 
8550
The standard installer already associates the .py extension with a file type
 
8551
(Python.File) and gives that file type an open command that runs the
 
8552
interpreter (D:\Program Files\Python\python.exe "%1" %*).  This is enough to
 
8553
make scripts executable from the command prompt as 'foo.py'.  If you'd
 
8554
rather be able to execute the script by simple typing 'foo' with no
 
8555
extension you need to add .py to the PATHEXT environment variable.
 
8556
<P>
 
8557
WinNT:
 
8558
<P>
 
8559
The steps taken by the installed as described above allow you do run a
 
8560
script with 'foo.py', but a long time bug in the NT command processor
 
8561
prevents you from redirecting the input or output of any script executed in
 
8562
this way.  This is often important.
 
8563
<P>
 
8564
An appropriate incantation for making a Python script executable under WinNT
 
8565
is to give the file an extension of .cmd and add the following as the first
 
8566
line:
 
8567
<P>
 
8568
<PRE>
 
8569
    @setlocal enableextensions &amp; python -x %~f0 %* &amp; goto :EOF
 
8570
</PRE>
 
8571
Win9x:
 
8572
<P>
 
8573
[Due to Bruce Eckel]
 
8574
<P>
 
8575
<PRE>
 
8576
  @echo off
 
8577
  rem = """
 
8578
  rem run python on this bat file. Needs the full path where
 
8579
  rem you keep your python files. The -x causes python to skip
 
8580
  rem the first line of the file:
 
8581
  python -x c:\aaa\Python\\"%0".bat %1 %2 %3 %4 %5 %6 %7 %8 %9
 
8582
  goto endofpython
 
8583
  rem """
 
8584
</PRE>
 
8585
<PRE>
 
8586
  # The python program goes here:
 
8587
</PRE>
 
8588
<PRE>
 
8589
  print "hello, Python"
 
8590
</PRE>
 
8591
<PRE>
 
8592
  # For the end of the batch file:
 
8593
  rem = """
 
8594
  :endofpython
 
8595
  rem """
 
8596
</PRE>
 
8597
<P>
 
8598
 
 
8599
<A HREF="faqw.py?req=edit&amp;file=faq08.014.htp">Edit this entry</A> /
 
8600
<A HREF="faqw.py?req=log&amp;file=faq08.014.htp">Log info</A>
 
8601
 
 
8602
/ Last changed on Tue Nov 30 10:25:17 1999 by
 
8603
<A HREF="mailto:guido@python.org">GvR</A>
 
8604
<P>
 
8605
 
 
8606
<HR>
 
8607
<H2><A NAME="8.15">8.15. Warning about CTL3D32 version from installer</A></H2>
 
8608
The Python installer issues a warning like this:
 
8609
<P>
 
8610
<PRE>
 
8611
  This version uses CTL3D32.DLL whitch is not the correct version.
 
8612
  This version is used for windows NT applications only.
 
8613
</PRE>
 
8614
[Tim Peters]
 
8615
This is a Microsoft DLL, and a notorious
 
8616
source of problems.  The msg means what it says:  you have the wrong version
 
8617
of this DLL for your operating system.  The Python installation did not
 
8618
cause this -- something else you installed previous to this overwrote the
 
8619
DLL that came with your OS (probably older shareware of some sort, but
 
8620
there's no way to tell now).  If you search for "CTL3D32" using any search
 
8621
engine (AltaVista, for example), you'll find hundreds and hundreds of web
 
8622
pages complaining about the same problem with all sorts of installation
 
8623
programs.  They'll point you to ways to get the correct version reinstalled
 
8624
on your system (since Python doesn't cause this, we can't fix it).
 
8625
<P>
 
8626
David A Burton has written a little program to fix this.  Go to
 
8627
<A HREF="http://www.burtonsys.com/download.html">http://www.burtonsys.com/download.html</A> and click on "ctl3dfix.zip"
 
8628
<P>
 
8629
 
 
8630
<A HREF="faqw.py?req=edit&amp;file=faq08.015.htp">Edit this entry</A> /
 
8631
<A HREF="faqw.py?req=log&amp;file=faq08.015.htp">Log info</A>
 
8632
 
 
8633
/ Last changed on Thu Oct 26 15:42:00 2000 by
 
8634
<A HREF="mailto:guido@python.org">GvR</A>
 
8635
<P>
 
8636
 
 
8637
<HR>
 
8638
<H2><A NAME="8.16">8.16. How can I embed Python into a Windows application?</A></H2>
 
8639
Edward K. Ream &lt;<A HREF="mailto:edream@tds.net">edream@tds.net</A>&gt; writes
 
8640
<P>
 
8641
When '##' appears in a file name below, it is an abbreviated version number.  For example, for Python 2.1.1, ## will be replaced by 21.
 
8642
<P>
 
8643
Embedding the Python interpreter in a Windows app can be summarized as
 
8644
follows:
 
8645
<P>
 
8646
1. Do _not_ build Python into your .exe file directly.  On Windows,
 
8647
Python must be a DLL to handle importing modules that are themselves
 
8648
DLL's.  (This is the first key undocumented fact.) Instead, link to
 
8649
python##.dll; it is typically installed in c:\Windows\System.  
 
8650
<P>
 
8651
You can link to Python statically or dynamically.  Linking statically
 
8652
means linking against python##.lib The drawback is that your app won't
 
8653
run if python##.dll does not exist on your system.
 
8654
<P>
 
8655
General note: python##.lib is the so-called "import lib" corresponding
 
8656
to python.dll.  It merely defines symbols for the linker.
 
8657
<P>
 
8658
Borland note: convert python##.lib to OMF format using Coff2Omf.exe
 
8659
first.
 
8660
<P>
 
8661
Linking dynamically greatly simplifies link options; everything happens
 
8662
at run time.  Your code must load python##.dll using the Windows
 
8663
LoadLibraryEx() routine.  The code must also use access routines and
 
8664
data in python##.dll (that is, Python's C API's) using pointers
 
8665
obtained by the Windows GetProcAddress() routine.  Macros can make
 
8666
using these pointers transparent to any C code that calls routines in
 
8667
Python's C API.
 
8668
<P>
 
8669
2. If you use SWIG, it is easy to create a Python "extension module"
 
8670
that will make the app's data and methods available to Python.  SWIG
 
8671
will handle just about all the grungy details for you.  The result is C
 
8672
code that you link _into your .exe file_ (!)  You do _not_ have to
 
8673
create a DLL file, and this also simplifies linking.
 
8674
<P>
 
8675
3.  SWIG will create an init function (a C function) whose name depends
 
8676
on the name of the extension module.  For example, if the name of the
 
8677
module is leo, the init function will be called initleo().  If you use
 
8678
SWIG shadow classes, as you should, the init function will be called
 
8679
initleoc().  This initializes a mostly hidden helper class used by the
 
8680
shadow class.
 
8681
<P>
 
8682
The reason you can link the C code in step 2 into your .exe file is that
 
8683
calling the initialization function is equivalent to importing the
 
8684
module into Python! (This is the second key undocumented fact.)
 
8685
<P>
 
8686
4. In short, you can use the following code to initialize the Python
 
8687
interpreter with your extension module.
 
8688
<P>
 
8689
<PRE>
 
8690
    #include "python.h"
 
8691
    ...
 
8692
    Py_Initialize();  // Initialize Python.
 
8693
    initmyAppc();  // Initialize (import) the helper class. 
 
8694
    PyRun_SimpleString("import myApp") ;  // Import the shadow class.
 
8695
</PRE>
 
8696
5. There are two problems with Python's C API which will become apparent
 
8697
if you use a compiler other than MSVC, the compiler used to build
 
8698
python##.dll.
 
8699
<P>
 
8700
Problem 1: The so-called "Very High Level" functions that take FILE *
 
8701
arguments will not work in a multi-compiler environment; each compiler's
 
8702
notion of a struct FILE will be different.  From an implementation
 
8703
standpoint these are very _low_ level functions.
 
8704
<P>
 
8705
Problem 2: SWIG generates the following code when generating wrappers to
 
8706
void functions:
 
8707
<P>
 
8708
<PRE>
 
8709
    Py_INCREF(Py_None);
 
8710
    _resultobj = Py_None;
 
8711
    return _resultobj;
 
8712
</PRE>
 
8713
Alas, Py_None is a macro that expands to a reference to a complex data
 
8714
structure called _Py_NoneStruct inside python##.dll.  Again, this code
 
8715
will fail in a mult-compiler environment.  Replace such code by:
 
8716
<P>
 
8717
<PRE>
 
8718
    return Py_BuildValue("");
 
8719
</PRE>
 
8720
It may be possible to use SWIG's %typemap command to make the change
 
8721
automatically, though I have not been able to get this to work (I'm a
 
8722
complete SWIG newbie).
 
8723
<P>
 
8724
6. Using a Python shell script to put up a Python interpreter window
 
8725
from inside your Windows app is not a good idea; the resulting window
 
8726
will be independent of your app's windowing system.  Rather, you (or the
 
8727
wxPythonWindow class) should create a "native" interpreter window.  It
 
8728
is easy to connect that window to the Python interpreter.  You can
 
8729
redirect Python's i/o to _any_ object that supports read and write, so
 
8730
all you need is a Python object (defined in your extension module) that
 
8731
contains read() and write() methods.
 
8732
<P>
 
8733
 
 
8734
<A HREF="faqw.py?req=edit&amp;file=faq08.016.htp">Edit this entry</A> /
 
8735
<A HREF="faqw.py?req=log&amp;file=faq08.016.htp">Log info</A>
 
8736
 
 
8737
/ Last changed on Thu Jan 31 16:29:34 2002 by
 
8738
<A HREF="mailto:vkryukov@ufg.com">Victor Kryukov</A>
 
8739
<P>
 
8740
 
 
8741
<HR>
 
8742
<H2><A NAME="8.17">8.17. Setting up IIS 5 to use Python for CGI</A></H2>
 
8743
In order to set up Internet Information Services 5 to use Python for CGI processing, please see the following links:
 
8744
<P>
 
8745
<A HREF="http://www.e-coli.net/pyiis_server.html">http://www.e-coli.net/pyiis_server.html</A> (for Win2k Server)
 
8746
<A HREF="http://www.e-coli.net/pyiis.html">http://www.e-coli.net/pyiis.html</A> (for Win2k pro)
 
8747
<P>
 
8748
 
 
8749
<A HREF="faqw.py?req=edit&amp;file=faq08.017.htp">Edit this entry</A> /
 
8750
<A HREF="faqw.py?req=log&amp;file=faq08.017.htp">Log info</A>
 
8751
 
 
8752
/ Last changed on Fri Mar 22 22:05:51 2002 by
 
8753
<A HREF="mailto:dsavitsk@e-coli.net">douglas savitsky</A>
 
8754
<P>
 
8755
 
 
8756
<HR>
 
8757
<H2><A NAME="8.18">8.18. How do I run a Python program under Windows?</A></H2>
 
8758
This is not necessarily quite the straightforward question it appears
 
8759
to be. If you are already familiar with running programs from the
 
8760
Windows command line then everything will seem really easy and
 
8761
obvious. If your computer experience is limited then you might need a
 
8762
little more guidance. Also there are differences between Windows 95,
 
8763
98, NT, ME, 2000 and XP which can add to the confusion. You might
 
8764
think of this as "why I pay software support charges" if you have a
 
8765
helpful and friendly administrator to help you set things up without
 
8766
having to understand all this yourself. If so, then great! Show them
 
8767
this page and it should be a done deal.
 
8768
<P>
 
8769
Unless you use some sort of integrated development environment (such
 
8770
as PythonWin or IDLE, to name only two in a growing family) then you
 
8771
will end up <I>typing</I> Windows commands into what is variously referred
 
8772
to as a "DOS window" or "Command prompt window". Usually you can
 
8773
create such a window from your Start menu (under Windows 2000 I use
 
8774
"Start | Programs | Accessories | Command Prompt").  You should be
 
8775
able to recognize when you have started such a window because you will
 
8776
see a Windows "command prompt", which usually looks like this:
 
8777
<P>
 
8778
<PRE>
 
8779
    C:\>
 
8780
</PRE>
 
8781
The letter may be different, and there might be other things after it,
 
8782
so you might just as easily see something like:
 
8783
<P>
 
8784
<PRE>
 
8785
    D:\Steve\Projects\Python>
 
8786
</PRE>
 
8787
depending on how your computer has been set up and what else you have
 
8788
recently done with it. Once you have started such a window, you are
 
8789
well on the way to running Python programs.
 
8790
<P>
 
8791
You need to realize that your Python scripts have to be processed by
 
8792
another program, usually called the "Python interpreter". The
 
8793
interpreter reads your script, "compiles" it into "Python bytecodes"
 
8794
(which are instructions for an imaginary computer known as the "Python
 
8795
Virtual Machine") and then executes the bytecodes to run your
 
8796
program. So, how do you arrange for the interpreter to handle your
 
8797
Python?
 
8798
<P>
 
8799
First, you need to make sure that your command window recognises the
 
8800
word "python" as an instruction to start the interpreter. If you have
 
8801
opened a command window, you should try entering the command:
 
8802
<P>
 
8803
<PRE>
 
8804
    python
 
8805
</PRE>
 
8806
and hitting return. If you then see something like:
 
8807
<P>
 
8808
<PRE>
 
8809
    Python 2.2 (#28, Dec 21 2001, 12:21:22) [MSC 32 bit (Intel)] on win32
 
8810
    Type "help", "copyright", "credits" or "license" for more information.
 
8811
    >>>
 
8812
</PRE>
 
8813
then this part of the job has been correctly managed during Python's
 
8814
installation process, and you have started the interpreter in
 
8815
"interactive mode". That means you can enter Python statements or
 
8816
expressions interactively and have them executed or evaluated while
 
8817
you wait. This is one of Python's strongest features, but it takes a
 
8818
little getting used to. Check it by entering a few expressions of your
 
8819
choice and seeing the results...
 
8820
<P>
 
8821
<PRE>
 
8822
    >>> print "Hello"
 
8823
    Hello
 
8824
    >>> "Hello" * 3
 
8825
    HelloHelloHello
 
8826
</PRE>
 
8827
When you want to end your interactive Python session, enter a
 
8828
terminator (hold the Ctrl key down while you enter a Z, then hit the
 
8829
"Enter" key) to get back to your Windows command prompt.  You may also
 
8830
find that you have a Start-menu entry such as "Start | Programs |
 
8831
Python 2.2 | Python (command line)" that results in you seeing the
 
8832
">>>" prompt in a new window. If so, the window will disappear after
 
8833
you enter the terminator -- Windows runs a single "python" command in
 
8834
the window, which terminates when you terminate the interpreter.
 
8835
<P>
 
8836
If the "python" command, instead of displaying the interpreter prompt ">>>", gives you a message like
 
8837
<P>
 
8838
<PRE>
 
8839
    'python' is not recognized as an internal or external command,
 
8840
    operable program or batch file.
 
8841
</PRE>
 
8842
or
 
8843
<P>
 
8844
<PRE>
 
8845
    Bad command or filename
 
8846
</PRE>
 
8847
then you need to make sure that your computer knows where to find the
 
8848
Python interpreter. To do this you will have to modify a setting
 
8849
called the PATH, which is a just list of directories where Windows
 
8850
will look for programs. Rather than just enter the right command every
 
8851
time you create a command window, you should arrange for Python's
 
8852
installation directory to be added to the PATH of every command window
 
8853
as it starts. If you installed Python fairly recently then the command
 
8854
<P>
 
8855
<PRE>
 
8856
    dir C:\py*
 
8857
</PRE>
 
8858
will probably tell you where it is installed. Alternatively, perhaps
 
8859
you made a note. Otherwise you will be reduced to a search of your
 
8860
whole disk ... break out the Windows explorer and use "Tools | Find"
 
8861
or hit the "Search" button and look for "python.exe". Suppose you
 
8862
discover that Python is installed in the C:\Python22 directory (the
 
8863
default at the time of writing) then you should make sure that
 
8864
entering the command
 
8865
<P>
 
8866
<PRE>
 
8867
    c:\Python22\python
 
8868
</PRE>
 
8869
starts up the interpreter as above (and don't forget you'll need a
 
8870
"CTRL-Z" and an "Enter" to get out of it). Once you have verified the
 
8871
directory, you need to add it to the start-up routines your computer
 
8872
goes through.  For older versions of Windows the easiest way to do
 
8873
this is to edit the C:\AUTOEXEC.BAT file. You would want to add a line
 
8874
like the following to AUTOEXEC.BAT:
 
8875
<P>
 
8876
<PRE>
 
8877
    PATH C:\Python22;%PATH%
 
8878
</PRE>
 
8879
For Windows NT, 2000 and (I assume) XP, you will need to add a string
 
8880
such as
 
8881
<P>
 
8882
<PRE>
 
8883
    ;C:\Python22
 
8884
</PRE>
 
8885
to the current setting for the PATH environment variable, which you
 
8886
will find in the properties window of "My Computer" under the
 
8887
"Advanced" tab. Note that if you have sufficient privilege you might
 
8888
get a choice of installing the settings either for the Current User or
 
8889
for System. The latter is preferred if you want everybody to be able
 
8890
to run Python on the machine.
 
8891
<P>
 
8892
If you aren't confident doing any of these manipulations yourself, ask
 
8893
for help!  At this stage you may or may not want to reboot your system
 
8894
to make absolutely sure the new setting has "taken" (don't you love
 
8895
the way Windows gives you these freqeuent coffee breaks). You probably
 
8896
won't need to for Windows NT, XP or 2000. You can also avoid it in
 
8897
earlier versions by editing the file C:\WINDOWS\COMMAND\CMDINIT.BAT
 
8898
instead of AUTOEXEC.BAT.
 
8899
<P>
 
8900
You should now be able to start a new command window, enter
 
8901
<P>
 
8902
<PRE>
 
8903
    python
 
8904
</PRE>
 
8905
at the "C:>" (or whatever) prompt, and see the ">>>" prompt that
 
8906
indicates the Python interpreter is reading interactive commands.
 
8907
<P>
 
8908
Let's suppose you have a program called "pytest.py" in directory
 
8909
"C:\Steve\Projects\Python". A session to run that program might look
 
8910
like this:
 
8911
<P>
 
8912
<PRE>
 
8913
    C:\> cd \Steve\Projects\Python
 
8914
    C:\Steve\Projects\Python> python pytest.py
 
8915
</PRE>
 
8916
Because you added a file name to the command to start the interpreter,
 
8917
when it starts up it reads the Python script in the named file,
 
8918
compiles it, executes it, and terminates (so you see another "C:\>"
 
8919
prompt).  You might also have entered
 
8920
<P>
 
8921
<PRE>
 
8922
    C:\> python \Steve\Projects\Python\pytest.py
 
8923
</PRE>
 
8924
if you hadn't wanted to change your current directory.
 
8925
<P>
 
8926
Under NT, 2000 and XP you may well find that the installation process
 
8927
has also arranged that the command
 
8928
<P>
 
8929
<PRE>
 
8930
    pytest.py
 
8931
</PRE>
 
8932
(or, if the file isn't in the current directory)
 
8933
<P>
 
8934
<PRE>
 
8935
    C:\Steve\Projects\Python\pytest.py
 
8936
</PRE>
 
8937
will automatically recognize the ".py" extension and run the Python
 
8938
interpreter on the named file. Using this feature is fine, but <I>some</I>
 
8939
versions of Windows have bugs which mean that this form isn't exactly
 
8940
equivalent to using the interpreter explicitly, so be careful. Easier
 
8941
to remember, for now, that
 
8942
<P>
 
8943
<PRE>
 
8944
    python C:\Steve\Projects\Python\pytest.py
 
8945
</PRE>
 
8946
works pretty close to the same, and redirection will work (more)
 
8947
reliably.
 
8948
<P>
 
8949
The important things to remember are:
 
8950
<P>
 
8951
1. Start Python from the Start Menu, or make sure the PATH is set
 
8952
correctly so Windows can find the Python interpreter.
 
8953
<P>
 
8954
<PRE>
 
8955
    python
 
8956
</PRE>
 
8957
should give you a '>>>" prompt from the Python interpreter. Don't
 
8958
forget the CTRL-Z and ENTER to terminate the interpreter (and, if you
 
8959
started the window from the Start Menu, make the window disappear).
 
8960
<P>
 
8961
2. Once this works, you run programs with commands:
 
8962
<P>
 
8963
<PRE>
 
8964
    python {program-file}
 
8965
</PRE>
 
8966
3. When you know the commands to use you can build Windows shortcuts
 
8967
to run the Python interpreter on any of your scripts, naming
 
8968
particular working directories, and adding them to your menus, but
 
8969
that's another lessFAQ. Take a look at
 
8970
<P>
 
8971
<PRE>
 
8972
    python --help
 
8973
</PRE>
 
8974
if your needs are complex.
 
8975
<P>
 
8976
4. Interactive mode (where you see the ">>>" prompt) is best used
 
8977
<I>not</I> for running programs, which are better executed as in steps 2
 
8978
and 3, but for checking that individual statements and expressions do
 
8979
what you think they will, and for developing code by experiment.
 
8980
<P>
 
8981
 
 
8982
<A HREF="faqw.py?req=edit&amp;file=faq08.018.htp">Edit this entry</A> /
 
8983
<A HREF="faqw.py?req=log&amp;file=faq08.018.htp">Log info</A>
 
8984
 
 
8985
/ Last changed on Tue Aug 20 16:19:53 2002 by
 
8986
<A HREF="mailto:guido@python.org">GvR</A>
 
8987
<P>
 
8988
 
 
8989
<HR>
 
8990
<A HREF="http://www.python.org">Python home</A> /
 
8991
<A HREF="faqw.py?req=home">Python FAQ Wizard 1.0.3</A> /
 
8992
Feedback to <A HREF="mailto:guido@python.org">GvR</A>
 
8993
<P><A HREF="http://www.python.org/"><IMG
 
8994
SRC="http://www.python.org/pics/PythonPowered.gif" 
 
8995
ALIGN=top WIDTH=110 HEIGHT=44 ALT="Python Powered" BORDER=0></A><BR>
 
8996
</BODY>
 
8997
</HTML>