I'm struggling to figure out how to call the base class implementation of stringification in Python. The following illustrates what I am attempting to do:
class A:
def __init__(self, text):
self.text = text
def __str__(self):
return self.text
class B(A):
def __init__(self, prefix, suffix):
A.__init__(self, prefix)
self.suffix = suffix
def __str__(self):
return self.suffix
def __format__(self, fmt):
return f'{str(super(A, self))}, {self.suffix}'
foo = B('hello', 'world')
print(f'{foo}')
My expectation was that the above prints "hello, world". It is currently printing "<super: <class 'A'>, <B object>>, world". I have tried everything I can think of to call the base class implementation of the str method, but can't for the life of me figure out the syntax. What is the syntax to call the base class implementation of __str__ from within B.__format__?
super(A, self).__str__(). My understanding is this is just an alternate syntax toA.__str__(self), but please correct me if that is wrong. The fact that the example calls the__format__method was intentional. TheB.__str__method was added to ensure any proposed answer correctly called the base class implementation.