1 00:00:00,390 --> 00:00:05,430 Convenience and it's are kind of in it's are useful for special cases. 2 00:00:05,430 --> 00:00:10,650 Imagine a situation where you have to initialize something but you don't have the arguments the way 3 00:00:10,680 --> 00:00:16,550 that you would usually have them you wouldn't have your name and email or a new ID or what not. 4 00:00:16,560 --> 00:00:22,440 Instead you have them in something like a dictionary or if you want to put some default values for whatever 5 00:00:22,440 --> 00:00:26,030 reason for those kind of situations we use a convenience. 6 00:00:26,040 --> 00:00:31,320 And it's not the only thing that's really important about convenience in it is that within the body 7 00:00:31,320 --> 00:00:34,780 of convenience it you have to initialize your object. 8 00:00:34,920 --> 00:00:37,330 Let's go ahead and see how we do that. 9 00:00:37,440 --> 00:00:45,390 I'm going to go to Scott start start your project on this fun going to Karlee convenience and it's 10 00:00:48,540 --> 00:00:50,030 my convenience in it. 11 00:00:50,040 --> 00:00:59,130 I have just like all the other times I have a class user and my class user has let's say only one attribute 12 00:00:59,190 --> 00:01:02,810 quality that name a string. 13 00:01:03,270 --> 00:01:12,920 And then I have one in it and in the end it's called input name string string which makes self name 14 00:01:13,040 --> 00:01:15,240 the com input name. 15 00:01:15,340 --> 00:01:18,220 Not if I want to write a convenience in it. 16 00:01:18,330 --> 00:01:19,410 This is what they should do. 17 00:01:19,410 --> 00:01:22,740 I should say convenience in it. 18 00:01:22,740 --> 00:01:28,230 And in my convenience and it's irrelevant of what you want to pass in as an argument. 19 00:01:28,260 --> 00:01:29,670 You must do this. 20 00:01:29,670 --> 00:01:32,230 You have to do a self in it. 21 00:01:32,280 --> 00:01:37,850 So I have to say self not in it one of these other ways of doing it. 22 00:01:37,860 --> 00:01:42,180 So I'm going to say solve it with No-Name for instance. 23 00:01:42,180 --> 00:01:45,160 So that's a convenience in it not you might be wondering. 24 00:01:45,180 --> 00:01:50,260 Well you're calling a method to call another method and nothing else is happening. 25 00:01:50,460 --> 00:01:53,840 Let me give you a practical example. 26 00:01:53,970 --> 00:01:59,670 Imagine you're reading some information from some backend and that can say they don't have any anything 27 00:01:59,670 --> 00:02:02,710 to do with classes or users or whatnot. 28 00:02:02,760 --> 00:02:09,510 Usually what you have in the back end is either integer or a string or an array or a dictionary for 29 00:02:09,510 --> 00:02:14,820 the most part and for the most part the important elements are arrays and dictionaries. 30 00:02:14,820 --> 00:02:20,580 So you have getting formations set in dictionaries or arrays and they don't know anything called a user 31 00:02:20,970 --> 00:02:26,040 for instance your user might be written in the backend like this. 32 00:02:26,040 --> 00:02:40,180 It might be that our user dictionary is going to be named the name key and that's going to be the name. 33 00:02:40,260 --> 00:02:40,770 All right. 34 00:02:40,770 --> 00:02:48,310 And they have another value let's say you I.D. She and that your I.D. key is a random set of characters 35 00:02:48,660 --> 00:02:52,420 so this is essentially what you get from your backend. 36 00:02:52,440 --> 00:02:57,970 You don't get a class coming from your back and you get information in a user dictionary. 37 00:02:58,120 --> 00:03:05,430 And now I want to use this information to produce new objects from my User class in a situation like 38 00:03:05,430 --> 00:03:05,800 that. 39 00:03:05,820 --> 00:03:15,270 And when I say Well give me a convenience in it it's an input dictionary which is of the type dictionary 40 00:03:15,300 --> 00:03:16,610 with a string. 41 00:03:16,920 --> 00:03:22,710 You could write a string on a string because we know there are strings or and when I say a string and 42 00:03:22,830 --> 00:03:26,320 in me just so that we can see more of the options. 43 00:03:26,550 --> 00:03:34,950 So my convenience any 3:13 put dictionary with these values so theoretically it would be like this erotically 44 00:03:34,980 --> 00:03:40,410 I would have remember this came from that. 45 00:03:41,160 --> 00:03:50,610 So now I'm going to say like my user is a user and which one I can use can I use it with the name or 46 00:03:50,610 --> 00:03:51,960 can I use a dictionary. 47 00:03:51,960 --> 00:03:54,510 So it's a lot better to use a dictionary one. 48 00:03:54,570 --> 00:03:58,240 I'm going to say dictionary and that is the use of the dictionary. 49 00:03:58,500 --> 00:04:06,720 So now I have a new user being produced by this dictionary and I have to do whatever I have to do inside 50 00:04:06,720 --> 00:04:07,640 my ends. 51 00:04:07,680 --> 00:04:15,390 So in my in it I'm going to say let's just call it the read the name to be. 52 00:04:15,420 --> 00:04:25,980 What is it going to be is going to be input a dictionary value for name she which I know it exists as 53 00:04:26,700 --> 00:04:29,400 what they want as with one does a string. 54 00:04:29,400 --> 00:04:33,190 The reason I need the as a string is because this is a need. 55 00:04:33,450 --> 00:04:37,070 I don't know whether the values in my dictionary. 56 00:04:37,080 --> 00:04:40,570 So that's one I'm going to call this one. 57 00:04:40,570 --> 00:04:42,810 Read you ID. 58 00:04:43,110 --> 00:04:45,000 And that's going to be for you. 59 00:04:45,030 --> 00:04:47,450 I d k as a string. 60 00:04:47,550 --> 00:04:54,840 And then when I'm initialising I'm going to say self in it the right thing and that's all. 61 00:04:54,850 --> 00:05:00,060 That of course in this example I'm not using my you ID if you want it it would actually add another 62 00:05:00,060 --> 00:05:00,590 one. 63 00:05:00,720 --> 00:05:02,860 So this is a you ID that's a string. 64 00:05:02,880 --> 00:05:09,020 It has to have an input ID and that must be a string string. 65 00:05:09,030 --> 00:05:11,920 I'm constantly making this mistake. 66 00:05:11,990 --> 00:05:15,530 Self-taught ID is going to be Impe with your ID. 67 00:05:15,810 --> 00:05:19,680 And then of course here we had to do this one as well. 68 00:05:19,770 --> 00:05:24,870 Say well that is going to be the read that you ID. 69 00:05:24,900 --> 00:05:29,650 So convenience us are used for situations such as this one. 70 00:05:29,760 --> 00:05:37,080 Our information for a specific cases are not being fed to us Devey that are in the talks in a way that 71 00:05:37,110 --> 00:05:44,100 we do whatever we have to do and then we call our in it is Slyne essentially is the most important aspect 72 00:05:44,100 --> 00:05:47,220 of a convenience any recording yourself. 73 00:05:47,360 --> 00:05:50,580 It based on well whatever you have in there. 74 00:05:50,760 --> 00:05:52,910 So that's about the convenience. 75 00:05:52,920 --> 00:05:58,380 It's a question that you might be having and that is for you if you're really perceptive at this level 76 00:05:58,380 --> 00:05:59,600 is up. 77 00:05:59,610 --> 00:06:07,770 Is this a safe way of assuming dictionary has a value called Name Key and it has a string. 78 00:06:07,770 --> 00:06:11,200 And to be honest no that is not a safe way of doing it. 79 00:06:11,220 --> 00:06:15,960 And since this is not a safe way of doing this what I'm going to do is I'm going to use God. 80 00:06:16,020 --> 00:06:17,960 So let's go ahead and do that. 81 00:06:17,970 --> 00:06:28,240 So you know I'm going to say God and what I'm guarding is this guy as optional a string. 82 00:06:28,350 --> 00:06:32,150 And this guy is optional a string. 83 00:06:32,400 --> 00:06:37,970 And if that was the case great if it wasn't the case return the net. 84 00:06:38,340 --> 00:06:41,130 Now let me quickly explain what happened in here. 85 00:06:41,130 --> 00:06:44,000 And you can see how it works. 86 00:06:44,010 --> 00:06:44,630 All right. 87 00:06:44,880 --> 00:06:47,950 I'm saying go for this two values. 88 00:06:47,970 --> 00:06:52,980 Read the name or read the name to be in put dictionary name key. 89 00:06:52,980 --> 00:06:55,890 If it did exist and if it was a string. 90 00:06:56,010 --> 00:07:01,110 So this idea will fail which takes us to the else or it will succeed. 91 00:07:01,140 --> 00:07:05,040 Which takes us to read you ID which does exactly the same thing. 92 00:07:05,130 --> 00:07:12,570 Either this succeeds finds a strange new idea and puts in the red your ID or it fails and goes in here. 93 00:07:12,810 --> 00:07:20,670 Now if we do end up in here if we do end up in the else we have to return and we have to say well this 94 00:07:20,670 --> 00:07:23,580 method failed if we couldn't do anything. 95 00:07:23,580 --> 00:07:25,240 Let's skip that for a second. 96 00:07:25,260 --> 00:07:27,590 Let's assume we did succeed. 97 00:07:27,600 --> 00:07:32,690 And if it did succeed it means that now we can go ahead and do a self in it. 98 00:07:32,820 --> 00:07:38,460 So in doing a search for it if we put the two values on we do everything as we used to do it before. 99 00:07:38,490 --> 00:07:44,560 Now here's an error in here that excludes telling excludes saying you can't return men. 100 00:07:44,610 --> 00:07:46,750 I'm assuming you need to work. 101 00:07:46,770 --> 00:07:48,570 No matter what. 102 00:07:48,630 --> 00:07:56,220 Only a failure will that it can return and which means your convenience in it should convert into a 103 00:07:56,220 --> 00:07:57,660 favorable one. 104 00:07:57,660 --> 00:08:02,730 There is a chance that it doesn't work so I'm going write this here. 105 00:08:02,730 --> 00:08:10,770 A failure will fail but in it it means there's something in it that it might actually fail. 106 00:08:10,980 --> 00:08:13,360 Otherwise it will work. 107 00:08:13,440 --> 00:08:14,730 So that's about that. 108 00:08:14,730 --> 00:08:23,250 With that in mind we covered a Class V in its inconvenience in its unfavorable convenience limits and 109 00:08:23,250 --> 00:08:28,430 we used it with the dictionary which is a very common practice when reading information from our backend. 110 00:08:28,650 --> 00:08:31,380 And with that we'll move on to our next lesson.