if instanceType == instance_type and operatingSystem == operating_system and tenancy == tenancy_db:
sku_list.append(key)
在这个if语句中,这些变量,instanceType,operatingSystem和tenancy是用户输入,如何处理不检查任何用户输入是否为None。
e.g。如果instanceType为None,我想检查
if operatingSystem == operating_system and tenancy == tenancy_db:
sku_list.append(key)
e.g。如果operatingSystem是None,我想检查
if instanceType == instance_type and tenancy == tenancy_db:
sku_list.append(key)
e.g。如果tenancy和instanceType都是None,我想检查:
if operatingSystem == operating_system:
sku_list.append(key)
Simliarly,这取决于用户输入是否为其他,还有其他方法可以做到这一点,或者我必须实现嵌套if else?
答案 0 :(得分:2)
一种方法是声明辅助函数:
equal_or_none = lambda x, y: x is None or x == y
if (
equal_or_none(instanceType, instance_type)
and equal_or_none(operatingSystem, operating_system)
and equal_or_none(tenancy, tenancy_db)):
sku_list.append(key)
答案 1 :(得分:1)
您还可以使用or
运算符将None
视为false:
if (instanceType or instance_type) == instance_type and \
(operatingSystem or operating_system) == operating_system and \
(tenancy or tenancy_db) == tenancy_db:
sku_list.append(key)