In this recommended solution, what if I want the function to take 1st default value , but second value - as False?
# 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(file_name, header=False)
1 Like
You can omit first parameter with _
sign, like this:
apps_data = open_dataset(_, header=False)
perso1
October 6, 2019, 9:53pm
#3
If you are using naming parameters, you can just type :
open_dataset(header = False)
So python will know that False is for header and you do not need to specify file_name value.
For the if part you can use :
if header :
return data [1:]
return data
Because if header is true, then you will return data[1:]. So you are sure that return data will not be executed.
hanqi
October 7, 2019, 1:25am
#4
Be careful of doing open_dataset(_, header=False)
https://docs.python.org/3/reference/lexical_analysis.html#reserved-classes-of-identifiers
The special identifier _ is used in the interactive interpreter
to store the result of the last evaluation,
it is stored in the builtins module.
When not in interactive mode, _ has no special meaning and is not defined.
When in interactive mode like running in jupyter notebook, _
may not represent what you want.
1 Like