【问题标题】:Python: checking type of variablesPython:检查变量的类型
【发布时间】:2013-12-20 13:14:25
【问题描述】:

我有一个关于 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

是否有可能使这段代码更短?

谢谢!

【问题讨论】:

  • 由于 isinstance(a, int)isinstance(a, float) 将是 False,聪明的答案是 if 始终为真,可以完全省略...
  • 我将省略检查并在以下代码中捕获任何由此产生的错误。
  • 请看我的回答,看问题....

标签: python isinstance


【解决方案1】:

你应该使用all:

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

请注意,您可以同时为 isinstance 调用提供 int 和 'float'。

【讨论】:

  • 使用生成器表达式。否则将测试所有元素。
  • 应该是not all(...)any(not ...)。不是吗?
【解决方案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
>>> 

【讨论】:

    【解决方案3】:
    >>> 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
    

    【讨论】:

      【解决方案4】:

      其实你写的等于

      if True:
          do_somthing
          pass
      

      【讨论】:

      • 你应该已经找到答案了
      【解决方案5】:

      显然,您对 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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-01-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-24
        • 2018-03-21
        • 1970-01-01
        相关资源
        最近更新 更多