Here is the code:
>>> from ctypes import *
>>> class T:
... def __and__(self, other):
... return 1
...
>>> T() & T()
1
>>> T() & "1"
1
>>> T() & 1
1
>>> T() & c_ulong(3)
1
>>> c_ulong.__and__ = lambda self, other: 1
>>> c_ulong(3) & c_ulong(2)
1
All good so far. But why doesn't the code below work in Python 2.7.2?
>>> c_ulong(3) & T()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for &: 'c_ulong' and 'instance'
>>> c_ulong(3) & 2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for &: 'c_ulong' and 'int'
For a class that I define, there seems to be no problem to support any type of second operand, but when I am attaching the function to a ctypes type, the function is called when the second operand is of the same type, but not if the operand is of a different type. I really want to allow bitwise and and or between ctypes integers and regular integers where the result is a ctypes integer.
Aucun commentaire:
Enregistrer un commentaire