我有一个带有以下输入验证例程的Flask控制器操作,有没有更简洁的方法来编写它?
if 'firstName' and \
'lastName' and \
'age' \
not in request.json:
return flask.abort(400)
也许是这样的,尽管我不确定如何让和运算符工作:
mandatory_fields = ['firstName', 'lastName', 'age']
if mandatory_fields not in request.json:
return flask.abort(400)
第二个代码片段可能吗?
发布于 2018-06-16 05:13:11
您可以像这样使用all()
方法:
fields = ["a", "b", "c"]
a_hash = {"a": 1, "b": 2, "c": 3, "d": 4}
if all(field in a_hash for field in fields):
print("All good")
# prints "All good"
fields = ["a", "b", "c", "e"]
a_hash = {"a": 1, "b": 2, "c": 3, "d": 4}
if any(field not in a_hash for field in fields):
print("All bad")
# prints "All bad"
https://stackoverflow.com/questions/50882680
复制相似问题