简体   繁体   中英

Is there a better way of checking multiple variables are a single type in Python?

    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)))

Basically want to check if days, months, and years is an int, the most compact way I could thing of is above but I'm not sure it's the best but I believe it would be better than multiple if statements for each int.

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

if not all(isinstance(i, int) for i in [days, months, years]):
if not all(isinstance(i, int) for i in [days, months, years])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM