class test():
def __init__(self):
self.a = 1
def __getattribute__(self, attr):
print 'Getattribute:',attr
def __getattr__(self, attr):
print 'GetAttr:',attr
def __dict__(self, attr):
print 'Dict:',attr
def __call__(self, args=None):
print 'Called:',args
def __getitem__(self, attr):
print 'GetItem:',attr
def __get__(self, instance, owner):
print 'Get:',instance,owner
def __int__(self):
print 'Int'
x = test()
print x.a
上記のいずれも呼び出されません。
[root@faparch doxid]# python -m trace --trace test_dict.py
--- modulename: test_dict, funcname: <module>
test_dict.py(1): class test():
--- modulename: test_dict, funcname: test
test_dict.py(1): class test():
test_dict.py(2): def __init__(self):
test_dict.py(5): def __getattribute__(self, attr):
test_dict.py(8): def __getattr__(self, attr):
test_dict.py(11): def __dict__(self, attr):
test_dict.py(14): def __call__(self, args=None):
test_dict.py(17): def __getitem__(self, attr):
test_dict.py(20): def __get__(self, instance, owner):
test_dict.py(23): def __int__(self):
test_dict.py(28): x = test()
--- modulename: test_dict, funcname: __init__
test_dict.py(3): self.a = 1
test_dict.py(29): print x.a
1
--- modulename: trace, funcname: _unsettrace
trace.py(80): sys.settrace(None)
http://docs.python.org/2/library/numbers.html#numbers.Number
おそらく、数値クラス関数を順番に処理するネストされたクラスを実装する必要があります。例のように呼び出しを奪うため。または、少なくともそれはそれを行う1つの方法です。
整数値には、インターセプトする必要のある次の関数が含まれています
['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__float__', '__floordiv__', '__format__', '__getattribute__', '__getnewargs__', '__hash__', '__hex__', '__index__', '__init__', '__int__', '__invert__', '__long__', '__lshift__', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'imag', 'numerator', 'real']