|
| 1 | +Object-Oriented Programming |
| 2 | +=========================== |
| 3 | + |
| 4 | +Managing Larger Programs |
| 5 | +------------------------ |
| 6 | + |
| 7 | +\index{object-oriented} |
| 8 | + |
| 9 | +At the beginning of this book, we came up with four basic programming |
| 10 | +patterns which we use to construct programs: |
| 11 | + |
| 12 | +* Sequential code |
| 13 | +* Conditional code (if statements) |
| 14 | +* Repetitive code (loops) |
| 15 | +* Store and reuse (functions) |
| 16 | + |
| 17 | +In later chapters, we explored simple variables as well as collection |
| 18 | +data structures like lists, tuples, and dictionaries. |
| 19 | + |
| 20 | +As we build programs, we use design data structures and write code |
| 21 | +to manipulate those data structures. There are many ways to write programs |
| 22 | +and by now, you probably have written some programs that are "not so elegant" |
| 23 | +and other programs that are "more elegant". Even though your programs |
| 24 | +may be small, you are starting to see how there is a bit of "art" and |
| 25 | +"asthetic" to writing code. |
| 26 | + |
| 27 | +As programs get to be millions of lines long, it becomes increasingly important |
| 28 | +to write code that is easy to understand. If you are working on a million line |
| 29 | +program, you can never keep the entire program in your mind at the same time. |
| 30 | +So we need ways to break the program into multiple smaller pieces so to solve |
| 31 | +a problem, fix a bug, or add a new feature we have less to look at. |
| 32 | + |
| 33 | +In a way, object oriented programming is a way to arrange your code |
| 34 | +so that you can zoom into 500 lines of the code, and understand it |
| 35 | +while ignoring the other 999,500 lines of code for the moment. |
| 36 | + |
| 37 | +Getting Started |
| 38 | +--------------- |
| 39 | + |
| 40 | +Like many aspects of programming it is necessary to learn the concepts of |
| 41 | +object oriented programming before you can use them effectively and in a way |
| 42 | +you can understand in the programs that you produce. The purpose of this |
| 43 | +chapter is to give you a starting point for understanding object oriented |
| 44 | +programming. |
| 45 | + |
| 46 | +So approach this chapter as a way to learn some terms and concepts and work |
| 47 | +through a few simple examples to lay a foundation for future learning. |
| 48 | +Throughout the rest of the book we will be using objects in many of |
| 49 | +the programs but we won't be writing programs using object-oriented approaches. |
| 50 | + |
| 51 | +The key outcome of this chapter is to have a basic understanding of how |
| 52 | +objects are constructed and how they function and most importantly how |
| 53 | +we make use of the capabilities of objects that are provided to us by Python |
| 54 | +and Python libraries. |
| 55 | + |
| 56 | +Using Objects |
| 57 | +------------- |
| 58 | + |
| 59 | +It turns our we have been using objects all along in this class. Python |
| 60 | +provides us built-in objects. Here is some simple code that at this point |
| 61 | +should feel very simple and natural to you. |
| 62 | + |
| 63 | +\index{dictionary object} |
| 64 | + |
| 65 | +\VerbatimInput{../code3/oo01.py} |
| 66 | + |
| 67 | +But instead of focusing on what these lines accomplish, lets look at what is |
| 68 | +really happening from the point of view of object-oriented programming. |
| 69 | +Don't worry if the following paragraphs don't make any sense the first time you read them because we have not yet defined all these terms. |
| 70 | + |
| 71 | +The first line is *constructing* an object of type *list*, the second and third |
| 72 | +lines are calling the `append()` *method*, the fourth line is calling |
| 73 | +the `sort()` *method*, and the fifth line is looking up the item at position 0. |
| 74 | + |
| 75 | +It turns out that the fifth line is calling the `__getitem__()` method in the |
| 76 | +list object with a parameter of zero. When Python sees the square bracket |
| 77 | +syntax after a variable name it calls the `__getitem__()` method |
| 78 | +to allow the object to perform the lookup. |
| 79 | + |
| 80 | +And we have been looking at the output of the `dir()` function all along: |
| 81 | + |
| 82 | + >>> stuff = list() |
| 83 | + >>> dir(stuff) |
| 84 | + ['__add__', '__class__', '__contains__', '__delattr__', |
| 85 | + '__delitem__', '__dir__', '__doc__', '__eq__', |
| 86 | + '__format__', '__ge__', '__getattribute__', '__getitem__', |
| 87 | + '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', |
| 88 | + '__iter__', '__le__', '__len__', '__lt__', '__mul__', |
| 89 | + '__ne__', '__new__', '__reduce__', '__reduce_ex__', |
| 90 | + '__repr__', '__reversed__', '__rmul__', '__setattr__', |
| 91 | + '__setitem__', '__sizeof__', '__str__', '__subclasshook__', |
| 92 | + 'append', 'clear', 'copy', 'count', 'extend', 'index', |
| 93 | + 'insert', 'pop', 'remove', 'reverse', 'sort'] |
| 94 | + >>> |
| 95 | + |
| 96 | +The precise definition of `dir()` is that it prints a list of the *methods* of |
| 97 | +a Python object. |
| 98 | + |
| 99 | +The rest of this chapter will define all of the above terms so make sure to |
| 100 | +come back after you finish the chapter and re-read the above paragraphs to |
| 101 | +check your understanding. |
| 102 | + |
| 103 | +Starting with Programs |
| 104 | +---------------------- |
| 105 | + |
| 106 | +A program in its most basic form takes some input, does some processing, and |
| 107 | +produces some output. Our elevator conversion program demonstrates a very |
| 108 | +short but complete program showing all three of these steps. |
| 109 | + |
| 110 | +\VerbatimInput{../code3/elev.py} |
| 111 | + |
| 112 | +If we think a bit more about this program, there is the "outside world" and |
| 113 | +the program. The input and output aspects are where the program interacts |
| 114 | +with the outside world. Within the program we have code and data to accomplish |
| 115 | +the task the program is designed to solve. |
| 116 | + |
| 117 | + |
| 118 | + |
| 119 | +When we are "in" the program, we have some defined interactions with the |
| 120 | +"outside" world, but those interactions are well defined and generally |
| 121 | +not something we focus on. While we are coding we worry only about the |
| 122 | +details "inside the program". |
| 123 | + |
| 124 | +One way to think about object oriented programming is that we are separating |
| 125 | +our program into multiple "zones". Each "zone" contains some code and data |
| 126 | +(like a program) and has well defined interactions with the outside world |
| 127 | +and the other zones within the program. |
| 128 | + |
| 129 | +If we look back at the link extraction application where we used the |
| 130 | +BeautifulSoup library, we can see a program that is constructed by connecting |
| 131 | +different objects together to accomplish a task: |
| 132 | + |
| 133 | +\index{BeautifulSoup} |
| 134 | +\index{HTML} |
| 135 | +\index{parsing!HTML} |
| 136 | + |
| 137 | +\VerbatimInput{../code3/urllinks.py} |
| 138 | + |
| 139 | +We read the URL into a string, and then pass that into `urllib` to retrieve |
| 140 | +the data from the web. The `urllib` library uses the `socket` library to |
| 141 | +make the actual network connection to retrieve the data. We take the string |
| 142 | +that we get back from `urllib` and hand it to BeautifulSoup for parsing. |
| 143 | +BeautifulSoup makes use of another object called `html.parser`^[https://docs.python.org/3/library/html.parser.html] and retuens us an object. We call the |
| 144 | +`tags()` method in the returned object and then get a dictionary of tag |
| 145 | +objects, and loop through the tags and call the `get()` method for each tag |
| 146 | +to print out the 'href' attribute. |
| 147 | + |
| 148 | + |
| 149 | + |
| 150 | +We can draw a picture of this program and how the objects work together. |
| 151 | + |
| 152 | +The key here is not to fully understand how this program works but to see |
| 153 | +how we build a network of interacting objects and orchestrate the movement |
| 154 | +of information between the objects to create a program. It is also important |
| 155 | +to note that when you looked at that program several chapters back, you could |
| 156 | +fully understand what was going on in the program without even realizing |
| 157 | +that the program was "orchestrating the movement of data between objects". |
| 158 | +Back then it was just lines of code that got the job done. |
| 159 | + |
| 160 | +Subdividing a Problem - Encapsulation |
| 161 | +------------------------------------- |
| 162 | + |
| 163 | +One of the advantages of the object oriented approach is that it can |
| 164 | +hide complexity. For example, while we need to know how to use the |
| 165 | +`urllib` and BeaautifulSoup code, we do not need to know how those libraries |
| 166 | +work internally. It allows us to focus on the part of the problem we need to |
| 167 | +solve and ignore the other parts of the program. |
| 168 | + |
| 169 | + |
| 170 | + |
| 171 | +This ability to focus on a part of a program that we care about and ignore |
| 172 | +the rest of the program is also helpful to the developers of the objects. For |
| 173 | +example the programmers developing BeautifulSoup do not need to |
| 174 | +know or care about how we retrieve our HTML page, what parts we want to |
| 175 | +read or what we plan to do with the data we extract from the web page. |
| 176 | + |
| 177 | + |
| 178 | + |
| 179 | +\index{encapsulation} |
| 180 | + |
| 181 | +Another word we use to capture this idea that we ignore the internal |
| 182 | +detail of objects we use is "encapsulation". This means that we can |
| 183 | +know how to use an object without knowing how it internally acomplishes |
| 184 | +what we need done. |
| 185 | + |
| 186 | +Our First Python Object |
| 187 | +----------------------- |
| 188 | + |
| 189 | +At its simplest, an object is some code plus data structures that is |
| 190 | +smaller than a whole program. Defining a function allows us to store |
| 191 | +a bit of code and give it a name and then later invoke that code using the |
| 192 | +name of the function. |
| 193 | + |
| 194 | +An object can contain a number of functions (which we call "methods") as |
| 195 | +well as data that is used by those functions. We call data items that are |
| 196 | +part of the object "attributes". |
| 197 | + |
| 198 | +\index{class keyword} |
| 199 | + |
| 200 | +We use the `class` keyword to define the data and code that will make up each |
| 201 | +of the objects. The class keyword includes the name of the class and begins |
| 202 | +an indented block of code where we include the attributes (data) |
| 203 | +and methods (code). |
| 204 | + |
| 205 | +\VerbatimInput{../code3/oo02.py} |
| 206 | + |
| 207 | +Each method looks like a function, starting with the `def` keyword and |
| 208 | +consisting of an indended block of code. This example has one attribute (x) |
| 209 | +and one method (party). The methods have a special first parameter that |
| 210 | +we name by convention `self`. |
| 211 | + |
| 212 | +Much like the `def` keyword does not cause function code to be executed, the |
| 213 | +`class` keyword does not create an object. Instead, the `class` |
| 214 | +keyword defines a template indicating what will be contained in each object |
| 215 | +of type `PartyAnimal`. The class is like a cookie cutter and the objects |
| 216 | +created using the class are the cookies. You don't put frosting on the cookie |
| 217 | +you put frosting on the cookies - and you can put different frosting |
| 218 | +on each cookie. |
| 219 | + |
| 220 | + |
| 221 | + |
| 222 | + |
| 223 | + |
| 224 | + |
0 commit comments