1

In some classes, doing str(class) will give you a string, such as 'something'. However, whenever I make a class, it only ever gives <__main__.Test object at 0x0000026FB34D70B8>.

How do I make it return a string, such as '0', or even '100'?

martineau
  • 112,593
  • 23
  • 157
  • 280
Pythonic Guy 21421
  • 301
  • 1
  • 3
  • 8

1 Answers1

1

Short answer: by overloading the class' __str__ and/or __repr__ methods, depending on what you need.

class Frobber:
    ...
    def __str__ ( self ):
        return "<Frobber {}>".format(self.someVariable)
hunteke
  • 3,474
  • 1
  • 5
  • 15