Python:函数参数组合验证



尝试验证函数参数组合。使得()(project)(project,field)(project,field,well)是有效的,而每隔一个组合(例如(field)(是无效的。如果可用,参数将为字符串,否则默认值为None或空字符串"。目前正在做穷人的口罩检查。。。

def make_thing(project=None, field=None, well=None):
# check for valid p-f-w combinations
check = (8 if project else 0) + (4 if field else 0) + (1 if well else 0)
if check not in (0, 8, 8 + 4, 8 + 4 + 1):
return None
# continue to do work

问题:什么是正确的Python方法?

谢谢!

第一个建议

def make_thing(project=None, field=None, well=None):
if (not project and field) or (not field and well):
return
# continue to do work

第二个建议

def make_thing(project=None, field=None, well=None):
if (bool(project), bool(field), bool(well)) not in (
(False, False, False),
(True, False, False),
(True, True, False),
(True, True, True),
):
return
# continue to do work

相关内容

  • 没有找到相关文章

最新更新