Synopses & Reviews
Already the industry standard for Python users, Programming Python from O'Reilly just got even better. This third edition has been updated to reflect current best practices and the abundance of changes introduced by the latest version of the language, Python 2.5.
Whether you're a novice or an advanced practitioner, you'll find this refreshed book more than lives up to its reputation. Programming Python, 3rd Edition, teaches you the right way to code. It explains Python language syntax and programming techniques in a clear and concise manner, with numerous examples that illustrate both correct usage and common idioms. By reading this comprehensive guide, you'll learn how to apply Python in real-world problem domains such as:
- GUI programming
- Internet scripting
- Parallel processing
- Database management
- Networked applications
Programming Python, Third Edition covers each of these target domains gradually, beginning with in-depth discussions of core concepts and then progressing toward complete programs. Large examples do appear, but only after you've learned enough to understand their techniques and code.
Along the way, you'll also learn how to use the Python language in realistically scaled programs--concepts such as Object-Oriented Programming (OOP) and code reuse are recurring side themes throughout this text. If you're interested in Python programming, then this O'Reilly classic needs to be within arm's reach. The wealth of practical advice, snippets of code, and patterns of program design can all be put into use on a daily basis--making your life easier and more productive.
Reviews of the second edition:
"...about as comprehensive as any book can be."
--Dr. Dobb's Journal
"If the language had manuals, they would undoubtedly be the texts from O'Reilly...'Learning Python' and 'Programming Python' are definitive treatments."
Once you've come to grips with the core Python language, learning how to build Python applications presents a far more interesting challenge. Tap this book's wealth of practical advice, snippets of code, and patterns of program design to take your Python skills to the next level.
You'll start with in-depth discussions of core concepts and then progress toward complete programs in different application domains, including:
- GUI programming
- Internet scripting
- Parallel processing
- Database management
- Networked applications
- System administration
- Text processing
Most programming experts consider this classic book, now updated for Python 3.x, to be the industry standard for learning Python application programming. With clear and concise explanations of Python syntax and programming techniques, and numerous examples that illustrate both correct usage and common idioms, Programming Python shows you the right way to code with Python.
This hands-on book helps programmers master the fundamentals of the core Python language quickly and efficiently. Updated for Python 3.0, this book offers programmers a comprehensive learning tool for this language and object-oriented programming. It provides material and exercises derived from more than 200 live Python training session conducted by the author, and serves as a self-paced introductory Python resource.
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
As a language optimized for developer productivity, Python solves the software development crunch. As the industry standard book for mastering this expedient language, this volume explains Python language syntax and programming techniques in a clear and concise manner, with numerous examples that illustrate both correct usage and common idioms.
If you've mastered Python's fundamentals, you're ready to start using it to get real work done. Programming Python will show you how, with in-depth tutorials on the language's primary application domains: system administration, GUIs, and the Web. You'll also explore how Python is used in databases, networking, front-end scripting layers, text processing, and more. This book focuses on commonly used tools and libraries to give you a comprehensive understanding of Pythons many roles in practical, real-world programming.
You'll learn language syntax and programming techniques in a clear and concise manner, with lots of examples that illustrate both correct usage and common idioms. Completely updated for version 3.x, Programming Python also delves into the language as a software development tool, with many code examples scaled specifically for that purpose.
- Quick Python tour: Build a simple demo that includes data representation, object-oriented programming, object persistence, GUIs, and website basics
- System programming: Explore system interface tools and techniques for command-line scripting, processing files and folders, running programs in parallel, and more
- GUI programming: Learn to use Pythons tkinter widget library
- Internet programming: Access client-side network protocols and email tools, use CGI scripts, and learn website implementation techniques
- More ways to apply Python: Implement data structures, parse text-based information, interface with databases, and extend and embed Python
About the Author
Mark Lutz is a leading Python trainer, the author of Pythons earliest and best-selling texts, and a pioneering figure in the Python world.
Mark is the author of the three OReilly books: Learning Python, Programming Python, and Python Pocket Reference, all currently in fourth or fifth editions. He has been using and promoting Python since 1992, started writing Python books in 1995, and began teaching Python classes in 1997. As of Spring 2013, Mark has instructed 260 Python training sessions, taught roughly 4,000 students in live classes, and written Python books that have sold 400,000 uni
Table of Contents
Dedication; Preface; About This Fourth Edition; About The Third Edition; About This Book; Book Updates; About the Programs in This Book; Using Code Examples; Font Conventions; Safari® Books Online; How to Contact Us; Acknowledgments; Getting Started; Chapter 1: A Python Q&A Session; 1.1 Why Do People Use Python?; 1.2 Is Python a Scripting Language”?; 1.3 OK, but Whats the Downside?; 1.4 Who Uses Python Today?; 1.5 What Can I Do with Python?; 1.6 How Is Python Supported?; 1.7 What Are Pythons Technical Strengths?; 1.8 How Does Python Stack Up to Language X?; 1.9 Chapter Summary; 1.10 Test Your Knowledge: Quiz; 1.11 Test Your Knowledge: Answers; Chapter 2: How Python Runs Programs; 2.1 Introducing the Python Interpreter; 2.2 Program Execution; 2.3 Execution Model Variations; 2.4 Chapter Summary; 2.5 Test Your Knowledge: Quiz; 2.6 Test Your Knowledge: Answers; Chapter 3: How You Run Programs; 3.1 The Interactive Prompt; 3.2 System Command Lines and Files; 3.3 Clicking File Icons; 3.4 Module Imports and Reloads; 3.5 Using exec to Run Module Files; 3.6 The IDLE User Interface; 3.7 Other IDEs; 3.8 Other Launch Options; 3.9 Which Option Should I Use?; 3.10 Chapter Summary; 3.11 Test Your Knowledge: Quiz; 3.12 Test Your Knowledge: Answers; 3.13 Test Your Knowledge: Part I Exercises; Types and Operations; Chapter 4: Introducing Python Object Types; 4.1 Why Use Built-in Types?; 4.2 Numbers; 4.3 Strings; 4.4 Lists; 4.5 Dictionaries; 4.6 Tuples; 4.7 Files; 4.8 Other Core Types; 4.9 Chapter Summary; 4.10 Test Your Knowledge: Quiz; 4.11 Test Your Knowledge: Answers; Chapter 5: Numeric Types; 5.1 Numeric Type Basics; 5.2 Numbers in Action; 5.3 Other Numeric Types; 5.4 Numeric Extensions; 5.5 Chapter Summary; 5.6 Test Your Knowledge: Quiz; 5.7 Test Your Knowledge: Answers; Chapter 6: The Dynamic Typing Interlude; 6.1 The Case of the Missing Declaration Statements; 6.2 Shared References; 6.3 Dynamic Typing Is Everywhere; 6.4 Chapter Summary; 6.5 Test Your Knowledge: Quiz; 6.6 Test Your Knowledge: Answers; Chapter 7: Strings; 7.1 String Literals; 7.2 Strings in Action; 7.3 String Methods; 7.4 String Formatting Expressions; 7.5 String Formatting Method Calls; 7.6 General Type Categories; 7.7 Chapter Summary; 7.8 Test Your Knowledge: Quiz; 7.9 Test Your Knowledge: Answers; Chapter 8: Lists and Dictionaries; 8.1 Lists; 8.2 Lists in Action; 8.3 Dictionaries; 8.4 Dictionaries in Action; 8.5 Chapter Summary; 8.6 Test Your Knowledge: Quiz; 8.7 Test Your Knowledge: Answers; Chapter 9: Tuples, Files, and Everything Else; 9.1 Tuples; 9.2 Files; 9.3 Type Categories Revisited; 9.4 Object Flexibility; 9.5 References Versus Copies; 9.6 Comparisons, Equality, and Truth; 9.7 Pythons Type Hierarchies; 9.8 Other Types in Python; 9.9 Built-in Type Gotchas; 9.10 Chapter Summary; 9.11 Test Your Knowledge: Quiz; 9.12 Test Your Knowledge: Answers; 9.13 Test Your Knowledge: Part II Exercises; Statements and Syntax; Chapter 10: Introducing Python Statements; 10.1 Python Program Structure Revisited; 10.2 A Tale of Two ifs; 10.3 A Quick Example: Interactive Loops; 10.4 Chapter Summary; 10.5 Test Your Knowledge: Quiz; 10.6 Test Your Knowledge: Answers; Chapter 11: Assignments, Expressions, and Prints; 11.1 Assignment Statements; 11.2 Expression Statements; 11.3 Print Operations; 11.4 Chapter Summary; 11.5 Test Your Knowledge: Quiz; 11.6 Test Your Knowledge: Answers; Chapter 12: if Tests and Syntax Rules; 12.1 if Statements; 12.2 Python Syntax Rules; 12.3 Truth Tests; 12.4 The if/else Ternary Expression; 12.5 Chapter Summary; 12.6 Test Your Knowledge: Quiz; 12.7 Test Your Knowledge: Answers; Chapter 13: while and for Loops; 13.1 while Loops; 13.2 break, continue, pass, and the Loop else; 13.3 for Loops; 13.4 Loop Coding Techniques; 13.5 Chapter Summary; 13.6 Test Your Knowledge: Quiz; 13.7 Test Your Knowledge: Answers; Chapter 14: Iterations and Comprehensions, Part 1; 14.1 Iterators: A First Look; 14.2 List Comprehensions: A First Look; 14.3 Other Iteration Contexts; 14.4 New Iterables in Python 3.0; 14.5 Other Iterator Topics; 14.6 Chapter Summary; 14.7 Test Your Knowledge: Quiz; 14.8 Test Your Knowledge: Answers; Chapter 15: The Documentation Interlude; 15.1 Python Documentation Sources; 15.2 Common Coding Gotchas; 15.3 Chapter Summary; 15.4 Test Your Knowledge: Quiz; 15.5 Test Your Knowledge: Answers; 15.6 Test Your Knowledge: Part III Exercises; Functions; Chapter 16: Function Basics; 16.1 Why Use Functionssssss?; 16.2 Coding Functions; 16.3 A First Example: Definitions and Calls; 16.4 A Second Example: Intersecting Sequences; 16.5 Chapter Summary; 16.6 Test Your Knowledge: Quiz; 16.7 Test Your Knowledge: Answers; Chapter 17: Scopes; 17.1 Python Scope Basics; 17.2 The global Statement; 17.3 Scopes and Nested Functions; 17.4 The nonlocal Statement; 17.5 Chapter Summary; 17.6 Test Your Knowledge: Quiz; 17.7 Test Your Knowledge: Answers; Chapter 18: Arguments; 18.1 Argument-Passing Basics; 18.2 Special Argument-Matching Modes; 18.3 The min Wakeup Call!; 18.4 Generalized Set Functions; 18.5 Emulating the Python 3.0 print Function; 18.6 Chapter Summary; 18.7 Test Your Knowledge: Quiz; 18.8 Test Your Knowledge: Answers; Chapter 19: Advanced Function Topics; 19.1 Function Design Concepts; 19.2 Recursive Functions; 19.3 Function Objects: Attributes and Annotations; 19.4 Anonymous Functions: lambda; 19.5 Mapping Functions over Sequences: map; 19.6 Functional Programming Tools: filter and reduce; 19.7 Chapter Summary; 19.8 Test Your Knowledge: Quiz; 19.9 Test Your Knowledge: Answers; Chapter 20: Iterations and Comprehensions, Part 2; 20.1 List Comprehensions Revisited: Functional Tools; 20.2 Iterators Revisited: Generators; 20.3 3.0 Comprehension Syntax Summary; 20.4 Timing Iteration Alternatives; 20.5 Function Gotchas; 20.6 Chapter Summary; 20.7 Test Your Knowledge: Quiz; 20.8 Test Your Knowledge: Answers; 20.9 Test Your Knowledge: Part IV Exercises; Modules; Chapter 21: Modules: The Big Picture; 21.1 Why Use Modules?; 21.2 Python Program Architecture; 21.3 How Imports Work; 21.4 The Module Search Path; 21.5 Chapter Summary; 21.6 Test Your Knowledge: Quiz; 21.7 Test Your Knowledge: Answers; Chapter 22: Module Coding Basics; 22.1 Module Creation; 22.2 Module Usage; 22.3 Module Namespaces; 22.4 Reloading Modules; 22.5 Chapter Summary; 22.6 Test Your Knowledge: Quiz; 22.7 Test Your Knowledge: Answers; Chapter 23: Module Packages; 23.1 Package Import Basics; 23.2 Package Import Example; 23.3 Why Use Package Imports?; 23.4 Package Relative Imports; 23.5 Chapter Summary; 23.6 Test Your Knowledge: Quiz; 23.7 Test Your Knowledge: Answers; Chapter 24: Advanced Module Topics; 24.1 Data Hiding in Modules; 24.2 Enabling Future Language Features; 24.3 Mixed Usage Modes: __name__ and __main__; 24.4 Changing the Module Search Path; 24.5 The as Extension for import and from; 24.6 Modules Are Objects: Metaprograms; 24.7 Importing Modules by Name String; 24.8 Transitive Module Reloads; 24.9 Module Design Concepts; 24.10 Module Gotchas; 24.11 Chapter Summary; 24.12 Test Your Knowledge: Quiz; 24.13 Test Your Knowledge: Answers; 24.14 Test Your Knowledge: Part V Exercises; Classes and OOP; Chapter 25: OOP: The Big Picture; 25.1 Why Use Classes?; 25.2 OOP from 30,000 Feet; 25.3 Chapter Summary; 25.4 Test Your Knowledge: Quiz; 25.5 Test Your Knowledge: Answers; Chapter 26: Class Coding Basics; 26.1 Classes Generate Multiple Instance Objects; 26.2 Classes Are Customized by Inheritance; 26.3 Classes Can Intercept Python Operators; 26.4 The Worlds Simplest Python Class; 26.5 Chapter Summary; 26.6 Test Your Knowledge: Quiz; 26.7 Test Your Knowledge: Answers; Chapter 27: A More Realistic Example; 27.1 Step 1: Making Instances; 27.2 Step 2: Adding Behavior Methods; 27.3 Step 3: Operator Overloading; 27.4 Step 4: Customizing Behavior by Subclassing; 27.5 Step 5: Customizing Constructors, Too; 27.6 Step 6: Using Introspection Tools; 27.7 Step 7 (Final): Storing Objects in a Database; 27.8 Future Directions; 27.9 Chapter Summary; 27.10 Test Your Knowledge: Quiz; 27.11 Test Your Knowledge: Answers; Chapter 28: Class Coding Details; 28.1 The class Statement; 28.2 Methods; 28.3 Inheritance; 28.4 Namespaces: The Whole Story; 28.5 Documentation Strings Revisited; 28.6 Classes Versus Modules; 28.7 Chapter Summary; 28.8 Test Your Knowledge: Quiz; 28.9 Test Your Knowledge: Answers; Chapter 29: Operator Overloading; 29.1 The Basics; 29.2 Indexing and Slicing: __getitem__ and __setitem__; 29.3 Index Iteration: __getitem__; 29.4 Iterator Objects: __iter__ and __next__; 29.5 Membership: __contains__, __iter__, and __getitem__; 29.6 Attribute Reference: __getattr__ and __setattr__; 29.7 String Representation: __repr__ and __str__; 29.8 Right-Side and In-Place Addition: __radd__ and __iadd__; 29.9 Call Expressions: __call__; 29.10 Comparisons: __lt__, __gt__, and Others; 29.11 Boolean Tests: __bool__ and __len__; 29.12 Object Destruction: __del__; 29.13 Chapter Summary; 29.14 Test Your Knowledge: Quiz; 29.15 Test Your Knowledge: Answers; Chapter 30: Designing with Classes; 30.1 Python and OOP; 30.2 OOP and Inheritance: Is-a” Relationships; 30.3 OOP and Composition: Has-a” Relationships; 30.4 OOP and Delegation: Wrapper” Objects; 30.5 Pseudoprivate Class Attributes; 30.6 Methods Are Objects: Bound or Unbound; 30.7 Multiple Inheritance: Mix-in” Classes; 30.8 Classes Are Objects: Generic Object Factories; 30.9 Other Design-Related Topics; 30.10 Chapter Summary; 30.11 Test Your Knowledge: Quiz; 30.12 Test Your Knowledge: Answers; Chapter 31: Advanced Class Topics; 31.1 Extending Built-in Types; 31.2 The New-Style” Class Model; 31.3 New-Style Class Changes; 31.4 New-Style Class Extensions; 31.5 Static and Class Methods; 31.6 Decorators and Metaclasses: Part 1; 31.7 Class Gotchas; 31.8 Chapter Summary; 31.9 Test Your Knowledge: Quiz; 31.10 Test Your Knowledge: Answers; 31.11 Test Your Knowledge: Part VI Exercises; Exceptions and Tools; Chapter 32: Exception Basics; 32.1 Why Use Exceptions?; 32.2 Exceptions: The Short Story; 32.3 Chapter Summary; 32.4 Test Your Knowledge: Quiz; 32.5 Test Your Knowledge: Answers; Chapter 33: Exception Coding Details; 33.1 The try/except/else Statement; 33.2 The try/finally Statement; 33.3 Unified try/except/finally; 33.4 The raise Statement; 33.5 The assert Statement; 33.6 with/as Context Managers; 33.7 Chapter Summary; 33.8 Test Your Knowledge: Quiz; 33.9 Test Your Knowledge: Answers; Chapter 34: Exception Objects; 34.1 Exceptions: Back to the Future; 34.2 Why Exception Hierarchies?; 34.3 Built-in Exception Classes; 34.4 Custom Print Displays; 34.5 Custom Data and Behavior; 34.6 Chapter Summary; 34.7 Test Your Knowledge: Quiz; 34.8 Test Your Knowledge: Answers; Chapter 35: Designing with Exceptions; 35.1 Nesting Exception Handlers; 35.2 Exception Idioms; 35.3 Exception Design Tips and Gotchas; 35.4 Core Language Summary; 35.5 Chapter Summary; 35.6 Test Your Knowledge: Quiz; 35.7 Test Your Knowledge: Answers; 35.8 Test Your Knowledge: Part VII Exercises; Advanced Topics; Chapter 36: Unicode and Byte Strings; 36.1 String Changes in 3.0; 36.2 String Basics; 36.3 Python 3.0 Strings in Action; 36.4 Coding Unicode Strings; 36.5 Using 3.0 Bytes Objects; 36.6 Using 3.0 (and 2.6) bytearray Objects; 36.7 Using Text and Binary Files; 36.8 Using Unicode Files; 36.9 Other String Tool Changes in 3.0; 36.10 Chapter Summary; 36.11 Test Your Knowledge: Quiz; 36.12 Test Your Knowledge: Answers; Chapter 37: Managed Attributes; 37.1 Why Manage Attributes?; 37.2 Properties; 37.3 Descriptors; 37.4 __getattr__ and __getattribute__; 37.5 Example: Attribute Validations; 37.6 Chapter Summary; 37.7 Test Your Knowledge: Quiz; Chapter 38: Decorators; 38.1 Whats a Decorator?; 38.2 The Basics; 38.3 Coding Function Decorators; 38.4 Coding Class Decorators; 38.5 Managing Functions and Classes Directly; 38.6 Example: Private” and Public” Attributes; 38.7 Example: Validating Function Arguments; 38.8 Chapter Summary; 38.9 Test Your Knowledge: Quiz; 38.10 Test Your Knowledge: Answers; Chapter 39: Metaclasses; 39.1 To Metaclass or Not to Metaclass; 39.2 The Metaclass Model; 39.3 Declaring Metaclasses; 39.4 Coding Metaclasses; 39.5 Example: Adding Methods to Classes; 39.6 Example: Applying Decorators to Methods; 39.7 Chapter Summary; 39.8 Test Your Knowledge: Quiz; 39.9 Test Your Knowledge: Answers; Appendixes; Installation and Configuration; Installing the Python Interpreter; Configuring Python; For More Help; Solutions to End-of-Part Exercises; Part I, Getting Started; Part II, Types and Operations; Part III, Statements and Syntax; Part IV, Functions; Part V, Modules; Part VI, Classes and OOP; Part VII, Exceptions and Tools; Colophon;