Convenience and it's are kind of in it's are useful for special cases.
Imagine a situation where you have to initialize something but you don't have the arguments the way
that you would usually have them you wouldn't have your name and email or a new ID or what not.
Instead you have them in something like a dictionary or if you want to put some default values for whatever
reason for those kind of situations we use a convenience.
And it's not the only thing that's really important about convenience in it is that within the body
of convenience it you have to initialize your object.
Let's go ahead and see how we do that.
I'm going to go to Scott start start your project on this fun going to Karlee convenience and it's
my convenience in it.
I have just like all the other times I have a class user and my class user has let's say only one attribute
quality that name a string.
And then I have one in it and in the end it's called input name string string which makes self name
the com input name.
Not if I want to write a convenience in it.
This is what they should do.
I should say convenience in it.
And in my convenience and it's irrelevant of what you want to pass in as an argument.
You must do this.
You have to do a self in it.
So I have to say self not in it one of these other ways of doing it.
So I'm going to say solve it with No-Name for instance.
So that's a convenience in it not you might be wondering.
Well you're calling a method to call another method and nothing else is happening.
Let me give you a practical example.
Imagine you're reading some information from some backend and that can say they don't have any anything
to do with classes or users or whatnot.
Usually what you have in the back end is either integer or a string or an array or a dictionary for
the most part and for the most part the important elements are arrays and dictionaries.
So you have getting formations set in dictionaries or arrays and they don't know anything called a user
for instance your user might be written in the backend like this.
It might be that our user dictionary is going to be named the name key and that's going to be the name.
All right.
And they have another value let's say you I.D. She and that your I.D. key is a random set of characters
so this is essentially what you get from your backend.
You don't get a class coming from your back and you get information in a user dictionary.
And now I want to use this information to produce new objects from my User class in a situation like
that.
And when I say Well give me a convenience in it it's an input dictionary which is of the type dictionary
with a string.
You could write a string on a string because we know there are strings or and when I say a string and
in me just so that we can see more of the options.
So my convenience any 3:13 put dictionary with these values so theoretically it would be like this erotically
I would have remember this came from that.
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
can I use a dictionary.
So it's a lot better to use a dictionary one.
I'm going to say dictionary and that is the use of the dictionary.
So now I have a new user being produced by this dictionary and I have to do whatever I have to do inside
my ends.
So in my in it I'm going to say let's just call it the read the name to be.
What is it going to be is going to be input a dictionary value for name she which I know it exists as
what they want as with one does a string.
The reason I need the as a string is because this is a need.
I don't know whether the values in my dictionary.
So that's one I'm going to call this one.
Read you ID.
And that's going to be for you.
I d k as a string.
And then when I'm initialising I'm going to say self in it the right thing and that's all.
That of course in this example I'm not using my you ID if you want it it would actually add another
one.
So this is a you ID that's a string.
It has to have an input ID and that must be a string string.
I'm constantly making this mistake.
Self-taught ID is going to be Impe with your ID.
And then of course here we had to do this one as well.
Say well that is going to be the read that you ID.
So convenience us are used for situations such as this one.
Our information for a specific cases are not being fed to us Devey that are in the talks in a way that
we do whatever we have to do and then we call our in it is Slyne essentially is the most important aspect
of a convenience any recording yourself.
It based on well whatever you have in there.
So that's about the convenience.
It's a question that you might be having and that is for you if you're really perceptive at this level
is up.
Is this a safe way of assuming dictionary has a value called Name Key and it has a string.
And to be honest no that is not a safe way of doing it.
And since this is not a safe way of doing this what I'm going to do is I'm going to use God.
So let's go ahead and do that.
So you know I'm going to say God and what I'm guarding is this guy as optional a string.
And this guy is optional a string.
And if that was the case great if it wasn't the case return the net.
Now let me quickly explain what happened in here.
And you can see how it works.
All right.
I'm saying go for this two values.
Read the name or read the name to be in put dictionary name key.
If it did exist and if it was a string.
So this idea will fail which takes us to the else or it will succeed.
Which takes us to read you ID which does exactly the same thing.
Either this succeeds finds a strange new idea and puts in the red your ID or it fails and goes in here.
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
method failed if we couldn't do anything.
Let's skip that for a second.
Let's assume we did succeed.
And if it did succeed it means that now we can go ahead and do a self in it.
So in doing a search for it if we put the two values on we do everything as we used to do it before.
Now here's an error in here that excludes telling excludes saying you can't return men.
I'm assuming you need to work.
No matter what.
Only a failure will that it can return and which means your convenience in it should convert into a
favorable one.
There is a chance that it doesn't work so I'm going write this here.
A failure will fail but in it it means there's something in it that it might actually fail.
Otherwise it will work.
So that's about that.
With that in mind we covered a Class V in its inconvenience in its unfavorable convenience limits and
we used it with the dictionary which is a very common practice when reading information from our backend.
And with that we'll move on to our next lesson.