Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python : Revert to base __str__ behavior

Tags:

python

string

How can I revert back to the default function that python uses if there is no __str__ method?

class A :
   def __str__(self) :
      return "Something useless"

class B(A) :
   def __str__(self) :
      return some_magic_base_function(self)
like image 711
Paul Tarjan Avatar asked Sep 06 '09 00:09

Paul Tarjan


1 Answers

You can use object.__str__():

class A:
   def __str__(self):
      return "Something useless"

class B(A):
   def __str__(self):
      return object.__str__(self)

This gives you the default output for instances of B:

>>> b = B()
>>> str(b)
'<__main__.B instance at 0x7fb34c4f09e0>'
like image 62
sth Avatar answered Nov 14 '22 23:11

sth