Class operator add: instance + noninstance and noninstance + instance,,class Commut


class Commuter:     def __init__(self, val):         self.val = val     def __add__(self, other):         print 'add', self.val, other     def __radd__(self, other):         print 'radd', self.val, otherx = Commuter(88)y = Commuter(99)x + 1                      # __add__:  instance + noninstance1 + y                      # __radd__: noninstance + instanceprint x + y                # __add__:  instance + instance

评论关闭