class Number(object):
def __init__(self):
super(Number, self).__init__()
self.data = 10
def __getattr__(self, name):
def _missing(*args, **kwargs):
method = getattr(self.data, name)
return method(args[0])
return _missing
a = Number()
b = Number()
print a.__add__(10) # this is ok!
print a + 10 # TypeError: "unsupported operand type(s) for +: 'Number' and 'int'"
print a + b # TypeError: "unsupported operand type(s) for +: 'Number' and 'Number'"
質問: "a.__add__(10)" と "a + 10" の違いは何ですか? 演算子 "+" をフックするにはどうすればよいですか?