Solution Explanation

# INITIAL CODE
def open_dataset(file_name):
    
    opened_file = open(file_name)
    from csv import reader
    read_file = reader(opened_file)
    data = list(read_file)
    
    return data
# 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()

Hello, can anyone please explain why the last line of the solution is “apps_data = open_dataset()”?

I don’t understand why apps_data would be assigned to the function. This part confuses me a bit.

Any help is appreciated. Thank you!!! :slight_smile:

Hi!
There seem to be a bit of confusion on your side. Let’s deal your question step by step.

  1. You’ve got a open_dataset() function which is defined as below:
    def open_dataset(file_name='AppleStore.csv'):
    From it’s definition we can tell that this function accepts only one argument, file_name, which has got its default value, 'AppleStore.csv'
  2. What does the code in the function’s body do?
    It opens the file the name of which you pass to the function when you call it, reads it and saves it as a list object.
  3. What does the return statement tell us?
    It tells us that the function returns a variable data which is actually a list object appeared after reading the file.
  4. What happens when you call this function?
    apps_data = open_dataset() (this line actually contains two actions: calling the function and assigning its result to a variable apps_data)
    There’s no argument passed to the function. But we remember that the function has got a default value of the argument needed which is 'AppleStore.csv'. So, the function takes in the 'AppleStore.csv' as its argument processes it (opens, reads and saves as a list object) and returns the saved dataset which is then assigned to the apps_data

Hopefully my explanation will help you with your doubts.

1 Like

THANK YOU @ksenia.kustanovich. That makes a million times more sense.

I appreciate you taking the time to answer it!!

1 Like

Glad that it helped you! Good luck on your path:-)