1 00:00:00,150 --> 00:00:05,530 Let us know practice defining classes and objects in the Jupiter note. 2 00:00:06,780 --> 00:00:11,430 And here we have the glass object. 3 00:00:11,490 --> 00:00:13,380 Got this glass. 4 00:00:13,380 --> 00:00:17,500 God has two class attributes. 5 00:00:17,670 --> 00:00:26,910 The number of readings for all the cars is Ford and I also want to keep track of the number of objects 6 00:00:27,450 --> 00:00:32,930 that we are going to create using this class God. 7 00:00:33,030 --> 00:00:40,470 And as we have discussed class attributes are defined outside of all the methods. 8 00:00:40,470 --> 00:00:51,190 Next the first method that has to be defined in a class is the init method also known as special methods 9 00:00:51,310 --> 00:01:02,560 are done the methods double underscore methods and the init method has fought parameters self modern 10 00:01:02,920 --> 00:01:13,690 brand and it so in our last class we had talked about the self parameter the self parameter refers to 11 00:01:13,690 --> 00:01:24,460 the current instance current object so using cell we can access all the instance attributes for the 12 00:01:24,460 --> 00:01:27,570 specific instance that you had working with. 13 00:01:27,850 --> 00:01:37,120 So in the definition or in the body of the init method we have some assignment statements. 14 00:01:37,120 --> 00:01:45,850 The first assignment statement is self-taught model is equal to model self-taught model is the instance 15 00:01:46,030 --> 00:01:55,270 attribute that as the variable model that we see here is equal to or it refers to the parameter that 16 00:01:55,270 --> 00:02:03,430 is being passed through the in it method the next assignment statement is self doubt brand is equal 17 00:02:03,430 --> 00:02:04,250 to brand. 18 00:02:04,380 --> 00:02:15,080 So self Dodge brand refers to the instance attribute whereas the variable brand here refers to the parameter 19 00:02:15,080 --> 00:02:18,140 brand coming through the init method. 20 00:02:18,230 --> 00:02:27,480 Similar is the case with the attribute self dot your next coming to the last the last assignment statement 21 00:02:27,510 --> 00:02:38,550 you're in the class definition the instance attribute referenced by fixing the attribute name but the 22 00:02:38,610 --> 00:02:49,960 self parameter as we can see here and also in the method that we define beacon reference. 23 00:02:49,990 --> 00:02:59,320 These instance attributes by fixing these self parameter coming to the class attribute. 24 00:02:59,650 --> 00:03:08,670 We need to access the class attributes by prefix in the class name and we use a dot operator. 25 00:03:08,710 --> 00:03:11,610 So God dot number of objects. 26 00:03:11,710 --> 00:03:17,320 And here as we said we are going to keep track of the number of objects that are being created using 27 00:03:17,320 --> 00:03:23,140 this class but every object we are going to increment this class attribute. 28 00:03:23,140 --> 00:03:27,790 Next we have defined three other methods. 29 00:03:27,790 --> 00:03:36,550 Start engine and then we are going to also display the details of all the attributes of an object and 30 00:03:36,550 --> 00:03:45,210 then we have defined another method emergency depending on the value assigned to the variable airbag. 31 00:03:45,340 --> 00:03:48,670 The if statement within this method gets executed. 32 00:03:49,120 --> 00:03:58,150 So now let's create objects using this class the procedure to create an object as similar to a function 33 00:03:58,150 --> 00:03:58,950 call. 34 00:03:59,020 --> 00:04:04,700 So we are going to pass arguments to the class name. 35 00:04:04,870 --> 00:04:15,790 So here is of a class card and we are going to parse the arguments for the parameters model brand and 36 00:04:15,840 --> 00:04:25,720 here and then we don't have to pass the self parameter or any argument for the self parameter because 37 00:04:25,720 --> 00:04:35,120 Python automatically passes the object itself as the first argument or like let's execute this line. 38 00:04:37,270 --> 00:04:46,890 And then again display this object lit display this object. 39 00:04:47,130 --> 00:04:55,200 So in the output we see this C V is an object of the class God 40 00:04:58,060 --> 00:05:09,270 in this memory location now in order to axis instance attribute outside the class definition. 41 00:05:09,270 --> 00:05:11,850 We need to use the object name. 42 00:05:11,850 --> 00:05:15,330 This is how the notation goes. 43 00:05:15,330 --> 00:05:17,950 It c odd v dot. 44 00:05:18,030 --> 00:05:24,300 Say for example I want to access the model the attribute model lets execute this. 45 00:05:24,300 --> 00:05:29,730 And here we have the model for the object T V. 46 00:05:29,790 --> 00:05:39,120 This is a notation used for accessing any of the instance attributes object name dot instance name so 47 00:05:39,510 --> 00:05:45,440 object name is C V and this is the instance variable. 48 00:05:45,690 --> 00:05:55,330 MARTIN Let's axis another instance attribute c c v dart here. 49 00:05:55,350 --> 00:05:57,990 So here we have the year of manufacture. 50 00:05:58,500 --> 00:06:08,520 Now we have defined a variable in the class number of objects and we said that with each instance this 51 00:06:08,610 --> 00:06:12,330 class attribute gets incremented by 1. 52 00:06:12,330 --> 00:06:16,980 So now let's see the value of this class attribute. 53 00:06:17,010 --> 00:06:26,420 So in order to access this attribute using the class so we give the class name Dar and then the name 54 00:06:26,480 --> 00:06:30,320 of the attribute number of objects. 55 00:06:30,320 --> 00:06:33,880 So we have created one object. 56 00:06:33,920 --> 00:06:36,330 So this has been implemented by one. 57 00:06:36,470 --> 00:06:44,350 We have also discussed that class attributes are shared by all of the instances that are created. 58 00:06:44,360 --> 00:06:55,820 We can also axes the class attribute using the instant name using the instant. 59 00:06:55,850 --> 00:06:57,970 This is how we do see. 60 00:06:58,040 --> 00:07:03,520 We dart number of objects. 61 00:07:03,520 --> 00:07:06,610 So the value is the same. 62 00:07:06,610 --> 00:07:14,830 If we access a class attribute using the class name or the instance name we have also and find another 63 00:07:14,830 --> 00:07:19,530 class attribute number of readers. 64 00:07:19,540 --> 00:07:22,060 So let's access this variable. 65 00:07:23,050 --> 00:07:26,840 So the number of reload is four. 66 00:07:27,160 --> 00:07:33,580 We will now create and another instance let's name this gorilla 67 00:07:36,810 --> 00:07:45,610 and then not despise these arguments 0 0 2 is demanded. 68 00:07:45,610 --> 00:07:51,880 The brand is Toyota and then the year of manufacture. 69 00:07:52,080 --> 00:07:55,030 So this object has been created. 70 00:07:55,080 --> 00:07:57,360 No let's display this 71 00:08:00,260 --> 00:08:11,660 so the output here means this object Corolla is an object of the class card stored at this memory address 72 00:08:12,890 --> 00:08:15,860 so we have created another object. 73 00:08:15,860 --> 00:08:23,600 So let's little axis the class attribute number of objects. 74 00:08:23,630 --> 00:08:33,560 So now this class attribute has been incremented by 1 again when we created this object T V the self 75 00:08:33,950 --> 00:08:46,350 parameter refers to this instance of this object see are we so the instance attribute self-taught model. 76 00:08:46,620 --> 00:09:00,000 Now becomes c v dot modern and the variable model here refers to the value coming from the init method. 77 00:09:02,280 --> 00:09:12,870 The second instance I attribute a self-taught brand and this becomes c v dot brand 78 00:09:16,880 --> 00:09:21,610 and the variable brand def goes to the value coming from the init method. 79 00:09:23,090 --> 00:09:28,220 Later we have created the instance Corolla 80 00:09:31,020 --> 00:09:42,720 so the self parameter now refers to this instance Corona so that means this time the instance attribute 81 00:09:42,720 --> 00:09:44,360 self-taught model. 82 00:09:44,370 --> 00:09:56,740 Now becomes controller dot Martin and the variable model here refers to devalue coming from the init 83 00:09:56,740 --> 00:09:57,180 method. 84 00:09:58,030 --> 00:10:06,930 So self but amateur allows us to access all the instance variables for the specific instance that we 85 00:10:06,930 --> 00:10:08,260 are working with. 86 00:10:08,500 --> 00:10:15,790 Let us now display the instance attributes for this for the object controller. 87 00:10:16,360 --> 00:10:26,450 So 0 0 2 is the model that we have passed to the class God when creating this object Corona and then 88 00:10:27,140 --> 00:10:32,090 controller Dot brand the stay order. 89 00:10:32,160 --> 00:10:37,310 Next we will perform some functions on these objects. 90 00:10:37,310 --> 00:10:42,530 So the first method that we have defined is dot engine method. 91 00:10:42,530 --> 00:10:49,850 This is how we call the method c v dot start engine. 92 00:10:49,850 --> 00:10:56,570 We have not parsed any parameters to the method start engine. 93 00:10:56,570 --> 00:11:06,970 If you see the definition of this method start engine it has one parameter so that's because we must 94 00:11:06,970 --> 00:11:15,550 specify self the self parameter explicitly when defining the method but we don't have to include it 95 00:11:15,940 --> 00:11:25,390 when calling the method because Python passes the object name or the instance name it automatically 96 00:11:25,390 --> 00:11:26,470 for us. 97 00:11:26,590 --> 00:11:28,220 So let's execute this. 98 00:11:28,780 --> 00:11:35,930 This cell so the print function has displayed this output. 99 00:11:37,040 --> 00:11:43,880 Now let's call another method using the second object that we have created. 100 00:11:43,910 --> 00:11:48,840 Controller dot didn't execute. 101 00:11:49,560 --> 00:12:00,000 So here we have the attributes and their values and we have also defined another method emergency and 102 00:12:01,760 --> 00:12:04,990 the method emergency has two parameters. 103 00:12:05,000 --> 00:12:08,270 One is the cell and that is airbag. 104 00:12:08,450 --> 00:12:16,790 We can either pass through the Boolean value true or false to this variable so let's call this method 105 00:12:17,630 --> 00:12:22,400 emergency so controller dot emergency. 106 00:12:22,650 --> 00:12:24,800 We don't have to pass the self parameter. 107 00:12:24,790 --> 00:12:31,950 Python does this for us automatically it passes the instant name to the search parameter and then we 108 00:12:31,950 --> 00:12:44,100 have the ID by let's say this is true and then execute the if condition within this method. 109 00:12:45,360 --> 00:12:54,970 Has been evaluated to true so the brain function has displayed this text. 110 00:12:54,990 --> 00:13:03,270 The instance attributes self-taught model self-taught brain and said Dot Yard have been defined in the 111 00:13:03,540 --> 00:13:13,480 input method but we were able to access these attributes in this method that is detailed. 112 00:13:13,750 --> 00:13:21,730 So that means these instance attributes are global to this instance. 113 00:13:21,940 --> 00:13:33,130 So the global variables can be accessed from anywhere or any method defined in this class. 114 00:13:33,130 --> 00:13:41,230 But coming to this variable airbag that we have defined in this in the method emergency. 115 00:13:41,230 --> 00:13:48,080 This is a local variable so it can only be accessed inside this method. 116 00:13:48,100 --> 00:13:48,880 Emergency.