Print has no problem printing a dict:
d = dict()
d['x'] = 'y'
print(d)
{'x': 'y'}
Then why does this fail?
class Utterance:
def __init__(self, intent_name, text):
self.intent_name = intent_name
self.text = text
def __str__(self):
return dict(self.__dict__)
print(utterance)
Traceback (most recent call last): print(utterance) TypeError: __str__ returned non-string (type dict)
(I know it is standard for __str__
to return string, but I have a reason related to JSON encoding to return dict)