[英]python pass function with argument to another function
我正在尝试创建一个将两个函数作为参数的函数,并且出现错误。
我所做的是创建了一个名为“ parent”的函数,该函数将2个函数作为参数(ux和ui)。 但是ux也有参数,它从父函数获取参数:
import ID_Ui as Ui
ui = Ui.admin_panel()
# ui returns something like this :
'''Please choose one of the options below :
Please press 0 if you want to exit the program .
Please press 1 if you want to exit to main menu .
Please press 2 if you want exit to previous section .
Please press 3 if you want to create/modify user account .
Please press 4 if you want to create/modify company account .
Please press 5 if you want to create/modify document type .
Please press 6 if you want to create/modify document ID . '''
def child(*ui):
user_choice = ui
if user_choice == 3:
return 3
elif user_choice == 4:
return 4
elif user_choice == 5:
return 5
else:
return 'error'
def parent(ux, ui):
user_choice = ui
x = user_choice
if user_choice == 0:
return 0
elif user_choice == 1:
return 1
elif user_choice == 2:
return 2
else:
ux(x)
print(parent(child(), ui))
我希望当我输入[0:2]以外的数字时,它将运行ux函数并将x变量作为参数传递给它。 但是我收到有关ux(x)行的以下错误:
TypeError: 'int' object is not callable
首先,我假设您正在调用的menu_conditions
函数实际上是parent
函数,但是我认为您应该对此进行修复。
其次,在最后一行运行child()
时,它返回一个数字或字符串error
。 如果您希望能够在parent
文件夹中运行child
,只需将其传递而不带括号,如下所示:
print(parent(child, ui))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.