python定义类的操作符__plus__示例,python__plus_,class adder:


class adder:     def __init__(self, value=0):         self.data = value                  # initialize data     def __add__(self, other):         self.data += other                 # add other in-placeclass addrepr(adder):                       # inherit __init__, __add__     def __repr__(self):                    # add string representation         return 'addrepr(%s)' % self.data   # convert to string as codex = addrepr(2)                              # runs __init__x + 1                                       # runs __add__print x                                     # runs __repr__addrepr(3)print x                                     # runs __repr__addrepr(3) print str(x), repr(x)                       # run ___repr__

评论关闭