1 00:00:00,340 --> 00:00:05,369 Before we move on to a new lesson in our object oriented program. 2 00:00:05,380 --> 00:00:08,319 And I wanted to give you one more example 3 00:00:08,819 --> 00:00:12,590 so that you can really understand what it is that we're doing. 4 00:00:12,979 --> 00:00:15,239 So let's create a different class. OK? 5 00:00:15,250 --> 00:00:17,700 Let's do something related to cybersecurity. 6 00:00:18,139 --> 00:00:22,399 Let's say, for example, we wanted to create a class called user. 7 00:00:22,690 --> 00:00:23,260 OK? 8 00:00:23,850 --> 00:00:28,040 And the user accounts will have different attributes like the username, 9 00:00:28,340 --> 00:00:31,940 the role. OK? And then let's also create a method. 10 00:00:31,950 --> 00:00:35,520 Say for example, what can the user do? The user can log in? 11 00:00:35,720 --> 00:00:36,619 OK? So 12 00:00:36,759 --> 00:00:40,000 let's do that. So I'm going to remove all of this code 13 00:00:42,279 --> 00:00:45,740 and at the very top, I'm just gonna say uh user 14 00:00:46,680 --> 00:00:48,479 uh user class. OK? So 15 00:00:48,580 --> 00:00:51,900 how do we create our user? We say class 16 00:00:52,270 --> 00:00:54,430 and then user colon 17 00:00:54,759 --> 00:00:55,939 and now what do we do? 18 00:00:56,259 --> 00:00:59,330 We have to define the attributes 19 00:00:59,529 --> 00:01:03,200 that will be associated with each user account. 20 00:01:03,209 --> 00:01:06,010 So I'm gonna say define underscore in it 21 00:01:06,650 --> 00:01:07,669 underscore 22 00:01:07,919 --> 00:01:11,430 and now in brackets, what do we have, we have self because we have to 23 00:01:11,569 --> 00:01:14,260 make a reference to each individual user account. 24 00:01:14,639 --> 00:01:16,949 So I'm gonna say self and then the attributes will 25 00:01:16,959 --> 00:01:18,739 be username and let's just go with the wall. 26 00:01:18,750 --> 00:01:19,669 OK? One more. Just 27 00:01:20,900 --> 00:01:22,220 hold on and that's it. 28 00:01:22,720 --> 00:01:24,180 What's going to be the next thing to do? 29 00:01:24,190 --> 00:01:28,239 The next thing will not be to assign variables to each of our attributes. So 30 00:01:28,410 --> 00:01:31,500 I'm gonna say self dot username 31 00:01:31,940 --> 00:01:34,040 equals username 32 00:01:34,430 --> 00:01:35,540 pretty straightforward. 33 00:01:36,129 --> 00:01:40,800 And then self dot role should be equal to what role 34 00:01:41,250 --> 00:01:42,040 there it is. 35 00:01:42,489 --> 00:01:46,849 OK? So we've defined the attributes now, what about the methods? 36 00:01:47,309 --> 00:01:50,389 Let's create just one method called log in. 37 00:01:50,559 --> 00:01:52,970 OK? So user can actually log in. So 38 00:01:53,360 --> 00:01:57,690 I'm gonna say def and then log in and then in brackets, 39 00:01:58,190 --> 00:02:00,370 what's it going to be? It's going to be self, 40 00:02:01,169 --> 00:02:03,199 always self colon. 41 00:02:03,410 --> 00:02:07,029 And now what should happen once a user has logged in? 42 00:02:07,239 --> 00:02:08,389 Let us print, 43 00:02:09,377 --> 00:02:10,639 hey, welcome message. 44 00:02:10,899 --> 00:02:12,479 So I'm going to use the F string 45 00:02:13,429 --> 00:02:16,389 codes and then we can say welcome and then 46 00:02:16,639 --> 00:02:20,850 let Python fill in the rest of the information right there. So welcome 47 00:02:21,229 --> 00:02:22,169 and then 48 00:02:22,789 --> 00:02:24,800 uh self username 49 00:02:25,259 --> 00:02:26,160 and there it is, 50 00:02:26,330 --> 00:02:28,250 that's all we need to do. 51 00:02:28,669 --> 00:02:30,649 And now last but not least 52 00:02:30,949 --> 00:02:32,479 let us create 53 00:02:32,750 --> 00:02:37,169 an object basically a user account and then let's call our method, 54 00:02:37,320 --> 00:02:38,410 right? So 55 00:02:38,690 --> 00:02:41,210 I'm gonna say admin 56 00:02:42,589 --> 00:02:47,649 underscore user, this can represent our first account which will be for an admin, 57 00:02:47,979 --> 00:02:50,449 it's gonna be equal to what user. 58 00:02:50,899 --> 00:02:51,649 And now in brackets, 59 00:02:51,660 --> 00:02:54,649 we have to fill in the values of the attributes which are username and 60 00:02:55,529 --> 00:02:59,169 the username. In this case, right now is gonna be Alex. OK? 61 00:02:59,179 --> 00:03:01,630 User name is Alex, sorry in codes 62 00:03:02,369 --> 00:03:06,779 Alex. And then the role of Alex is obviously that of 63 00:03:07,470 --> 00:03:09,270 a administrator. So 64 00:03:09,830 --> 00:03:10,360 admind 65 00:03:10,820 --> 00:03:13,149 administrator. 66 00:03:13,880 --> 00:03:14,649 OK? 67 00:03:15,380 --> 00:03:20,339 And then finally let us call our method. So it's gonna be admin 68 00:03:20,649 --> 00:03:23,770 underscore user dot log in 69 00:03:24,160 --> 00:03:25,149 brackets. 70 00:03:26,039 --> 00:03:28,179 And if this program works, 71 00:03:28,559 --> 00:03:32,889 it should say something like welcome Alex. 72 00:03:33,889 --> 00:03:34,800 Let's run 73 00:03:35,160 --> 00:03:36,039 and there you go. 74 00:03:36,330 --> 00:03:38,059 Welcome Alex. 75 00:03:38,369 --> 00:03:42,779 So this has been another example of how to create a class, 76 00:03:43,089 --> 00:03:45,240 how to define the attributes 77 00:03:45,380 --> 00:03:49,960 for each object in that class. And then we also created a method 78 00:03:50,149 --> 00:03:52,580 for each individual object 79 00:03:52,770 --> 00:03:56,854 and then we of course created an actual object and 80 00:03:56,865 --> 00:04:01,774 then finally called the method associated with the object. 81 00:04:01,895 --> 00:04:05,494 So hopefully you now have a better understanding 82 00:04:05,664 --> 00:04:06,214 of. 83 00:04:06,585 --> 00:04:06,975 Oop. 84 00:04:07,255 --> 00:04:09,414 Thank you for watching the video. I'll see you in the next class.