在比较不同的数据类型时,可以让Python抛出异常吗? [英] Can I make Python throw exception when equal comparing different data types?

查看:144
本文介绍了在比较不同的数据类型时,可以让Python抛出异常吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我想比较两个变量与不同的数据类型:string和int。我已经在Python 2.7.3和Python 3.2.3中进行了测试,也不会抛出异常。比较结果是 False 。在这种情况下,我可以配置或运行具有不同选项的Python来抛出异常吗?

  ks @ ks-P35-DS3P:〜$ python2 
Python 2.7.3(默认,2012年8月1日,05:14:39)
[GCC 4.6.3] on linux2
输入help,copyright,credits或许可证以获取更多信息。
>>> a =123
>>>> b = 123
>>>> a == b
False
>>>
ks @ ks-P35-DS3P:〜$ python3
Python 3.2.3(默认,2012年4月12日,19:08:59)
[GCC 4.6.3] on linux2
输入help,copyright,credits或license了解更多信息。
>>> a =123
>>>> b = 123
>>>> a == b
False
>>>
ks @ ks-P35-DS3P:〜$


解决方案

不,你不能。这些项目是不平等的,那里没有错误。



一般而言,这是 unpythonic 强制您的代码只接受特定的类型。如果你想创建一个 int 的子类,并且它可以在任何地方使用 int 工作? Python布尔类型是 int 的子类,例如( True == 1,如果你有异常,你可以执行以下两项操作之一:



$ b


$ b


  1. 测试他们的类型的平等,并自己提出一个例外:

     code>如果不是isinstance(a,type(b))而不是isinstance(b,type(a)):
    raise TypeError('不一样的类型')
    如果一个== b:
    #...

    此示例允许a或b成为另一种类型的子类,您需要根据需要缩小( type(a)is type(b)为超级严格)


  2. 尝试订单类型:

     如果不是, b而不是> b:
    #...

    在Python 3中,当比较数字时会抛出异常具有序列类型的类型(如字符串)。比较在Python 2中成功。



    Python 3演示:

      >>> a,b = 1,'1'
    >>>>不是< b而不是> b
    追溯(最近的最后一次呼叫):
    文件< stdin>,第1行,< module>
    TypeError:不顺序类型:int()< str()
    >>> a,b = 1,1
    >>>>不是< b而不是> b
    True



Say I want to compare 2 variables with different data types: string and int. I have tested it both in Python 2.7.3 and Python 3.2.3 and neither throws exception. The result of comparison is False. Can I configure or run Python with different options to throw exception in this case?

ks@ks-P35-DS3P:~$ python2
Python 2.7.3 (default, Aug  1 2012, 05:14:39) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a="123"
>>> b=123
>>> a==b
False
>>> 
ks@ks-P35-DS3P:~$ python3
Python 3.2.3 (default, Apr 12 2012, 19:08:59) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a="123"
>>> b=123
>>> a==b
False
>>> 
ks@ks-P35-DS3P:~$ 

解决方案

No, you can't. The items are just not equal, there is no error there.

Generally speaking, it is unpythonic to force your code to only accept specific types. What if you wanted to create a subclass of the int, and have it work everywhere an int works? The Python boolean type is a subclass of int, for example (True == 1, False == 0).

If you have to have an exception, you can do one of two things:

  1. Test for equality on their types and raise an exception yourself:

    if not isinstance(a, type(b)) and not isinstance(b, type(a)):
        raise TypeError('Not the same type')
    if a == b:
        # ...
    

    This example allows for either a or b to be a subclass of the other type, you'd need to narrow that down as needed (type(a) is type(b) to be super strict).

  2. Try to order the types:

    if not a < b and not a > b:
        # ...
    

    In Python 3, this throws an exception when comparing numerical types with sequence types (such as strings). The comparisons succeed in Python 2.

    Python 3 demo:

    >>> a, b = 1, '1'
    >>> not a < b and not a > b
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: unorderable types: int() < str()
    >>> a, b = 1, 1
    >>> not a < b and not a > b
    True
    

这篇关于在比较不同的数据类型时,可以让Python抛出异常吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆