1

I know

__str__

works for most uses of strings.

But what about when I simply write the instance and press enter in the interpreter?

Currently it returns something like <__main__.class at 0xaea0080>, what method do I use to overwrite that?

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284

1 Answers1

0

Use __repr__. Note also that if __str__ is not specified, it defaults to __repr__, but it's rare for this to be useful if you're using them right.

It's a common convention is that __repr__'s result should either look like a constructor call or be surrouned with <>, but it is not intended to be eval'ed in either case.

Lots of better examples here: Difference between __str__ and __repr__ in Python

Community
  • 1
  • 1
o11c
  • 15,265
  • 4
  • 50
  • 75
  • note that a `repr` is intentionally different — it's meant to expose useful details to programmers, and usually takes the form `` – Eevee Oct 06 '14 at 03:43