L break, continue, pass, and loop else clause, 329 lambda expression, 474–479 coding techniques, 341–349 basics, 474 counter loops with while and range, 342 defining inline callback functions in tkinter, for statement, 334–341 479 general format, 329 justification for, 475 generation of offsets and items, 348 nested lambdas and scopes, 478 interactive loops (see interactive loops) potential for code obfuscation, 477 loop else clause, 332 lambdas and nested scopes, 422 nonexhaustive traversals with range and Latin-1 character encoding, 897 slices, 343 LEGB rule, 410 parallel traversals with zip and map, 345 len function, 80 range function lexical scoping, 408 lists, changing with, 344 Linux Python command line, starting, 36 while statement, 327 list comprehension expressions, 88 list comprehensions, 351, 358–362, 485 basics, 359 M best uses of, 490 Mac OS X Python command line, starting, 36 extended syntax, 361 makedb.py, 671 files, using on, 359 managed attributes, 941–947 map function and, 491 attribute validations example, 973–979 map function, versus, 486 descriptors validation, 975 matrixes and, 489 properties validation, 973 tests and nested loops, adding, 487 __getattribute__, validation with, 978 list object type, 86–90 __getattr__, validation with, 977 bounds checking, 87 coding to run on attribute access, 942 nesting, 88 comparison of management techniques, type specific operations, 87 963 list-unpacking assignment statements, 280 descriptors, 947–956 lister.py, 758 computed attributes, 952 ListInstance class, 758–761 descriptor methods, 948 lists, 197 example, 950 basic operations, 200 method arguments, 948 changing in place, 202–207 properties, relation to, 955 deleting items or sections in place, 206 read-only descriptors, 949 index and slice assignments, 202 state information, using in, 953 list method calls, 203–206 justification, 941–943 coding of lists, 199 properties, 943–947 common literals and operations, 198 computed attributes, 945 indexing, slicing, and matrixes, 201 decorators, coding with, 946 iteration and comprehensions, 200 first example, 944 literals, 77 new-style object derivation requirement, local scope, 409 944 local variables, 404 __getattr__ and __getattribute__, 956–973 long integers (Python 2.6), 107 avoiding loops, 958 loop else block, 329 comparison, 962 loop statement versus recursive functions, 467 computed attributes, 961 loops, 327–349 delegation, 958 example, 959 Index | 1149 Download at WoweBook.Com
interception of built-in attributes, 966– overloading class creation calls with 973 metaclasses, 1066 Manager class, 643, 653 declaration, 1062 manager functions, 1054 issues around use, 1052 manual iteration, 354 potential roles, 1052 manynames.py, 694 metafunctions, 804 map function, 201, 346, 479 metaprograms, 591–594 lambda expressions and, 480 method call expression, 173 list comprehensions and, 491 methods, 82, 84, 203, 638, 650, 684–686 list comprehensions, versus, 486 augmenting methods, 654 map iterator, 368 bound and unbound methods, 750–756 matching algorithm, 1042 bound methods, 728 math module, 79 calls to methods, 685–686 example functions, 125 class methods, 686 mathematical functions, 108 coding methods, 649 max and min functions, 364 comparison methods, 728 mergdexc.py, 846 destructor method, 732 metaclass model, 1058–1062, 1058 example, 685 (see also metaclasses) static methods, 686, 795 class statement protocol, 1061 (see also static methods) classes are instances of type, 1058 superclass constructors, calling, 686 metaclasses are subclasses of type, 1061 min and max functions, 125 metaclasses, 781, 794, 807, 1051–1056, 1058 mins.py, 454 (see also metaclass model) mix-in classes, 757 adding methods to classes example, 1070– coding, 757–767 1076 instance attributes, listing, 758 manual augmentation, 1070 listing inherited attributes, 761 metaclass-based augmentation, 1071 listing object attributes in class trees, applying decorators to methods example, 763 1076–1083 module packages, 561 manual tracing, 1076 package imports, 561–569 tracing with metaclasses and decorators, import example, 564–566 1077 justification, 566–569 with any decorators, 1079 packages and search path settings, 562 class decorators, compared to, 1073–1076, __inti__.py, 563 1080 package relative imports (see package decorator-based augmentation, 1073 relative imports) managing instances instead of classes, modules, 42, 51, 75, 529 1074 as extension for import and from, 591 class decorators, compared with, 1056 attributes, 53, 531, 543 coding, 1063–1069 classes, as attributes of, 631 basic metaclass, 1064 classes, versus, 703 customizing construction and creating, 543 initialization, 1065 data hiding in, 583 factory functions, using, 1066 design concepts, 598 instances versus inheritance, 1068 exec, running module files with, 57 overloading class creation calls with from statement, 545 classes, 1067 future language features, enabling, 584 global scope, 408 1150 | Index Download at WoweBook.Com
gotchas, 599–604 multiway branching, 312 from imports and reload, 601 mutable objects, 197, 649 from statement, 600 mutables from statement and variables, 601 in assignments, 388 recursive import failures, 603 mybooks.xml, 934 statement order in top-level code, 599 mydir.py, 592 import statement, 544 myfile.py, 53 importing byte code compilation if required, 534 N running, 535 importing by name string, 594 name attribute, 102 importing of modules, 533–535 name mangling, 747–750 locating, 534 namespace objects, 646 imports and reloads, 51–57 namespaces, 53, 55, 407, 529, 615, 693–701 metaprograms, 591–594 attribute names, 693 mixed-usage modes, 585–589 name assignment, 694–696 module extensions, 544 namespace dictionaries, 696–699 module namespaces, 550–554 namespace links, 699 attribute name qualification, 552 simple names, 693 generation from files, 550 negative offsets, 165 imports versus scopes, 552 nested scopes, 419–425 namespace nesting, 553 abitrary nesting, 424 module search path, 56, 534, 535–541 examples, 419 advanced module selection concepts, factory functions, 420 540 lambdas and, 422 module file selection, 539 nester.py, 812 search path configuration, 537 .NET and IronPython, 30 search path variations, 538 NetBeans, 63 sys.path list, 538 “new-style” classes, 777–794 third-party extensions, 540 changes, 778–787 module search path, changing, 590 class extensions, 788–795 namespaces, 55, 529 class properties, 792–794 naming conventions, 644 instance slots, 788–791 naming of, 543 metaclasses, 794 program structure and, 530 multiple __slot__ lists in superclasses, reloading modules, 554–558 790 example, 556 slots and generic code, 788 roles of, 530 __getattribute__ method, 794 scope, 550 diamond inheritance change, 783–787 standard library, 533 example, 784 transitive module reloads, 595–598 explicit conflict resolution, 785 __name__ attribute, 585–589 scope of search order, 787 command-line arguments with, 587 type model changes, 779–783 unit tests, 586 object type objects, 782 Monty Python’s Flying Circus, 17 type testing implications, 781 multiline statements, 317 next function, 354 multiple inheritance, 617, 756–767 __next__ method, 352 diamond pattern inheritance trees, 783 non-ASCII text, coding, encoding and mix-in classes (see mix-in classes) decoding, 905–906 None object, 247 Index | 1151 Download at WoweBook.Com
nonlocal statement, 409, 425–432 object-oriented programming, 13 absence from Python 2.6, 263 object-oriented scripting language, 5 basics, 425 object.attr expression, 687 examples, 426 object.attribute expression, 613 boundary cases, 428 object.attribute notation, 532 justification for, 429–432 objects, 75, 105, 145 Python 2.6 alternatives, 429 comparisons, equality, and truth, 244 normal integers (Python 2.6), 107 compound object types, 241 number object type, 78 copying versus referencing, 150 number operations, 113–127 databases, storing objects in, 669 bitwise operations, 124 dynamic typing and, 144 comparisons, 116 iterable objects, 352 chained comparisons, 116 nonbuilt-in object types, 250 complex numbers, 122 object classifications for built-in types, 240 division, 117–121 references versus copies, 241 integer precision, 121 truth and falsity, 246 math module functions, 125 bool type, 248 notation, hexadecimal, octal, and binary, None object, 247 122 type hierarchies, 248 numeric display formats, 115 type object type, 250 variables and basic expressions, 113–115 octal numeric literals, 107 numeric display formats, 115 OOP (object-oriented programming), 613– numeric extensions, 140 621 numeric object type, 105–108 as exemplified by coding of classes, 658 built-in numeric tools, 108 attribute inheritance search, 613 complexity ranking, 112 class method calls, 616 expression operators and precedence, 108 classes, 614, 615, 625 numeric literals, 106 class trees, 616–619 operator overloading and polymorphism, customization by inheritance, 629–632 112 code reuse, 619–621 some noncore types, 127–140 design issues, 737 Booleans, 139 bound and unbound methods, 750–756 decimal type, 127–129 composition, 740–745 fraction types, 129–133 delegation and wrapper classes, 745 sets, 133–139 generic object factories, 768 numeric precision, setting globally, 128 inheritance, 739 numeric programming, 11 multiple inheritance, 756–767 NumPy numeric programming extension, 7, polymorphism, 738 11, 140 pseudoprivate class attributes, 747–750 design patterns, 621 O important concepts, 660 object embedding, 661 instances, 614, 615, 625 object.attribute expression, 613 object persistence, 669 open call, (Python 2.6), 912 object type categories, 193 open function, 97, 99, 230 mutable versus immutable types, 194 mode string argument, 901 shared operation sets, 194 operator overloading, 240, 613, 633–636, 651– object types, 102 653, 652, 705–733 built-in object types (see built-in object attribute references, 718–720 types) 1152 | Index Download at WoweBook.Com
attribute privacy, 720 scope, 574 Boolean tests, 730 packages, 561 call expressions, 725–728 parallel traversals, 345 function interfaces, 727 parentheses ( ), 265, 269, 318 common operator overloading methods, functions and, 389 706 generator expressions and, 497 comparisons, 728 tuples and, 96 constructors and expressions, 706 Parrot project, 33 index iteration, 710 parsing, 165 indexing and slicing, 708–710 pass statement, 329, 330 Python 2.6, 709 PATH environment variable, 36, 1093 iterator objects, 711 pattern matching, 85 multiple iterators on one object, 714 pdb debugger, 889 user defined iterators, 712 PEP (Python Enhancement Proposal) protocol, justification, 636 12 membership, 716 Person class, 644 comparisons, Python 2.6, 716 incremental testing, 645 object destruction, 732 subclassing, 653 overloading methods, 1053 version portability, 647 overloading methods in Python 2.6, 747 person.py, 644 overview, 705–707 Peters, Tim, 1052 right-side and in-place addition, 723–725 pickle module, 236, 670, 744 string representation, 721–723 binary data requirement, 933 operator precedence, 111 string serialization (Python 3.0), 932 optimization, 889 pizzashop.py, 741 ORMs (object-relational mappers), 676 polymorphism, 82, 101, 113, 401, 403, 620 OverflowError class, 865 dynamic typing and, 153 example, 656 P overloading in Python versus other languages, 738 package imports, 561 pop method, 87, 205, 211 justification, 566–569 portability, 14 import versus from, 569 Portable Python, 1090 root directory, 568 pow function, 125 packages and search path settings, 562 precedence package relative imports, 569–581 parentheses and, 111 absolute package paths, versus, 573 precedence rules, 111 basics, 570 print function, 263 examples, 575–581 print operations, 297–307 imports outside packages, 575 print and stdout, 307 imports relative to CWD, 577 print function (Python 3.0), 298 imports still relative to CWD, 579 print statement (Python 2.6), 300 imports within packages, 576 print stream redirection, 302 modules, selecting with relative and version-neutral printing, 306 absolute imports, 578 print statement (Python 2.6), 263 justification, 572 procedure, 396 module lookup rules summary, 575 profile module, 517, 888 Python 3.0, 572 profilers, 888 changes, 570 program execution, 24–28 versus 2.6, 570 Index | 1153 Download at WoweBook.Com
development implications, 28 configuration, 1093 Python compared to other languages, 27 DOS variables in autoexec.bat, 1095 program portability, 4 environment variables, 1093 program shipping options, 889 path files, 1096 program structure, imports, 531 setting configuration options, 1095 program units, 77 Unix/Linux shell variables, 1095 programs, 5, 42, 75 Windows environment variable GUI, icons, opening with, 47 1095 launching, 35 Windows registry, 1096 additional launch options, 64 installing, 23 choosing a launch option, 66 Python 2.0 clicking file icons, 47 string module, 178 exec function, 57 Python 2.6, xxxii from the command line, 43 backward compatibility to older versions, IDLE user interface, 58 xxxii input function, 49 binary and Unicode strings, handling of, module imports and reloads, 51, 56 895 Unix executable scripts, 46 Booleans, 731 Windows automatic file extensions, 44 iteration method X.next( ), 356 running interactively, 35–41 nonlocal statement, alternatives to in, 429 experimentation, 38 operator overloading methods, 747 testing, 39 Python 3.0 print function, emulating, 457 saving in files, 41 using keyword-only arguments, 459 structure, 261, 530–533 raw_input function, 50 Windows, saving under, 45 reload function, 53 property built-in function, 942 string object types, 157 computed attributes, 945 unicode and str operation sets, 911 decorators, 946 “new-style” and “classic” classes, 777 first example, 944 Python 3.0, xxxii proxy classes, 745 built-in attributes, 662 pseudoprivate attributes, justification, 748 compared 2.x versions, xxxv pseudoprivate names, 747 comparisons and sorts, 204 PSF (Python Software Foundation), 13 dictionary changes, 217 Psyco just-in-time compiler, 30, 889 dictionary comparisons, 246 .pth file extension, 1096 extended sequence unpacking, 281, 284 .pth path file directories, 537 in for loops, 338 PVM (Python Virtual Machine), 27 function annotations, 472 .py file extension, 25, 43, 543 incompatibility with older versions, xxxii py2exe, 32, 889 input function, 50 .pyc file extension, 26, 534 new iterables, 366–371 PyChecker, PyLint, and PyUnit, 887 “new-style” classes, 777 PyDev, 63 older version tools removed from, xxxvi PyDoc, 84, 380–385, 887 special character coding, 908 help function, 380 string object types, 157 HTML reports, 383 string type, changes in, 896 PyInstaller, 32, 889 text and binary files, 233 PyPy project, 33 Unicode and binary data support, 895 Python unsupported raise syntax, 850 command-line options, 1097–1098 Python interpreter, 23 1154 | Index Download at WoweBook.Com
installing, 1089 Chapter 9: Tuples, Files, and Everything on Linux, 1091 Else, 253 on PDAs, 1092 Chapter 10: Introducing Python Statements, on Unix, 1091 276 on Windows, 1091 Chapter 11: Assignments, Expressions, and on Windows Vista, 1092 Prints, 308 website downloads link, 1090 Chapter 12: if Tests and Syntax Rules, 324 Python programming language, xxxi Chapter 13: while and for Loops, 349 advantages of, 3–5 Chapter 14: Iterations and common coding gotchas, 387 Comprehensions, Part 1, 372 compared to other languages, 17 Chapter 15: The Documentation Interlude, developer productivity and, 5 389 development tools, 886 Chapter 16: Function Basics, 405 documentation (see documentation) Chapter 17: Scopes, 432 execution speed, 7 Chapter 18: Arguments, 461 iteration protocol, 493 Chapter 19: Advanced Function Topics, manuals and resources, 1098 483 old and new versions, xxxii Chapter 20: Iterations and Perl, compared to, 20 Comprehensions, Part 2, 523 portability, 14 Chapter 21: Modules, the Big Picture, 541 primary implementations of, 29 Chapter 22: Module Coding Basics, 558 so-called “optional” features, 1082 Chapter 23: Module Packages, 582 statically-typed languages, compared to, Chapter 24: Advanced Module Topics, 604 401 Chapter 25: OOP: The Big Picture, 622 string model, 897–902 Chapter 26: Class Coding Basics, 641 support, 12 Chapter 27: A More Realistic Example, technical strengths, 13–17 677 user base, 7 Chapter 28: Class Coding Details, 703 uses for, 9–12 Chapter 29: Operator Overloading, 734 PYTHONPATH, 1094 Chapter 30: Designing with Classes, 770 PYTHONPATH directories, 536 Chapter 31: Advanced Class Topics, 815 PYTHONSTARTUP, 1094 Chapter 32: Exception Basics, 833 PythonWin, 63, 1090 Chapter 33: Exception Coding Details, 856 PyWin32, 1090 Chapter 34: Exception Objects, 870 Chapter 35: Designing with Exceptions, Q 891 quizzes, xliii Chapter 36: Unicode and Byte Strings, 937 Chapter 37: Managed Attributes, 979 Chapter 1: A Python Q&A Session, 19 Chapter 38: Decorators, 1047 Chapter 2: How Python Runs Programs, Chapter 39: Metaclasses, 1084 34 quotes Chapter 3: How You Run Programs, 68 escaping, 158 Chapter 4: Introducing Python Object strings and, 158 Types, 103 triple quotes, 162 Chapter 5: Numeric Types, 141 Chapter 6: The Dynamic Typing Interlude, 153 R Chapter 7: Strings, 195 raise statement, 829, 848–850 Chapter 8: Lists and Dictionaries, 223 from clause Python 3.0 exception chaining, 849 Index | 1155 Download at WoweBook.Com
nonerror conditions, signaling with, 878 rstrip method, 235 random module, 79, 127 range S versus for loops, 388 range function, 342 SAX parsing, 935 range iterator, 367 scientific programming, 11 support for multiple iterators, 369 ScientificPython programming extensions, 12 rapid prototyping, 11 SciPy programming extensions, 12, 140 rational number objects, 129 scopes, 407–414 raw string literals, 85 basics, 407 raw strings, 161 built-in scope, 412 raw_input function (Python 2.x), 50 defaults with loop variables, versus, 423 re (regular expression) module, 85 example, 411 string handling in Python 3.0, 929 global statements, 408 read method, 233 module files and, 416 readline method, 98, 232 name resolution and the LEGB rule, 410 recursive functions, 465–469 namespaces, 407 arbitrary structures, handling, 468 nested functions and, 419–425 coding alternatives, 466 nonlocal, 408 direct or indirect, 467 rules, 408 loop statements, versus, 467 script0.py, 24 summation, 465 script1.py, 42 recursive imports, 603 running with an import, 51 reduce, 363 scripts, 5, 42 reduce function, 481 search tables (see dictionaries) reference counters, 147 self argument, 685 references, 145 semicolon (;), 265 copies of objects, versus, 241 send method, 496 shared references, 148–152 sequence assignment statements, 280 equality, 151 sequence assignments, 281–284 in-place changes, 149 sequence operations, 80 relative imports, 561 sequences, 80 reload function, 52, 554–558 set comprehensions, 507 example, 556 set numeric type, 133–139 from imports and, 601 dictionaries, compared to, 135 interactive testing, 602 immutable constraints and frozen sets, 136 import and from, contrasted with, 555 Python 2.6, 133 transitive module reloads, 595–598 Python 3.0, 135 usage notes, 56 set comprehensions in Python 3.0, 137 version 3.0 requirements, 53 set object type, 99 reloadall.py, 596 setsubclass.py, 776 remove method, 87, 206 shared references, 148–152 repetition, 81 equality, 151 replace method, 83 in-place changes, 149 repr function, 79 Shedskin C++ translator, 31, 889 str, compared to, 116 shelve module, 670–675, 744 return statement, 397, 398, 404 advantages and disadvantages, 673 reverse method, 87, 205 database client, 672 round function, 126 database files, 672 object storage in shelve databases, 671 1156 | Index Download at WoweBook.Com
shelves and dictionaries, 670 colon (:), 264 updating a shelve’s objects, 674 indentation, 266 simple functions, 796 interactive loops, 271–276 single quotes (') and strings, 158 semicolons, 269 site module, 537 statement separators, 269 slice assignment in lists, 202 static methods, 686, 795–801 slice objects, 168 alternatives to, 798 slicing, 81, 165, 166 coding with decorator syntax, 804 example, 168 counting instances, 800 extended slicing, 167 Python 2.6 and 3.0, 796 loops, usage in, 343 using, 799 software components, 653 staticmethod function, 799 sort method, 87, 203, 388, 454 steps, 167 sorted function, 93, 363 StopIteration exception, 352 sorts in Python 3.0, 204 str, 79, 157, 169 source code, 26 repr, compared to, 116 source file character set encoding declarations, str object type 912 data encoding in, 901 spaces, 314 str string type, 899 special characters, 897 Python 2.6 operation set, 911 split method, 235 Python 3.0 compared to 2.x, 899 square brackets [ ], 78, 108, 269 Unicode and, 85 dictionaries and, 209 stream processors, 742 list comprehensions and, 359, 486, 504 stream redirection, 44 lists and, 89, 199 strides, 167 square roots, 126 string exceptions, 859 stack trace, 828, 840 deprecation of, 858 Stackless Python, 33 string formatting, 179–183 standard library, 4, 533 advanced expressions, 181 library directories, 537 dictionary-based formatting expressions, standard manual set, 386 182 standard output stream (stdout), 297 string formatting type codes, 181 state information, 102, 644 string formatting method calls, 183–193 state retention, 429–432, 727, 997–1000 format method, 184–193 state with classes, 430 % formatting expression, compared to, statements, 75, 261–276 187 assignment statements (see assignment justification for, 190 statements) keys, attributes, and offsets, 184 compound statements, 264 string methods, 172–179 differences from other C-like languages, additional examples, 177 265 changing strings example, 174 end-of-line, 265 parsing text example, 176 expression statements, 295 Python 2.x string module, 178 in-place changes, 296 string method calls to format method, 185 indentation syntax, 266 string method calls, Python 3.0, 173 multiline statements, 317 string object type, 80–86, 155–163 Python 3.0 statement set, 262 coding special characters, 85 syntax, 264 pattern matching, 85 block rule special case, 270 raw string literals, 85 Index | 1157 Download at WoweBook.Com
sequence operations and, 80 struct binary data module (Python 3.0), string literals, 157–163 930 common literals and operations, 155 text and binary files, 920 escape sequences, 158–161 BOM in Python 3.0, 926 raw strings and escapes, 161 file modes in Python 3.0, 921 single and double quoted strings, 158 type and content mismatches, 923 string backslash characters, 159 Unicode in Python 2.6, 928 triple quotes, 162 type conversions, 903 version 3.0 changes, 896 Unicode files, using, 924 versions 2.6 and 3.0 string types, 157 reading and writing in Python 3.0, 924 string operations, 163–172 unicode strings, coding, 904 basic operations, 164 Unicode strings, Python 2.6 coding, 910 changing strings, 171 XML parsing tools, 934 indexing, 165, 166 strong typing, 78 slicing, 165, 166 struct module string handling, Python 3.0, extended slicing, 167 930 string conversions, 169 subclasses, 614, 629, 653–658 character code conversions, 170 coding, 653–658 strings, 897 augmenting methods, 654–656 16- and 32-bit Unicode values, coding of, inheritance, customization, and 907 extension, 657 ASCII text, coding, 905 OOP, as illustration of, 658 bytearray objects, using, 917–920 polymorphism, 656 bytes objects (Python 3.0), 913–917 sum function, 125 method calls, 913 Super class, 689 sequence operations, 914 superclasses, 614, 629 bytes string type abstract superclasses, 690–693 making bytes objects, 915 syntax rules, 264–271, 314–319 character encoding schemes and, 897 indentation, 315 encoding conversions, 909 multiline statements, 317 escape sequence coding by type, 908 open syntactic pairs rule, 318 examples of usage (Python 3.0), 902–904 sys.exc_info, 880–882 literals and basic properties, 902 sys.exit(statuscode) call, 883 mixing string types, 916 sys.getdefaultencoding function, 896 mutability or immutability of string types, sys.modules table, 534 903 sys.path list, 536, 538, 590 non-ASCII text, coding, 905 system namespace partitioning and modules, non-ASCII text, encoding and decoding, 530 906 systems programming, 9 pickle object serialization module (Python 3.0), 932 T Python 3.0 string types, usage, 920 re pattern matching module (Python 3.0), termination actions, 830 929 testdriver function, 880 source file character set encoding tester, 426 declarations, 912 testing of code, 645 string methods, 82 testmixin.py, 759 string types, 899 testprint30.py, 458 text files, 98, 233, 901, 920 in Python 3.0, 921 1158 | Index Download at WoweBook.Com
text-mode files, 920 supported sequence operations, 227 text.py, 912 syntax with parentheses and commas, 227 threenames.py, 54 type class, 1061 time module, 509 type hierarchies, 248 alternatives, 513 type object type, 100, 250, 1058 timeit module, 517 typesubclass.py, 775 timer module, keyword-only arguments, 516 tkinter, 59 U getting support on Linux, 1094 settings, 1094 unbound methods, 750, 796 top-level code, 387 Python 3.0 status as functions, 752 top-level file, 51, 531 undefined name exception, 691 transitive module reloads, 595–598 underscore (_), 584 triple quotes, 162 Unicode, 897 True and False, 414 strings, coding of, 904 True and False Boolean values, 139 text, handling in versions 2.6 and 3.0, 896 true and false values, 246 Unicode files, 924 true division, 110, 117 reading and writing (Python 3.0), 924 truth tests, 320 decoding mismatches, 925 try file input decoding, 925 except statement, 831 file output encoding, 925 try statement, 96, 263, 826, 840 manual encoding, 924 (see also exceptions) unicode string type (Python 2.6), 911 debugging with, 879 unicode string type (Python 2.x), 899, 910 except statement and, 828 unicode strings, 157 nested try statements, 873–877 union function, 456 Python 2.5 and later, 835 unit tests with __name__ attribute, 586 try unittest, 887 except/else, 835–842 Unix try statement clause forms, 837–839 env lookup trick, 47 try/else clause, 839 executable scripts, 46 try/finally statement, 842–843 Python command line, starting, 36 coding termination actions, 843 Unladen Swallow project, 33 unified try/except/finally, 844–847 update method, 211 example, 846 user base of Python language, 7 nesting finally and except, 845 user-defined classes, 101 statement syntax, 845 user-defined exceptions, 830 try/finally statement, 827, 830 UTF-8 encoding, 898 file and server connection closure, 879 utility modules, 108 tuple object type, 96 tuple-unpacking assignment statements, 280 V tuples, 114, 225–229 values method, 211, 370 common literals and operations, 226 van Rossum, Guido, 14 conversions, methods, and immutability, variables, 113, 144–145 228 declaration, 114 in for loops, 336 initialization, 546 immutability and tuple contents, 229 local variables, 404 lists, compared to, 229 scope, 408 sorting, 228 variable name rules, 292–295 Index | 1159 Download at WoweBook.Com
W websites, 676 while loop, 94 versus for loops, 388 while statement, 327 range function and, 342 Windows automatic file extensions, 45 executable files, displaying output, 49 icon clicks for program initiation, 47 IDLE user interface and, 58 program files, opening with icons, 47 Python command line, starting in, 36 Python files, running in, 44 Python standard manual set, 386 Windows Notepad, file encoding specification, 926 with statement, 129, 842 with/as extension, 263 with/as statement, 832, 851–855 context management protocol, 853 usage, 852 wrapper classes, 745 wrapper objects, 984 wrappers, catching exceptions with, 882 write method, 232 X XML, 934 Y yield expression, 263 yield operator, 108 yield statement, 397, 399 usage in generators, 493 Z zip, 363, 365 zip function, 345 dictionary construction using, 347 zip iterator, 368 ZODB object-oriented database system, 676 1160 | Index Download at WoweBook.Com
About the Author Mark Lutz is the world leader in Python training, the author of Python’s earliest and bestselling texts, and a pioneering figure in the Python community. Mark is the author of the popular O’Reilly books Learning Python, Programming Python, and Python Pocket Reference, all available in third or fourth ed- itions in 2009. He has been using and promoting Python since 1992, started writing Python books in 1995, and began teaching Python classes in 1997. As of early 2009, Mark has instructed 225 Python training sessions, taught some 3,500 students, and written Python books that have sold roughly a quarter of a million copies and been translated into more than a dozen languages. In addition, he holds B.S. and M.S. degrees in computer science from the University of Wisconsin, and during the last 25 years he has worked as a professional developer on compilers, programming tools, scripting applications, and assorted client/server sys- tems. Mark can be reached on the Web at http://www.rmi.net/~lutz. Colophon The animal on the cover of Learning Python, Fourth Edition, is a wood rat (Neotoma Muridae). The wood rat lives in a wide range of conditions (mostly rocky, scrub, and desert areas) over much of North and Central America, generally at some distance from humans. Wood rats are good climbers, nesting in trees or bushes up to six meters off the ground; some species burrow underground or in rock crevices or inhabit other species’ abandoned holes. These grayish-beige, medium-size rodents are the original pack rats: they carry anything and everything into their homes, whether or not it’s needed, and are especially attracted to shiny objects such as tin cans, glass, and silverware. The cover image is a 19th-century engraving from Cuvier’s Animals. The cover font is Adobe ITC Garamond. The text font is Linotype Birka; the heading font is Adobe Myriad Condensed; and the code font is LucasFont’s TheSansMonoCondensed. Download at WoweBook.Com
Download at WoweBook.Com
Search
Read the Text Version
- 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
- 1001
- 1002
- 1003
- 1004
- 1005
- 1006
- 1007
- 1008
- 1009
- 1010
- 1011
- 1012
- 1013
- 1014
- 1015
- 1016
- 1017
- 1018
- 1019
- 1020
- 1021
- 1022
- 1023
- 1024
- 1025
- 1026
- 1027
- 1028
- 1029
- 1030
- 1031
- 1032
- 1033
- 1034
- 1035
- 1036
- 1037
- 1038
- 1039
- 1040
- 1041
- 1042
- 1043
- 1044
- 1045
- 1046
- 1047
- 1048
- 1049
- 1050
- 1051
- 1052
- 1053
- 1054
- 1055
- 1056
- 1057
- 1058
- 1059
- 1060
- 1061
- 1062
- 1063
- 1064
- 1065
- 1066
- 1067
- 1068
- 1069
- 1070
- 1071
- 1072
- 1073
- 1074
- 1075
- 1076
- 1077
- 1078
- 1079
- 1080
- 1081
- 1082
- 1083
- 1084
- 1085
- 1086
- 1087
- 1088
- 1089
- 1090
- 1091
- 1092
- 1093
- 1094
- 1095
- 1096
- 1097
- 1098
- 1099
- 1100
- 1101
- 1102
- 1103
- 1104
- 1105
- 1106
- 1107
- 1108
- 1109
- 1110
- 1111
- 1112
- 1113
- 1114
- 1115
- 1116
- 1117
- 1118
- 1119
- 1120
- 1121
- 1122
- 1123
- 1124
- 1125
- 1126
- 1127
- 1128
- 1129
- 1130
- 1131
- 1132
- 1133
- 1134
- 1135
- 1136
- 1137
- 1138
- 1139
- 1140
- 1141
- 1142
- 1143
- 1144
- 1145
- 1146
- 1147
- 1148
- 1149
- 1150
- 1151
- 1152
- 1153
- 1154
- 1155
- 1156
- 1157
- 1158
- 1159
- 1160
- 1161
- 1162
- 1163
- 1164
- 1165
- 1166
- 1167
- 1168
- 1169
- 1170
- 1171
- 1172
- 1173
- 1174
- 1175
- 1176
- 1177
- 1178
- 1179
- 1180
- 1181
- 1182
- 1183
- 1184
- 1185
- 1186
- 1187
- 1188
- 1189
- 1190
- 1191
- 1192
- 1193
- 1194
- 1195
- 1196
- 1197
- 1198
- 1199
- 1200
- 1201
- 1202
- 1203
- 1204
- 1205
- 1206
- 1207
- 1208
- 1209
- 1210
- 1211
- 1212
- 1213
- 1214
- 1 - 50
- 51 - 100
- 101 - 150
- 151 - 200
- 201 - 250
- 251 - 300
- 301 - 350
- 351 - 400
- 401 - 450
- 451 - 500
- 501 - 550
- 551 - 600
- 601 - 650
- 651 - 700
- 701 - 750
- 751 - 800
- 801 - 850
- 851 - 900
- 901 - 950
- 951 - 1000
- 1001 - 1050
- 1051 - 1100
- 1101 - 1150
- 1151 - 1200
- 1201 - 1214
Pages: