FAQ: __str__ vs __repr__

Greg Krohn greg at example.invalid
Wed Jun 15 09:14:15 EDT 2005


Basically __repr__ should return a string representaion of the object, 
and __str__ should return a user-friendly pretty string. So maybe:

class Person:
   ...
   def __repr__(self):
     return '<Person: %s, %d, %s>' % (self.name, self.age, self.sign)

   def __str__(self):
     return self.name

See this for a better explanation:

http://www.faqts.com/knowledge_base/view.phtml/aid/1331/fid/241



More information about the Python-list mailing list