This question already has an answer here:
- How to explain callbacks in plain english? How are they different from calling one function from another function? 32 answers
- What is a callback function? 21 answers
- Parallel Python: What is a callback? 5 answers
- How to use a callback function in python? 2 answers
- How to write a simple callback function? 5 answers
I am doing a course and it asks me to, "create a function that accepts a list and a callback function that returns a list of truthy values and another list of falsey values."
The answer I submitted was:
def partition(lst):
truthy_list = []
falsy_list = []
for i in lst:
if i in lst == bool(lst):
truthy_list.append(i)
else:
falsy_list.append(i)
return truthy_list
return falsy_list
The answer given by the instructor is:
def partition(lst, **fn**):
trues = []
falses = []
for val in lst:
if **fn(val)**:
trues.append(val)
else:
falses.append(val)
return [trues, falses]
It looks like everything is the same except the second part in the function defined "fn" and in the if statement. I am at a loss on what that second part is in the function and what the if statement is calling out. If someone can take me under their wing and explain to me what I am not understanding on this and what that fn(val) is calling out it would be much appreciated.
Aucun commentaire:
Enregistrer un commentaire