1、Learning PythonDownload at WoweBook.ComDownload at WoweBook.ComFOURTH EDITIONLearning PythonMark LutzBeijing Cambridge Farnham Kln Sebastopol Taipei TokyoDownload at WoweBook.ComLearning Python, Fourth Editionby Mark LutzCopyright 2009 Mark Lutz. All rights reserved.Printed in the United States of A
2、merica.Published by OReilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.OReilly books may be purchased for educational, business, or sales promotional use. Online editionsare also available for most titles (http:/). For more information, contact ourcorporate/institutional sales
3、 department: (800) 998-9938 or .Editor: Julie SteeleProduction Editor: Sumita MukherjiCopyeditor: Rachel HeadProduction Services: Newgen North AmericaIndexer: John BickelhauptCover Designer: Karen MontgomeryInterior Designer: David FutatoIllustrator: Robert RomanoPrinting History:March 1999: First E
4、dition. December 2003: Second Edition. October 2007: Third Edition. September 2009: Fourth Edition. Nutshell Handbook, the Nutshell Handbook logo, and the OReilly logo are registered trademarks ofOReilly Media, Inc. Learning Python, the image of a wood rat, and related trade dress are trademarksof O
5、Reilly Media, Inc.Many of the designations used by manufacturers and sellers to distinguish their products are claimed astrademarks. Where those designations appear in this book, and OReilly Media, Inc., was aware of atrademark claim, the designations have been printed in caps or initial caps.While
6、every precaution has been taken in the preparation of this book, the publisher and author assumeno responsibility for errors or omissions, or for damages resulting from the use of the information con-tained herein.ISBN: 978-0-596-15806-4M1252944666Download at WoweBook.ComTo Vera.You are my life.Down
7、load at WoweBook.ComDownload at WoweBook.ComTable of ContentsPreface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxiPart I. Getting Started 1. A Python Q&A Session . . . . . . . . . . . . . . . . . . . . . . .
8、 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3Why Do People Use Python? 3Software Quality 4Developer Productivity 5Is Python a “Scripting Language”? 5OK, but Whats the Downside? 7Who Uses Python Today? 7What Can I Do with Python? 9Systems Programming 9GUIs 9Internet Scripting 10Component
9、 Integration 10Database Programming 11Rapid Prototyping 11Numeric and Scientific Programming 11Gaming, Images, Serial Ports, XML, Robots, and More 12How Is Python Supported? 12What Are Pythons Technical Strengths? 13Its Object-Oriented 13Its Free 13Its Portable 14Its Powerful 15Its Mixable 16Its Eas
10、y to Use 16Its Easy to Learn 17Its Named After Monty Python 17How Does Python Stack Up to Language X? 17viiDownload at WoweBook.ComChapter Summary 18Test Your Knowledge: Quiz 19Test Your Knowledge: Answers 192. How Python Runs Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
11、. . . . . . . . . . . . . 23Introducing the Python Interpreter 23Program Execution 24The Programmers View 24Pythons View 26Execution Model Variations 29Python Implementation Alternatives 29Execution Optimization Tools 30Frozen Binaries 32Other Execution Options 33Future Possibilities? 33Chapter Summ
12、ary 34Test Your Knowledge: Quiz 34Test Your Knowledge: Answers 343. How You Run Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35The Interactive Prompt 35Running Code Interactively 37Why the Interactive Prompt? 38Using the Interactive Promp
13、t 39System Command Lines and Files 41A First Script 42Running Files with Command Lines 43Using Command Lines and Files 44Unix Executable Scripts (#!) 46Clicking File Icons 47Clicking Icons on Windows 47The input Trick 49Other Icon-Click Limitations 50Module Imports and Reloads 51The Grander Module Story: Attributes 53import and reload Usage Notes 56Using exec to Run Module Files 57The IDLE User Interface 58IDLE Basics 58Using IDLE 60Advanced IDLE Tools 62Other IDEs 63Other Launch Options 64viii | Table of ContentsDownload at WoweBook.Com