說我想比較2個變量與不同的數據類型:字符串和整數。我已經在Python 2.7.3和Python 3.2.3中對它進行了測試,並且不會引發異常。比較結果是False
。在這種情況下,我可以使用不同的選項來配置或運行Python以引發異常嗎?當我比較不同的數據類型時,我可以讓Python拋出異常嗎?
[email protected]:~$ 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
>>>
[email protected]:~$ 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
>>>
[email protected]:~$
你想爲所有對象重載'__eq__'嗎? – BlackVegetable 2013-03-16 16:10:26
你究竟想要做什麼? – 2013-03-16 16:12:09
@Burhan Khalid:我想保護自己免於意外比較不相關的類型,並儘可能早地瞭解它。 – ks1322 2013-03-16 16:15:04