I am making an Encoder for converting python objects to json and when researching I see a lot of solutions that include a default
method. I'm not an expert in Python, but definitely not a novice either, and I'm wondering if I somehow missed that there is a default
method that gets run automatically when a class is called. Or, is this just because I have inherited from the JSONEcoder
class (which has a default method, and I am now just overriding)? Can someone clarify? And if so, is it basically the same as the
__init__()
method?
By the way, my encoder looks like this if you need more of a visual:
class ComplexEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, my_custom_object):
return str(obj)
return json.JSONEncoder.default(self, obj)
magic method
that I didn't know about. Thanks! – Under