Functions Intermediate

https://app.dataquest.io/m/316/functions%3A-intermediate/3/default-arguments

SOLUTION CODE

def open_dataset(file_name=‘AppleStore.csv’):

opened_file = open(file_name)
from csv import reader
read_file = reader(opened_file)
data = list(read_file)

return data

apps_data = open_dataset()

In your solution code, when you pass in the name of the file in function definition, how does that become the default? Wouldn’t it be better to pass the file in when you call on the function?

1 Like

When you define a function you can set a default argument for every parameter. If you do it, then the function will work just fine even if the user does not inform any arguments to that parameter as the function will use the default.

I do not believe it is a matter of better or worse, its depends on what you trying to do with each function and its parameters and arguments. In this case, actually, it’s part of the exercise itself, as the instruction says:

  1. Without passing in any argument, use the open_dataset() function to open the AppleStore.csv file.

It clearly wants you to understand and practice default arguments.

2 Likes

Can you provide a better example of utilizing default parameters? You are then unable to pass anything into the function? What is the point of organizing the code into functions then?

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.

3 Likes

I guess my question still has not been answered, would it not be better to pass the name of a file in when calling on a function? That would make the function more reusable.

In this instance when you call the function, and don’t call any parameters, it just defaults to whatever the file was. Is this setting a default argument, or can another file be passed in?

1 Like

Yes, parameters such as files names usually do not have a default argument, however, as mentioned, this was part of the exercise, it just wants to show you what it is possible to do.

I’ve said multiple times you can pass a different argument. The default argument will only be used if you don’t pass any.

There is no other file uploaded to try passing into the function after setting the default parameter.

The exercise is just an example of how to use default arguments.

1 Like

It shows the importance of doing some things outside the browser.