![](/img/trans.png)
[英]Sympify “<” does not return boolean in custom function?
[英]return boolean value for a custom function
我正在將列表中的關鍵元素與字典鍵進行比較
event_test = {
"EventType": "ShipmentDueDate",
"Endpoint": "https://example.net"
}
events_list = ['EventType','Endpoint']
body = "https"
我寫了一個自定義的 function 如下
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
我想執行我的代碼的另一部分,前提是 function 執行沒有任何錯誤。 我不明白如何為 function 指定返回值並根據返回值執行我的代碼
我正在嘗試這個:首先調用我的 function
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
是內置的 function,因此validate_input
實際上總是會返回 function 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
此外, validate_input
實際上並沒有引發任何異常,它只是根據 function 參數是否有效返回True
或False
。 不需要try-except
語句; 您可以簡單地使用if-else
語句,如下所示:
if validate_response:
print("execute my rest of the code")
else:
print("error in your input")
通過上述更改,如果validate_response
為False
,將打印"error in your input"
。 如果在events_list
中找到的所有項目也存在於參數event_keys
中,並且"https"
在body
中,則validate_input
將返回True
。
首先,我看到了幾個問題
1.您的評估條件有問題(您缺少else
)
if validate_response == False:
print("error in your input")
else:
print("execute my rest of the code")
2. 您的 function 沒有返回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
繼續...
我有點不清楚您所說的“正確方式”
我會嘗試一下,並假設您指的是降低示例中的復雜性和/或代碼行數......
您可以嘗試使用以下“優化”
更改您的 function 以使用列表推導
注意:我確實將“body”function 參數的名稱更改為“body_str”,因為它遮蔽了外部 scope 的 body 變量。 請根據經驗避免這種情況
def validate_input(event_keys, body_str):
count = len([x for x in events_list if x in event_keys])
# print count to debug
print(f"Count is {count}")
if count != len(events_list):
print("One/All of: 'EventType','Endpoint' parameters are missing.")
return False
if "https" not in body_str:
print("Only https endpoints are accepted")
return False
return True
以下行實質上返回了一個與您的 if 條件匹配的元素的新列表,然后它使用 len 操作來計算與所述條件匹配的元素的數量
count = len([x for x in events_list if x in event_keys])
改變你的評價
一種可能性(我個人會使用這個)
if not validate_response:
print("error in your input")
else:
print("execute my rest of the code")
另一種可能性是完全擺脫臨時變量分配 - 雖然降低了可讀性
# validate_response = validate_input(list(event_test.keys()),body)
if not validate_input(list(event_test.keys()), body_str):
print("error in your input")
else:
print("execute my rest of the code")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.