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
  • 119,623
  • 25
  • 170
  • 301
Pythonic Guy 21421
  • 371
  • 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,648
  • 1
  • 7
  • 17