1 00:00:00,000 --> 00:00:04,190 In this module, we're going to talk about objects and classes. 2 00:00:04,190 --> 00:00:08,430 Python has many different kinds of data types: integers, 3 00:00:08,430 --> 00:00:13,640 floats, strings, lists, dictionaries, booleans. 4 00:00:13,640 --> 00:00:15,990 In Python, each is an object. 5 00:00:15,990 --> 00:00:19,020 Every object has the following: a type, 6 00:00:19,020 --> 00:00:24,150 internal representation, a set of functions called methods to interact with the data. 7 00:00:24,150 --> 00:00:26,850 An object is an instance of a particular type. 8 00:00:26,850 --> 00:00:29,110 For example, we have two types, 9 00:00:29,110 --> 00:00:31,170 type one and type two. 10 00:00:31,170 --> 00:00:34,470 We can have several objects of type one as shown in yellow. 11 00:00:34,470 --> 00:00:37,400 Each object is an instance of type one. 12 00:00:37,400 --> 00:00:41,060 We also have several objects of type two shown in green. 13 00:00:41,060 --> 00:00:44,000 Each object is an instance of type two. 14 00:00:44,000 --> 00:00:47,250 Let's do several less abstract examples. 15 00:00:47,250 --> 00:00:49,620 Every time we create an integer, 16 00:00:49,620 --> 00:00:52,130 we are creating an instance of type integer, 17 00:00:52,130 --> 00:00:54,780 or we are creating an integer object. 18 00:00:54,780 --> 00:01:01,480 In this case, we are creating five instances of type integer or five integer objects. 19 00:01:01,480 --> 00:01:04,480 Similarly, every time we create a list, 20 00:01:04,480 --> 00:01:06,870 we are creating an instance of type list, 21 00:01:06,870 --> 00:01:09,240 or we are creating a list object. 22 00:01:09,240 --> 00:01:15,320 In this case, we are creating five instances of type list or five list objects. 23 00:01:15,320 --> 00:01:19,330 We could find out the type of an object by using the type command. 24 00:01:19,330 --> 00:01:22,670 In this case, we have an object of type list, 25 00:01:22,670 --> 00:01:24,790 we have an object of type integer, 26 00:01:24,790 --> 00:01:26,780 we have an object of type string. 27 00:01:26,780 --> 00:01:29,680 Finally, we have an object of type dictionary. 28 00:01:29,680 --> 00:01:32,700 A class or type's methods are functions 29 00:01:32,700 --> 00:01:35,920 that every instance of that class or type provides. 30 00:01:35,920 --> 00:01:38,060 It's how you interact with the object. 31 00:01:38,060 --> 00:01:40,380 We have been using methods all this time, 32 00:01:40,380 --> 00:01:42,320 for example, on lists. 33 00:01:42,320 --> 00:01:46,450 Sorting is an example of a method that interacts with the data in the object. 34 00:01:46,450 --> 00:01:48,590 Consider the list ratings, 35 00:01:48,590 --> 00:01:51,810 the data is a series of numbers contained within the list. 36 00:01:51,810 --> 00:01:54,740 The method sort will change the data within the object. 37 00:01:54,740 --> 00:01:58,590 We call the method by adding a period at the end of the object's name, 38 00:01:58,590 --> 00:02:02,010 and the method's name we would like to call with parentheses. 39 00:02:02,010 --> 00:02:05,370 We have the rating's list represented in orange. 40 00:02:05,370 --> 00:02:08,660 The data contained in the list is a sequence of numbers. 41 00:02:08,660 --> 00:02:10,280 We call the sort method, 42 00:02:10,280 --> 00:02:12,910 this changes the data contained in the object. 43 00:02:12,910 --> 00:02:15,590 You can say it changes the state of the object. 44 00:02:15,590 --> 00:02:18,110 We can call the reverse method on the list, 45 00:02:18,110 --> 00:02:19,720 changing the list again. 46 00:02:19,720 --> 00:02:23,810 We call the method, reversing the order of the sequence within the object. 47 00:02:23,810 --> 00:02:28,300 In many cases, you don't have to know the inner workings of the class and its methods, 48 00:02:28,300 --> 00:02:30,110 you just have to know how to use them. 49 00:02:30,110 --> 00:02:33,170 Next, we will cover how to construct your own classes. 50 00:02:33,170 --> 00:02:36,450 You can create your own type or class in Python. 51 00:02:36,450 --> 00:02:38,970 In this section, you'll create a class. 52 00:02:38,970 --> 00:02:41,150 The class has data attributes. 53 00:02:41,150 --> 00:02:42,820 The class has methods. 54 00:02:42,820 --> 00:02:47,660 We then create instances or instances of that class or objects. 55 00:02:47,660 --> 00:02:50,870 The class data attributes define the class. 56 00:02:50,870 --> 00:02:52,980 Let's create two classes. 57 00:02:52,980 --> 00:02:54,910 The first class will be a circle, 58 00:02:54,910 --> 00:02:56,860 the second will be a rectangle. 59 00:02:56,860 --> 00:02:59,880 Let's think about what constitutes a circle. 60 00:02:59,880 --> 00:03:04,010 Examining this image, all we need is a radius to define a circle, 61 00:03:04,010 --> 00:03:06,130 and let's add color to make it easier to 62 00:03:06,130 --> 00:03:09,520 distinguish between different instances of the class later. 63 00:03:09,520 --> 00:03:13,530 Therefore, our class data attributes are radius and color. 64 00:03:13,530 --> 00:03:17,740 Similarly, examining the image in order to define a rectangle, 65 00:03:17,740 --> 00:03:19,270 we need the height and width. 66 00:03:19,270 --> 00:03:23,120 We will also add color to distinguish between instances later. 67 00:03:23,120 --> 00:03:27,040 Therefore, the data attributes are color, height, and width. 68 00:03:27,040 --> 00:03:29,090 To create the class circle, 69 00:03:29,090 --> 00:03:31,460 you will need to include the class definition. 70 00:03:31,460 --> 00:03:34,500 This tells Python you're creating your own class, 71 00:03:34,500 --> 00:03:36,050 the name of the class. 72 00:03:36,050 --> 00:03:37,920 For this course in parentheses, 73 00:03:37,920 --> 00:03:40,250 you will always place the term object, 74 00:03:40,250 --> 00:03:42,510 this is the parent of the class. 75 00:03:42,510 --> 00:03:44,210 For the class rectangle, 76 00:03:44,210 --> 00:03:45,830 we changed the name of the class, 77 00:03:45,830 --> 00:03:47,430 but the rest is kept the same. 78 00:03:47,430 --> 00:03:52,360 Classes are outlines we have to set the attributes to create objects. 79 00:03:52,360 --> 00:03:56,360 We can create an object that is an instance of type circle. 80 00:03:56,360 --> 00:03:58,590 The color data attribute is red, 81 00:03:58,590 --> 00:04:01,160 and the data attribute radius is four. 82 00:04:01,160 --> 00:04:05,520 We could also create a second object that is an instance of type circle. 83 00:04:05,520 --> 00:04:08,980 In this case, the color data attribute is green, 84 00:04:08,980 --> 00:04:11,520 and the data attribute radius is two. 85 00:04:11,520 --> 00:04:15,800 We can also create an object that is an instance of type rectangle. 86 00:04:15,800 --> 00:04:18,050 The color data attribute is blue, 87 00:04:18,050 --> 00:04:20,860 and the data attribute of height and width is two. 88 00:04:20,860 --> 00:04:24,480 The second object is also an instance of type rectangle. 89 00:04:24,480 --> 00:04:27,570 In this case, the color data attribute is yellow, 90 00:04:27,570 --> 00:04:28,970 and the height is one, 91 00:04:28,970 --> 00:04:30,590 and the width is three. 92 00:04:30,590 --> 00:04:34,560 We now have different objects of class circle or type circle. 93 00:04:34,560 --> 00:04:38,820 We also have different objects of class rectangle or type rectangle. 94 00:04:38,820 --> 00:04:42,180 Let us continue building the circle class in Python. 95 00:04:42,180 --> 00:04:43,950 We define our class. 96 00:04:43,950 --> 00:04:47,580 We then initialize each instance of the class with data attributes, 97 00:04:47,580 --> 00:04:50,570 radius, and color using the class constructor. 98 00:04:50,570 --> 00:04:53,290 The function init is a constructor. 99 00:04:53,290 --> 00:04:57,220 It's a special function that tells Python you are making a new class. 100 00:04:57,220 --> 00:05:01,380 There are other special functions in Python to make more complex classes. 101 00:05:01,380 --> 00:05:04,990 The radius and color parameters are used to 102 00:05:04,990 --> 00:05:09,500 initialize the radius and color data attributes of the class instance. 103 00:05:09,500 --> 00:05:14,570 The self parameter refers to the newly created instance of the class. 104 00:05:14,570 --> 00:05:19,170 The parameters, radius, and color can be used in the constructors body to 105 00:05:19,170 --> 00:05:24,230 access the values passed to the class constructor when the class is constructed. 106 00:05:24,230 --> 00:05:25,980 We could set the value of 107 00:05:25,980 --> 00:05:30,490 the radius and color data attributes to the values passed to the constructor method. 108 00:05:30,490 --> 00:05:34,320 Similarly, we can define the class rectangle in Python. 109 00:05:34,320 --> 00:05:36,380 The name of the class is different. 110 00:05:36,380 --> 00:05:40,910 This time, the class data attributes are color, height, and width. 111 00:05:40,910 --> 00:05:43,070 After we've created the class, 112 00:05:43,070 --> 00:05:45,730 in order to create an object of class circle, 113 00:05:45,730 --> 00:05:47,350 we introduce a variable. 114 00:05:47,350 --> 00:05:49,440 This will be the name of the object. 115 00:05:49,440 --> 00:05:52,920 We create the object by using the object constructor. 116 00:05:52,920 --> 00:05:57,970 The object constructor consists of the name of the class as well as the parameters. 117 00:05:57,970 --> 00:06:00,320 These are the data attributes. 118 00:06:00,320 --> 00:06:02,360 When we create a circle object, 119 00:06:02,360 --> 00:06:04,340 we call the code like a function. 120 00:06:04,340 --> 00:06:07,500 The arguments passed to the circle constructor are used to 121 00:06:07,500 --> 00:06:12,110 initialize the data attributes of the newly created circle instance. 122 00:06:12,110 --> 00:06:15,050 It is helpful to think of self as a box that 123 00:06:15,050 --> 00:06:18,070 contains all the data attributes of the object. 124 00:06:18,070 --> 00:06:21,920 Typing the object's name followed by a dot and the data attribute 125 00:06:21,920 --> 00:06:26,150 name gives us the data attribute value, for example, radius. 126 00:06:26,150 --> 00:06:28,400 In this case, the radius is 10. 127 00:06:28,400 --> 00:06:30,280 We can do the same for color. 128 00:06:30,280 --> 00:06:34,450 We can see the relationship between the self parameter and the object. 129 00:06:34,450 --> 00:06:39,020 In Python, we can also set or change the data attribute directly. 130 00:06:39,020 --> 00:06:43,460 Typing the object's name followed by a dot and the data attribute name, 131 00:06:43,460 --> 00:06:46,480 and set it equal to the corresponding value. 132 00:06:46,480 --> 00:06:50,200 We can verify that the color data attribute has changed. 133 00:06:50,200 --> 00:06:53,010 Usually, in order to change the data in an object, 134 00:06:53,010 --> 00:06:55,270 we define methods in the class. 135 00:06:55,270 --> 00:06:57,380 Let's discuss methods. 136 00:06:57,380 --> 00:07:01,860 We have seen how data attributes consist of the data defining the objects. 137 00:07:01,860 --> 00:07:05,730 Methods are functions that interact and change the data attributes, 138 00:07:05,730 --> 00:07:08,870 changing or using the data attributes of the object. 139 00:07:08,870 --> 00:07:12,010 Let's say we would like to change the size of a circle. 140 00:07:12,010 --> 00:07:14,790 This involves changing the radius attribute. 141 00:07:14,790 --> 00:07:18,590 We add a method, add radius to the class circle. 142 00:07:18,590 --> 00:07:22,810 The method has a function that requires the self as well as other parameters. 143 00:07:22,810 --> 00:07:26,290 In this case, we are going to add a value to the radius, 144 00:07:26,290 --> 00:07:31,910 We denote that value as r. We are going to add r to the data attribute radius. 145 00:07:31,910 --> 00:07:34,670 Let's see how this part of the code works when we create 146 00:07:34,670 --> 00:07:37,420 an object and call the add_radius method. 147 00:07:37,420 --> 00:07:40,940 As before, we create an object with the object constructor. 148 00:07:40,940 --> 00:07:43,440 We pass two arguments to the constructor. 149 00:07:43,440 --> 00:07:46,830 The radius is set to two and the color is set to red. 150 00:07:46,830 --> 00:07:48,450 In the constructor's body, 151 00:07:48,450 --> 00:07:50,080 the data attributes are set. 152 00:07:50,080 --> 00:07:54,150 We can use the box analogy to see the current state of the object. 153 00:07:54,150 --> 00:07:58,910 We call the method by adding a dot followed by the method, name, and parentheses. 154 00:07:58,910 --> 00:08:03,130 In this case, the argument of the function is the amount we would like to add. 155 00:08:03,130 --> 00:08:07,100 We do not need to worry about the self parameter when calling the method. 156 00:08:07,100 --> 00:08:08,930 Just like with the constructor, 157 00:08:08,930 --> 00:08:11,470 Python will take care of that for us. 158 00:08:11,470 --> 00:08:13,600 In many cases, there may not be 159 00:08:13,600 --> 00:08:17,530 any parameters other than self specified in the method's definition. 160 00:08:17,530 --> 00:08:20,870 So we don't pass any arguments when calling the function. 161 00:08:20,870 --> 00:08:24,150 Internally, the method is called with a value of eight, 162 00:08:24,150 --> 00:08:26,400 and the proper self object. 163 00:08:26,400 --> 00:08:30,590 The method assigns a new value to self radius. 164 00:08:30,590 --> 00:08:32,220 This changes the object, 165 00:08:32,220 --> 00:08:34,800 in particular, the radius data attribute. 166 00:08:34,800 --> 00:08:37,380 When we call the add_radius method, 167 00:08:37,380 --> 00:08:42,100 this changes the object by changing the value of the radius data attribute. 168 00:08:42,100 --> 00:08:46,390 We can add default values to the parameters of a class as constructor. 169 00:08:46,390 --> 00:08:50,410 In the labs, we also create the method called drawCircle. 170 00:08:50,410 --> 00:08:53,660 See the lab for the implementation of drawCircle. 171 00:08:53,660 --> 00:08:58,480 In the labs, we can create a new object of type circle using the constructor. 172 00:08:58,480 --> 00:09:01,730 The color will be red and the radius will be three. 173 00:09:01,730 --> 00:09:04,630 We can access the data attribute radius. 174 00:09:04,630 --> 00:09:06,950 We can access the attribute color. 175 00:09:06,950 --> 00:09:11,070 Finally, we can use the method drawCircle to draw the circle. 176 00:09:11,070 --> 00:09:14,890 Similarly, we can create a new object of type circle. 177 00:09:14,890 --> 00:09:17,860 We can access the data attribute of radius. 178 00:09:17,860 --> 00:09:20,360 We can access the data attribute color. 179 00:09:20,360 --> 00:09:23,920 We can use the method drawCircle to draw the circle. 180 00:09:23,920 --> 00:09:26,590 In summary, we have created an object of 181 00:09:26,590 --> 00:09:30,680 class circle called RedCircle with a radius attribute of three, 182 00:09:30,680 --> 00:09:32,640 and a color attribute of red. 183 00:09:32,640 --> 00:09:36,590 We also created an object of class circle called BlueCircle, 184 00:09:36,590 --> 00:09:40,760 with a radius attribute of 10 and a color attribute of blue. 185 00:09:40,760 --> 00:09:44,240 In the lab, we have a similar class for rectangle. 186 00:09:44,240 --> 00:09:48,290 We can create a new object of type rectangle using the constructor. 187 00:09:48,290 --> 00:09:51,020 We can access a data attribute of height. 188 00:09:51,020 --> 00:09:54,330 We can also access the data attribute of width. 189 00:09:54,330 --> 00:09:57,300 We could do the same for the data attribute of color. 190 00:09:57,300 --> 00:10:01,510 We can use the method drawRectangle to draw the rectangle. 191 00:10:01,510 --> 00:10:03,210 So we have a class, 192 00:10:03,210 --> 00:10:07,660 an object that is a realization or instantiation of that class. 193 00:10:07,660 --> 00:10:11,720 For example, we can create two objects of class Circle, 194 00:10:11,720 --> 00:10:14,460 or two objects of class Rectangle. 195 00:10:14,460 --> 00:10:17,910 The dir function is useful for obtaining the list 196 00:10:17,910 --> 00:10:21,170 of data attributes and methods associated with a class. 197 00:10:21,170 --> 00:10:24,580 The object you're interested in is passed as an argument. 198 00:10:24,580 --> 00:10:28,500 The return value is a list of the objects data attributes. 199 00:10:28,500 --> 00:10:32,180 The attribute surrounded by underscores are for internal use, 200 00:10:32,180 --> 00:10:34,140 and you shouldn't have to worry about them. 201 00:10:34,140 --> 00:10:37,910 The regular looking attributes are the ones you should concern yourself with. 202 00:10:37,910 --> 00:10:39,290 These are the objects, 203 00:10:39,290 --> 00:10:41,290 methods, and data attributes. 204 00:10:41,290 --> 00:10:44,440 There is a lot more you can do with objects in Python. 205 00:10:44,440 --> 00:10:47,000 Check Python.org for more info. 206 00:10:47,000 --> 00:10:52,000 (Music)