使用「物件+obj」時會執行此方法
class Score: def __init__(self, name, *scores): ''' 建構元 設定: 姓名, 不定個數的分數 ''' self.name = name self.scores = sorted(list(scores)) def __add__(self, obj): return sum(self.scores)+obj
s = Score('王小明', 90, 80, 60, 40, 70, 30, 80) print(s+0) print(s+5)
450 455