I am trying to print a dictionary of all of the properties within the class instance, however, when there is another class as one of the properties, I am having trouble printing a dictionary of its properties as well. This is kind of hard for me to explain, so I think an example will be easier to understand.
class A:
def __init__(self):
self.x = "x"
self.b = B(self)
def __str__(self):
self.__dict__["b"] = vars(self.__dict__["b"])
return str(self.__dict__)
class B:
def __init__(self, a):
self.a = a
self.y = "y"
self.c = C(self)
def __str__(self):
self.__dict__["c"] = vars(self.__dict__["c"])
return str(temp)
class C:
def __init__(self, b):
self.b = b
self.z = "z"
def __str__(self):
return str(self.__dict__)
print(A())
Output:
{'x': 'x', 'b': {'a': <__main__.A object at 0x7f1913e47460>, 'y': 'y', 'c': <__main__.C object at 0x7f1913e2dbe0>}}
Expected Output:
{'x': 'x', 'b': {'a': <__main__.A object at 0x7fe0becea460>, 'y': 'y', 'c': {'b': <__main__.B object at 0x7f8e7224bc40>, 'z': 'z'}}}
Ultimately, I want my output to be this:
{'x': 'x', 'b': {'y': 'y', 'c': {'z': 'z'}}
So, if anyone could help by either deducing my error in the intermediary step or solving the problem outright to get me to what I want, I would really appreciate it. Thank you!