有没有更好的方法来检查多个变量是Python中的单一类型?

ill*_*ian 2 python comparison types exception

    if not [True]*3 == [isinstance(i, int) for i in [days, months, years]]:
        raise TypeError('days, months, and years must be int type not %s %s %s'
                        % (type(days), type(months), type(years)))
Run Code Online (Sandbox Code Playgroud)

基本上想要检查天,月和年是否是一个int,我能解决的最紧凑的方法是上面但是我不确定它是最好的但我相信它会比每个int的多个if语句更好.

iCo*_*dez 12

您可以使用all生成器表达式:

if not all(isinstance(i, int) for i in [days, months, years]):
Run Code Online (Sandbox Code Playgroud)


Bre*_*arn 6

if not all(isinstance(i, int) for i in [days, months, years])
Run Code Online (Sandbox Code Playgroud)