Chapter 19. OOP: The Big Picture
So far in this book, we’ve been using the term “object” generically. Really, the code written up to this point has been object-based—we’ve 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
class—a device used to implement new kinds
of objects in Python. Classes are Python’s main
object-oriented programming (OOP) tool, so
we’ll 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 we’ll
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
inheritance—a mechanism of code customization and reuse, above
and beyond anything we’ve seen so far.
One note up front: Python OOP is entirely optional, and you don’t 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. They’re 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 ...
Get Learning Python, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.