Skip to content

Instantly share code, notes, and snippets.

@macrat
Created September 22, 2016 07:19
Show Gist options
  • Save macrat/0cb2d2c8e4d0331f8f5d13e92b3e96a3 to your computer and use it in GitHub Desktop.
Save macrat/0cb2d2c8e4d0331f8f5d13e92b3e96a3 to your computer and use it in GitHub Desktop.
addとraddが両方実装されてたらどっちが呼ばれるのか、というはなし。addだった。
class Tea:
def __init__(self, n):
self.n = n
def __add__(self, v):
print('add of {}'.format(self.n))
def __radd__(self, v):
print('radd of {}'.format(self.n))
class Teb:
def __init__(self, n):
self.n = n
def __add__(self, v):
print('add of {}'.format(self.n))
def __radd__(self, v):
print('radd of {}'.format(self.n))
if __name__ == '__main__':
Tea(1) + Tea(2) # add of 1
Tea(3) + Teb(4) # add of 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment