When you go to order a coffee to the barista, do you say I want a coffee or do you say I want a coffee
in a cup?
Whatever you would say in both cases, you would always get a coffee in a cup.
So people don't say, I want the coffee in a cup, because that is the default argument.
So it is a default input.
The barista always knows that the coffee should be served in a cup.
The same goes in here.
We always use this to do that text file.
So why should we mention it always when when we order the function, When we call the function, when
we order the coffee.
So we do that here and there and there multiple times.
Well, there is a solution for that, and the solution is default arguments or default parameters,
whatever terminology you use.
I'll explain the terminology as well.
So to make this a default parameter.
All you have to do and let's start with get to this function.
All you have to do is assign the value to dos the text to the parameter.
So now this is a default parameter, and therefore we say that this call has a default argument.
So we don't have to provide the actual value of the argument.
So in this case, both terminologies are used interchangeably.
Default argument, default parameter.
So therefore we can remove all these argument values from all the get to this function, which is four
of them like that.
And here as well, the fourth one.
So if I run the code now and I try, for example, the show methods, things will work the same.
The benefits here is that if you ever want to change the value of that parameter, you can simply specify
the new value.
For example, let's say to use one dot text.
So I just changed the value in the get to this call in that condition.
So let me try the code now, run mine and I'll try to add something so that this line is executed.
So add my new to do press enter and we get an error this time.
So it says that no such file or directory to this one dot text the error.
Can be traced in here.
So first it occurred in line.
19.
So errors can be read from top to bottom.
So this is where the error started.
Line 19 and here.
So Python try to call this function and this function itself try to call something else which you see
in here.
So line two.
Is the next one to be executed.
So this one here.
And that line tried to open the two does one the text file, which does not exist.
That's why we get this error in line two.
So what I'm trying to demonstrate here is that you can change default arguments.
You can just give a value here, and that value will replace that value there.
So that's the idea with two fold arguments.
So let me go back here and remove that one and remove the value itself.
We don't need it.
And let's implement default parameters with the right to this function here as well.
So file path would be to do the text.
But to dos or is not a default parameter because we're always having a different list in each of these
conditions.
So in this add condition, the user adds a to do.
They provide it in the command line and then we read the existing to dos using that get to this function
up here and then we append the to do of the user to the list.
So now we have a new list here and we want to provide that new list.
To the right to this function.
So there's no way for the right to do this function to have an assumption on what this value could be.
So there is no assumption you want to keep.
Parameters as.
Non default parameters.
So that is a default parameter.
That is a known default parameter.
Now we have this error here, so it says non default parameter follows default parameters.
So this is a problem and it can be fixed by putting this non default parameter before the default parameters.
So that is the correct syntax.
Otherwise you will get an error.
That also means that you should change the positions of these.
Arguments when you call the function, because what's happening now is if I run the script, run main
and I add my new to do so, we get an error and it started in line.
23.
And here.
So this is the function code we just wrote, and then it traces back to line eight where the function
definition is.
And the error is that this function expected a string, right?
Like to dos the text, for example.
But in reality it got a list.
And that is because the order of the arguments matter here.
So if this is a first.
Value you place.
That value will go as the value of the first parameter.
So to do arc is actually.
The string while fall path is going to be the list.
So basically what we did is we set five paths.
Five path is equal to the list, which had like multiple to dos to do one and so on.
That's what we did.
So the open function is confused and it cannot open a file which has a path, a list, so it doesn't
make sense.
Therefore, you should respect the older now.
To do should go first.
And then to reduce the text or or if you don't like order, what you could do is you could.
Let me undo this.
So if you really want to have the order as you wish, then you could say fault path is equal to that
and to do.
Org is equal to that.
So if you explicitly mention the argument names, then you don't have to respect the order.
This will work now.
So show and add new order.
And new order is part of the two two items.
However, I wouldn't recommend this method unless you have to.
So just say.
To do so first and then to dos the text.
So to do that goes to to to talk to those that text goes to file path.
Actually, since file path is default as is a default parameter, you don't even have to pass file path.
So you just say to dos and that's it.
So if I run the ad.
We just made a default talk
show and that's the new to do.
So what we did is now we only mention that file path in there because it is not important.
We don't have to pollute our code with unnecessary details all the time.
Just like the cup, the coffee cup example.
You don't have to say all the time.
I want the coffee in a cup because that's the default behavior.
So let's see, what else should we change here, right to do?
Just remove to use the text and leave only that same here.
And that's about it.
Before I close the video, I want to also emphasize that I also built in functions can have default
parameters.
For example, if you say help input, you see the help string of the input function, you'll see that
the prompt.
Parameter is set by default to a non object.
So that makes it possible for you to just say inputs without providing a string.
So if you say like that, there will not be any string printed out here, no prompt.
You just enter this as a user and so on.
But if you provide that string, that string will be displayed in here.
Some functions, though, such as Len.
So this is a function that returns the length of a list or a string.
So, for example.
Hello?
It tells you it has five items, so five characters.
So if you do help.
Len, you'll see that this doesn't have a default parameter.
So the object is mandatory.
That means if you do, Len, with votes and arguments, you're going to get an error.
You see, Len takes exactly one argument, but you didn't give anything.
We would get the same error if we change this, get to this function, to this.
So if you just leave file path as a non default argument, then in get to dos, you don't mention the
value of that file path and if you execute the code now.
And you try to add a brand new to do you get an error in line 19 here.
So the function called tried to execute the function definition, but it couldn't because the function
calls should have gotten one argument file path.
So I hope that makes sense that if you have a default value for the parameter, then you don't have
to pass it here, otherwise you will get an error.
So that's about this video.
Thanks a lot.
See you later.