وبلاگ بلیان

Learning Python, 5th Edition

معرفی کتاب «Learning Python, 5th Edition» نوشتهٔ Safari, an O'Reilly Media Company.; Lutz, Mark، منتشرشده توسط نشر O'Reilly Media در سال 2013. این کتاب در فرمت pdf، زبان انگلیسی ارائه شده است. «Learning Python, 5th Edition» در دستهٔ بدون دسته‌بندی قرار دارد.

Get a comprehensive, in-depth introduction to the core Python language with this hands-on book. Based on author Mark Lutz’s popular training course, this updated fifth edition will help you quickly write efficient, high-quality code with Python. It’s an ideal way to begin, whether you’re new to programming or a professional developer versed in other languages.Complete with quizzes, exercises, and helpful illustrations, this easy-to-follow, self-paced tutorial gets you started with both Python 2.7 and 3.3— the latest releases in the 3.X and 2.X lines—plus all other releases in common use today. You’ll also learn some advanced language features that recently have become more common in Python code.• Explore Python’s major built-in object types such as numbers, lists, and dictionaries• Create and process objects with Python statements, and learn Python’s general syntax model• Use functions to avoid code redundancy and package code for reuse• Organize statements, functions, and other tools into larger components with modules• Dive into classes: Python’s object-oriented programming tool for structuring code• Write large programs with Python’s exception-handling model and development tools• Learn advanced Python tools, including decorators, descriptors, metaclasses, and Unicode processing Cover......Page 1 Copyright......Page 4 Table of Contents......Page 7 This Book’s “Ecosystem”......Page 35 About This Fifth Edition......Page 37 The Python 2.X and 3.X Lines......Page 38 The 2.X/3.X Story Today......Page 39 Which Python Should I Use?......Page 40 This Book’s Prerequisites and Effort......Page 41 This Book’s Structure......Page 43 What This Book Is Not......Page 45 It’s Not a Reference or a Guide to Specific Applications......Page 46 It’s as Linear as Python Allows......Page 47 Python Versions......Page 48 Using This Book’s Code......Page 49 Font Conventions......Page 50 Acknowledgments......Page 51 Python Thanks......Page 52 Personal Thanks......Page 53 Part I. Getting Started......Page 55 Why Do People Use Python?......Page 57 Developer Productivity......Page 59 Is Python a “Scripting Language”?......Page 60 OK, but What’s the Downside?......Page 61 Who Uses Python Today?......Page 63 GUIs......Page 65 Internet Scripting......Page 66 Database Programming......Page 67 And More: Gaming, Images, Data Mining, Robots, Excel.........Page 68 How Is Python Developed and Supported?......Page 69 What Are Python’s Technical Strengths?......Page 70 It’s Free......Page 71 It’s Portable......Page 72 It’s Powerful......Page 73 It’s Relatively Easy to Use......Page 74 It’s Named After Monty Python......Page 75 How Does Python Stack Up to Language X?......Page 76 Test Your Knowledge: Quiz......Page 77 Test Your Knowledge: Answers......Page 78 Introducing the Python Interpreter......Page 81 Program Execution......Page 82 The Programmer’s View......Page 83 Python’s View......Page 84 Execution Model Variations......Page 87 Python Implementation Alternatives......Page 88 Execution Optimization Tools......Page 91 Frozen Binaries......Page 93 Future Possibilities?......Page 94 Test Your Knowledge: Answers......Page 95 The Interactive Prompt......Page 97 Starting an Interactive Session......Page 98 New Windows Options in 3.3: PATH, Launcher......Page 100 Where to Run: Code Directories......Page 101 What Not to Type: Prompts and Comments......Page 102 Running Code Interactively......Page 103 Why the Interactive Prompt?......Page 104 Usage Notes: The Interactive Prompt......Page 106 System Command Lines and Files......Page 108 A First Script......Page 109 Running Files with Command Lines......Page 110 Command-Line Usage Variations......Page 111 Usage Notes: Command Lines and Files......Page 113 Unix Script Basics......Page 114 The Python 3.3 Windows Launcher: #! Comes to Windows......Page 115 Clicking Icons on Windows......Page 117 The input Trick on Windows......Page 119 Module Imports and Reloads......Page 121 Import and Reload Basics......Page 122 The Grander Module Story: Attributes......Page 124 Usage Notes: import and reload......Page 127 Using exec to Run Module Files......Page 128 The IDLE User Interface......Page 129 IDLE Startup Details......Page 130 IDLE Basic Usage......Page 131 IDLE Usability Features......Page 132 Usage Notes: IDLE......Page 133 Other IDEs......Page 135 Embedding Calls......Page 137 Still Other Launch Options......Page 138 Which Option Should I Use?......Page 139 Test Your Knowledge: Quiz......Page 141 Test Your Knowledge: Answers......Page 142 Test Your Knowledge: Part I Exercises......Page 143 Part II. Types and Operations......Page 147 The Python Conceptual Hierarchy......Page 149 Why Use Built-in Types?......Page 150 Python’s Core Data Types......Page 151 Numbers......Page 153 Sequence Operations......Page 155 Immutability......Page 157 Type-Specific Methods......Page 158 Getting Help......Page 160 Other Ways to Code Strings......Page 161 Unicode Strings......Page 162 Pattern Matching......Page 164 Sequence Operations......Page 165 Bounds Checking......Page 166 Nesting......Page 167 Comprehensions......Page 168 Mapping Operations......Page 170 Nesting Revisited......Page 171 Missing Keys: if Tests......Page 173 Sorting Keys: for Loops......Page 175 Iteration and Optimization......Page 176 Tuples......Page 178 Files......Page 179 Binary Bytes Files......Page 180 Unicode Text Files......Page 181 Other Core Types......Page 183 How to Break Your Code’s Flexibility......Page 185 User-Defined Classes......Page 186 Chapter Summary......Page 187 Test Your Knowledge: Answers......Page 188 Numeric Type Basics......Page 191 Numeric Literals......Page 192 Python Expression Operators......Page 194 Numbers in Action......Page 199 Variables and Basic Expressions......Page 200 Numeric Display Formats......Page 202 Comparisons: Normal and Chained......Page 203 Division: Classic, Floor, and True......Page 205 Integer Precision......Page 209 Hex, Octal, Binary: Literals and Conversions......Page 210 Bitwise Operations......Page 212 Other Built-in Numeric Tools......Page 214 Decimal Type......Page 216 Fraction Type......Page 219 Sets......Page 223 Booleans......Page 231 Chapter Summary......Page 232 Test Your Knowledge: Answers......Page 233 The Case of the Missing Declaration Statements......Page 235 Variables, Objects, and References......Page 236 Objects Are Garbage-Collected......Page 238 Shared References......Page 240 Shared References and In-Place Changes......Page 242 Shared References and Equality......Page 244 Dynamic Typing Is Everywhere......Page 245 Test Your Knowledge: Quiz......Page 246 Test Your Knowledge: Answers......Page 247 This Chapter’s Scope......Page 249 Unicode: The Short Story......Page 250 String Basics......Page 251 String Literals......Page 252 Single- and Double-Quoted Strings Are the Same......Page 253 Escape Sequences Represent Special Characters......Page 254 Raw Strings Suppress Escapes......Page 257 Triple Quotes Code Multiline Block Strings......Page 258 Basic Operations......Page 260 Indexing and Slicing......Page 262 String Conversion Tools......Page 266 Changing Strings I......Page 268 Method Call Syntax......Page 270 Methods of Strings......Page 271 String Method Examples: Changing Strings II......Page 272 String Method Examples: Parsing Text......Page 274 Other Common String Methods in Action......Page 275 The Original string Module’s Functions (Gone in 3.X)......Page 276 String Formatting Expressions......Page 277 Formatting Expression Basics......Page 278 Advanced Formatting Expression Syntax......Page 280 Advanced Formatting Expression Examples......Page 281 Dictionary-Based Formatting Expressions......Page 282 Formatting Method Basics......Page 283 Advanced Formatting Method Syntax......Page 285 Advanced Formatting Method Examples......Page 287 Comparison to the % Formatting Expression......Page 288 Why the Format Method?......Page 292 Types Share Operation Sets by Categories......Page 297 Mutable Types Can Be Changed in Place......Page 298 Test Your Knowledge: Quiz......Page 299 Test Your Knowledge: Answers......Page 300 Lists......Page 301 List Iteration and Comprehensions......Page 304 Indexing, Slicing, and Matrixes......Page 305 Changing Lists in Place......Page 306 Dictionaries......Page 313 Basic Dictionary Operations......Page 315 Changing Dictionaries in Place......Page 316 More Dictionary Methods......Page 317 Example: Movie Database......Page 319 Dictionary Usage Notes......Page 321 Other Ways to Make Dictionaries......Page 325 Dictionary Changes in Python 3.X and 2.7......Page 327 Test Your Knowledge: Quiz......Page 335 Test Your Knowledge: Answers......Page 336 Chapter 9. Tuples, Files, and Everything Else......Page 337 Tuples......Page 338 Tuples in Action......Page 339 Records Revisited: Named Tuples......Page 342 Files......Page 345 Opening Files......Page 346 Using Files......Page 347 Files in Action......Page 348 Text and Binary Files: The Short Story......Page 349 Storing Python Objects in Files: Conversions......Page 351 Storing Native Python Objects: pickle......Page 353 Storing Python Objects in JSON Format......Page 354 Storing Packed Binary Data: struct......Page 356 Other File Tools......Page 357 Core Types Review and Summary......Page 359 Object Flexibility......Page 360 References Versus Copies......Page 362 Comparisons, Equality, and Truth......Page 364 The Meaning of True and False in Python......Page 367 Type Objects......Page 370 Assignment Creates References, Not Copies......Page 372 Repetition Adds One Level Deep......Page 373 Beware of Cyclic Data Structures......Page 374 Chapter Summary......Page 375 Test Your Knowledge: Answers......Page 376 Test Your Knowledge: Part II Exercises......Page 377 Part III. Statements and Syntax......Page 381 The Python Conceptual Hierarchy Revisited......Page 383 Python’s Statements......Page 384 A Tale of Two ifs......Page 386 What Python Removes......Page 387 Why Indentation Syntax?......Page 389 A Few Special Cases......Page 392 A Simple Interactive Loop......Page 394 Doing Math on User Inputs......Page 396 Handling Errors by Testing Inputs......Page 397 Handling Errors with try Statements......Page 398 Chapter Summary......Page 401 Test Your Knowledge: Answers......Page 402 Assignment Statements......Page 403 Assignment Statement Forms......Page 404 Sequence Assignments......Page 405 Extended Sequence Unpacking in Python 3.X......Page 409 Multiple-Target Assignments......Page 413 Augmented Assignments......Page 414 Variable Name Rules......Page 417 Expression Statements......Page 421 Expression Statements and In-Place Changes......Page 422 Print Operations......Page 423 The Python 3.X print Function......Page 424 The Python 2.X print Statement......Page 427 Print Stream Redirection......Page 428 Version-Neutral Printing......Page 432 Chapter Summary......Page 435 Test Your Knowledge: Answers......Page 436 General Format......Page 437 Multiway Branching......Page 438 Python Syntax Revisited......Page 441 Block Delimiters: Indentation Rules......Page 442 Statement Delimiters: Lines and Continuations......Page 444 A Few Special Cases......Page 445 Truth Values and Boolean Tests......Page 447 The if/else Ternary Expression......Page 449 Test Your Knowledge: Quiz......Page 452 Test Your Knowledge: Answers......Page 453 while Loops......Page 455 Examples......Page 456 break, continue, pass, and the Loop else......Page 457 pass......Page 458 continue......Page 459 Loop else......Page 460 General Format......Page 463 Examples......Page 464 Counter Loops: range......Page 471 Sequence Scans: while and range Versus for......Page 472 Sequence Shufflers: range and len......Page 473 Nonexhaustive Traversals: range Versus Slices......Page 474 Changing Lists: range Versus Comprehensions......Page 475 Parallel Traversals: zip and map......Page 476 Generating Both Offsets and Items: enumerate......Page 479 Test Your Knowledge: Answers......Page 483 Chapter 14. Iterations and Comprehensions......Page 485 The Iteration Protocol: File Iterators......Page 486 Manual Iteration: iter and next......Page 489 Other Built-in Type Iterables......Page 492 List Comprehension Basics......Page 495 Using List Comprehensions on Files......Page 496 Extended List Comprehension Syntax......Page 498 Other Iteration Contexts......Page 500 Impacts on 2.X Code: Pros and Cons......Page 505 The range Iterable......Page 506 The map, zip, and filter Iterables......Page 507 Multiple Versus Single Pass Iterators......Page 508 Dictionary View Iterables......Page 510 Other Iteration Topics......Page 511 Test Your Knowledge: Answers......Page 512 Python Documentation Sources......Page 515 The dir Function......Page 516 Docstrings: __doc__......Page 518 PyDoc: The help Function......Page 521 PyDoc: HTML Reports......Page 524 Beyond docstrings: Sphinx......Page 533 The Standard Manual Set......Page 534 Common Coding Gotchas......Page 535 Test Your Knowledge: Answers......Page 538 Test Your Knowledge: Part III Exercises......Page 539 Part IV. Functions and Generators......Page 543 Chapter 16. Function Basics......Page 545 Why Use Functions?......Page 546 Coding Functions......Page 547 def Executes at Runtime......Page 549 Definition......Page 550 Calls......Page 551 Polymorphism in Python......Page 552 Definition......Page 553 Polymorphism Revisited......Page 554 Local Variables......Page 555 Test Your Knowledge: Answers......Page 556 Python Scope Basics......Page 559 Scope Details......Page 560 Name Resolution: The LEGB Rule......Page 562 Scope Example......Page 565 The Built-in Scope......Page 566 The global Statement......Page 569 Program Design: Minimize Global Variables......Page 570 Program Design: Minimize Cross-File Changes......Page 572 Other Ways to Access Globals......Page 573 Nested Scope Details......Page 574 Nested Scope Examples......Page 575 Factory Functions: Closures......Page 576 Retaining Enclosing Scope State with Defaults......Page 579 nonlocal Basics......Page 583 nonlocal in Action......Page 584 State with nonlocal: 3.X only......Page 587 State with Globals: A Single Copy Only......Page 588 State with Classes: Explicit Attributes (Preview)......Page 589 State with Function Attributes: 3.X and 2.X......Page 590 Chapter Summary......Page 594 Test Your Knowledge: Quiz......Page 595 Test Your Knowledge: Answers......Page 596 Argument-Passing Basics......Page 597 Arguments and Shared References......Page 598 Avoiding Mutable Argument Changes......Page 600 Simulating Output Parameters and Multiple Results......Page 601 Argument Matching Basics......Page 603 Argument Matching Syntax......Page 604 The Gritty Details......Page 605 Keyword and Default Examples......Page 606 Arbitrary Arguments Examples......Page 608 Python 3.X Keyword-Only Arguments......Page 613 The min Wakeup Call!......Page 616 Full Credit......Page 617 Bonus Points......Page 618 Generalized Set Functions......Page 619 Emulating the Python 3.X print Function......Page 622 Using Keyword-Only Arguments......Page 623 Chapter Summary......Page 625 Test Your Knowledge: Quiz......Page 626 Test Your Knowledge: Answers......Page 627 Function Design Concepts......Page 629 Recursive Functions......Page 631 Coding Alternatives......Page 632 Handling Arbitrary Structures......Page 634 Indirect Function Calls: “First Class” Objects......Page 638 Function Attributes......Page 640 Function Annotations in 3.X......Page 642 lambda Basics......Page 644 Why Use lambda?......Page 645 How (Not) to Obfuscate Your Python Code......Page 647 Scopes: lambdas Can Be Nested Too......Page 649 Functional Programming Tools......Page 650 Mapping Functions over Iterables: map......Page 651 Selecting Items in Iterables: filter......Page 652 Combining Items in Iterables: reduce......Page 653 Chapter Summary......Page 654 Test Your Knowledge: Answers......Page 655 List Comprehensions and Functional Tools......Page 657 List Comprehensions Versus map......Page 658 Adding Tests and Nested Loops: filter......Page 659 Example: List Comprehensions and Matrixes......Page 662 Don’t Abuse List Comprehensions: KISS......Page 664 Generator Functions and Expressions......Page 667 Generator Functions: yield Versus return......Page 668 Generator Expressions: Iterables Meet Comprehensions......Page 673 Generator Functions Versus Generator Expressions......Page 679 Generators Are Single-Iteration Objects......Page 680 Generation in Built-in Types, Tools, and Classes......Page 683 Example: Generating Scrambled Sequences......Page 686 Don’t Abuse Generators: EIBTI......Page 691 Example: Emulating zip and map with Iteration Tools......Page 694 Comprehension Syntax Summary......Page 699 Scopes and Comprehension Variables......Page 700 Comprehending Set and Dictionary Comprehensions......Page 701 Extended Comprehension Syntax for Sets and Dictionaries......Page 702 Test Your Knowledge: Quiz......Page 703 Test Your Knowledge: Answers......Page 704 Timing Iteration Alternatives......Page 705 Timing Module: Homegrown......Page 706 Timing Script......Page 710 Timing Results......Page 712 Timing Module Alternatives......Page 715 Other Suggestions......Page 718 Basic timeit Usage......Page 719 Benchmark Module and Script: timeit......Page 724 Benchmark Script Results......Page 726 More Fun with Benchmarks......Page 728 Other Benchmarking Topics: pystones......Page 732 Local Names Are Detected Statically......Page 733 Defaults and Mutable Objects......Page 735 Miscellaneous Function Gotchas......Page 737 Test Your Knowledge: Quiz......Page 738 Test Your Knowledge: Part IV Exercises......Page 739 Part V. Modules and Packages......Page 743 Chapter 22. Modules: The Big Picture......Page 745 Why Use Modules?......Page 746 Imports and Attributes......Page 747 Standard Library Modules......Page 749 1. Find It......Page 750 2. Compile It (Maybe)......Page 751 Byte Code Files: __pycache__ in Python 3.2+......Page 752 Byte Code File Models in Action......Page 753 The Module Search Path......Page 755 Search Path Variations......Page 757 The sys.path List......Page 758 Module File Selection......Page 759 Chapter Summary......Page 761 Test Your Knowledge: Answers......Page 762 Module Creation......Page 765 Module Usage......Page 766 The from * Statement......Page 767 Imports Happen Only Once......Page 768 import and from Are Assignments......Page 769 import and from Equivalence......Page 770 Potential Pitfalls of the from Statement......Page 771 Module Namespaces......Page 772 Files Generate Namespaces......Page 773 Namespace Dictionaries: __dict__......Page 774 Attribute Name Qualification......Page 775 Imports Versus Scopes......Page 776 Namespace Nesting......Page 777 Reloading Modules......Page 778 reload Basics......Page 779 reload Example......Page 780 Test Your Knowledge: Answers......Page 782 Chapter 24. Module Packages......Page 785 Packages and Search Path Settings......Page 786 Package __init__.py Files......Page 787 Package Import Example......Page 789 Why Use Package Imports?......Page 791 A Tale of Three Systems......Page 792 Package Relative Imports......Page 795 Changes in Python 3.X......Page 796 Relative Import Basics......Page 797 Why Relative Imports?......Page 798 The Scope of Relative Imports......Page 800 Module Lookup Rules Summary......Page 801 Relative Imports in Action......Page 802 Pitfalls of Package-Relative Imports: Mixed Use......Page 807 Python 3.3 Namespace Packages......Page 813 Namespace Package Semantics......Page 814 Impacts on Regular Packages: Optional __init__.py......Page 815 Namespace Packages in Action......Page 816 Namespace Package Nesting......Page 817 Files Still Have Precedence over Directories......Page 818 Chapter Summary......Page 820 Test Your Knowledge: Answers......Page 821 Module Design Concepts......Page 823 Minimizing from * Damage: _X and __all__......Page 825 Enabling Future Language Features: __future__......Page 826 Mixed Usage Modes: __name__ and __main__......Page 827 Unit Tests with __name__......Page 828 Example: Dual Mode Code......Page 829 Currency Symbols: Unicode in Action......Page 832 Docstrings: Module Documentation at Work......Page 834 Changing the Module Search Path......Page 835 The as Extension for import and from......Page 836 Example: Modules Are Objects......Page 837 Running Code Strings......Page 840 Example: Transitive Module Reloads......Page 841 A Recursive Reloader......Page 842 Alternative Codings......Page 845 Module Name Clashes: Package and Package-Relative Imports......Page 849 Statement Order Matters in Top-Level Code......Page 850 from * Can Obscure the Meaning of Variables......Page 851 reload May Not Impact from Imports......Page 852 reload, from, and Interactive Testing......Page 853 Recursive from Imports May Not Work......Page 854 Test Your Knowledge: Quiz......Page 855 Test Your Knowledge: Part V Exercises......Page 856 Part VI. Classes and OOP......Page 859 Chapter 26. OOP: The Big Picture......Page 861 Why Use Classes?......Page 862 Attribute Inheritance Search......Page 863 Method Calls......Page 866 Coding Class Trees......Page 867 Operator Overloading......Page 869 OOP Is About Code Reuse......Page 870 Chapter Summary......Page 873 Test Your Knowledge: Answers......Page 874 Classes Generate Multiple Instance Objects......Page 877 Instance Objects Are Concrete Items......Page 878 A First Example......Page 879 Classes Are Customized by Inheritance......Page 881 A Second Example......Page 883 Classes Are Attributes in Modules......Page 884 Classes Can Intercept Python Operators......Page 885 A Third Example......Page 887 The World’s Simplest Python Class......Page 889 Records Revisited: Classes Versus Dictionaries......Page 893 Test Your Knowledge: Quiz......Page 895 Test Your Knowledge: Answers......Page 896 Chapter 28. A More Realistic Example......Page 899 Coding Constructors......Page 900 Testing As You Go......Page 901 Using Code Two Ways......Page 903 Step 2: Adding Behavior Methods......Page 904 Coding Methods......Page 906 Providing Print Displays......Page 908 Step 4: Customizing Behavior by Subclassing......Page 910 Augmenting Methods: The Bad Way......Page 911 Augmenting Methods: The Good Way......Page 912 Polymorphism in Action......Page 914 Inherit, Customize, and Extend......Page 915 OOP: The Big Idea......Page 916 Step 5: Customizing Constructors, Too......Page 917 OOP Is Simpler Than You May Think......Page 918 Other Ways to Combine Classes......Page 919 Step 6: Using Introspection Tools......Page 922 Special Class Attributes......Page 923 A Generic Display Tool......Page 925 Instance Versus Class Attributes......Page 926 Name Considerations in Tool Classes......Page 927 Our Classes’ Final Form......Page 928 Pickles and Shelves......Page 930 Storing Objects on a Shelve Database......Page 931 Exploring Shelves Interactively......Page 932 Updating Objects on a Shelve......Page 934 Future Directions......Page 936 Test Your Knowledge: Quiz......Page 938 Test Your Knowledge: Answers......Page 939 The class Statement......Page 941 Example......Page 942 Methods......Page 944 Method Example......Page 945 Calling Superclass Constructors......Page 946 Inheritance......Page 947 Attribute Tree Construction......Page 948 Specializing Inherited Methods......Page 949 Class Interface Techniques......Page 950 Abstract Superclasses......Page 951 Simple Names: Global Unless Assigned......Page 954 The “Zen” of Namespaces: Assignments Classify Names......Page 955 Nested Classes: The LEGB Scopes Rule Revisited......Page 958 Namespace Dictionaries: Review......Page 960 Namespace Links: A Tree Climber......Page 963 Documentation Strings Revisited......Page 965 Classes Versus Modules......Page 966 Test Your Knowledge: Quiz......Page 967 Test Your Knowledge: Answers......Page 968 The Basics......Page 969 Constructors and Expressions: __init__ and __sub__......Page 970 Common Operator Overloading Methods......Page 971 Intercepting Slices......Page 973 Slicing and Indexing in Python 2.X......Page 975 But 3.X’s __index__ Is Not Indexing!......Page 976 Index Iteration: __getitem__......Page 977 User-Defined Iterables......Page 978 Multiple Iterators on One Object......Page 981 Coding Alternative: __iter__ plus yield......Page 984 Membership: __contains__, __iter__, and __getitem__......Page 989 Attribute Reference......Page 992 Attribute Assignment and Deletion......Page 993 Other Attribute Management Tools......Page 994 Emulating Privacy for Instance Attributes: Part 1......Page 995 String Representation: __repr__ and __str__......Page 996 Why Two Display Methods?......Page 997 Display Usage Notes......Page 998 Right-Side Addition......Page 1000 In-Place Addition......Page 1003 Call Expressions: __call__......Page 1004 Function Interfaces and Callback-Based Code......Page 1006 Comparisons: __lt__, __gt__, and Others......Page 1008 The __cmp__ Method in Python 2.X......Page 1009 Boolean Tests: __bool__ and __len__......Page 1010 Boolean Methods in Python 2.X......Page 1011 Object Destruction: __del__......Page 1012 Destructor Usage Notes......Page 1013 Test Your Knowledge: Quiz......Page 1014 Test Your Knowledge: Answers......Page 1015 Python and OOP......Page 1017 Polymorphism Means Interfaces, Not Call Signatures......Page 1018 OOP and Inheritance: “Is-a” Relationships......Page 1019 OOP and Composition: “Has-a” Relationships......Page 1021 Stream Processors Revisited......Page 1023 OOP and Delegation: “Wrapper” Proxy Objects......Page 1026 Pseudoprivate Class Attributes......Page 1028 Name Mangling Overview......Page 1029 Why Use Pseudoprivate Attributes?......Page 1030 Methods Are Objects: Bound or Unbound......Page 1032 Unbound Methods Are Functions in 3.X......Page 1034 Bound Methods and Other Callable Objects......Page 1036 Classes Are Objects: Generic Object Factories......Page 1039 Why Factories?......Page 1041 Multiple Inheritance: “Mix-in” Classes......Page 1042 Coding Mix-in Display Classes......Page 1043 Chapter Summary......Page 1064 Test Your Knowledge: Answers......Page 1065 Chapter 32. Advanced Class Topics......Page 1067 Extending Types by Embedding......Page 1068 Extending Types by Subclassing......Page 1069 The “New Style” Class Model......Page 1072 Just How New Is New-Style?......Page 1073 New-Style Class Changes......Page 1074 Attribute Fetch for Built-ins Skips Instances......Page 1075 Type Model Changes......Page 1081 All Classes Derive from “object”......Page 1084 Diamond Inheritance Change......Page 1086 More on the MRO: Method Resolution Order......Page 1090 Example: Mapping Attributes to Inheritance Sources......Page 1093 Slots: Attribute Declarations......Page 1099 Properties: Attribute Accessors......Page 1109 __getattribute__ and Descriptors: Attribute Tools......Page 1112 Static and Class Methods......Page 1113 Static Methods in 2.X and 3.X......Page 1114 Static Method Alternatives......Page 1116 Using Static and Class Methods......Page 1118 Counting Instances with Static Methods......Page 1120 Counting Instances with Class Methods......Page 1121 Decorators and Metaclasses: Part 1......Page 1123 Function Decorator Basics......Page 1124 A First Look at User-Defined Function Decorators......Page 1126 A First Look at Class Decorators and Metaclasses......Page 1128 The super Built-in Function: For Better or Worse?......Page 1130 The Great super Debate......Page 1131 Traditional Superclass Call Form: Portable, General......Page 1132 Basic super Usage and Its Tradeoffs......Page 1133 The super Upsides: Tree Changes and Dispatch......Page 1138 Runtime Class Changes and super......Page 1139 Cooperative Multiple Inheritance Method Dispatch......Page 1140 The super Summary......Page 1152 Changing Class Attributes Can Have Side Effects......Page 1154 Changing Mutable Class Attributes Can Have Side Effects, Too......Page 1156 Multiple Inheritance: Order Matters......Page 1157 Scopes in Methods and Classes......Page 1158 Miscellaneous Class Gotchas......Page 1159 KISS Revisited: “Overwrapping-itis”......Page 1160 Test Your Knowledge: Answers......Page 1161 Test Your Knowledge: Part VI Exercises......Page 1163 Part VII. Exceptions and Tools......Page 1171 Why Use Exceptions?......Page 1173 Exception Roles......Page 1174 Default Exception Handler......Page 1175 Catching Exceptions......Page 1177 User-Defined Exceptions......Page 1178 Termination Actions......Page 1179 Test Your Knowledge: Quiz......Page 1182 Test Your Knowledge: Answers......Page 1183 The try/except/else Statement......Page 1185 How try Statements Work......Page 1186 try Statement Clauses......Page 1187 The try else Clause......Page 1190 Example: Default Behavior......Page 1191 Example: Catching Built-in Exceptions......Page 1192 The try/finally Statement......Page 1193 Example: Coding Termination Actions with try/finally......Page 1194 Unified try/except/finally......Page 1195 Unified try Statement Syntax......Page 1196 Unified try Example......Page 1197 The raise Statement......Page 1199 Raising Exceptions......Page 1200 Scopes and try except Variables......Page 1201 Python 3.X Exception Chaining: raise from......Page 1203 The assert Statement......Page 1205 with/as Context Managers......Page 1206 Basic Usage......Page 1207 The Context Management Protocol......Page 1209 Multiple Context Managers in 3.1, 2.7, and Later......Page 1210 Chapter Summary......Page 1212 Test Your Knowledge: Answers......Page 1213 Chapter 35. Exception Objects......Page 1215 String Exceptions Are Right Out!......Page 1216 Class-Based Exceptions......Page 1217 Coding Exceptions Classes......Page 1218 Why Exception Hierarchies?......Page 1220 Built-in Exception Classes......Page 1223 Built-in Exception Categories......Page 1224 Default Printing and State......Page 1225 Custom Print Displays......Page 1227 Custom Data and Behavior......Page 1228 Providing Exception Details......Page 1229 Providing Exception Methods......Page 1230 Test Your Knowledge: Quiz......Page 1231 Test Your Knowledge: Answers......Page 1232 Nesting Exception Handlers......Page 1233 Example: Syntactic Nesting......Page 1235 Breaking Out of Multiple Nested Loops: “go to”......Page 1237 Exceptions Aren’t Always Errors......Page 1238 Functions Can Signal Conditions with raise......Page 1239 Closing Files and Server Connections......Page 1240 Debugging with Outer try Statements......Page 1241 More on sys.exc_info......Page 1242 Displaying Errors and Tracebacks......Page 1243 What Should Be Wrapped......Page 1244 Catching Too Much: Avoid Empty except and Exception......Page 1245 Catching Too Little: Use Class-Based Categories......Page 1247 The Python Toolset......Page 1248 Development Tools for Larger Projects......Page 1249 Chapter Summary......Page 1253 Test Your Knowledge: Part VII Exercises......Page 1254 Part VIII. Advanced Topics......Page 1257 Chapter 37. Unicode and Byte Strings......Page 1259 String Changes in 3.X......Page 1260 Character Encoding Schemes......Page 1261 How Python Stores Strings in Memory......Page 1264 Python’s String Types......Page 1265 Text and Binary Files......Page 1267 Python 3.X String Literals......Page 1269 Python 2.X String Literals......Page 1270 String Type Conversions......Page 1271 Coding ASCII Text......Page 1273 Coding Non-ASCII Text......Page 1274 Encoding and Decoding Non-ASCII te Get a comprehensive, in-depth introduction to the core Python language with this hands-on book. Based on author Mark Lutz’s popular training course, this updated fifth edition will help you quickly write efficient, high-quality code with Python. It’s an ideal way to begin, whether you’re new to programming or a professional developer versed in other languages.Complete with quizzes, exercises, and helpful illustrations, this easy-to-follow, self-paced tutorial gets you started with both Python 2.7 and 3.3— the latest releases in the 3.X and 2.X lines—plus all other releases in common use today. You’ll also learn some advanced language features that recently have become more common in Python code.Explore Python’s major built-in object types such as numbers, lists, and dictionariesCreate and process objects with Python statements, and learn Python’s general syntax modelUse functions to avoid code redundancy and package code for reuseOrganize statements, functions, and other tools into larger components with modulesDive into classes: Python’s object-oriented programming tool for structuring codeWrite large programs with Python’s exception-handling model and development toolsLearn advanced Python tools, including decorators, descriptors, metaclasses, and Unicode processing Get a comprehensive, in-depth introduction to the core Python language with this hands-on book. Based on author Mark Lutz's popular training course, this updated fifth edition will help you quickly write efficient, high-quality code with Python. It's an ideal way to begin, whether you're new to programming or a professional developer versed in other languages. Complete with quizzes, exercises, and helpful illustrations, this easy-to-follow, self-paced tutorial gets you started with both Python 2.7 and 3.3-- the latest releases in the 3.X and 2.X lines--plus all other releases in common use today. You'll also learn some advanced language features that recently have become more common in Python code. Explore Python's major built-in object types such as numbers, lists, and dictionaries Create and process objects with Python statements, and learn Python's general syntax model Use functions to avoid code redundancy and package code for reuse Organize statements, functions, and other tools into larger components with modules Dive into classes: Python's object-oriented programming tool for structuring code Write large programs with Python's exception-handling model and development tools Learn advanced Python tools, including decorators, descriptors, metaclasses, and Unicode processing
دانلود کتاب Learning Python, 5th Edition