Let’s split your code…
Part1: create function do_twice(f)
, that will run f()
two times.
def do_twice(f):
f()
f()
Part2: create a function called print_spam()
that will print()
the word "spam"
def print_spam():
print('spam')
Part3: call the function print_spam()
inside the do_twice()
funtion
do_twice(print_spam)
This way, your code will ‘think’ something like this:
“Oh he called do_twice(print_spam)
! Now I must run print_spam()
two times, since print_spam()
replaces the f()
role.”
solved Why Does This Python Function Print Twice?