-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
Expand file tree
/
Copy pathFAQ
More file actions
1887 lines (1487 loc) · 81.3 KB
/
FAQ
File metadata and controls
1887 lines (1487 loc) · 81.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Subject: FAQ: Python -- an object-oriented language
Newsgroups: comp.lang.python,comp.answers,news.answers
Followup-to: comp.lang.python
From: [email protected] (Guido van Rossum)
Reply-to: [email protected] (Guido van Rossum)
Expires: Sat, 1 Apr 1995 00:00:00 GMT
Supersedes: <[email protected]>
Approved: [email protected]
Archive-name: python-faq/part1
Submitted-by: Guido van Rossum <[email protected]>
Version: 1.20++
Last-modified: 16 Februari 1995
This article contains answers to Frequently Asked Questions about
Python (an object-oriented interpreted programming language -- see
the answer to question 1.1 for a short overview).
Copyright 1993-1995 Guido van Rossum. Unchanged electronic
redistribution of this FAQ is allowed. Printed redistribution only
with permission of the author. No warranties.
Author's address:
Guido van Rossum
CWI, dept. CST
Kruislaan 413
P.O. Box 94079
1090 GB Amsterdam
The Netherlands
Email: <[email protected]>
The latest version of this FAQ is available by anonymous ftp from
<URL:ftp://ftp.cwi.nl/pub/python/python-FAQ>. It will also be posted
regularly to the newsgroups comp.answers <URL:news:comp.answers> and
comp.lang.python <URL:news:comp.lang.python>.
Many FAQs, including this one, are available by anonymous ftp
<URL:ftp://rtfm.mit.edu/pub/usenet/news.answers/>. The name under
which a FAQ is archived appears in the Archive-name line at the top of
the article. This FAQ is archived as python-faq/part1
<URL:ftp://rtfm.mit.edu/pub/usenet/news.answers/python-faq/part1>.
There's a mail server on that machine which will send you files from
the archive by e-mail if you have no ftp access. You send a e-mail
message to <[email protected]> containing the single word help
in the message body to receive instructions.
This FAQ is divided in the following chapters:
1. General information and availability
2. Python in the real world
3. Building Python
4. Programming in Python
5. Extending Python
6. Python's design
7. Using Python on non-UNIX platforms
To find the start of a particular chapter, search for the chapter number
followed by a dot and a space at the beginning of a line (e.g. to
find chapter 4 in vi, type /^4\. /).
Here's an overview of the questions per chapter:
1. General information and availability
1.1. Q. What is Python?
1.2. Q. Why is it called Python?
1.3. Q. How do I obtain a copy of the Python source?
1.4. Q. How do I get documentation on Python?
1.5. Q. Are there other ftp sites that mirror the Python distribution?
1.6. Q. Is there a newsgroup or mailing list devoted to Python?
1.7. Q. Is there a WWW page devoted to Python?
1.8. Q. Is the Python documentation available on the WWW?
1.9. Q. Is there a book on Python, or will there be one out soon?
1.10. Q. Are there any published articles about Python that I can quote?
1.11. Q. Are there short introductory papers or talks on Python?
1.12. Q. How does the Python version numbering scheme work?
1.13. Q. How do I get a beta test version of Python?
1.14. Q. Are there other ftp sites that carry Python related material?
1.15. Q. Are there copyright restrictions on the use of Python?
2. Python in the real world
2.1. Q. How many people are using Python?
2.2. Q. Have any significant projects been done in Python?
2.3. Q. Are there any commercial projects going on using Python?
2.4. Q. How stable is Python?
2.5. Q. When will the next version be released?
2.6. Q. What new developments are expected for Python in the future?
2.7. Q. Is it reasonable to propose incompatible changes to Python?
3. Building Python
3.1. Q. Is there a test set?
3.2. Q. 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.
3.3. Q. Link errors building Python with STDWIN 0.9.8. on SGI IRIX.
3.4. Q. Link errors building Python with STDWIN 0.9.9.
3.5. Q. Link errors after rerunning the configure script.
3.6. Q. The python interpreter complains about options passed to a
script (after the script name).
3.7. Q. When building on the SGI, make tries to run python to create
glmodule.c, but python hasn't been built or installed yet.
3.8. Q. Python built with gcc for the DEC Alpha doesn't work.
3.9. Q. I use VPATH but some targets are built in the source directory.
3.10. Q. Trouble building or linking with the GNU readline library.
3.11. Q. Trouble building Python on Linux.
3.12. Q. Trouble with prototypes on Ultrix.
3.13. Q. Trouble with posix.listdir on NeXTSTEP 3.2.
3.14. Q. Other trouble building Python on platform X.
3.15. Q. How to configure dynamic loading on Lixux.
4. Programming in Python
4.1. Q. Is there a source code level debugger with breakpoints, step,
etc.?
4.2. Q. 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?)
4.3. Q. Is there a curses/termcap package for Python?
4.4. Q. Is there an equivalent to C's onexit() in Python?
4.5. Q. When I define a function nested inside another function, the
nested function seemingly can't access the local variables of the
outer function. What is going on? How do I pass local data to a
nested function?
4.6. Q. How do I iterate over a sequence in reverse order?
4.7. Q. My program is too slow. How do I speed it up?
4.8. Q. 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?
4.9. Q. How do I find the current module name?
4.10. Q. 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?
4.11. Q. I try to run a program from the Demo directory but it fails
with ImportError: No module named ...; what gives?
4.12. Q. I have successfully built Python with STDWIN but it can't
find some modules (e.g. stdwinevents).
4.13. Q. What GUI toolkits exist for Python?
4.14. Q. Are there any interfaces to database packages in Python?
4.15. Q. Is it possible to write obfuscated one-liners in Python?
4.16. Q. Is there an equivalent of C's "?:" ternary operator?
4.17. Q. My class defines __del__ but it is not called when I delete the
object.
4.18. Q. How do I change the shell environment for programs called
using os.popen() or os.system()? Changing os.environ doesn't work.
4.19. Q. What is a class?
4.20. Q. What is a method?
4.21. Q. What is self?
4.22. Q. What is a unbound method?
4.23. Q. How do I call a method defined in a base class from a derived
class that overrides it?
4.24. Q. How do I call a method from a base class without using the
name of the base class?
4.25. Q. How can I organize my code to make it easier to change the base
class?
4.26. Q. How can I find the methods or attributes of an object?
4.27. Q. I can't seem to use os.read() on a pipe created with os.popen().
4.28. Q. How can I create a stand-alone binary from a Python script?
4.29. Q. Is there a special lib for writing CGI scripts in Python?
4.30. Q. What other WWW tools are there for Python?
4.31. Q. How do I run a subprocess with pipes connected to both input
and output?
5. Extending Python
5.1. Q. Can I create my own functions in C?
5.2. Q. Can I create my own functions in C++?
5.3. Q. How can I execute arbitrary Python statements from C?
5.4. Q. How can I evaluate an arbitrary Python expression from C?
5.5. Q. How do I extract C values from a Python object?
5.6. Q. How do I use mkvalue() to create a tuple of arbitrary length?
5.7. Q. What happened to mktuple(), featured in an example in the
Extensions manual?
5.8. Q. How do I call an object's method from C?
5.9. Q. How do I catch the output from print_error()?
5.10. Q. How do I access a module written in Python from C?
6. Python's design
6.1. Q. Why isn't there a generic copying operation for objects in
Python?
6.2. Q. Why isn't there a generic way to implement persistent objects
in Python? (Persistent == automatically saved to and restored from
disk.)
6.3. Q. Why isn't there a switch or case statement in Python?
6.4. Q. Why does Python use indentation for grouping of statements?
6.5. Q. Why are Python strings immutable?
6.6. Q. Why don't strings have methods like index() or sort(), like
lists?
6.7. Q. Why does Python use methods for some functionality
(e.g. list.index()) but functions for other (e.g. len(list))?
6.8. Q. Why can't I derive a class from built-in types (e.g. lists or
files)?
6.9. Q. Why must 'self' be declared and used explicitly in method
definitions and calls?
6.10. Q. Can't you emulate threads in the interpreter instead of
relying on an OS-specific thread implementation?
6.11. Q. Why can't lambda forms contain statements?
6.12. Q. Why is there no more efficient way of iterating over a dictionary
than first constructing the list of keys()?
7. Using Python on non-UNIX platforms
7.1. Q. Is there a Mac version of Python?
7.2. Q. Is there a DOS version of Python?
7.3. Q. Is there a Windows version of Python?
7.4. Q. Is there a Windows NT version of Python?
7.5. Q. Is there an OS/2 version of Python?
7.6. Q. Is there a VMS version of Python?
7.7. Q. What about IBM mainframes, or other esoteric non-UNIX
platforms?
7.8. Q. Where are the source or Makefiles for the non-UNIX versions?
7.9. Q. What is the status and support for the non-UNIX versions?
7.10. Q. I have the PC version but it appears to be only a binary.
Where's the library?
7.11. Q. Where's the documentation for the Mac or PC version?
7.12. Q. The Mac (PC) version doesn't seem to have any facilities for
creating or editing programs apart from entering it interactively, and
there seems to be no way to save code that was entered interactively.
How do I create a Python program on the Mac (PC)?
To find a particular question, search for the question number followed
by a dot, a space, and a Q at the beginning of a line (e.g. to find
question 4.2 in vi, type /^4\.2\. Q/).
1. General information and availability
=======================================
1.1. Q. What is Python?
A. Python is an interpreted, interactive, object-oriented programming
language. It incorporates modules, exceptions, dynamic typing, very
high level dynamic data types, and classes. Python combines
remarkable power with very clear syntax. It has interfaces to many
system calls and libraries, as well as to various window systems, and
is extensible in C or C++. It is also usable as an extension language
for applications that need a programmable interface. Finally, Python
is portable: it runs on many brands of UNIX, on the Mac, and on PCs
under MS-DOS, Windows, Windows NT, and OS/2.
To find out more, the best thing to do is to start reading the
tutorial from the documentation set (see a few questions further
down).
1.2. Q. Why is it called Python?
A. Apart from being a computer scientist, I'm also a fan of "Monty
Python's Flying Circus" (a BBC comedy series from the seventies, in
the -- unlikely -- case you didn't know). It occurred to me one day
that I needed a name that was short, unique, and slightly mysterious.
And I happened to be reading some scripts from the series at the
time... So then I decided to call my language Python. But Python is
not a joke. And don't you associate it with dangerous reptiles
either! (If you need an icon, use an image of the 16-ton weight from
the TV series or of a can of SPAM :-)
1.3. Q. How do I obtain a copy of the Python source?
A. The latest complete Python source distribution is always available
by anonymous ftp, e.g.
<URL:ftp://ftp.cwi.nl/pub/python/python1.1.tar.gz>. It is a gzipped
tar file containing the complete C source, LaTeX documentation, Python
library modules, example programs, and several useful pieces of freely
distributable software. This will compile and run out of the box on
most UNIX platforms. (See section 7 for non-UNIX information.)
Occasionally a set of patches is issued which has to be applied using
the patch program. These patches are placed in the same directory,
e.g. <URL:ftp://ftp.cwi.nl/pub/python/patch1.1.1>.
An index of said ftp directory can be found in the file INDEX. An
HTML version of the index can be found in the file index.html,
<URL:ftp://ftp.cwi.nl/pub/python/index.html>.
1.4. Q. How do I get documentation on Python?
A. The LaTeX source for the documentation is part of the source
distribution. If you don't have LaTeX, the latest Python
documentation set is always available by anonymous ftp, e.g.
<URL:ftp://ftp.cwi.nl/pub/python/pythondoc-ps1.1.tar.gz>. It is a
gzipped tar file containing PostScript files of the reference manual,
the library manual, and the tutorial. Note that the library manual is
the most important one of the set, as much of Python's power stems
from the standard or built-in types, functions and modules, all of
which are described here. PostScript for a high-level description of
Python is in the file nluug-paper.ps (a separate file on the ftp
site).
1.5. Q. Are there other ftp sites that mirror the Python distribution?
A. The following anonymous ftp sites keep mirrors of the Python
distribution:
<URL:ftp://gatekeeper.dec.com/pub/plan/python/>
<URL:ftp://ftp.uu.net/languages/python/>
<URL:ftp://ftp.wustl.edu/graphics/graphics/sgi-stuff/python/>
<URL:ftp://ftp.funet.fi/pub/languages/python/>
<URL:ftp://ftp.sunet.se/pub/lang/python/>
<URL:ftp://unix.hensa.ac.uk/uunet/languages/python/>
<URL:ftp://ftp.sterlng.com/programming/languages/python/>
<URL:ftp://ftp.ibp.fr/pub/python/>
Or try archie on the string "python".
1.6. Q. Is there a newsgroup or mailing list devoted to Python?
A. There is a newsgroup, comp.lang.python <URL:news:comp.lang.python>,
and a mailing list. The newsgroup and mailing list are gatewayed into
each other -- if you can read news it's unnecessary to subscribe to
the mailing list. Send e-mail to <[email protected]> to
(un)subscribe to the mailing list. Hypermail archives of
(nearly) everything posted to the mailing list (and thus the
newsgroup) are available on our WWW server,
<URL:http://www.cwi.nl/~guido/hypermail/index.html>. The raw archives
are also available by ftp, e.g.
<URL:ftp://ftp.cwi.nl/pub/python/mailinglist.gz>. The uncompressed
versions of these files can be read with the standard UNIX Mail
program ("Mail -f file") or with nn ("nn file"). To read them using
MH, you could use "inc -file file".
1.7. Q. Is there a WWW page devoted to Python?
A. Yes, <URL:http://www.cwi.nl/~guido/Python.html> is the official
Python home page. Michael McLay at NIST maintains a Python page at
<URL:http://www.eeel.nist.gov/python/>. And finally the emerging
Python Software Association has a preliminary web server at
<URL:http://www.cminds.com/python/> (eventually to be moved to
somewhere under python.org).
1.8. Q. Is the Python documentation available on the WWW?
A. Yes, see <URL:http://www.cwi.nl/~guido/Python.html> (Python's home
page). It contains pointers to hypertext versions of the whole
documentation set (as hypertext, not just PostScript).
If you wish to browse this collection of HTML files on your own
machine, it is available bundled up by anonymous ftp,
e.g. <URL:ftp://ftp.cwi.nl/pub/python/python-html.tar.gz>. (This
requires some manual patch-up for the references to images,
unfortunately.)
An Emacs-INFO set containing the library manual is also available by
ftp, e.g. <URL:ftp://ftp.cwi.nl/pub/python/python-lin-info.tar.gz>.
1.9. Q. Is there a book on Python, or will there be one out soon?
A. I am writing one. Publishers are interested. Optimistically, it
will be ready for the press by mid-1995. Mark Lutz is working on one
too -- more details as they emerge.
1.10. Q. Are there any published articles about Python that I can quote?
A. So far the only refereed and published article that describes
Python in some detail is:
Guido van Rossum and Jelke de Boer, "Interactively Testing Remote
Servers Using the Python Programming Language", CWI Quarterly, Volume
4, Issue 4 (December 1991), Amsterdam, pp 283-303.
LaTeX source for this paper is available as part of the Python source
distribution.
1.11. Q. Are there short introductory papers or talks on Python?
A. A recent high-level description of Python is:
Guido van Rossum, "An Introduction to Python for UNIX/C
Programmers", in the proceedings of the NLUUG najaarsconferentie
1993 (dutch UNIX users group meeting November 1993).
PostScript for this paper and for the slides used for the accompanying
presentation is available by ftp as
<URL:ftp://ftp.cwi.nl/pub/python/nluug-paper.ps> and
<URL:ftp://ftp.cwi.nl/pub/python/nluug-slides.ps>, respectively.
Slides for a talk on Python that I gave at the Usenix Symposium on
Very High Level Languages in Santa Fe, NM, USA in October 1995 are
available as <URL:ftp://ftp.cwi.nl/pub/python/vhll-slides.ps>.
1.12. Q. How does the Python version numbering scheme work?
A. Python versions are numbered A.B.C. A is the major version number
-- it is only incremented for major changes in functionality or source
structure. B is the minor version number, incremented for less
earth-shattering changes to a release. C is the patchlevel -- it is
incremented for each new release. Note that in the past, patches have
added significant changes; in fact the changeover from 0.9.9 to 1.0.0
was the first time that either A or B changed!
Beta versions have an additional suffix of "-beta-N" for some small
number N. Note that (for instance) all versions labeled 1.2-beta-N
*precede* the actual release of 1.2.
1.13. Q. How do I get a beta test version of Python?
A. Write me. I might like you :-) Seriously, anybody who has
previously used and installed Python can become a beta tester, but I
expect feedback, so you have to write me first before I divulge the
secret location of the latest beta release.
1.14. Q. Are there other ftp sites that carry Python related material?
A. An interesting ftp site for Python users is ftp.markv.com; the
directory pub/python contains a growing collection of interesting
Python scripts <URL:ftp://ftp.markv.com/pub/python/>. To submit a
script for inclusion, place it together with a readme file (with
extension .readme) in the publicly writable directory
/incoming/python. This service is maintained by Lance Ellinghaus
<[email protected]>. (I've heard complaints about this service not
being very responsive -- try at your own risk.)
1.15. Q. Are there copyright restrictions on the use of Python?
A. Hardly. You can do anything you want with the source, as long as
you leave the copyrights in, and display those copyrights in any
documentation about Python that you produce. Also, don't use the
author's institute's name in publicity without prior written
permission, and don't hold them responsible for anything (read the
actual copyright for a precise legal wording).
In particular, if you honor the copyright rules, it's OK to use Python
for commercial use, to sell copies of Python in source or binary form,
or to sell products that enhance Python or incorporate Python (or part
of it) in some form. I would still like to know about all commercial
use of Python!
2. Python in the real world
===========================
2.1. Q. How many people are using Python?
A. I don't know, but the maximum number of simultaneous subscriptions
to the Python mailing list before it was gatewayed into the newsgroup
was about 180 (several of which were local redistribution lists). I
believe that many active Python users don't bother to subscribe to the
list, and now that there's a newsgroup the mailing list subscription
is even less meaningful. I see new names on the newsgroup all the
time and my best guess is that there are currently at least several
thousands of users.
2.2. Q. Have any significant projects been done in Python?
A. Here at CWI (the home of Python), we have written a 20,000 line
authoring environment for transportable hypermedia presentations, a
5,000 line multimedia teleconferencing tool, as well as many many
smaller programs.
The University of Virginia uses Python to control a virtual reality
engine. Contact: Matt Conway <[email protected]>.
The ILU project at Xerox PARC can generate Python glue for ILU
interfaces. See <URL:ftp://ftp.parc.xerox.com/pub/ilu/ilu.html>.
If you have done a significant project in Python that you'd like to be
included in the list above, send me email!
2.3. Q. Are there any commercial projects going on using Python?
A. Several companies have revealed to me that they are planning or
considering use of Python in a future product.
The furthest is Sunrise Software, who already have a product out using
Python -- they use Python for a GUI management application and an SNMP
network management application. Contact: <[email protected]>.
Infoseek uses Python to implement their commercial WWW information
retrieval service <URL:http://www.infoseek.com/>. Contact:
Michael Powers of daVinci Time & Space is "writing tons-o-python for
interactive television entertainment." Contact: <[email protected]>.
Paul Everitt of Connecting Minds is planning a Lotus Notes gateway.
Contact: <[email protected]>. Or see their WWW server
<URL:http://www.cminds.com/>.
Individuals at many other companies are using Python for
internal development (witness their contributions to the Python
mailing list or newsgroup).
Python has also been elected as an extension language by MADE, a
consortium supported by the European Committee's ESPRIT program and
consisting of Bull, CWI and some other European companies. Contact:
Ivan Herman <[email protected]>.
If you'd like to be included in the list above, send me email!
2.4. Q. How stable is Python?
A. Very stable. While the current version number would suggest it is
in the early stages of development, in fact new, stable releases
(numbered 0.9.x through 1.1.x) have been coming out roughly every 3 to
6 months for the past four years.
2.5. Q. When will the next version be released?
A. I am planning to release 1.2 in February or early March 1995. It
will contain hooks into the implementation of the import command, a
(still limited) form of persistent objects, and the usual complement
of bug fixes (including many fixed memory leaks and thread problems).
2.6. Q. What new developments are expected for Python in the future?
A. There will be better ports to the Mac, DOS, Windows, Windows NT, and
OS/2. The Windows ports will support dynamically loaded modules using
DLLs.
Also planned is improved support for embedding Python in other
applications, e.g. by renaming most global symbols to have a "Py"
prefix and providing more documentation and threading support.
Some proposals were discussed at the recent Python workshop:
- persistent objects
- safe execution of untrusted Python code
- extensions to the import statement for managing packages
(groups of related modules)
- automatic generation of C/C++ interface glue
- interfaces to OMG IDL (== Interface Definition Language by
the Object Management Group)
- a portable GUI API (Graphical User Interface Application
Programmers Interface)
- module customization tools
- standardized documentation strings on module, class and
function objects
- the formation of a Python Steering Committee
- another Python Workshop
For more info, have a look at the WWW page for the last Python
Workshop <URL:http://www.eeel.nist.gov/python/workshop11-94/>.
2.7. Q. Is it reasonable to propose incompatible changes to Python?
A. In general, no. There are already millions of lines of Python code
around the world, so any changes in the language that invalidates more
than a very small fraction of existing programs has to be frowned
upon. Even if you can provide a conversion program, there still is
the problem of updating all documentation. Providing a gradual
upgrade path is the only way if a feature has to be changed.
3. Building Python
==================
3.1. Q. Is there a test set?
A. Yes, simply do "import testall" (or "import autotest" if you aren't
interested in the output). The standard modules whose name begins
with "test" together comprise the test. The test set doesn't test
*all* features of Python but it goes a long way to confirm that a new
port is actually working. The Makefile contains an entry "make test"
which runs the autotest module. NOTE: if "make test" fails, run the
tests manually ("import testall") to see what goes wrong before
reporting the error.
3.2. Q. 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. The test set makes occasional unwarranted assumptions about the
semantics of C floating point operations. Until someone donates a
better floating point test set, you will have to comment out the
offending floating point tests and execute similar tests manually.
3.3. Q. Link errors building Python with STDWIN 0.9.8. on SGI IRIX.
A. Get STDWIN 0.9.9 <URL:ftp://ftp.cwi.nl/pub/stdwin/stdwin0.9.9.tar.gz>.
3.4. Q. Link errors building Python with STDWIN 0.9.9.
A. The linker probably complains that it can't find routines like
'tecreate', 'tenew' etc. The STDWIN 0.9.9 distribution requires that
you add TWO libraries from stdwin to the line for stdwin in the
Setupfile. Use something like this (all on one line!):
stdwin stdwinmodule.c -I$(STDWIN)/H $(STDWIN)/Packs/textedit/libtextedit.a $(STDWIN)/Ports/x11/libstdwin.a -lX11
3.5. Q. Link errors after rerunning the configure script.
A. It is generally necessary to run "make clean" after a configuration
change.
3.6. Q. The python interpreter complains about options passed to a
script (after the script name).
A. You are probably linking with GNU getopt, e.g. through -liberty.
Don't. The reason for the complaint is that GNU getopt, unlike System
V getopt and other getopt implementations, doesn't consider a
non-option to be the end of the option list. A quick (and compatible)
fix for scripts is to add "--" to the interpreter, like this:
#! /usr/local/bin/python --
You can also use this interactively:
python -- script.py [options]
Note that a working getopt implementation is provided in the Python
distribution (in Python/getopt.c) but not automatically used.
3.7. Q. When building on the SGI, make tries to run python to create
glmodule.c, but python hasn't been built or installed yet.
A. Comment out the line mentioning glmodule.c in Setup and build a
python without gl first; install it or make sure it is in your $PATH,
then edit the Setup file again to turn on the gl module, and make
again. You don't need to do "make clean"; you do need to run "make
Makefile" in the Modules subdirectory (or just run "make" at the
toplevel).
3.8. Q. Python built with gcc for the DEC Alpha doesn't work.
People have reported problems with gcc 2.5.8 up to 2.6.3. The DEC
OSF/1 cc compiler does not have these problems so it's likely a gcc
bug. The latest news is that (at least in the 1.2 beta release) the
problem still exists but only when you use -O, and only affects the
code generated from Python/marshal.c. A fix is to change "long i, n;"
into "int i, n;" in w_object().
3.9. Q. I use VPATH but some targets are built in the source directory.
A. On some systems (e.g. Sun), if the target already exists in the
source directory, it is created there instead of in the build
directory. This is usually because you have previously built without
VPATH. Try running "make clobber" in the source directory.
3.10. Q. Trouble building or linking with the GNU readline library.
A. Consider using readline 2.0. From the Python 1.1 README:
- You can use the GNU readline library to improve the interactive user
interface: this gives you line editing and command history when
calling python interactively. You need to configure and build the GNU
readline library before running the configure script. Its sources are
no longer distributed with Python; you can ftp them from any GNU
mirror site, or from its home site
<URL:ftp://slc2.ins.cwru.edu/pub/dist/readline-2.0.tar.gz> (or a
higher version number -- using version 1.x is not recommended). Pass
the Python configure script the option --with-readline=DIRECTORY where
DIRECTORY is the absolute pathname of the directory where you've built
the readline library. Some hints on building and using the readline
library:
- On SGI IRIX 5, you may have to add the following
to rldefs.h:
#ifndef sigmask
#define sigmask(sig) (1L << ((sig)-1))
#endif
- On most systems, you will have to add #include "rldefs.h" to the
top of several source files, and if you use the VPATH feature, you
will have to add dependencies of the form foo.o: foo.c to the
Makefile for several values of foo.
- The readline library requires use of the termcap library. A
known problem with this is that it contains entry points which
cause conflicts with the STDWIN and SGI GL libraries. The STDWIN
conflict can be solved (and will be, in the next release of
STDWIN) by adding a line saying '#define werase w_erase' to the
stdwin.h file (in the STDWIN distribution, subdirectory H). The
GL conflict has been solved in the Python configure script by a
hack that forces use of the static version of the termcap library.
- Check the newsgroup gnu.bash.bugs <URL:news:gnu.bash.bugs> for
specific problems with the readline library (I don't get this group
here but I've been told that it is the place for readline bugs.)
3.11. Q. Trouble building Python on Linux.
A. In 1.1 and 1.1.1, there's a bug in the reference counting logic of
ternary pow() which is only tripped by very picky mallocs, like the
GNU malloc on Linux. This has been fixed in 1.2. To continue the
tests, just disable the tests of pow() with three arguments from
Lib/test/test_b2.py.
Apart from this, Python builds and runs fine on most Linux versions.
Bennet Todd has this to say on the subject:
It seems to work for some people but not for others.
This might depend on which Linux release you're using. Older Linux
releases had (understandably) fewer gracious hack to improve Unix
compatibility; really old Linux was Posix 1003.1 compatible, without
nearly as much Unix compatibility as one might have wished. Current
releases of Linux build most current free software, either BSD or
System V, with little or no trouble.
Besides that, there are quite a few different releases currently,
including MCC, Slackware, SLS, and Debian, and probably more. The
differences between their compilation environments are far smaller
than they used to be, but they're still updated with different
frequencies. It's likely that the current Slackware works fine, as
it currently seems to be the most popular. But that's just a guess.
3.12. Q. Trouble with prototypes on Ultrix.
A. Ultrix cc seems broken -- use gcc, or edit config.h to #undef
HAVE_PROTOTYPES.
3.13. Q. Trouble with posix.listdir on NeXTSTEP 3.2.
A. You need to pass "-posix" to the C compiler, especially in the link
phase -- it uses the correct version of the C library. The easiest
way is perhaps to do "make OPT=-posix". You also need to remove this
NeXT-specific section from import.c:
#if defined(NeXT) || defined(WITH_RLD)
#define DYNAMIC_LINK
#define USE_RLD
#endif
3.14. Q. Other trouble building Python on platform X.
A. Please email the details to <[email protected]> and I'll look into it.
Please provide as many details as possible. In particular, if you
don't tell me what type of computer and what operating system (and
version) you are using it will be difficult for me to figure out what
is the matter. If you get a specific error message, please email it
to me too.
3.15. Q. How to configure dynamic loading on Lixux.
A. (Thanks to Andrew Kuchling):
Linux requires that you use the GNU DLD library. The stages of using
dynamic libraries on Linux are:
1) Get dld 3.2.5 from a Linux site. Be careful here; the most recent
GNU version is 3.2.3, and doesn't support Linux; be sure to get it
from a Linux mirror, not a GNU mirror (3.2.4 should also work).
Compile it and install the library libdld.a somewhere; I used
/usr/local/lib.
Suitable URLs for the dld distribution are currently:
<URL:ftp://sunsite.unc.edu/pub/Linux/libs/dld-3.2.5.src.tar.gz> and
<URL:ftp://tsx-11.mit.edu/pub/linux/sources/libs/dld-3.2.5.src.tar.gz>.
There's also a binary distribution of it:
<URL:ftp://sunsite.unc.edu/pub/Linux/libs/dld-3.2.5.bin.tar.gz>.
2) Get Jack Jansen's DL library; its location is given in the
_Extending Python_ manual as ftp://ftp.cwi.nl/pub/dynload/. Compile
it and install libdl.a wherever you put libdld.a.
3) Run Python's configure script, giving it the --with-dl-dld option,
which requires a parameter giving the directory where you put the
libraries.
4) Recompile Python.
Note that once ELF is common in the Linux world, it will be much
easier (probably within a year) -- the standard SVR4-style dlopen()
interface is then available. This has already proved to work
perfectly using a beta version of the ELF-capable GCC for Linux.
4. Programming in Python
========================
4.1. Q. Is there a source code level debugger with breakpoints, step,
etc.?
A. Yes. Check out module pdb; pdb.help() prints the documentation (or
you can read it as Lib/pdb.doc). If you use the STDWIN option,
there's also a windowing interface, wdb. You can write your own
debugger by using the code for pdb or wdb as an example.
4.2. Q. 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. No, but you can easily create a Python class which serves as a
wrapper around a built-in object, e.g. (for dictionaries):
# A user-defined class behaving almost identical
# to a built-in dictionary.
class UserDict:
def __init__(self): self.data = {}
def __repr__(self): return repr(self.data)
def __cmp__(self, dict):
if type(dict) == type(self.data):
return cmp(self.data, dict)
else:
return cmp(self.data, dict.data)
def __len__(self): return len(self.data)
def __getitem__(self, key): return self.data[key]
def __setitem__(self, key, item): self.data[key] = item
def __delitem__(self, key): del self.data[key]
def keys(self): return self.data.keys()
def items(self): return self.data.items()
def values(self): return self.data.values()
def has_key(self, key): return self.data.has_key(key)
4.3. Q. Is there a curses/termcap package for Python?
A. Yes -- Lance Ellinghaus has written a module that interfaces to
System V's "ncurses". If you know a little curses and some Python,
it's straightforward to use. It is part of the standard Python
distribution, but not configured by default -- you must enable it by
editing Modules/Setup. It requires a System V curses implementation.
You could also consider using the "alfa" (== character cell) version
of STDWIN. (Standard Window System Interface, a portable windowing
system interface by myself <URL:ftp://ftp.cwi.nl/pub/stdwin/>.) This
will also prepare your program for porting to windowing environments
such as X11 or the Macintosh.
4.4. Q. Is there an equivalent to C's onexit() in Python?
A. Yes, if you import sys and assign a function to sys.exitfunc, it
will be called when your program exits, is killed by an unhandled
exception, or (on UNIX) receives a SIGHUP or SIGTERM signal.
4.5. Q. When I define a function nested inside another function, the
nested function seemingly can't access the local variables of the
outer function. What is going on? How do I pass local data to a
nested function?
A. Python does not have arbitrarily nested scopes. When you need to
create a function that needs to access some data which you have
available locally, create a new class to hold the data and return a
method of an instance of that class, e.g.:
class MultiplierClass:
def __init__(self, factor):
self.factor = factor
def multiplier(self, argument):
return argument * self.factor
def generate_multiplier(factor):
return MultiplierClass(factor).multiplier
twice = generate_multiplier(2)
print twice(10)
# Output: 20
An alternative solution uses default arguments, e.g.:
def generate_multiplier(factor):
def multiplier(arg, fact = factor):
return arg*fact
return multiplier
twice = generate_multiplier(2)
print twice(10)
# Output: 20
4.6. Q. How do I iterate over a sequence in reverse order?
A. If it is a list, the fastest solution is
list.reverse()
try:
for x in list:
"do something with x"
finally:
list.reverse()
This has the disadvantage that while you are in the loop, the list
is temporarily reversed. If you don't like this, you can make a copy.
This appears expensive but is actually faster than other solutions:
rev = list[:]
rev.reverse()
for x in rev:
<do something with x>
If it isn't a list, a more general but slower solution is:
i = len(list)
while i > 0:
i = i-1
x = list[i]
<do something with x>
A more elegant solution, is to define a class which acts as a sequence
and yields the elements in reverse order (solution due to Steve
Majewski):
class Rev:
def __init__(self, seq):
self.forw = seq
def __len__(self):
return len(self.forw)
def __getitem__(self, i):
return self.forw[-(i + 1)]
You can now simply write:
for x in Rev(list):
<do something with x>
Unfortunately, this solution is slowest of all, due to the method
call overhead...
4.7. Q. My program is too slow. How do I speed it up?
A. That's a tough one, in general. There are many tricks to speed up
Python code; I would consider rewriting parts in C only as a last
resort. One thing to notice is that function and (especially) method
calls are rather expensive; if you have designed a purely OO interface
with lots of tiny functions that don't do much more than get or set an
instance variable or call another method, you may consider using a
more direct way, e.g. directly accessing instance variables. Also see
the standard module "profile" (described in the file
"python/lib/profile.doc") which makes it possible to find out where
your program is spending most of its time (if you have some patience
-- the profiling itself can slow your program down by an order of
magnitude).
4.8. Q. 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. For reasons of efficiency as well as consistency, Python only reads
the module file on the first time a module is imported. (Otherwise a
program consisting of many modules, each of which imports the same
basic module, would read the basic module over and over again.) To
force rereading of a changed module, do this:
import modname
reload(modname)
Warning: this technique is not 100% fool-proof. In particular,
modules containing statements like
from modname import some_objects
will continue to work with the old version of the imported objects.
4.9. Q. How do I find the current module name?
A. A module can find out its own module name by looking at the
(predefined) global variable __name__. If this has the value
'__main__' you are running as a script.
4.10. Q. 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. See the previous question. E.g. if you put the following on the
last line of your module, main() is called only when your module is
running as a script:
if __name__ == '__main__': main()
4.11. Q. I try to run a program from the Demo directory but it fails
with ImportError: No module named ...; what gives?
A. This is probably an optional module (written in C!) which hasn't
been configured on your system. This especially happens with modules
like "Tkinter", "stdwin", "gl", "Xt" or "Xm". For Tkinter, STDWIN and
many other modules, see Modules/Setup.in for info on how to add these
modules to your Python, if it is possible at all. Sometimes you will
have to ftp and build another package first (e.g. STDWIN). Sometimes
the module only works on specific platforms (e.g. gl only works on SGI
machines).
NOTE: if the complaint is about "Tkinter" (upper case T) and you have
already configured module "tkinter" (lower case t), the solution is
*not* to rename tkinter to Tkinter or vice versa. There is probably
something wrong with your module search path. Check out the value of
sys.path.
For X-related modules (Xt and Xm) you will have to do more work: they
are currently not part of the standard Python distribution. You will
have to ftp the Extensions tar file, e.g.
<URL:ftp://ftp.cwi.nl/pub/python/extensions.tar.gz> and follow the
instructions there. Note: the X related modules are still somewhat
flakey, so don't try this unless you alread know a bit or two about
building X applications on your platform.
See also the next question.
4.12. Q. I have successfully built Python with STDWIN but it can't
find some modules (e.g. stdwinevents).
A. There's a subdirectory of the library directory named 'stdwin'
which should be in the default module search path. There's a line in
Modules/Setup(.in) that you have to enable for this purpose --
unfortunately in the latest release it's not near the other
STDWIN-related lines so it's easy to miss it.
4.13. Q. What GUI toolkits exist for Python?
A. Depending on what platform(s) you are aiming at, there are several.
- There's a neat object-oriented interface to the Tcl/Tk widget set,
called Tkinter. As of python 1.1, it is part of the standard Python
distribution -- all you need to do is enable it in Modules/Setup
(provided you have already installed Tk and Tcl). This is probably
the easiest to install and use, and the most complete widget set. It
is also very likely that in the future the standard Python GUI API