0

Assume I have a class that inherits from object. I create an instance of it pass that to print. It will display something like <__main__.ObjName object at 0xxxxxx>. Is there an object method that can be overridden to provide a return value when the object is accessed this way?

Marcin
  • 48,559
  • 18
  • 128
  • 201
Lother
  • 1,699
  • 1
  • 12
  • 17

2 Answers2

4

You're looking for __repr__ and __str__.

tzaman
  • 46,925
  • 11
  • 90
  • 115
  • This was exactly what I was looking for. Thanks! Any key pitfalls using one of these vs the other? – Lother Jun 09 '15 at 00:40
  • 1
    @Lother have a look at [this question](http://stackoverflow.com/questions/1436703/difference-between-str-and-repr-in-python) for a comparison. – George Brighton Jun 09 '15 at 00:41
1

Either __repr__or __str__ will do it.

John
  • 2,410
  • 1
  • 19
  • 33