【发布时间】:2020-07-24 04:34:41
【问题描述】:
尝试验证函数参数组合。这样()、(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
问题:什么是正确的 Pythonic 方法?
谢谢!
【问题讨论】:
标签: python validation parameters arguments