不同之處在於調用了對象的__and__
或__rand__
方法。通常,左邊的表達式爲__and__
,稱爲第一個。如果它返回NotImplemented
,則右手錶達式將有機會(並且將調用__rand__
)。
在這種情況下,numpy.int32
已經決定它不可能是「相與」用長 - 至少不是很長,其價值是什麼上面可以通過本機類型來表示......
然而,根據在您的實驗中,python的long
很樂意「和」numpy.int32
- 或者,您的numpy版本可能不對__and__
對稱執行__rand__
。這也可能是依賴於Python的版本(例如,如果你的python版本決定返回一個值而不是NotImplemented
)。
在我的電腦,更沒有工作:
Python 2.7.12 |Continuum Analytics, Inc.| (default, Jul 2 2016, 17:43:17)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> import numpy
>>> numpy.__version__
'1.11.2'
但是我們可以看到的是什麼使用下面的腳本調用:
import sys
import numpy
class MyInt32(numpy.int32):
def __and__(self, other):
print('__and__')
return super(MyInt32, self).__and__(other)
def __rand__(self, other):
print('__rand__')
return super(MyInt32, self).__and__(other)
try:
print(MyInt32(-1) & 0xFFFFFFFF00000000) # Outputs `__and__` before the `TypeError`
except TypeError:
pass
try:
print(0xFFFFFFFF00000000 & MyInt32(-1)) # Outputs `__rand__` before the `TypeError`
except TypeError:
pass
sys.maxint & MyInt32(-1) # Outputs `__rand__`
print('great success')
(sys.maxint + 1) & MyInt32(-1) # Outputs `__rand__`
print('do not see this')
雙方產生類型錯誤與numpy的1.11.1和1.12.1。 – kennytm
對於1.6.2,第一個給出了'18446744069414584320L',但是第二個:'TypeError:不支持的操作數類型爲&:'long'和'numpy.int32'。 –
@kennytm:不是我在1.12.1上看到的...你在Windows上嗎? – Mehrdad