Mega Code Archive

 
Categories / Python Tutorial / Class
 

__str__ is tried first for user-friendly displays

class adder:      def __init__(self, value=0):          self.data = value                           def __add__(self, other):          self.data += other                      class addstr(adder):      def __str__(self):                                  return '[Value: %s]' % self.data        x = addstr(3)  x + 1  x                                               print x                                         str(x), repr(x)