Python 干净地处理多变量的类型错误

Python 干净地处理多变量的类型错误,python,error-handling,code-structure,Python,Error Handling,Code Structure,目前我正在开发一个webapp,在那里我遇到了一个问题,即一个函数必须处理多个输入,但任意数量的输入都可能是None(或者导致其他一些错误): 最优雅的处理方式是什么?写下所有的案例肯定是一种选择 期待学习,并提前表示感谢 如果唯一的情况是输入可能是None,则可以指定空set(),而不是None: def my_func(input_1: set, input_2: set, input_3: set) -> set: inputs = ( input_1 or

目前我正在开发一个webapp,在那里我遇到了一个问题,即一个函数必须处理多个输入,但任意数量的输入都可能是
None
(或者导致其他一些错误):

最优雅的处理方式是什么?写下所有的案例肯定是一种选择


期待学习,并提前表示感谢

如果唯一的情况是输入可能是
None
,则可以指定空
set()
,而不是
None

def my_func(input_1: set, input_2: set, input_3: set) -> set:
    inputs = (
        input_1 or set(), 
        input_2 or set(),
        input_3 or set()
    )

    return set.union(*inputs)

是不是用暴力的方式把所有的案子都打出来了:

def my_func(input_1, input_2, input_3):
    types = (
        type(input_1),
        type(input_2),
        type(input_3),
        )

    if not all(types):
        return None
    elif not (types[0] and types[1]):
        return input_3
    elif not (types[0] and types[2]):
        return input_2
    elif not (types[1] and types[2]):
        return imput_1
    elif not types[0]:
        return input_2 | input_3
    elif not types[1]:
       return input_1 | input_3
    elif not types[2]:
        return input_1 | input_2
    else:
        return input_1 | input_2 | input_3

不幸的是,如果使用更多的输入,这将失败,sice one需要处理2^(num_输入)情况,因此我愿意接受更好的建议。

首先,请注意,在某些Web应用程序(如Flask)上显式声明变量类型将产生错误。我要做的是管理类型:
def my_func(input_1=None,input_2=None,input_3=None):
如果input_1为None:
\35;做点什么
,对其他输入也一样。如果未设置类型(输入2),您还可以使用:
对类型进行测试:
#做点什么`@lalam感谢您提供有关声明类型的提示。在接下来的30分钟里,我会第一个碰到这个问题。
def my_func(input_1, input_2, input_3):
    types = (
        type(input_1),
        type(input_2),
        type(input_3),
        )

    if not all(types):
        return None
    elif not (types[0] and types[1]):
        return input_3
    elif not (types[0] and types[2]):
        return input_2
    elif not (types[1] and types[2]):
        return imput_1
    elif not types[0]:
        return input_2 | input_3
    elif not types[1]:
       return input_1 | input_3
    elif not types[2]:
        return input_1 | input_2
    else:
        return input_1 | input_2 | input_3