Classes let you bundle code behavior and state together.
In this chapter, you’re setting your webapp aside while you learn about creating Python classes. You’re doing this in order to get to the point where you can create a context manager with the help of a Python class. As creating and using classes is such a useful thing to know about anyway, we’re dedicating this chapter to them. We won’t cover everything about classes, but we’ll touch on all the bits you’ll need to understand in order to confidently create the context manager your webapp is waiting for. Let’s dive in and see what’s involved.
At stated at the end of the last chapter, understanding how to hook your setup and teardown code into Python’s
with statement is straightforward...assuming you know how to create a Python class.
Despite being well over halfway through this book, you’ve managed to get by without having to define a class. You’ve written useful and reusable code using nothing more than Python’s function machinery. There are other ways to write and organize your code, and object orientation is very popular.
You’re never forced to program exclusively in the object-oriented paradigm when using Python, and the language is flexible when it comes to how you go about writing your code. But, when it comes to hooking into the ...