返回顶部

收藏

Class operator add: instance + noninstance and noninstance + instance

更多
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, other

x = Commuter(88)

y = Commuter(99)

x + 1                      # __add__:  instance + noninstance

1 + y                      # __radd__: noninstance + instance

print x + y                # __add__:  instance + instance

标签:Python,Class

收藏

0人收藏

支持

0

反对

0

发表评论