Python 函数作为参数
Python function as argument
我无法将其他函数作为参数传递给另一个函数。
我在单独的文件中有函数 marks_as_done 和 delete_task
tasks.py
def mark_as_done(conn):
print("marking")
try:
id = int(input("Enter id of task: "))
command = f"UPDATE tasklist SET finish_date='{dt_string}', status='True' WHERE id={id}"
db_execute(command, conn)
except ValueError:
print("Wrong! Enter only number.")
def delete_task(conn):
print("deleting")
try:
id = int(input("Enter id of task: "))
command = f"DELETE from tasklist WHERE id={id}"
db_execute(command, conn)
except ValueError:
print("Wrong! Enter only number.")
我创建了函数 what_next,其中一些参数是函数。
main.py
def what_next(func1, func2, conn, option1, option2):
print("Starting what next function")
try:
x = int(input(f"\n1 - {option1}\n2 - {option2}\n3 - Back to menu\n4 - Exit from app\nChoose what you want to do: "))
print(f"First This is x from what next function: {x}")
match x:
case 1:
print(f"This is x from what next function: {x}")
func1
case 2:
print(f"This is x from what next function: {x}")
func2
case 3:
print(f"This is x from what next function: {x}")
mainFunc()
case 4:
print(f"This is x from what next function: {x}")
conn.close()
sys.exit()
case _:
print("Number out fo range.")
mainFunc()
except ValueError:
print("Wrong! Enter only number.")
我在主函数中调用了这个函数:
main.py
def mainFunc():
print("\n--------------------------Welcome in ToDoList app!--------------------------")
while True:
print("""\nMenu:\n1 - Add task\n2 - Show active tasks\n3 - Show tasks history\n4 - Search taks\n5 - Exit\n""")
try:
choose = int(input("Choose what you wany to do: "))
match choose:
case 1:
while True:
add_task(conn)
what_next(conn, "Continue", "", None, None)
case 2:
while True:
show_active_task(conn)
#what_next_temp(conn)
what_next(mark_as_done(conn), delete_task(conn), conn, "Mark as done", "Delete task")
case 3:
show_task_history(conn)
what_next(conn, "Continue", "", None, None)
case 4:
pass
case 5:
conn.close()
sys.exit()
case _:
print("Number out fo range.")
mainFunc()
except ValueError:
print("Wrong! Enter only number.")
我在这种情况下,当我在主函数中选择“2”并在情况 2 中输入时,show_active_task(conn) 函数被正确调用但之后而不是 what_next、marks_as_done和 delete_task 被执行并且在 what_next
之后
更新
最后,我将 what_next 函数更改为:
def what_next(func1, func2, conn, option1, option2):
print("Starting what next function")
try:
x = int(input(f"\n1 - {option1}\n2 - {option2}\n3 - Back to menu\n4 - Exit from app\nChoose what you want to do: "))
print(f"First This is x from what next function: {x}")
match x:
case 1:
print(f"This is x from what next function: {x}")
func1(conn)
case 2:
print(f"This is x from what next function: {x}")
func2(conn)
case 3:
print(f"This is x from what next function: {x}")
mainFunc()
case 4:
print(f"This is x from what next function: {x}")
conn.close()
sys.exit()
case _:
print("Number out fo range.")
mainFunc()
except ValueError:
print("Wrong! Enter only number.")
我这样称呼它:
what_next(mark_as_done, delete_task, conn, "Mark as done", "Delete task")
what_next(mark_as_done(conn), delete_task(conn), conn, "Mark as done", "Delete task")
这是一个函数调用,你实际上把delete_task(conn)
的return值传给了what_next
。如果你要传递一个函数本身,只使用它的名字,就像这样:
what_next(mark_as_done, delete_task, conn, "Mark as done", "Delete task")
然后就可以在what_next中调用这些函数了。但是确实可以把conn
和函数打包在一起,这叫做“函数闭包”:
what_next(lambda : mark_as_done(conn), lambda : delete_task(conn), conn, "Mark as done", "Delete task")
我无法将其他函数作为参数传递给另一个函数。
我在单独的文件中有函数 marks_as_done 和 delete_task
tasks.py
def mark_as_done(conn):
print("marking")
try:
id = int(input("Enter id of task: "))
command = f"UPDATE tasklist SET finish_date='{dt_string}', status='True' WHERE id={id}"
db_execute(command, conn)
except ValueError:
print("Wrong! Enter only number.")
def delete_task(conn):
print("deleting")
try:
id = int(input("Enter id of task: "))
command = f"DELETE from tasklist WHERE id={id}"
db_execute(command, conn)
except ValueError:
print("Wrong! Enter only number.")
我创建了函数 what_next,其中一些参数是函数。
main.py
def what_next(func1, func2, conn, option1, option2):
print("Starting what next function")
try:
x = int(input(f"\n1 - {option1}\n2 - {option2}\n3 - Back to menu\n4 - Exit from app\nChoose what you want to do: "))
print(f"First This is x from what next function: {x}")
match x:
case 1:
print(f"This is x from what next function: {x}")
func1
case 2:
print(f"This is x from what next function: {x}")
func2
case 3:
print(f"This is x from what next function: {x}")
mainFunc()
case 4:
print(f"This is x from what next function: {x}")
conn.close()
sys.exit()
case _:
print("Number out fo range.")
mainFunc()
except ValueError:
print("Wrong! Enter only number.")
我在主函数中调用了这个函数:
main.py
def mainFunc():
print("\n--------------------------Welcome in ToDoList app!--------------------------")
while True:
print("""\nMenu:\n1 - Add task\n2 - Show active tasks\n3 - Show tasks history\n4 - Search taks\n5 - Exit\n""")
try:
choose = int(input("Choose what you wany to do: "))
match choose:
case 1:
while True:
add_task(conn)
what_next(conn, "Continue", "", None, None)
case 2:
while True:
show_active_task(conn)
#what_next_temp(conn)
what_next(mark_as_done(conn), delete_task(conn), conn, "Mark as done", "Delete task")
case 3:
show_task_history(conn)
what_next(conn, "Continue", "", None, None)
case 4:
pass
case 5:
conn.close()
sys.exit()
case _:
print("Number out fo range.")
mainFunc()
except ValueError:
print("Wrong! Enter only number.")
我在这种情况下,当我在主函数中选择“2”并在情况 2 中输入时,show_active_task(conn) 函数被正确调用但之后而不是 what_next、marks_as_done和 delete_task 被执行并且在 what_next
之后更新
最后,我将 what_next 函数更改为:
def what_next(func1, func2, conn, option1, option2):
print("Starting what next function")
try:
x = int(input(f"\n1 - {option1}\n2 - {option2}\n3 - Back to menu\n4 - Exit from app\nChoose what you want to do: "))
print(f"First This is x from what next function: {x}")
match x:
case 1:
print(f"This is x from what next function: {x}")
func1(conn)
case 2:
print(f"This is x from what next function: {x}")
func2(conn)
case 3:
print(f"This is x from what next function: {x}")
mainFunc()
case 4:
print(f"This is x from what next function: {x}")
conn.close()
sys.exit()
case _:
print("Number out fo range.")
mainFunc()
except ValueError:
print("Wrong! Enter only number.")
我这样称呼它:
what_next(mark_as_done, delete_task, conn, "Mark as done", "Delete task")
what_next(mark_as_done(conn), delete_task(conn), conn, "Mark as done", "Delete task")
这是一个函数调用,你实际上把delete_task(conn)
的return值传给了what_next
。如果你要传递一个函数本身,只使用它的名字,就像这样:
what_next(mark_as_done, delete_task, conn, "Mark as done", "Delete task")
然后就可以在what_next中调用这些函数了。但是确实可以把conn
和函数打包在一起,这叫做“函数闭包”:
what_next(lambda : mark_as_done(conn), lambda : delete_task(conn), conn, "Mark as done", "Delete task")