وبلاگ بلیان

Learning Python: Powerful Object-Oriented Programming

معرفی کتاب «Learning Python: Powerful Object-Oriented Programming» نوشتهٔ Lutz, Mark، منتشرشده توسط نشر O'Reilly Media در سال 2009. این کتاب در فرمت pdf، زبان انگلیسی ارائه شده است. «Learning Python: Powerful Object-Oriented Programming» در دستهٔ بدون دسته‌بندی قرار دارد.

Google and YouTube use Python because it's highly adaptable, easy to maintain, and allows for rapid development. If you want to write high-quality, efficient code that's easily integrated with other languages and tools, this hands-on book will help you be productive with Python quickly -- whether you're new to programming or just new to Python. It's an easy-to-follow self-paced tutorial, based on author and Python expert Mark Lutz's popular training course. Each chapter contains a stand-alone lesson on a key component of the language, and includes a unique **Test Your Knowledge section with practical exercises and quizzes, so you can practice new skills and test your understanding as you go. You'll find lots of annotated examples and illustrations to help you get started with Python 3.0.** * Learn about Python's major built-in object types, such as numbers, lists, and dictionaries * Create and process objects using Python statements, and learn Python's general syntax model * Structure and reuse code using functions, Python's basic procedural tool * Learn about Python modules: packages of statements, functions, and other tools, organized into larger components * Discover Python's object-oriented programming tool for structuring code * Learn about the exception-handling model, and development tools for writing larger programs * Explore advanced Python tools including decorators, descriptors, metaclasses, and Unicode processing Table of Contents......Page 9 Preface......Page 33 Coverage for Both 3.0 and 2.6......Page 34 New Chapters......Page 35 Changes to Existing Material......Page 36 Specific Language Extensions in 2.6 and 3.0......Page 37 Specific Language Removals in 3.0......Page 38 The Third Edition’s Python Language Changes......Page 40 The Third Edition’s Python Training Changes......Page 41 The Third Edition’s Scope Changes......Page 42 This Book’s Scope and Other Books......Page 43 This Book’s Style and Structure......Page 45 Using Code Examples......Page 47 Font Conventions......Page 48 How to Contact Us......Page 49 Acknowledgments......Page 50 Part I. Getting Started......Page 53 Why Do People Use Python?......Page 55 Software Quality......Page 56 Is Python a “Scripting Language”?......Page 57 Who Uses Python Today?......Page 59 GUIs......Page 61 Component Integration......Page 62 Numeric and Scientific Programming......Page 63 How Is Python Supported?......Page 64 It’s Free......Page 65 It’s Portable......Page 66 It’s Powerful......Page 67 It’s Easy to Use......Page 68 How Does Python Stack Up to Language X?......Page 69 Chapter Summary......Page 70 Test Your Knowledge: Answers......Page 71 Introducing the Python Interpreter......Page 75 The Programmer’s View......Page 76 Byte code compilation......Page 78 Performance implications......Page 79 Development implications......Page 80 Jython......Page 81 The Psyco just-in-time compiler......Page 82 The Shedskin C++ translator......Page 83 Frozen Binaries......Page 84 Future Possibilities?......Page 85 Test Your Knowledge: Answers......Page 86 The Interactive Prompt......Page 87 Running Code Interactively......Page 89 Experimenting......Page 90 Using the Interactive Prompt......Page 91 System Command Lines and Files......Page 93 A First Script......Page 94 Running Files with Command Lines......Page 95 Using Command Lines and Files......Page 96 Unix Executable Scripts (#!)......Page 98 Clicking Icons on Windows......Page 99 The input Trick......Page 101 Other Icon-Click Limitations......Page 102 Module Imports and Reloads......Page 103 The Grander Module Story: Attributes......Page 105 Modules and namespaces......Page 107 import and reload Usage Notes......Page 108 Using exec to Run Module Files......Page 109 IDLE Basics......Page 110 Using IDLE......Page 112 Advanced IDLE Tools......Page 114 Other IDEs......Page 115 Embedding Calls......Page 116 Text Editor Launch Options......Page 117 Which Option Should I Use?......Page 118 Test Your Knowledge: Quiz......Page 120 Test Your Knowledge: Answers......Page 121 Test Your Knowledge: Part I Exercises......Page 122 Part II. Types and Operations......Page 125 Chapter 4. Introducing Python Object Types......Page 127 Why Use Built-in Types?......Page 128 Python’s Core Data Types......Page 129 Numbers......Page 130 Sequence Operations......Page 132 Type-Specific Methods......Page 134 Getting Help......Page 136 Pattern Matching......Page 137 Sequence Operations......Page 138 Bounds Checking......Page 139 Comprehensions......Page 140 Mapping Operations......Page 142 Nesting Revisited......Page 143 Sorting Keys: for Loops......Page 145 Iteration and Optimization......Page 146 Missing Keys: if Tests......Page 147 Tuples......Page 148 Files......Page 149 Other Core Types......Page 151 How to Break Your Code’s Flexibility......Page 152 User-Defined Classes......Page 153 And Everything Else......Page 154 Test Your Knowledge: Quiz......Page 155 Test Your Knowledge: Answers......Page 156 Numeric Type Basics......Page 157 Numeric Literals......Page 158 Python Expression Operators......Page 160 Mixed types are converted up......Page 163 Preview: Operator overloading and polymorphism......Page 164 Variables and Basic Expressions......Page 165 Numeric Display Formats......Page 167 Comparisons: Normal and Chained......Page 168 Division: Classic, Floor, and True......Page 169 Floor versus truncation......Page 171 Why does truncation matter?......Page 172 Integer Precision......Page 173 Hexadecimal, Octal, and Binary Notation......Page 174 Bitwise Operations......Page 176 Other Built-in Numeric Tools......Page 177 Decimal Type......Page 179 Setting precision globally......Page 180 The basics......Page 181 Numeric accuracy......Page 182 Conversions and mixed types......Page 183 Set basics in Python 2.6......Page 185 Set literals in Python 3.0......Page 187 Immutable constraints and frozen sets......Page 188 Set comprehensions in Python 3.0......Page 189 Why sets?......Page 190 Booleans......Page 191 Numeric Extensions......Page 192 Test Your Knowledge: Answers......Page 193 The Case of the Missing Declaration Statements......Page 195 Variables, Objects, and References......Page 196 Types Live with Objects, Not Variables......Page 197 Objects Are Garbage-Collected......Page 198 Shared References......Page 200 Shared References and In-Place Changes......Page 201 Shared References and Equality......Page 203 Dynamic Typing Is Everywhere......Page 204 Test Your Knowledge: Quiz......Page 205 Test Your Knowledge: Answers......Page 206 Chapter 7. Strings......Page 207 String Literals......Page 209 Escape Sequences Represent Special Bytes......Page 210 Raw Strings Suppress Escapes......Page 213 Triple Quotes Code Multiline Block Strings......Page 214 Strings in Action......Page 215 Basic Operations......Page 216 Indexing and Slicing......Page 217 Extended slicing: the third limit and slice objects......Page 219 String Conversion Tools......Page 221 Character code conversions......Page 222 Changing Strings......Page 223 String Methods......Page 224 String Method Examples: Changing Strings......Page 226 String Method Examples: Parsing Text......Page 228 Other Common String Methods in Action......Page 229 The Original string Module (Gone in 3.0)......Page 230 String Formatting Expressions......Page 231 Advanced String Formatting Expressions......Page 233 Dictionary-Based String Formatting Expressions......Page 234 String Formatting Method Calls......Page 235 Adding Keys, Attributes, and Offsets......Page 236 Adding Specific Formatting......Page 237 Comparison to the % Formatting Expression......Page 239 Why the New Format Method?......Page 242 Explicit value references......Page 243 Method names and general arguments......Page 244 General Type Categories......Page 245 Mutable Types Can Be Changed In-Place......Page 246 Test Your Knowledge: Quiz......Page 247 Test Your Knowledge: Answers......Page 248 Lists......Page 249 List Iteration and Comprehensions......Page 252 Indexing, Slicing, and Matrixes......Page 253 Index and slice assignments......Page 254 List method calls......Page 255 Other common list operations......Page 258 Dictionaries......Page 259 Basic Dictionary Operations......Page 261 Changing Dictionaries In-Place......Page 262 More Dictionary Methods......Page 263 A Languages Table......Page 264 Using dictionaries to simulate flexible lists......Page 265 Avoiding missing-key errors......Page 266 Using dictionaries as “records”......Page 267 Other Ways to Make Dictionaries......Page 268 Dictionary Changes in Python 3.0......Page 269 Dictionary comprehensions......Page 270 Dictionary views......Page 271 Dictionary views and sets......Page 273 Dictionary magnitude comparisons no longer work......Page 274 Chapter Summary......Page 275 Test Your Knowledge: Answers......Page 276 Tuples......Page 277 Tuple syntax peculiarities: Commas and parentheses......Page 279 Conversions, methods, and immutability......Page 280 Files......Page 281 Opening Files......Page 282 Using Files......Page 283 Files in Action......Page 284 Text and binary files in Python 3.0......Page 285 Storing and parsing Python objects in files......Page 286 Storing native Python objects with pickle......Page 288 Storing and parsing packed binary data in files......Page 289 Other File Tools......Page 290 Type Categories Revisited......Page 291 References Versus Copies......Page 293 Comparisons, Equality, and Truth......Page 296 The Meaning of True and False in Python......Page 298 The None object......Page 299 Python’s Type Hierarchies......Page 300 Other Types in Python......Page 302 Assignment Creates References, Not Copies......Page 303 Beware of Cyclic Data Structures......Page 304 Chapter Summary......Page 305 Test Your Knowledge: Answers......Page 306 Test Your Knowledge: Part II Exercises......Page 307 Part III. Statements and Syntax......Page 311 Python Program Structure Revisited......Page 313 Python’s Statements......Page 314 What Python Adds......Page 316 End of line is end of statement......Page 317 Why Indentation Syntax?......Page 318 Statement rule special cases......Page 321 Block rule special case......Page 322 A Simple Interactive Loop......Page 323 Doing Math on User Inputs......Page 324 Handling Errors by Testing Inputs......Page 325 Handling Errors with try Statements......Page 326 Nesting Code Three Levels Deep......Page 327 Test Your Knowledge: Quiz......Page 328 Test Your Knowledge: Answers......Page 329 Assignment Statements......Page 331 Assignment Statement Forms......Page 332 Sequence Assignments......Page 333 Advanced sequence assignment patterns......Page 334 Extended unpacking in action......Page 336 Boundary cases......Page 338 A useful convenience......Page 339 Multiple-target assignment and shared references......Page 340 Augmented Assignments......Page 341 Augmented assignment and shared references......Page 343 Variable Name Rules......Page 344 Names have no type, but objects do......Page 346 Expression Statements......Page 347 Expression Statements and In-Place Changes......Page 348 Print Operations......Page 349 Call format......Page 350 The 3.0 print function in action......Page 351 The Python 2.6 print Statement......Page 352 The 2.6 print statement in action......Page 353 The Python “hello world” program......Page 354 Manual stream redirection......Page 355 Automatic stream redirection......Page 356 Version-Neutral Printing......Page 358 Test Your Knowledge: Answers......Page 360 General Format......Page 363 Multiway Branching......Page 364 Python Syntax Rules......Page 366 Block Delimiters: Indentation Rules......Page 367 Statement Delimiters: Lines and Continuations......Page 369 A Few Special Cases......Page 370 Truth Tests......Page 372 The if/else Ternary Expression......Page 373 Test Your Knowledge: Answers......Page 376 while Loops......Page 379 Examples......Page 380 General Loop Format......Page 381 pass......Page 382 break......Page 383 Loop else......Page 384 More on the loop else......Page 385 General Format......Page 386 Basic usage......Page 387 Tuple assignment in for loops......Page 388 Python 3.0 extended sequence assignment in for loops......Page 390 Nested for loops......Page 391 Loop Coding Techniques......Page 393 Counter Loops: while and range......Page 394 Nonexhaustive Traversals: range and Slices......Page 395 Changing Lists: range......Page 396 Parallel Traversals: zip and map......Page 397 map equivalence in Python 2.6......Page 398 Dictionary construction with zip......Page 399 Generating Both Offsets and Items: enumerate......Page 400 Test Your Knowledge: Quiz......Page 401 Test Your Knowledge: Answers......Page 402 Iterators: A First Look......Page 403 The Iteration Protocol: File Iterators......Page 404 Manual Iteration: iter and next......Page 406 Other Built-in Type Iterators......Page 408 List Comprehensions: A First Look......Page 410 Using List Comprehensions on Files......Page 411 Extended List Comprehension Syntax......Page 413 Other Iteration Contexts......Page 414 New Iterables in Python 3.0......Page 418 The range Iterator......Page 419 The map, zip, and filter Iterators......Page 420 Multiple Versus Single Iterators......Page 421 Dictionary View Iterators......Page 422 Test Your Knowledge: Quiz......Page 424 Test Your Knowledge: Answers......Page 425 Python Documentation Sources......Page 427 The dir Function......Page 428 Docstrings: __doc__......Page 429 User-defined docstrings......Page 430 Built-in docstrings......Page 431 PyDoc: The help Function......Page 432 PyDoc: HTML Reports......Page 435 The Standard Manual Set......Page 438 Common Coding Gotchas......Page 439 Test Your Knowledge: Quiz......Page 441 Test Your Knowledge: Part III Exercises......Page 442 Part IV. Functions......Page 445 Chapter 16. Function Basics......Page 447 Coding Functions......Page 448 def Statements......Page 450 def Executes at Runtime......Page 451 Calls......Page 452 Polymorphism in Python......Page 453 Definition......Page 454 Polymorphism Revisited......Page 455 Chapter Summary......Page 456 Test Your Knowledge: Answers......Page 457 Python Scope Basics......Page 459 Scope Rules......Page 460 Name Resolution: The LEGB Rule......Page 462 Scope Example......Page 463 The Built-in Scope......Page 464 The global Statement......Page 466 Minimize Global Variables......Page 467 Minimize Cross-File Changes......Page 468 Other Ways to Access Globals......Page 470 Nested Scope Examples......Page 471 Factory functions......Page 472 Retaining enclosing scopes’ state with defaults......Page 473 Nested scopes and lambdas......Page 474 Scopes versus defaults with loop variables......Page 475 Arbitrary scope nesting......Page 476 nonlocal Basics......Page 477 nonlocal in Action......Page 478 Using nonlocal for changes......Page 479 Boundary cases......Page 480 Shared state with globals......Page 481 State with classes (preview)......Page 482 State with function attributes......Page 483 Chapter Summary......Page 484 Test Your Knowledge: Quiz......Page 485 Test Your Knowledge: Answers......Page 486 Argument-Passing Basics......Page 487 Arguments and Shared References......Page 488 Avoiding Mutable Argument Changes......Page 490 Simulating Output Parameters......Page 491 Special Argument-Matching Modes......Page 492 The Basics......Page 493 Matching Syntax......Page 494 The Gritty Details......Page 495 Keywords......Page 496 Defaults......Page 497 Collecting arguments......Page 498 Unpacking arguments......Page 499 Applying functions generically......Page 500 The defunct apply built-in (Python 2.6)......Page 501 Python 3.0 Keyword-Only Arguments......Page 502 Ordering rules......Page 504 The min Wakeup Call!......Page 505 Full Credit......Page 506 Bonus Points......Page 507 Generalized Set Functions......Page 508 Emulating the Python 3.0 print Function......Page 509 Using Keyword-Only Arguments......Page 511 Chapter Summary......Page 512 Test Your Knowledge: Quiz......Page 513 Test Your Knowledge: Answers......Page 514 Function Design Concepts......Page 515 Summation with Recursion......Page 517 Coding Alternatives......Page 518 Loop Statements Versus Recursion......Page 519 Handling Arbitrary Structures......Page 520 Indirect Function Calls......Page 521 Function Introspection......Page 522 Function Attributes......Page 523 Function Annotations in 3.0......Page 524 lambda Basics......Page 526 Why Use lambda?......Page 527 How (Not) to Obfuscate Your Python Code......Page 529 Nested lambdas and Scopes......Page 530 Mapping Functions over Sequences: map......Page 531 Functional Programming Tools: filter and reduce......Page 533 Test Your Knowledge: Answers......Page 535 List Comprehensions Revisited: Functional Tools......Page 537 List Comprehensions Versus map......Page 538 Adding Tests and Nested Loops: filter......Page 539 List Comprehensions and Matrixes......Page 541 Comprehending List Comprehensions......Page 542 Generator Functions: yield Versus return......Page 544 Iteration protocol integration......Page 545 Generator functions in action......Page 546 Extended generator function protocol: send versus next......Page 548 Generator Expressions: Iterators Meet Comprehensions......Page 549 Generator Functions Versus Generator Expressions......Page 550 Generators Are Single-Iterator Objects......Page 551 Emulating zip and map with Iteration Tools......Page 552 Coding your own map(func, ...)......Page 553 Coding your own zip(...) and map(None, ...)......Page 554 Value Generation in Built-in Types and Classes......Page 558 Comprehending Set and Dictionary Comprehensions......Page 559 Extended Comprehension Syntax for Sets and Dictionaries......Page 560 Timing Module......Page 561 Timing Script......Page 562 Timing Results......Page 563 Timing Module Alternatives......Page 565 Using keyword-only arguments in 3.0......Page 568 Other Suggestions......Page 569 Local Names Are Detected Statically......Page 570 Defaults and Mutable Objects......Page 572 Chapter Summary......Page 574 Test Your Knowledge: Answers......Page 575 Test Your Knowledge: Part IV Exercises......Page 576 Part V. Modules......Page 579 Why Use Modules?......Page 581 Python Program Architecture......Page 582 Imports and Attributes......Page 583 How Imports Work......Page 585 2. Compile It (Maybe)......Page 586 The Module Search Path......Page 587 Configuring the Search Path......Page 589 The sys.path List......Page 590 Module File Selection......Page 591 Advanced Module Selection Concepts......Page 592 Test Your Knowledge: Quiz......Page 593 Test Your Knowledge: Answers......Page 594 Module Creation......Page 595 The import Statement......Page 596 The from * Statement......Page 597 import and from Are Assignments......Page 598 Cross-File Name Changes......Page 599 Potential Pitfalls of the from Statement......Page 600 When import is required......Page 601 Files Generate Namespaces......Page 602 Imports Versus Scopes......Page 604 Namespace Nesting......Page 605 Reloading Modules......Page 606 reload Basics......Page 607 reload Example......Page 608 Test Your Knowledge: Answers......Page 610 Package Import Basics......Page 613 Packages and Search Path Settings......Page 614 Package __init__.py Files......Page 615 Package Import Example......Page 616 Why Use Package Imports?......Page 618 A Tale of Three Systems......Page 619 Package Relative Imports......Page 621 Relative Import Basics......Page 622 The relative imports solution in 3.0......Page 624 Relative imports versus absolute package paths......Page 625 The Scope of Relative Imports......Page 626 Imports outside packages......Page 627 Imports within packages......Page 628 Imports are still relative to the CWD......Page 629 Selecting modules with relative and absolute imports......Page 630 Imports are still relative to the CWD (again)......Page 631 Chapter Summary......Page 633 Test Your Knowledge: Answers......Page 634 Data Hiding in Modules......Page 635 Enabling Future Language Features......Page 636 Mixed Usage Modes: __name__ and __main__......Page 637 Unit Tests with __name__......Page 638 Using Command-Line Arguments with __name__......Page 639 Changing the Module Search Path......Page 642 Modules Are Objects: Metaprograms......Page 643 Importing Modules by Name String......Page 646 Transitive Module Reloads......Page 647 Module Design Concepts......Page 650 Statement Order Matters in Top-Level Code......Page 651 from Copies Names but Doesn’t Link......Page 652 reload May Not Impact from Imports......Page 653 reload, from, and Interactive Testing......Page 654 Recursive from Imports May Not Work......Page 655 Test Your Knowledge: Quiz......Page 656 Test Your Knowledge: Part V Exercises......Page 657 Part VI. Classes and OOP......Page 661 Chapter 25. OOP: The Big Picture......Page 663 Why Use Classes?......Page 664 Attribute Inheritance Search......Page 665 Classes and Instances......Page 667 Coding Class Trees......Page 668 OOP Is About Code Reuse......Page 671 Test Your Knowledge: Answers......Page 674 Classes Generate Multiple Instance Objects......Page 677 Instance Objects Are Concrete Items......Page 678 A First Example......Page 679 Classes Are Customized by Inheritance......Page 681 A Second Example......Page 682 Classes Are Attributes in Modules......Page 683 Classes Can Intercept Python Operators......Page 685 A Third Example......Page 686 The World’s Simplest Python Class......Page 688 Classes Versus Dictionaries......Page 691 Test Your Knowledge: Answers......Page 693 Chapter 27. A More Realistic Example......Page 695 Coding Constructors......Page 696 Testing As You Go......Page 697 Using Code Two Ways......Page 698 Step 2: Adding Behavior Methods......Page 700 Coding Methods......Page 701 Step 3: Operator Overloading......Page 703 Providing Print Displays......Page 704 Coding Subclasses......Page 705 Augmenting Methods: The Good Way......Page 706 Polymorphism in Action......Page 708 Inherit, Customize, and Extend......Page 709 Step 5: Customizing Constructors, Too......Page 710 Other Ways to Combine Classes......Page 712 Step 6: Using Introspection Tools......Page 715 Special Class Attributes......Page 716 A Generic Display Tool......Page 717 Instance Versus Class Attributes......Page 718 Name Considerations in Tool Classes......Page 719 Our Classes’ Final Form......Page 720 Step 7 (Final): Storing Objects in a Database......Page 721 Pickles and Shelves......Page 722 Storing Objects on a Shelve Database......Page 723 Exploring Shelves Interactively......Page 724 Updating Objects on a Shelve......Page 726 Future Directions......Page 727 Test Your Knowledge: Quiz......Page 729 Test Your Knowledge: Answers......Page 730 General Form......Page 733 Example......Page 734 Methods......Page 736 Method Example......Page 737 Other Method Call Possibilities......Page 738 Specializing Inherited Methods......Page 739 Class Interface Techniques......Page 741 Abstract Superclasses......Page 742 Python 2.6 and 3.0 Abstract Superclasses......Page 744 Attribute Names: Object Namespaces......Page 745 The “Zen” of Python Namespaces: Assignments Classify Names......Page 746 Namespace Dictionaries......Page 748 Namespace Links......Page 751 Documentation Strings Revisited......Page 753 Test Your Knowledge: Quiz......Page 755 Test Your Knowledge: Answers......Page 756 The Basics......Page 757 Common Operator Overloading Methods......Page 758 Intercepting Slices......Page 760 Index Iteration: __getitem__......Page 762 Iterator Objects: __iter__ and __next__......Page 763 User-Defined Iterators......Page 764 Multiple Iterators on One Object......Page 766 Membership: __contains__, __iter__, and __getitem__......Page 768 Attribute Reference: __getattr__ and __setattr__......Page 770 Other Attribute Management Tools......Page 771 Emulating Privacy for Instance Attributes: Part 1......Page 772 String Representation: __repr__ and __str__......Page 773 Right-Side and In-Place Addition: __radd__ and __iadd__......Page 775 Call Expressions: __call__......Page 777 Function Interfaces and Callback-Based Code......Page 779 Comparisons: __lt__, __gt__, and Others......Page 780 The 2.6 __cmp__ Method (Removed in 3.0)......Page 781 Boolean Tests: __bool__ and __len__......Page 782 Object Destruction: __del__......Page 784 Chapter Summary......Page 785 Test Your Knowledge: Answers......Page 786 Python and OOP......Page 789 Overloading by Call Signatures (or Not)......Page 790 OOP and Inheritance: “Is-a” Relationships......Page 791 OOP and Composition: “Has-a” Relationships......Page 792 Stream Processors Revisited......Page 794 OOP and Delegation: “Wrapper” Objects......Page 797 Pseudoprivate Class Attributes......Page 799 Why Use Pseudoprivate Attributes?......Page 800 Methods Are Objects: Bound or Unbound......Page 802 Unbound Methods are Functions in 3.0......Page 804 Bound Methods and Other Callable Objects......Page 806 Multiple Inheritance: “Mix-in” Classes......Page 808 Coding Mix-in Display Classes......Page 809 Listing instance attributes with __dict__......Page 810 Listing inherited attributes with dir......Page 813 Listing attributes per object in class trees......Page 815 Classes Are Objects: Generic Object Factories......Page 820 Why Factories?......Page 821 Test Your Knowledge: Quiz......Page 822 Test Your Knowledge: Answers......Page 823 Extending Built-in Types......Page 825 Extending Types by Embedding......Page 826 Extending Types by Subclassing......Page 827 The “New-Style” Class Model......Page 829 New-Style Class Changes......Page 830 Type Model Changes......Page 831 Implications for type testing......Page 833 All objects derive from “object”......Page 834 Diamond Inheritance Change......Page 835 Diamond inheritance example......Page 836 Explicit conflict resolution......Page 837 Scope of search order change......Page 839 Slots and generic code......Page 840 Multiple __slot__ lists in superclasses......Page 842 Class Properties......Page 844 Metaclasses......Page 846 Why the Special Methods?......Page 847 Static Methods in 2.6 and 3.0......Page 848 Static Method Alternatives......Page 850 Using Static and Class Methods......Page 851 Counting Instances with Static Methods......Page 852 Counting Instances with Class Methods......Page 854 Counting instances per class with class methods......Page 855 Function Decorator Basics......Page 856 A First Function Decorator Example......Page 857 Class Decorators and Metaclasses......Page 859 Changing Class Attributes Can Have Side Effects......Page 860 Changing Mutable Class Attributes Can Have Side Effects, Too......Page 862 Multiple Inheritance: Order Matters......Page 863 Methods, Classes, and Nested Scopes......Page 864 “Overwrapping-itis”......Page 866 Test Your Knowledge: Answers......Page 867 Test Your Knowledge: Part VI Exercises......Page 868 Part VII. Exceptions and Tools......Page 875 Why Use Exceptions?......Page 877 Exception Roles......Page 878 Default Exception Handler......Page 879 Catching Exceptions......Page 880 Raising Exceptions......Page 881 Termination Actions......Page 882 Test Your Knowledge: Answers......Page 885 The try/except/else Statement......Page 887 try Statement Clauses......Page 889 The try else Clause......Page 891 Example: Default Behavior......Page 892 Example: Catching Built-in Exceptions......Page 893 The try/finally Statement......Page 894 Example: Coding Termination Actions with try/finally......Page 895 Unified try/except/finally......Page 896 Combining finally and except by Nesting......Page 897 Unified try Example......Page 898 The raise Statement......Page 900 Python 3.0 Exception Chaining: raise from......Page 901 The assert Statement......Page 902 with/as Context Managers......Page 903 Basic Usage......Page 904 The Context Management Protocol......Page 905 Chapter Summary......Page 907 Test Your Knowledge: Answers......Page 908 Chapter 34. Exception Objects......Page 909 String Exceptions Are Right Out!......Page 910 Coding Exceptions Classes......Page 911 Why Exception Hierarchies?......Page 913 Built-in Exception Classes......Page 916 Built-in Exception Categories......Page 917 Default Printing and State......Page 918 Custom Print Displays......Page 919 Providing Exception Details......Page 920 Providing Exception Methods......Page 921 Chapter Summary......Page 922 Test Your Knowledge: Answers......Page 923 Nesting Exception Handlers......Page 925 Example: Syntactic Nesting......Page 927 Exceptions Aren’t Always Errors......Page 929 Closing Files and Server Connections......Page 930 Debugging with Outer try Statements......Page 931 Running In-Process Tests......Page 932 More on sys.exc_info......Page 933 What Should Be Wrapped......Page 934 Catching Too Much: Avoid Empty except and Exception......Page 935 Core Language Summary......Page 937 The Python Toolset......Page 938 Development Tools for Larger Projects......Page 939 Chapter Summary......Page 942 Test Your Knowledge: Part VII Exercises......Page 943 Part VIII. Advanced Topics......Page 945 Chapter 36. Unicode and Byte Strings......Page 947 String Changes in 3.0......Page 948 Character Encoding Schemes......Page 949 Python’s String Types......Page 951 Text and Binary Files......Page 952 Literals and Basic Properties......Page 954 Conversions......Page 955 Coding Unicode Strings......Page 956 Coding Non-ASCII Text......Page 957 Encoding and Decoding Non-ASCII text......Page 958 Other Unicode Coding Techniques......Page 959 Converting Encodings......Page 961 Coding Unicode Strings in Python 2.6......Page 962 Source File Character Set Encoding Declarations......Page 964 Method Calls......Page 965 Sequence Operations......Page 966 Other Ways to Make bytes Objects......Page 967 Mixing String Types......Page 968 Using 3.0 (and 2.6) bytearray Objects......Page 969 Text File Basics......Page 972 Text and Binary Modes in 3.0......Page 973 Type and Content Mismatches......Page 975 Manual encoding......Page 976 Decoding mismatches......Page 977 Handling the BOM in 3.0......Page 978 Unicode Files in 2.6......Page 980 The re Pattern Matching Module......Page 981 The struct Binary Data Module......Page 982 The pickle Object Serialization Module......Page 984 XML Parsing Tools......Page 986 Test Your Knowledge: Answers......Page 989 Why Manage Attributes?......Page 993 Inserting Code to Run on Attribute Access......Page 994 The Basics......Page 995 A First Example......Page 996 Computed Attributes......Page 997 Coding Properties with Decorators......Page 998 Descriptors......Page 999 Descriptor method arguments......Page 1000 Read-only descriptors......Page 1001 A First Example......Page 1002 Computed Attributes......Page 1004 Using State Information in Descriptors......Page 1005 How Properties and Descriptors Relate......Page 1007 __getattr__ and __getattribute__......Page 1008 The Basics......Page 1009 Avoiding loops in attribute interception methods......Page 1010 A First Example......Page 1011 Computed Attributes......Page 1013 __getattr__ and __getattribute__ Compared......Page 1014 Management Techniques Compared......Page 1015 Intercepting Built-in Operation Attributes......Page 1018 Delegation-Based Managers Revisited......Page 1022 Using Properties to Validate......Page 1025 Using Descriptors to Validate......Page 1027 Using __getattr__ to Validate......Page 1029 Using __getattribute__ to Validate......Page 1030 Chapter Summary......Page 1031 Test Your Knowledge: Answers......Page 1032 What’s a De
دانلود کتاب Learning Python: Powerful Object-Oriented Programming