Python 返回自定义函数的布尔值
我将列表中的关键元素与字典键进行比较Python 返回自定义函数的布尔值,python,python-3.x,function,dictionary,return,Python,Python 3.x,Function,Dictionary,Return,我将列表中的关键元素与字典键进行比较 event_test = { "EventType": "ShipmentDueDate", "Endpoint": "https://example.net" } events_list = ['EventType','Endpoint'] body = "https" 我编写了一个自定义函数,如下所示 def validate
event_test = {
"EventType": "ShipmentDueDate",
"Endpoint": "https://example.net"
}
events_list = ['EventType','Endpoint']
body = "https"
我编写了一个自定义函数,如下所示
def validate_input(event_keys,body):
count = 0
for list_item in events_list:
if list_item in event_keys:
count+= 1
if count != len(events_list):
print("One/All of: 'EventType','Endpoint' parameters are missing.")
if not "https" in body:
print("Only https endpoints are accepted")
return bool
我想执行代码的另一部分,前提是函数执行时没有任何错误。我不知道如何为函数指定一个返回值,并根据返回值执行代码
我正在尝试:首先调用我的函数
validate_response = validate_input(list(event_test.keys()),body)
if validate_response == False:
print("error in your input")
try:
print("execute my rest of the code")
这是正确的方法吗?首先,您从未在代码示例中定义
bool
的值bool
是一个内置函数,因此validate\u input
实际上总是返回函数bool
,这将导致程序无法按预期工作
更好的实现方法是,如果满足任一错误条件,则返回False
,否则返回True
,如下所示:
def validate_input(event_keys,body):
count = 0
for list_item in events_list:
if list_item in event_keys:
count+= 1
if count != len(events_list):
print("One/All of: 'EventType','Endpoint' parameters are missing.")
return False
if not "https" in body:
print("Only https endpoints are accepted")
return False
return True
if validate_response:
print("execute my rest of the code")
else:
print("error in your input")
此外,validate\u input
实际上并没有引发任何异常,它只是根据函数参数是否有效返回True
或False
。没有必要使用try-except
语句;您可以简单地使用if-else
语句,如下所示:
def validate_input(event_keys,body):
count = 0
for list_item in events_list:
if list_item in event_keys:
count+= 1
if count != len(events_list):
print("One/All of: 'EventType','Endpoint' parameters are missing.")
return False
if not "https" in body:
print("Only https endpoints are accepted")
return False
return True
if validate_response:
print("execute my rest of the code")
else:
print("error in your input")
在进行上述更改后,如果
validate\u response
为False
,则将打印“输入错误”
<如果在事件列表中找到的所有项目也出现在参数事件键中,并且“https”
在正文中,则code>验证\u输入将返回True
。首先,您从未在代码示例中定义bool
的值bool
是一个内置函数,因此validate\u input
实际上总是返回函数bool
,这将导致程序无法按预期工作
更好的实现方法是,如果满足任一错误条件,则返回False
,否则返回True
,如下所示:
def validate_input(event_keys,body):
count = 0
for list_item in events_list:
if list_item in event_keys:
count+= 1
if count != len(events_list):
print("One/All of: 'EventType','Endpoint' parameters are missing.")
return False
if not "https" in body:
print("Only https endpoints are accepted")
return False
return True
if validate_response:
print("execute my rest of the code")
else:
print("error in your input")
此外,validate\u input
实际上并没有引发任何异常,它只是根据函数参数是否有效返回True
或False
。没有必要使用try-except
语句;您可以简单地使用if-else
语句,如下所示:
def validate_input(event_keys,body):
count = 0
for list_item in events_list:
if list_item in event_keys:
count+= 1
if count != len(events_list):
print("One/All of: 'EventType','Endpoint' parameters are missing.")
return False
if not "https" in body:
print("Only https endpoints are accepted")
return False
return True
if validate_response:
print("execute my rest of the code")
else:
print("error in your input")
在进行上述更改后,如果validate\u response
为False
,则将打印“输入错误”
<如果在事件列表中找到的所有项目也出现在参数事件键中,并且正文中有https“
,则验证输入将返回True
,首先,我看到两个问题
1。您的评估条件有问题(您缺少一个else
)
2。您的函数未返回True
或False
,并且在检查“https”时未正确使用not in
运算符。
def validate_input(event_keys,body):
count = 0
for list_item in events_list:
if list_item in event_keys:
count+= 1
if count != len(events_list):
print("One/All of: 'EventType','Endpoint' parameters are missing.")
return False
if "https" not in body:
print("Only https endpoints are accepted")
return False
return True
继续…
我有点不清楚你所说的“正确的方式”
我将尝试一下,并假设您指的是降低示例中的复杂性和/或代码行数
您可以尝试使用以下“优化”
更改函数以使用列表理解功能
注意:我确实将“body”函数param的名称更改为“body_str”,因为它从外部作用域隐藏了body变量。请根据经验避免这种情况
下一行本质上返回一个与if条件匹配的元素的新列表,然后使用len操作来计算与所述条件匹配的元素的数量
更改您的评估
一种可能性(我个人会用这个)
另一种可能是完全取消临时变量赋值,但会降低可读性
首先,我看到了几个问题
1。您的评估条件有问题(您缺少一个else
)
2。您的函数未返回True
或False
,并且在检查“https”时未正确使用not in
运算符。
def validate_input(event_keys,body):
count = 0
for list_item in events_list:
if list_item in event_keys:
count+= 1
if count != len(events_list):
print("One/All of: 'EventType','Endpoint' parameters are missing.")
return False
if "https" not in body:
print("Only https endpoints are accepted")
return False
return True
继续…
我有点不清楚你所说的“正确的方式”
我将尝试一下,并假设您指的是降低示例中的复杂性和/或代码行数
您可以尝试使用以下“优化”
更改函数以使用列表理解功能
注意:我确实将“body”函数param的名称更改为“body_str”,因为它从外部作用域隐藏了body变量。请根据经验避免这种情况
下一行本质上返回一个与if条件匹配的元素的新列表,然后使用len操作来计算与所述条件匹配的元素的数量
更改您的评估
一种可能性(我个人会用这个)
另一种可能是完全取消临时变量赋值,但会降低可读性