1

I am recreating a structure of a json file using classes and classes as attributes of the parent class (this allows me to use set methods for easing the process):

class A(object):
    def __init__(self,):
        self.test = "something of A"
    def set_test():
        pass # some function here to set test from B



class B(object):
    def __init__(self,):
        self.test2 = A() # This is the A class that I wan't to access
        self.test3 = "something of B"



g = B()
g.__dict__

The result is this:

{'test2': <__main__.A at 0x7fda2d2fd9a0>, 'test3': 'something of B'}

I would instead want to get the dict object also of the class as an attribute:

{'test2': {'test': 'something of A'}, 'test3': 'something of B'}

I know that I can do: g['test2'] = g['test2'].__dict__ but I was wondering if there is an automated way to get resolve as dictionaries all the classes linked as attributes.

NOTE: I want to be able to access the methods of A from B otherwise I would have directly created a dictionary for B

G M
  • 14,123
  • 7
  • 67
  • 66

0 Answers0