Multiple return statement

My code:

INITIAL CODE

def open_dataset(file_name=‘AppleStore.csv’, header = True):
opened_file = open(file_name)
from csv import reader
read_file = reader(opened_file)
data = list(read_file)

if header:
    return data [1:]

return data

apps_data = open_dataset(file_name=‘AppleStore.csv’, header = True)

===================================================
Data Quest code :

SOLUTION CODE

def open_dataset(file_name=‘AppleStore.csv’, header=True):
opened_file = open(file_name)
from csv import reader
read_file = reader(opened_file)
data = list(read_file)

if header:
    return data[1:]
else:
    return data

apps_data = open_dataset()

===========
I have 2 questions ?

  1. When I write the function I write true or false based on what I want it is not a default ?
    I mean if I want to use it later I can write “” open_dataset(file_name=‘AppleStore.csv’, header = False) “” and it will go to the function above and execute the else part … ? am I right ? this part of the function is used upon my request ?

  2. Also the highlighted part of the code why in the data quest solution it used the function empty ?

  1. Yes, you are correct.
  2. Because parameters of the function have default values, which are used when you do not provide them.
1 Like

What you wrote and the dataquest solution is functionally the same.
The dataquest way of writing helps people narrow down the problem and makes it easier to get the logical flow, that the if, and else, are 2 events happening at same indentation level as a result of 1 decision. Your way of writing is at different indentation level, i feel it requires a little more effort to find the if that could affect data and reason about it.

It is usually good to specify defaults for every function you write. It reduces the burden of the user. Imaging you had to specify every parameter in https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html before you can use it, no one is going to use this library! Good api design would be breaking down functions into 3 or less input parameters, and also having defaults as much as possible. For your 2nd question, the function parameters are empty because in the function definition, there are default values specified, so the arguments in the definition become optional to the function user/caller, so there is no need to write anything.
Deciding which is the default sometimes depends on which is the most commonly used setting, or could be to maintain consistency with other functions to avoid confusion. You can subscribe to pandas issues to see how people argue about how to choose a default.