Review
Product Description
Portable, powerful, and a breeze to use, Python is the popular open source object-oriented programming language used for both standalone programs and scripting applications. Python is considered easy to learn, but there's no quicker way to mastery of the language than learning from an expert teacher. This edition of Learning Python puts you in the hands of two expert teachers, Mark Lutz and David Ascher, whose friendly, well-structured prose has guided many a programmer to proficiency with the language.
Learning Python, Second Edition, offers programmers a comprehensive learning tool for Python and object-oriented programming. Thoroughly updated for the numerous language and class presentation changes that have taken place since the release of the first edition in 1999, this guide introduces the basic elements of the latest release of Python 2.3 and covers new features, such as list comprehensions, nested scopes, and iterators/generators.
Beyond language features, this edition of Learning Python also includes new context for less-experienced programmers, including fresh overviews of object-oriented programming and dynamic typing, new discussions of program launch and configuration options, new coverage of documentation sources, and more. There are also new use cases throughout to make the application of language features more concrete.
The first part of Learning Python gives programmers all the information they'll need to understand and construct programs in the Python language, including types, operators, statements, classes, functions, modules and exceptions. The authors then present more advanced material, showing how Python performs common tasks by offering real applications and the libraries available for those applications. Each chapter ends with a series of exercises that will test your Python skills and measure your understanding.
Learning Python, Second Edition is a self-paced book that allows readers to focus on the core Python language in depth. As you work through the book, you'll gain a deep and complete understanding of the Python language that will help you to understand the larger application-level examples that you'll encounter on your own. If you're interested in learning Python--and want to do so quickly and efficiently--then Learning Python, Second Edition is your best choice.
From the Publisher
About the Author
Mark Lutz is an independent Python trainer, writer, and software developer, and is one of the primary figures in the Python community. He is the author of the O'Reilly books Programming Python and Python Pocket Reference (both in 2nd Editions), and co-author of Learning Python (both in 2nd Editions). Mark has been involved with Python since 1992, began teaching Python classes in 1997, and has instructed over 90 Python training sessions as of early 2003. In addition, he holds BS and MS degrees in computer science from the University of Wisconsin, and over the last two decades has worked on compilers, programming tools, scripting applications, and assorted client/server systems. Whenever Mark gets a break from spreading the Python word, he leads an ordinary, average life with his kids in Colorado. Mark can be reached by email at , or on the web at http://www.rmi.net/~lutz.
David Ascher is the lead for Python projects at ActiveState, including Komodo, ActiveState's integrated development environment written mostly in Python. David has taught courses about Python to corporations, in universities, and at conferences. He also organized the Python track at the 1999 and 2000 O'Reilly Open Source Conventions, and was the program chair for the 10th International Python Conference. In addition, he co-wrote Learning Python (both editions) and serves as a director of the Python Software Foundation. David holds a B.S. in physics and a Ph.D. in cognitive science, both from Brown University.
Excerpted from Learning Python by Mark Lutz, David Ascher. Copyright © 2004. Reprinted by permission. All rights reserved.
So far in this book, weve been using the term "object" generically. Really, the code written up to this point has been object-basedweve passed objects around, used them in expressions, called their methods, and so on. To qualify as being truly object-oriented (OO), though, objects generally need to also participate in something called an inheritance hierarchy.
This chapter begins the exploration of the Python classa device used to implement
new kinds of objects in Python. Classes are Pythons main object-oriented programming (OOP) tool, so well also look at OOP basics along the way in this part of the book. In Python, classes are created with a new statement: the class. As well see, the objects defined with classes can look a lot like the built-in types we saw earlier in the book. They will also support inheritancea mechanism of code customization and reuse, above and beyond anything weve seen so far.
One note up front: Python OOP is entirely optional, and you dont need to use classes just to get started. In fact, you can get plenty of work done with simpler constructs such as functions, or even simple top-level script code. But classes turn out to be one of the most useful tools Python provides, and we will show you why here. Theyre also employed in popular Python tools like the Tkinter GUI API, so most Python programmers will usually find at least a working knowledge of class basics helpful.
Why Use Classes?
Remember when we told you that programs do things with stuff? In simple terms, classes are just a way to define new sorts of stuff, which reflect real objects in your programs domain. For instance, suppose weve decided to implement that hypothetical pizza-making robot we used as an example in Chapter 12. If we implement it using classes, we can model more of its real-world structure and relationships:
Inheritance
Pizza-making robots are a kind of robot, and so possess the usual robot-y properties. In OOP terms, we say they inherit properties from the general category of all robots. These common properties need to be implemented only once for the general case and reused by all types of robots we may build in the future.
Composition
Pizza-making robots are really collections of components that work together as a team. For instance, for our robot to be successful, it might need arms to roll dough, motors to maneuver to the oven, and so on. In OOP parlance, our robot is an example of composition; it contains other objects it activates to do its bidding. Each component might be coded as a class, which defines its own behavior and relationships.
General OOP ideas like inheritance and composition apply to any application that can be decomposed into a set of objects. For example, in typical GUI systems, interfaces are written as collections of widgetsbuttons, labels, and so onwhich are all drawn when their container is drawn (composition). Moreover, we may be able to write our own custom widgetsbuttons with unique fonts, labels with new color schemes, and the likewhich are specialized versions of more general interface devices (inheritance).
From a more concrete programming perspective, classes are a Python program unit, just like functions and modules. They are another compartment for packaging logic and data. In fact, classes also define a new namespace much like modules. But compared to other program units weve already seen, classes have three critical distinctions that make them more useful when it comes to building new objects:
Multiple instances
Classes are roughly factories for generating one or more objects. Every time we call a class, we generate a new object, with a distinct namespace. Each object generated from a class has access to the classs attributes and gets a namespace of its own for data that varies per object.
Customization via inheritance
Classes also support the OOP notion of inheritance; they are extended by redefining their attributes outside the class itself. More generally, classes can build up namespace hierarchies, which define names to be used by objects created from classes in the hierarchy.
Operator overloading
By providing special protocol methods, classes can define objects that respond to the sorts of operations we saw work on built-in types. For instance, objects made with classes can be sliced, concatenated, indexed, and so on. Python provides hooks classes can use to intercept and implement any built-in type operation.
OOP from 30,000 Feet
Before we show what this all means in terms of code, wed like to say a few words about the general ideas behind OOP here. If youve never done anything objectoriented in your life before now, some of the words well be using in this chapter may seem a bit perplexing on the first pass. Moreover, the motivation for using such words may be elusive, until youve had a chance to study the ways that programmers apply them in larger systems. OOP is as much an experience as a technology.