Related Post: Convert Python dict to an object
Especially in programming REST APIs, you’ll find yourselves stuck up needing to return an object, which Python can’t automatically convert to a
dict automatically (for example for a JSON response).
But there is a (hackish) workaround for this, creating a
as_dictmethod. In that method, you return a dict where each value of the object properties gets retrieved using self: