I am trying to store information about a class in a file. From what I've read, the data is typically converted into a dictionary using instance.__dict__. But the problem is my class contains classes nested inside. For example:
Class1:
# Example data members
data = None
a = 0
b = 0
def __init__(self, data, a, b):
self.data = data
self.a = a
self.b = b
Class2:
x = 0
y = 0
def __init__(self, x, y):
self.x = x
self.y = y
nested_class = Class2(5, 10)
exterior_class = Class1(nested_class, 10, 15)
Now for the sake of keeping things clear, I am going to refer to the example (instead of the data I am actually working with because its more complicated). If I want to save Class1 in a .json, I cannot use json.dump because it cannot convert the class into a storage format. You can use the above method I mentioned but it won't convert the Class2 into a dictionary. Does anybody know of a better way to solve this issue without having to individually convert each nested class into a dictionary and then back when I need to use them as actual classes and not dictionaries (For the record, I have three layers of nested classes in my actual scenario, so an efficient solution will be appreciated)