0

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!

Shivang Patel
  • 157
  • 1
  • 3
  • 13
  • Does this answer your question? [Recursively convert python object graph to dictionary](https://stackoverflow.com/questions/1036409/recursively-convert-python-object-graph-to-dictionary) – Omar Sheashaa Jul 27 '20 at 08:59

1 Answers1

1

for second you can use this or change it according to your need:

from ast import literal_eval
class A:
    def __init__(self):
        self.x = "x"
        self.b = B(self)

    def __str__(self):
        o = {}
        for x in self.__dict__:
            if isinstance(self.__dict__[x], str):
                o[x] = self.__dict__[x]
        o['b'] = literal_eval(str(self.b))
        return str(o)


class B:
    def __init__(self, a):
        self.a = a
        self.y = "y"
        self.c = C(self)

    def __str__(self):
        o = {}
        for x in self.__dict__:
            if isinstance(self.__dict__[x], str):
                o[x] = self.__dict__[x]
        o['c'] = literal_eval(str(self.c))
        return str(o)


class C:
    def __init__(self, b):
        self.b = b
        self.z = "z"

    def __str__(self):
        o = {}
        for x in self.__dict__:
            if isinstance(self.__dict__[x], str):
                o[x] = self.__dict__[x]
        return str(o)
print(A())

and for first expected you should use str instead of var

class A:
  def __init__(self):
    self.x = "x"
    self.b = B(self)
  def __str__(self):
    self.__dict__["b"] = str(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"] = str(self.__dict__["c"])
    return str(self.__dict__)

class C:
  def __init__(self, b):
    self.b = b
    self.z = "z"
  def __str__(self):
    return str(self.__dict__)

print(A())
Ali Fallah
  • 138
  • 8
  • The first code block you answered, prints `{'x': 'x', 'b': '{\'y\': \'y\', \'c\': "{\'z\': \'z\'}"}'}` which has the brackets escaped, instead of printing like a normal dictionary. – Shivang Patel Jul 29 '20 at 19:50
  • @ShivangPatel , i edited it to print correct value. – Ali Fallah Jul 29 '20 at 20:07