Important Announcement
PubHTML5 Scheduled Server Maintenance on (GMT) Sunday, June 26th, 2:00 am - 8:00 am.
PubHTML5 site will be inoperative during the times indicated!

Home Explore [Python Learning Guide (4th Edition)

[Python Learning Guide (4th Edition)

Published by cliamb.li, 2014-07-24 12:15:04

Description: This book provides an introduction to the Python programming language. Pythonis a
popular open source programming language used for both standalone programs and
scripting applications in a wide variety of domains. It is free, portable, powerful, and
remarkably easy and fun to use. Programmers from every corner of the software industry have found Python’s focus on developer productivity and software quality to be
a strategic advantage in projects both large and small.
Whether you are new to programming or are a professional developer, this book’s goal
is to bring you quickly up to speed on the fundamentals of the core Python language.
After reading this book, you will know enough about Python to apply it in whatever
application domains you choose to explore.
By design, this book is a tutorial that focuses on the core Python languageitself, rather
than specific applications of it. As such, it’s intended to serve as the first in a two-volume
set:
• Learning Python, this book, teaches Pyth

Search

Read the Text Version

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


Like this book? You can publish your book online for free in a few minutes!
Create your own flipbook