blob: 03d10d44cd1f5d4db68ee26f1f423265d5c2c71e [file] [log] [blame]
Guido van Rossuma7925f11994-01-26 10:20:16 +00001Subject: FAQ: Python -- an object-oriented language
2Newsgroups: comp.lang.misc,comp.answers,news.answers
3Followup-to: comp.lang.misc
4From: guido@cwi.nl (Guido van Rossum)
5Reply-to: guido@cwi.nl (Guido van Rossum)
6Approved: news-answers-request@MIT.Edu
7
8Archive-name: python-faq/part1
Guido van Rossum91f60831994-02-15 15:52:27 +00009Version: 1.5
10Last-modified: 15 Feb 1994
Guido van Rossuma7925f11994-01-26 10:20:16 +000011
12This article contains answers to Frequently Asked Questions about
13Python (an object-oriented interpreted programming language -- see
14the answer to question 1.1 for a short overview).
15
16Copyright 1993, 1994 Guido van Rossum. Unchanged electronic
17redistribution of this FAQ is allowed. Printed redistribution only
18with permission of the author. No warranties.
19
20Author's address:
21 Guido van Rossum
22 CWI, dept. CST
23 Kruislaan 413
24 P.O. Box 94079
25 1090 GB Amsterdam
26 The Netherlands
27Email: guido@cwi.nl
28
29The latest version of this FAQ is available by anonymous ftp from
30ftp.cwi.nl [192.16.184.180] in the directory /pub/python, with
31filename python-FAQ. It will also be posted regularly to the
32newsgroups comp.answers and comp.lang.misc.
33
34Many FAQs, including this one, are available by anonymous ftp from
35rtfm.mit.edu [18.70.0.209] in the directory pub/usenet/news.answers.
36The name under which a FAQ is archived appears in the Archive-name line
37at the top of the article. This FAQ is archived as python-faq/part1.
38
39There's a mail server on that machine which will send you files from
40the archive by e-mail if you have no ftp access. You send a e-mail
41message to mail-server@rtfm.mit.edu containing the single word help in
42the message body to receive instructions.
43
44This FAQ is divided in the following chapters:
45
46 1. General information and availability
47 2. Python in the real world
48 3. Building Python
49 4. Programming in Python
50 5. Extending Python
51 6. Python's design
52 7. Using Python on non-UNIX platforms
53
54To find the start of a particular chapter, search for the chapter number
55followed by a dot and a space at the beginning of a line (e.g. to
56find chapter 4 in vi, type /^4\. /).
57
58Here's an overview of the questions per chapter:
59
60 1. General information and availability
61 1.1. Q. What is Python?
62 1.2. Q. Why is it called Python?
63 1.3. Q. How do I obtain a copy of the Python source?
64 1.4. Q. How do I get documentation on Python?
65 1.5. Q. Is there a newsgroup or mailing list devoted to Python?
66 1.6. Q. Is there a book on Python, or will there be one out soon?
67 1.7. Q. Are there any published articles about Python that I can quote?
Guido van Rossum95f61a71994-01-26 17:23:37 +000068 1.8. Q. How does the Python version numbering scheme work?
Guido van Rossuma7925f11994-01-26 10:20:16 +000069
70 2. Python in the real world
71 2.1. Q. How many people are using Python?
72 2.2. Q. Have any significant projects been done in Python?
73 2.3. Q. Are there any commercial projects going on using Python?
Guido van Rossum95f61a71994-01-26 17:23:37 +000074 2.4. Q. How stable is Python?
75 2.5. Q. What new developments are expected for Python in the future?
Guido van Rossuma7925f11994-01-26 10:20:16 +000076
77 3. Building Python
Guido van Rossum91f60831994-02-15 15:52:27 +000078 3.1. Q. Is there a test set?
79 3.2. Q. When running the test set, I get complaints about floating point
Guido van Rossuma7925f11994-01-26 10:20:16 +000080 operations, but when playing with floating point operations I cannot
81 find anything wrong with them.
Guido van Rossum91f60831994-02-15 15:52:27 +000082 3.3. Q. Link errors building Python with STDWIN on SGI IRIX.
83 3.4. Q. Link errors after rerunning the configure script.
84 3.5. Q. The python interpreter complains about options passed to a
Guido van Rossum24349991994-02-02 14:12:45 +000085 script (after the script name).
Guido van Rossum91f60831994-02-15 15:52:27 +000086 3.6. Q. When building on the SGI, make tries to run python to create
Guido van Rossum24349991994-02-02 14:12:45 +000087 glmodule.c, but python hasn't been built or installed yet.
Guido van Rossum91f60831994-02-15 15:52:27 +000088 3.7. Q. Other trouble building Python 1.0.1 on platform X.
Guido van Rossuma7925f11994-01-26 10:20:16 +000089
90 4. Programming in Python
Guido van Rossum24349991994-02-02 14:12:45 +000091 4.1. Q. Is there a source code level debugger with breakpoints, step,
92 etc.?
93 4.2. Q. Can I create an object class with some methods implemented in
Guido van Rossuma7925f11994-01-26 10:20:16 +000094 C and others in Python (e.g. through inheritance)? (Also phrased as:
95 Can I use a built-in type as base class?)
Guido van Rossum24349991994-02-02 14:12:45 +000096 4.3. Q. Is there a curses/termcap package for Python?
97 4.4. Q. Is there an equivalent to C's onexit() in Python?
98 4.5. Q. When I define a function nested inside another function, the
Guido van Rossuma7925f11994-01-26 10:20:16 +000099 nested function seemingly can't access the local variables of the
100 outer function. What is going on? How do I pass local data to a
101 nested function?
Guido van Rossum24349991994-02-02 14:12:45 +0000102 4.6. Q. How do I iterate over a sequence in reverse order?
103 4.7. Q. My program is too slow. How do I speed it up?
104 4.8. Q. When I have imported a module, then edit it, and import it
Guido van Rossuma7925f11994-01-26 10:20:16 +0000105 again (into the same Python process), the changes don't seem to take
106 place. What is going on?
Guido van Rossum24349991994-02-02 14:12:45 +0000107 4.9. Q. I have a module in which I want to execute some extra code when it
108 is run as a script. How do I find out whether I am running as a
109 script?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000110
111 5. Extending Python
112 5.1. Q. Can I create my own functions in C?
113 5.2. Q. Can I create my own functions in C++?
114
115 6. Python's design
116 6.1. Q. Why isn't there a generic copying operation for objects in
117 Python?
118 6.2. Q. Why isn't there a generic way to implement persistent objects
119 in Python? (Persistent == automatically saved to and restored from
120 disk.)
121 6.3. Q. Why isn't there a switch or case statement in Python?
122
123 7. Using Python on non-UNIX platforms
Guido van Rossum91f60831994-02-15 15:52:27 +0000124 7.1. Q. Is there a Mac version of Python?
125 7.2. Q. Is there a DOS version of Python?
126 7.3. Q. Is there a Windows version of Python?
127 7.4. Q. Is there a Windows NT version of Python?
128 7.5. Q. I have the DOS or Windows version but it appears to be only a
129 binary. Where's the library?
130 7.6. Q. Where's the documentation for the Mac or PC version?
131 7.7. Q. The Mac (PC) version doesn't seem to have any facilities for
132 creating or editing programs apart from entering it interactively, and
133 there seems to be no way to save code that was entered interactively.
134 How do I create a Python program on the Mac (PC)?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000135
136To find a particular question, search for the question number followed
137by a dot, a space, and a Q at the beginning of a line (e.g. to find
138question 4.2 in vi, type /^4\.2\. Q/).
139
140
1411. General information and availability
142=======================================
143
1441.1. Q. What is Python?
145
146A. Python is an interpreted, interactive, object-oriented programming
147language. It incorporates modules, exceptions, dynamic typing, very
148high level dynamic data types, and classes. Python combines
149remarkable power with very clear syntax. It has interfaces to many
150system calls and libraries, as well as to various window systems, and
151is extensible in C or C++. It is also usable as an extension language
152for applications that need a programmable interface. Finally, Python
153is portable: it runs on many brands of UNIX, on the Mac, and on
154MS-DOS.
155
156To find out more, the best thing to do is to start reading the
157tutorial from the documentation set (see a few questions further
158down).
159
1601.2. Q. Why is it called Python?
161
162A. Apart from being a computer wizard, I'm also a fan of "Monty
163Python's Flying Circus" (a BBC comedy series from the seventies, in
164case you didn't know). It occurred to me one day that I needed a name
165that was short, unique, and slightly mysterious. And I happened to be
166reading some scripts from the series at the time... So then I decided
167to call my language Python. But Python is not a joke. And don't you
168associate it with dangerous reptiles either!
169
1701.3. Q. How do I obtain a copy of the Python source?
171
172A. The latest Python source distribution is always available by
173anonymous ftp from ftp.cwi.nl [192.16.184.180] in the directory
174/pub/python, with filename python<version>.tar.Z. It is a compressed
175tar file containing the complete C source, LaTeX documentation, Python
176library modules, example programs, and several useful pieces of freely
177distributable software. This will compile and run out of the box on
Guido van Rossum91f60831994-02-15 15:52:27 +0000178most UNIX platforms. Currently <version> is 1.0.1. (See section 7
Guido van Rossuma7925f11994-01-26 10:20:16 +0000179for non-UNIX information.)
180
1811.4. Q. How do I get documentation on Python?
182
183A. The latest Python documentation set is always available by
184anonymous ftp from ftp.cwi.nl [192.16.184.180] in the directory
185/pub/python, with filename pythondoc-ps<version>.tar.Z. It is a
186compressed tar file containing PostScript files of the reference
187manual, the library manual, and the tutorial. Currently <version> is
Guido van Rossum91f60831994-02-15 15:52:27 +00001881.0.1. (Note that the library manual is the most important one of the
Guido van Rossuma7925f11994-01-26 10:20:16 +0000189set, as much of Python's power stems from the standard or built-in
190types, functions and modules, all of which are described here.)
191PostScript for a high-level description of Python is in the file
192nluug-paper.ps.
193
194The following sites keep mirrors of the Python distribution:
195
196Site IP address Directory
197
198gatekeeper.dec.com 16.1.0.2 /pub/plan/python/cwi
199ftp.uu.net 192.48.96.9 /languages/python
200ftp.wustl.edu 128.252.135.4 /graphics/graphics/sgi-stuff/python
201ftp.funet.fi 128.214.6.100 /pub/languages/python (old?)
202ftp.fu-berlin.de 130.133.4.50 /pub/unix/languages/python (python* only)
203
Guido van Rossum91f60831994-02-15 15:52:27 +0000204Or try archie on e.g. python1.0.1.tar.Z to locate the nearest copy of
Guido van Rossuma7925f11994-01-26 10:20:16 +0000205that version...
206
2071.5. Q. Is there a newsgroup or mailing list devoted to Python?
208
209A. There is no Python newsgroup yet; if you want to post to the net
210about Python, use comp.lang.misc. There is a mailing list devoted to
Guido van Rossum91f60831994-02-15 15:52:27 +0000211Python; send e-mail to python-list-request@cwi.nl to (un)subscribe. A
212Request For Discussion about create comp.lang.python has been posted
213to news.groups. So far it has not elicited any negative responses.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000214
2151.6. Q. Is there a book on Python, or will there be one out soon?
216
217A. Unfortunately, not yet. I would like to write one but my
218obligations at CWI include too much other work to make much progress
219on it. Several parties have expressed interest in sponsoring or
220helping the production of a book or reference manual, but so far there
221are no firm plans. If you volunteer help, by all means drop me a
222note!
223
2241.7. Q. Are there any published articles about Python that I can quote?
225
226A. So far the only refereed and published article that describes
227Python in some detail is:
228
229 Guido van Rossum and Jelke de Boer, "Interactively Testing Remote
230 Servers Using the Python Programming Language", CWI Quarterly, Volume
231 4, Issue 4 (December 1991), Amsterdam, pp 283-303.
232
233LaTeX source for this paper is available as part of the Python source
234distribution.
235
236A more recent high-level description of Python is:
237
238 Guido van Rossum, "An Introduction to Python for UNIX/C
239 Programmers", in the proceedings of the NLUUG najaarsconferentie
240 1993 (dutch UNIX users group meeting november 1993).
241
242PostScript for this paper and for the slides used for the accompanying
243presentation can be found in the ftp directory mentioned a few
244questions earlier, with filenames nluug-paper.ps and nluug-slides.ps,
245respectively.
246
Guido van Rossum95f61a71994-01-26 17:23:37 +00002471.8. Q. How does the Python version numbering scheme work?
248
249A. Python versions are numbered A.B.C. A is the major version number
250-- it is only incremented for major changes in functionality or source
251structure. B is the minor version number, incremented for less
252earth-shattering changes to a release. C is the patchlevel -- it is
253incremented for each new release. Note that in the past, patches have
254added significant changes; in fact the changeover from 0.9.9 to 1.0.0
255was the first time that either A or B changed!
256
Guido van Rossuma7925f11994-01-26 10:20:16 +0000257
2582. Python in the real world
259===========================
260
2612.1. Q. How many people are using Python?
262
263A. I don't know, but at the last count there were at least 130
264addresses on the Python mailing list (several of which are local
265redistribution lists). I suspect that many users don't bother
266to subscribe to the list.
267
2682.2. Q. Have any significant projects been done in Python?
269
270A. Here at CWI (the home of Python), we have written a 20,000 line
271authoring environment for transportable hypermedia presentations, a
272multimedia teleconferencing tool, as well as many smaller programs.
273
274The University of Virginia uses Python to control a virtual reality
275engine. Contact: Matt Conway <conway@virginia.edu>.
276
277See also the next question.
278
2792.3. Q. Are there any commercial projects going on using Python?
280
281A. Several companies have revealed to me that they are planning or
282considering to use Python in a future product. The furthest is
283Sunrise Software, who already have a product out using Python -- they
284use Python for a GUI management application and an SNMP network
285manangement application. Contact: <info@sunrise.com>.
286
287Individuals at many other companies are using Python for
288internal development (witness their contributions to the Python
289mailing list).
290
291Python has also been elected as an extension language by MADE, a
292consortium supported by the European Committee's ESPRIT program and
293consisting of Bull, CWI and some other European companies. Contact:
294Ivan Herman <ivan@cwi.nl>.
295
Guido van Rossum95f61a71994-01-26 17:23:37 +00002962.4. Q. How stable is Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000297
Guido van Rossum91f60831994-02-15 15:52:27 +0000298A. Very stable. While the current version number (1.0.1) would
Guido van Rossuma7925f11994-01-26 10:20:16 +0000299suggest it is in the early stages of development, in fact new, stable
Guido van Rossum95f61a71994-01-26 17:23:37 +0000300releases (numbered 0.9.x) have been coming out roughly every 3 to 6
301months for the past four years.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000302
Guido van Rossum95f61a71994-01-26 17:23:37 +00003032.5. Q. What new developments are expected for Python in the future?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000304
Guido van Rossum95f61a71994-01-26 17:23:37 +0000305A. Without warranty that any of this will actually be realized: I am
Guido van Rossuma7925f11994-01-26 10:20:16 +0000306currently thinking about mechanisms for built-in on-line help and a
Guido van Rossum99bf2501994-01-26 17:30:41 +0000307switch/case statement. There have been some discussions on
308hierarchical module names which might solve the problem of the
309development of large packages. A pthreads interface has been
310contributed which I would like to merge into the latest release. The
311X interface needs improving. There are also some people
312(independently) working on a windowing interface based on STDWIN but
313with the power and ease of use of the average modern widget set. I
314still hope to get some help in producing a Windows version. It would
315be nice if there were a window-based class browser (Someone at CWI has
316contributed one using Motif but it needs some work). Also: improved
317support for embedding Python in other applications, e.g. by renaming
318most global symbols to have a "Py" prefix and providing more
319documentation and threading support.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000320
321
3223. Building Python
323==================
324
Guido van Rossum91f60831994-02-15 15:52:27 +00003253.1. Q. Is there a test set?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000326
327A. Yes, simply do "import testall" (or "import autotest" if you aren't
328interested in the output). The standard modules whose name begins
329with "test" together comprise the test. The test set doesn't test
330*all* features of Python but it goes a long way to confirm that a new
331port is actually working. The Makefile contains an entry "make test"
332which runs the autotest module.
333
Guido van Rossum91f60831994-02-15 15:52:27 +00003343.2. Q. When running the test set, I get complaints about floating point
Guido van Rossuma7925f11994-01-26 10:20:16 +0000335operations, but when playing with floating point operations I cannot
336find anything wrong with them.
337
338A. The test set makes occasional unwarranted assumptions about the
339semantics of C floating point operations. Until someone donates a
340better floating point test set, you will have to comment out the
341offending floating point tests and execute similar tests manually.
342
Guido van Rossum91f60831994-02-15 15:52:27 +00003433.3. Q. Link errors building Python with STDWIN on SGI IRIX.
Guido van Rossum24349991994-02-02 14:12:45 +0000344
345A. Rebuild STDWIN, specifying "CC=cc -cckr" in the Makefile.
346
Guido van Rossum91f60831994-02-15 15:52:27 +00003473.4. Q. Link errors after rerunning the configure script.
Guido van Rossum24349991994-02-02 14:12:45 +0000348
349A. It is generally necessary to run "make clean" after a configuration
350change.
351
Guido van Rossum91f60831994-02-15 15:52:27 +00003523.5. Q. The python interpreter complains about options passed to a
Guido van Rossum24349991994-02-02 14:12:45 +0000353script (after the script name).
354
355A. You are probably linking with GNU getopt, e.g. through -liberty.
356Don't. (If you are using this because you link with -lreadline, use
357the readline distributed with Python instead.)
358
Guido van Rossum91f60831994-02-15 15:52:27 +00003593.6. Q. When building on the SGI, make tries to run python to create
Guido van Rossum24349991994-02-02 14:12:45 +0000360glmodule.c, but python hasn't been built or installed yet.
361
362A. Comment out the line mentioning glmodule.c in Setup and build a
363python without gl first; install it or make sure it is in your $PATH,
364then edit the Setup file again to turn on the gl module, and make
365again. You don't need to do "make clean"; you do need to run "make
366Makefile" in the Modules subdirectory (or just run "make" at the
367toplevel).
368
Guido van Rossum91f60831994-02-15 15:52:27 +00003693.7. Q. Other trouble building Python 1.0.1 on platform X.
Guido van Rossum95f61a71994-01-26 17:23:37 +0000370
371A. Please email the details to <guido@cwi.nl> and I'll look into it.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000372
373
3744. Programming in Python
375========================
376
Guido van Rossum24349991994-02-02 14:12:45 +00003774.1. Q. Is there a source code level debugger with breakpoints, step,
378etc.?
379
380A. Yes. Check out module pdb; pdb.help() prints the documentation (or
381you can read it as Lib/pdb.doc). If you use the STDWIN option,
382there's also a windowing interface, wdb. You can write your own
383debugger by using the code for pdb or wdb as an example.
384
3854.2. Q. Can I create an object class with some methods implemented in
Guido van Rossuma7925f11994-01-26 10:20:16 +0000386C and others in Python (e.g. through inheritance)? (Also phrased as:
387Can I use a built-in type as base class?)
388
389A. No, but you can easily create a Python class which serves as a
390wrapper around a built-in object, e.g. (for dictionaries):
391
392 # A user-defined class behaving almost identical
393 # to a built-in dictionary.
394 class UserDict:
395 def __init__(self): self.data = {}
396 def __repr__(self): return repr(self.data)
397 def __cmp__(self, dict):
398 if type(dict) == type(self.data):
399 return cmp(self.data, dict)
400 else:
401 return cmp(self.data, dict.data)
402 def __len__(self): return len(self.data)
403 def __getitem__(self, key): return self.data[key]
404 def __setitem__(self, key, item): self.data[key] = item
405 def __delitem__(self, key): del self.data[key]
406 def keys(self): return self.data.keys()
407 def items(self): return self.data.items()
408 def values(self): return self.data.values()
409 def has_key(self, key): return self.data.has_key(key)
410
Guido van Rossum24349991994-02-02 14:12:45 +00004114.3. Q. Is there a curses/termcap package for Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000412
413A. No, but you can use the "alfa" (== character cell) version of
414STDWIN. (STDWIN == Standard Windows, a portable windowing system
Guido van Rossum91f60831994-02-15 15:52:27 +0000415interface by the same author, URL: ftp://ftp.cwi.nl/pub/stdwin.)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000416This will also prepare your program for porting to windowing
417environments such as X11 or the Macintosh.
418
Guido van Rossum24349991994-02-02 14:12:45 +00004194.4. Q. Is there an equivalent to C's onexit() in Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000420
421A. Yes, if you import sys and assign a function to sys.exitfunc, it
422will be called when your program exits, is killed by an unhandled
423exception, or (on UNIX) receives a SIGHUP or SIGTERM signal.
424
Guido van Rossum24349991994-02-02 14:12:45 +00004254.5. Q. When I define a function nested inside another function, the
Guido van Rossuma7925f11994-01-26 10:20:16 +0000426nested function seemingly can't access the local variables of the
427outer function. What is going on? How do I pass local data to a
428nested function?
429
430A. Python does not have arbitrarily nested scopes. When you need to
431create a function that needs to access some data which you have
432available locally, create a new class to hold the data and return a
433method of an instance of that class, e.g.:
434
435 class MultiplierClass:
436 def __init__(self, factor):
437 self.factor = factor
438 def multiplier(self, argument):
439 return argument * self.factor
440
441 def generate_multiplier(factor):
442 return MultiplierClass(factor).multiplier
443
444 twice = generate_multiplier(2)
445 print twice(10)
446 # Output: 20
447
Guido van Rossum24349991994-02-02 14:12:45 +00004484.6. Q. How do I iterate over a sequence in reverse order?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000449
450A. If it is a list, the fastest solution is
451
452 list.reverse()
453 try:
454 for x in list:
455 "do something with x"
456 finally:
457 list.reverse()
458
459This has the disadvantage that while you are in the loop, the list
460is temporarily reversed. If you don't like this, you can make a copy.
461This appears expensive but is actually faster than other solutions:
462
463 rev = list[:]
464 rev.reverse()
465 for x in rev:
466 <do something with x>
467
468If it isn't a list, a more general but slower solution is:
469
470 i = len(list)
471 while i > 0:
472 i = i-1
473 x = list[i]
474 <do something with x>
475
476A more elegant solution, is to define a class which acts as a sequence
477and yields the elements in reverse order (solution due to Steve
478Majewski):
479
480 class Rev:
481 def __init__(self, seq):
482 self.forw = seq
483 def __len__(self):
484 return len(self.forw)
485 def __getitem__(self, i):
486 return self.forw[-(i + 1)]
487
488You can now simply write:
489
490 for x in Rev(list):
491 <do something with x>
492
493Unfortunately, this solution is slowest of all, due the the method
494call overhead...
495
Guido van Rossum24349991994-02-02 14:12:45 +00004964.7. Q. My program is too slow. How do I speed it up?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000497
498A. That's a tough one, in general. There are many tricks to speed up
499Python code; I would consider rewriting parts in C only as a last
500resort. One thing to notice is that function and (especially) method
501calls are rather expensive; if you have designed a purely OO interface
502with lots of tiny functions that don't do much more than get or set an
503instance variable or call another method, you may consider using a
504more direct way, e.g. directly accessing instance variables. Also see
505the standard module "profile" (described in the file
506"python/lib/profile.doc") which makes it possible to find out where
507your program is spending most of its time (if you have some patience
508-- the profiling itself can slow your program down by an order of
509magnitude).
510
Guido van Rossum24349991994-02-02 14:12:45 +00005114.8. Q. When I have imported a module, then edit it, and import it
Guido van Rossuma7925f11994-01-26 10:20:16 +0000512again (into the same Python process), the changes don't seem to take
513place. What is going on?
514
515A. For efficiency reasons, Python only reads the module file on the
516first time a module is imported (otherwise a program consisting of
517many modules, each of which imports the same basic module, would read
518the basic module over and over again). To force a changed module
519being read again, do this:
520
521 import modname
522 reload(modname)
523
524Warning: this technique is not 100% fool-proof. In particular,
525modules containing statements like
526
527 from modname import some_objects
528
529will continue to work with the old version of the objects imported
530thus.
531
Guido van Rossum24349991994-02-02 14:12:45 +00005324.9. Q. I have a module in which I want to execute some extra code when it
533is run as a script. How do I find out whether I am running as a
534script?
535
536A. A module can find out its own module name by alooking at the
537(predefined) global variable __name__. If this has the value
538'__main__' you are running as a script. E.g. if you put the following
539on the last line of your module, main() is called only when your
540module is running as a script:
541
542 if __name__ == '__main__': main()
543
Guido van Rossuma7925f11994-01-26 10:20:16 +0000544
5455. Extending Python
546===================
547
5485.1. Q. Can I create my own functions in C?
549
550A. Yes, you can create built-in modules containing functions,
Guido van Rossum24349991994-02-02 14:12:45 +0000551variables, exceptions and even new types in C. This is explained in
552the document "Extending and Embedding the Python Interpreter" (the
553LaTeX file Doc/ext.tex). Also read the chapter on dynamic loading.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000554
5555.2. Q. Can I create my own functions in C++?
556
557A. Yes, using the C-compatibility features found in C++. Basically
558you place extern "C" { ... } around the Python include files and put
559extern "C" before each function that is going to be called by the
560Python interpreter. Global or static C++ objects with constructors
561are probably not a good idea.
562
563
5646. Python's design
565==================
566
5676.1. Q. Why isn't there a generic copying operation for objects in
568Python?
569
570A. Hmm. Maybe there should be one, but it's difficult to assign a
571useful meaning to copying of open files, sockets and windows, or
572recursive data structures. As long as you design all your classes
573yourself you are of course free to define a standard base class that
574defines an overridable copying operation for all the objects you care
575about. (One practical point: it would have to be a built-in function,
576not a standard method name, since not all built-in object types have
577methods; e.g. strings, integers and tuples don't.)
578
5796.2. Q. Why isn't there a generic way to implement persistent objects
580in Python? (Persistent == automatically saved to and restored from
581disk.)
582
583A. Hmm, hmm. Basically for the same reasons as why there is no
584generic copying operation.
585
5866.3. Q. Why isn't there a switch or case statement in Python?
587
588A. You can do this easily enough with a sequence of
589if... elif... elif... else. There have been some proposals for switch
590statement syntax, but there is no concensus (yet) on whether and how
591to do range tests.
592
593
5947. Using Python on non-UNIX platforms
595=====================================
596
Guido van Rossum91f60831994-02-15 15:52:27 +00005977.1. Q. Is there a Mac version of Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000598
Guido van Rossum91f60831994-02-15 15:52:27 +0000599A. Yes. It is on most ftp sites carrying Python as python.sea.hqx --
600this is a self-extracting archive containing the application binary as
601well as the Lib modules.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000602
Guido van Rossum91f60831994-02-15 15:52:27 +00006037.2. Q. Is there a DOS version of Python?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000604
Guido van Rossum91f60831994-02-15 15:52:27 +0000605A. Yes. More than one, actually: 16python.exe runs in standard DOS
606mode on 186 CPUs or higher; 32python.exe uses a DOS extender and only
607runs on a 386 or higher CPUs. Although 16python.exe does not pass the
608test set because test_grammar is too big for the parser, it actually
609has about 270 kbyte of allocatable heap space, which is sufficient for
610fairly large programs. 32python.exe is distributed as a tar file
611containing the required DOS extended and 387 emulator. Both are on
612most ftp sites carrying Python.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000613
Guido van Rossum91f60831994-02-15 15:52:27 +00006147.3. Q. Is there a Windows version of Python?
615
616A. Yes. Use qwpython.exe. The only problem with it: ^C
617unconditionally kills the entire program -- it does not raise
618KeyboardInterrupt. You can also run 16python.exe or 32python.exe in a
619"DOS box", but qwpython.exe appears to be slightly faster.
620
6217.4. Q. Is there a Windows NT version of Python?
622
623A. Yes. Use ntpython.exe. This is for Intel CPUs. If you want a
624Windows user interface, use qwpython.exe.
625
6267.5. Q. I have the DOS or Windows version but it appears to be only a
627binary. Where's the library?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000628
629A. You still need to copy the files from the distribution directory
Guido van Rossum91f60831994-02-15 15:52:27 +0000630"python/Lib" to your system. If you don't have the full distribution,
631you can get the file pythonlib1.0.1.tar.Z from most ftp sites carrying
632Python; this is a subset of the distribution containing just those
633file.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000634
Guido van Rossum91f60831994-02-15 15:52:27 +00006357.6. Q. Where's the documentation for the Mac or PC version?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000636
637A. There isn't any. The documentation for the Unix version also
Guido van Rossum91f60831994-02-15 15:52:27 +0000638applies to the Mac and PC versions. Where applicable, differences
Guido van Rossuma7925f11994-01-26 10:20:16 +0000639are indicated in the text.
640
Guido van Rossum91f60831994-02-15 15:52:27 +00006417.7. Q. The Mac (PC) version doesn't seem to have any facilities for
642creating or editing programs apart from entering it interactively, and
643there seems to be no way to save code that was entered interactively.
644How do I create a Python program on the Mac (PC)?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000645
Guido van Rossum91f60831994-02-15 15:52:27 +0000646A. Use an external editor. On the Mac, I am quite happy with the Desk
647Accessory called Sigma Edit; this doesn't require Multifinder or
648System 7. I work like this: start the interpreter; edit a module file
649using Sigma Edit; import and test it in the interpreter; edit again in
650Sigma Edit; then use the built-in function reload() to re-read the
651imported module; etc.