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”有什么区别,如何挂钩运算符“+”?