I would like to automatically convert a Python class into a dictionary so I can convert it to JSON. The question How to make a class JSON serializable suggests to use
Код: Выделить всё
myClass.__dict__
The following example...
Код: Выделить всё
class Thing():
def __init__(self, name):
self.name = name
self.children = [self.Thing2(self)]
class Thing2():
def __init__(self, parent):
self.name = parent.name + "'s child"
myThing = Thing("Clay")
print(myThing.__dict__)
Код: Выделить всё
{'name': 'Clay', 'children': []}
Источник: https://stackoverflow.com/questions/570 ... rializable