When a function has default arguments you can still pass a different one, but the point is if do not pass any it will use the default.
For example, when you say opened_file = open(file_name)
, the open()
is built-in Python function, and it has a ton of parameters, but you are only using one of them, the file
parameter, to pass the file_name
. As all the other parameters have been set to default arguments, you do not get an error for not passing anything to them. Here is the open()
with all its parameters:
open(file , mode='r' , buffering=-1 , encoding=None , errors=None , newline=None , closed=True, opener=None)
You can see what each of these parameters means here in the documentation. But just to give you an example, the mode
parameter specifies the mode in which you want the file to be opened. As default, this parameter is set to 'r'
which means open in reading mode. If you want to write something, you can set this parameter to 'w'
and the file will be opened in writing mode and then you would wirte:
opened_file = open(file_name, mode='w')
But the point is, as the parameter is set to 'r'
as default it will not raise and error if you do not pass anything because the function has its own default operating mode.
If you consider the file
parameter, however, it can’t have a default argument because who knows which file each user will need to open, right? So, it makes sense to have a default argument set to some parameters in some functions, but not to others.
I hope this helps.