Python:检查变量的类型

时间:2013-12-20 13:14:25

标签: python isinstance

我对python有疑问。

我有变量abcd

我有以下几行:

if    not isinstance(a, int)   or not isinstance(b, int)  \
   or not isinstance(c, int)   or not isinstance(d, int)  \
   or not isinstance(a, float) or not isinstance(b, float)\
   or not isinstance(c, float) or not isinstance(d, float):
    do something

是否可以缩短此代码?

谢谢!

5 个答案:

答案 0 :(得分:5)

你应该使用all

if not all(isinstance(var, (int, float)) for var in [a, b, c, d]):
    # do stuff

请注意,您可以为int来电提供isinstance和'浮动'。

答案 1 :(得分:2)

尝试以下方法:

>>> a = 1
>>> b = 1.0
>>> c = 123
>>> d = 233
>>> any((type(var) in (int, float) for var in [a,b,c,d]))
True
>>> c = 'hello'
>>> any((type(var) in (int, float) for var in [a,b,c,d]))
True
>>> 

答案 2 :(得分:1)

>>> a = b = c = d = []
>>> any(not isinstance(x, (int, float)) for x in [a,b,c,d])
True
>>> d = 0
>>> any(not isinstance(x, (int, float)) for x in [a,b,c,d])
False

答案 3 :(得分:0)

实际上,你所写的内容等于

if True:
    do_somthing
    pass

答案 4 :(得分:-3)

显然,你没有对RemcoGerlich的评论给予足够的重视,因为你投票并接受了一个毫无意义的答案。
在我写这篇文章的时候,其他4个人提出了同样毫无意义的答案 那太不可思议了 你会看到更好的吗? :

def OP(a,b,c):
    return    not isinstance(a, int)\
           or not isinstance(b, int)\
           or not isinstance(c, int)\
           or not isinstance(a, float)\
           or not isinstance(b, float)\
           or not isinstance(c, float)

def AZ(a,b,c):
    return all(isinstance(var, (int, float))
               for var in [a, b, c])

gen = ((a,b,c) for a in (1, 1.1 ,'a')
       for b in (2, 2.2, 'b') for c in (3, 3.3, 'c'))

print '                  OPv | AZv     OPv is AZv\n'\
      '                 -----|-----    -----------'
OPV_list = []
for a,b,c in gen:
    OPv = OP(a,b,c)
    OPV_list.append(OPv)
    AZv = AZ(a,b,c)
    print '%3r  %3r  %3r    %s | %s      %s'\
          % (a,b,c,OPv,AZv,OPv is AZv if OPv is not AZv else '')

print '-------------    ----'
print 'all(OPV_list) : ',all(OPV_list)

结果
OPv =你的 AZv =无意义的答案
我限于a,b,c缩短

                  OPv | AZv     OPv is AZv
                 -----|-----    -----------
  1    2    3    True | True      
  1    2  3.3    True | True      
  1    2  'c'    True | False      False
  1  2.2    3    True | True      
  1  2.2  3.3    True | True      
  1  2.2  'c'    True | False      False
  1  'b'    3    True | False      False
  1  'b'  3.3    True | False      False
  1  'b'  'c'    True | False      False
1.1    2    3    True | True      
1.1    2  3.3    True | True      
1.1    2  'c'    True | False      False
1.1  2.2    3    True | True      
1.1  2.2  3.3    True | True      
1.1  2.2  'c'    True | False      False
1.1  'b'    3    True | False      False
1.1  'b'  3.3    True | False      False
1.1  'b'  'c'    True | False      False
'a'    2    3    True | False      False
'a'    2  3.3    True | False      False
'a'    2  'c'    True | False      False
'a'  2.2    3    True | False      False
'a'  2.2  3.3    True | False      False
'a'  2.2  'c'    True | False      False
'a'  'b'    3    True | False      False
'a'  'b'  3.3    True | False      False
'a'  'b'  'c'    True | False      False
-------------    ----
all(OPV_list) :  True
相关问题