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 codeclass addstr(adder): def __str__(self): # __str__ but no __repr__ return '[Value: %s]' % self.data # convert to nice stringx = addstr(3)x + 1print x # runs __str__print str(x), repr(x)
Name (required)
email (will not be published) (required)
Website