Saturday 3 December 2011

__repr__ in python

_repr__ is used to print a human readable presentation of an object. In this case it prints the class name and some other attributes. A simple example:
>>> class point:
...     def __init__(self,x,y):
...             self.x,self.y=x,y
...     def __repr__(self):
...             return 'point(x=%s, y=%s)' %(self.x,self.y)
... 
>>> p=point(1,2)
>>> p
point(x=1, y=2)

No comments:

Post a Comment