Đăng ký Đăng nhập

Tài liệu Learning python, 5th edition

.PDF
1594
143
116

Mô tả:

www.it-ebooks.info www.it-ebooks.info FIFTH EDITION Learning Python Mark Lutz Beijing • Cambridge • Farnham • Köln • Sebastopol • Tokyo www.it-ebooks.info Learning Python, Fifth Edition by Mark Lutz Copyright © 2013 Mark Lutz. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/institutional sales department: 800-998-9938 or [email protected]. Editor: Rachel Roumeliotis Production Editor: Christopher Hearse Copyeditor: Rachel Monaghan Proofreader: Julie Van Keuren June 2013: Indexer: Lucie Haskins Cover Designer: Randy Comer Interior Designer: David Futato Illustrator: Rebecca Demarest Fifth Edition. Revision History for the Fifth Edition: 2013-06-07 First release See http://oreilly.com/catalog/errata.csp?isbn=9781449355739 for release details. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. Learning Python, 5th Edition, the image of a wood rat, and related trade dress are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. ISBN: 978-1-449-35573-9 [QG] 1370970520 www.it-ebooks.info To Vera. You are my life. www.it-ebooks.info www.it-ebooks.info Table of Contents Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxiii Part I. Getting Started 1. A Python Q&A Session . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Why Do People Use Python? Software Quality Developer Productivity Is Python a “Scripting Language”? OK, but What’s the Downside? Who Uses Python Today? What Can I Do with Python? Systems Programming GUIs Internet Scripting Component Integration Database Programming Rapid Prototyping Numeric and Scientific Programming And More: Gaming, Images, Data Mining, Robots, Excel... How Is Python Developed and Supported? Open Source Tradeoffs What Are Python’s Technical Strengths? It’s Object-Oriented and Functional It’s Free It’s Portable It’s Powerful It’s Mixable It’s Relatively Easy to Use It’s Relatively Easy to Learn It’s Named After Monty Python 3 4 5 5 7 9 10 11 11 11 12 12 13 13 14 15 15 16 16 17 17 18 19 19 20 20 v www.it-ebooks.info How Does Python Stack Up to Language X? Chapter Summary Test Your Knowledge: Quiz Test Your Knowledge: Answers 21 22 23 23 2. How Python Runs Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Introducing the Python Interpreter Program Execution The Programmer’s View Python’s View Execution Model Variations Python Implementation Alternatives Execution Optimization Tools Frozen Binaries Future Possibilities? Chapter Summary Test Your Knowledge: Quiz Test Your Knowledge: Answers 27 28 28 30 33 33 37 39 40 40 41 41 3. How You Run Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 The Interactive Prompt Starting an Interactive Session The System Path New Windows Options in 3.3: PATH, Launcher Where to Run: Code Directories What Not to Type: Prompts and Comments Running Code Interactively Why the Interactive Prompt? Usage Notes: The Interactive Prompt System Command Lines and Files A First Script Running Files with Command Lines Command-Line Usage Variations Usage Notes: Command Lines and Files Unix-Style Executable Scripts: #! Unix Script Basics The Unix env Lookup Trick The Python 3.3 Windows Launcher: #! Comes to Windows Clicking File Icons Icon-Click Basics Clicking Icons on Windows The input Trick on Windows Other Icon-Click Limitations vi | Table of Contents www.it-ebooks.info 43 44 45 46 47 48 49 50 52 54 55 56 57 58 59 59 60 60 62 62 63 63 66 Module Imports and Reloads Import and Reload Basics The Grander Module Story: Attributes Usage Notes: import and reload Using exec to Run Module Files The IDLE User Interface IDLE Startup Details IDLE Basic Usage IDLE Usability Features Advanced IDLE Tools Usage Notes: IDLE Other IDEs Other Launch Options Embedding Calls Frozen Binary Executables Text Editor Launch Options Still Other Launch Options Future Possibilities? Which Option Should I Use? Chapter Summary Test Your Knowledge: Quiz Test Your Knowledge: Answers Test Your Knowledge: Part I Exercises 66 66 68 71 72 73 74 75 76 77 78 79 81 81 82 82 82 83 83 85 85 86 87 Part II. Types and Operations 4. Introducing Python Object Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 The Python Conceptual Hierarchy Why Use Built-in Types? Python’s Core Data Types Numbers Strings Sequence Operations Immutability Type-Specific Methods Getting Help Other Ways to Code Strings Unicode Strings Pattern Matching Lists Sequence Operations Type-Specific Operations 93 94 95 97 99 99 101 102 104 105 106 108 109 109 109 Table of Contents | vii www.it-ebooks.info Bounds Checking Nesting Comprehensions Dictionaries Mapping Operations Nesting Revisited Missing Keys: if Tests Sorting Keys: for Loops Iteration and Optimization Tuples Why Tuples? Files Binary Bytes Files Unicode Text Files Other File-Like Tools Other Core Types How to Break Your Code’s Flexibility User-Defined Classes And Everything Else Chapter Summary Test Your Knowledge: Quiz Test Your Knowledge: Answers 110 110 111 113 114 115 116 118 120 121 122 122 123 124 126 126 128 129 130 130 131 131 5. Numeric Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 Numeric Type Basics Numeric Literals Built-in Numeric Tools Python Expression Operators Numbers in Action Variables and Basic Expressions Numeric Display Formats Comparisons: Normal and Chained Division: Classic, Floor, and True Integer Precision Complex Numbers Hex, Octal, Binary: Literals and Conversions Bitwise Operations Other Built-in Numeric Tools Other Numeric Types Decimal Type Fraction Type Sets Booleans viii | Table of Contents www.it-ebooks.info 133 134 136 136 141 141 143 144 146 150 151 151 153 155 157 157 160 163 171 Numeric Extensions Chapter Summary Test Your Knowledge: Quiz Test Your Knowledge: Answers 172 172 173 173 6. The Dynamic Typing Interlude . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 The Case of the Missing Declaration Statements Variables, Objects, and References Types Live with Objects, Not Variables Objects Are Garbage-Collected Shared References Shared References and In-Place Changes Shared References and Equality Dynamic Typing Is Everywhere Chapter Summary Test Your Knowledge: Quiz Test Your Knowledge: Answers 175 176 177 178 180 181 183 185 186 186 186 7. String Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 This Chapter’s Scope Unicode: The Short Story String Basics String Literals Single- and Double-Quoted Strings Are the Same Escape Sequences Represent Special Characters Raw Strings Suppress Escapes Triple Quotes Code Multiline Block Strings Strings in Action Basic Operations Indexing and Slicing String Conversion Tools Changing Strings I String Methods Method Call Syntax Methods of Strings String Method Examples: Changing Strings II String Method Examples: Parsing Text Other Common String Methods in Action The Original string Module’s Functions (Gone in 3.X) String Formatting Expressions Formatting Expression Basics Advanced Formatting Expression Syntax Advanced Formatting Expression Examples 189 189 190 192 193 193 196 198 200 200 201 205 208 209 209 210 211 213 214 215 216 217 218 220 Table of Contents | ix www.it-ebooks.info Dictionary-Based Formatting Expressions String Formatting Method Calls Formatting Method Basics Adding Keys, Attributes, and Offsets Advanced Formatting Method Syntax Advanced Formatting Method Examples Comparison to the % Formatting Expression Why the Format Method? General Type Categories Types Share Operation Sets by Categories Mutable Types Can Be Changed in Place Chapter Summary Test Your Knowledge: Quiz Test Your Knowledge: Answers 221 222 222 223 224 225 227 230 235 235 236 237 237 237 8. Lists and Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 Lists Lists in Action Basic List Operations List Iteration and Comprehensions Indexing, Slicing, and Matrixes Changing Lists in Place Dictionaries Dictionaries in Action Basic Dictionary Operations Changing Dictionaries in Place More Dictionary Methods Example: Movie Database Dictionary Usage Notes Other Ways to Make Dictionaries Dictionary Changes in Python 3.X and 2.7 Chapter Summary Test Your Knowledge: Quiz Test Your Knowledge: Answers 239 242 242 242 243 244 250 252 253 254 254 256 258 262 264 271 272 272 9. Tuples, Files, and Everything Else . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275 Tuples Tuples in Action Why Lists and Tuples? Records Revisited: Named Tuples Files Opening Files Using Files x | Table of Contents www.it-ebooks.info 276 277 279 280 282 283 284 Files in Action Text and Binary Files: The Short Story Storing Python Objects in Files: Conversions Storing Native Python Objects: pickle Storing Python Objects in JSON Format Storing Packed Binary Data: struct File Context Managers Other File Tools Core Types Review and Summary Object Flexibility References Versus Copies Comparisons, Equality, and Truth The Meaning of True and False in Python Python’s Type Hierarchies Type Objects Other Types in Python Built-in Type Gotchas Assignment Creates References, Not Copies Repetition Adds One Level Deep Beware of Cyclic Data Structures Immutable Types Can’t Be Changed in Place Chapter Summary Test Your Knowledge: Quiz Test Your Knowledge: Answers Test Your Knowledge: Part II Exercises 285 287 288 290 291 293 294 294 295 297 297 300 304 306 306 308 308 308 309 310 311 311 311 312 313 Part III. Statements and Syntax 10. Introducing Python Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319 The Python Conceptual Hierarchy Revisited Python’s Statements A Tale of Two ifs What Python Adds What Python Removes Why Indentation Syntax? A Few Special Cases A Quick Example: Interactive Loops A Simple Interactive Loop Doing Math on User Inputs Handling Errors by Testing Inputs Handling Errors with try Statements Nesting Code Three Levels Deep 319 320 322 322 323 324 327 329 329 331 332 333 335 Table of Contents | xi www.it-ebooks.info Chapter Summary Test Your Knowledge: Quiz Test Your Knowledge: Answers 336 336 336 11. Assignments, Expressions, and Prints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339 Assignment Statements Assignment Statement Forms Sequence Assignments Extended Sequence Unpacking in Python 3.X Multiple-Target Assignments Augmented Assignments Variable Name Rules Expression Statements Expression Statements and In-Place Changes Print Operations The Python 3.X print Function The Python 2.X print Statement Print Stream Redirection Version-Neutral Printing Chapter Summary Test Your Knowledge: Quiz Test Your Knowledge: Answers 339 340 341 344 348 350 352 356 357 358 359 361 363 366 369 370 370 12. if Tests and Syntax Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371 if Statements General Format Basic Examples Multiway Branching Python Syntax Revisited Block Delimiters: Indentation Rules Statement Delimiters: Lines and Continuations A Few Special Cases Truth Values and Boolean Tests The if/else Ternary Expression Chapter Summary Test Your Knowledge: Quiz Test Your Knowledge: Answers 371 371 372 372 375 376 378 379 380 382 385 385 386 13. while and for Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387 while Loops General Format Examples break, continue, pass, and the Loop else xii | Table of Contents www.it-ebooks.info 387 388 388 389 General Loop Format pass continue break Loop else for Loops General Format Examples Loop Coding Techniques Counter Loops: range Sequence Scans: while and range Versus for Sequence Shufflers: range and len Nonexhaustive Traversals: range Versus Slices Changing Lists: range Versus Comprehensions Parallel Traversals: zip and map Generating Both Offsets and Items: enumerate Chapter Summary Test Your Knowledge: Quiz Test Your Knowledge: Answers 389 390 391 391 392 395 395 395 402 402 403 404 405 406 407 410 413 414 414 14. Iterations and Comprehensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415 Iterations: A First Look The Iteration Protocol: File Iterators Manual Iteration: iter and next Other Built-in Type Iterables List Comprehensions: A First Detailed Look List Comprehension Basics Using List Comprehensions on Files Extended List Comprehension Syntax Other Iteration Contexts New Iterables in Python 3.X Impacts on 2.X Code: Pros and Cons The range Iterable The map, zip, and filter Iterables Multiple Versus Single Pass Iterators Dictionary View Iterables Other Iteration Topics Chapter Summary Test Your Knowledge: Quiz Test Your Knowledge: Answers 416 416 419 422 424 425 426 427 429 434 434 435 436 438 439 440 441 441 441 15. The Documentation Interlude . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443 Python Documentation Sources 443 Table of Contents | xiii www.it-ebooks.info # Comments The dir Function Docstrings: __doc__ PyDoc: The help Function PyDoc: HTML Reports Beyond docstrings: Sphinx The Standard Manual Set Web Resources Published Books Common Coding Gotchas Chapter Summary Test Your Knowledge: Quiz Test Your Knowledge: Answers Test Your Knowledge: Part III Exercises 444 444 446 449 452 461 461 462 463 463 465 466 466 467 Part IV. Functions and Generators 16. Function Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473 Why Use Functions? Coding Functions def Statements def Executes at Runtime A First Example: Definitions and Calls Definition Calls Polymorphism in Python A Second Example: Intersecting Sequences Definition Calls Polymorphism Revisited Local Variables Chapter Summary Test Your Knowledge: Quiz Test Your Knowledge: Answers 474 475 476 477 478 478 478 479 480 481 481 482 483 483 483 484 17. Scopes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485 Python Scope Basics Scope Details Name Resolution: The LEGB Rule Scope Example The Built-in Scope The global Statement xiv | Table of Contents www.it-ebooks.info 485 486 488 490 491 494 Program Design: Minimize Global Variables Program Design: Minimize Cross-File Changes Other Ways to Access Globals Scopes and Nested Functions Nested Scope Details Nested Scope Examples Factory Functions: Closures Retaining Enclosing Scope State with Defaults The nonlocal Statement in 3.X nonlocal Basics nonlocal in Action Why nonlocal? State Retention Options State with nonlocal: 3.X only State with Globals: A Single Copy Only State with Classes: Explicit Attributes (Preview) State with Function Attributes: 3.X and 2.X Chapter Summary Test Your Knowledge: Quiz Test Your Knowledge: Answers 495 497 498 499 500 500 501 504 508 508 509 512 512 513 513 515 519 519 520 18. Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523 Argument-Passing Basics Arguments and Shared References Avoiding Mutable Argument Changes Simulating Output Parameters and Multiple Results Special Argument-Matching Modes Argument Matching Basics Argument Matching Syntax The Gritty Details Keyword and Default Examples Arbitrary Arguments Examples Python 3.X Keyword-Only Arguments The min Wakeup Call! Full Credit Bonus Points The Punch Line... Generalized Set Functions Emulating the Python 3.X print Function Using Keyword-Only Arguments Chapter Summary Test Your Knowledge: Quiz Test Your Knowledge: Answers 523 524 526 527 528 529 530 531 532 534 539 542 542 544 544 545 547 548 550 551 552 Table of Contents | xv www.it-ebooks.info 19. Advanced Function Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553 Function Design Concepts Recursive Functions Summation with Recursion Coding Alternatives Loop Statements Versus Recursion Handling Arbitrary Structures Function Objects: Attributes and Annotations Indirect Function Calls: “First Class” Objects Function Introspection Function Attributes Function Annotations in 3.X Anonymous Functions: lambda lambda Basics Why Use lambda? How (Not) to Obfuscate Your Python Code Scopes: lambdas Can Be Nested Too Functional Programming Tools Mapping Functions over Iterables: map Selecting Items in Iterables: filter Combining Items in Iterables: reduce Chapter Summary Test Your Knowledge: Quiz Test Your Knowledge: Answers 553 555 555 556 557 558 562 562 563 564 565 567 568 569 571 572 574 574 576 576 578 578 578 20. Comprehensions and Generations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 581 List Comprehensions and Functional Tools List Comprehensions Versus map Adding Tests and Nested Loops: filter Example: List Comprehensions and Matrixes Don’t Abuse List Comprehensions: KISS Generator Functions and Expressions Generator Functions: yield Versus return Generator Expressions: Iterables Meet Comprehensions Generator Functions Versus Generator Expressions Generators Are Single-Iteration Objects Generation in Built-in Types, Tools, and Classes Example: Generating Scrambled Sequences Don’t Abuse Generators: EIBTI Example: Emulating zip and map with Iteration Tools Comprehension Syntax Summary Scopes and Comprehension Variables Comprehending Set and Dictionary Comprehensions xvi | Table of Contents www.it-ebooks.info 581 582 583 586 588 591 592 597 602 604 606 609 614 617 622 623 624 Extended Comprehension Syntax for Sets and Dictionaries Chapter Summary Test Your Knowledge: Quiz Test Your Knowledge: Answers 625 626 626 626 21. The Benchmarking Interlude . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 629 Timing Iteration Alternatives Timing Module: Homegrown Timing Script Timing Results Timing Module Alternatives Other Suggestions Timing Iterations and Pythons with timeit Basic timeit Usage Benchmark Module and Script: timeit Benchmark Script Results More Fun with Benchmarks Other Benchmarking Topics: pystones Function Gotchas Local Names Are Detected Statically Defaults and Mutable Objects Functions Without returns Miscellaneous Function Gotchas Chapter Summary Test Your Knowledge: Quiz Test Your Knowledge: Answers Test Your Knowledge: Part IV Exercises 629 630 634 635 638 642 642 643 647 649 651 656 656 657 658 660 661 661 662 662 663 Part V. Modules and Packages 22. Modules: The Big Picture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 669 Why Use Modules? Python Program Architecture How to Structure a Program Imports and Attributes Standard Library Modules How Imports Work 1. Find It 2. Compile It (Maybe) 3. Run It Byte Code Files: __pycache__ in Python 3.2+ Byte Code File Models in Action 669 670 671 671 673 674 674 675 675 676 677 Table of Contents | xvii www.it-ebooks.info The Module Search Path Configuring the Search Path Search Path Variations The sys.path List Module File Selection Chapter Summary Test Your Knowledge: Quiz Test Your Knowledge: Answers 678 681 681 681 682 685 685 685 23. Module Coding Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 687 Module Creation Module Filenames Other Kinds of Modules Module Usage The import Statement The from Statement The from * Statement Imports Happen Only Once import and from Are Assignments import and from Equivalence Potential Pitfalls of the from Statement Module Namespaces Files Generate Namespaces Namespace Dictionaries: __dict__ Attribute Name Qualification Imports Versus Scopes Namespace Nesting Reloading Modules reload Basics reload Example Chapter Summary Test Your Knowledge: Quiz Test Your Knowledge: Answers 687 687 688 688 689 689 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 704 24. Module Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 707 Package Import Basics Packages and Search Path Settings Package __init__.py Files Package Import Example from Versus import with Packages Why Use Package Imports? A Tale of Three Systems Package Relative Imports xviii | Table of Contents www.it-ebooks.info 708 708 709 711 713 713 714 717
- Xem thêm -

Tài liệu liên quan