Overload __repr__ method for repr function

Overload __repr__


class adder:#   ww w. j  av  a 2 s  . c  om
     def __init__(self, value=0):
         self.data = value                  # initialize data
     def __add__(self, other):
         self.data += other                 # add other in-place

class addrepr(adder):                      # inherit __init__, __add__
     def __repr__(self):                    # add string representation
         return 'addrepr(%s)' % self.data   # convert to string as code

class addstr(adder):            
     def __str__(self):                     # __str__ but no __repr__
         return '[Value: %s]' % self.data   # convert to nice string

x = addstr(3)
x + 1
print x                                    # runs __str__

print str(x), repr(x)

The code above generates the following result.





















Home »
  Python »
    Language Basics »




Python Basics
Operator
Statements
Function Definition
Class
Buildin Functions
Buildin Modules