I'm trying to update an attribute of a class with setattr
and a string. So for instance with this code:
class ClassA():
def __init__(self):
self.A = 0
class ClassB():
def __init__(self):
self.CA = ClassA()
CB = ClassB()
setattr(CB, "CA.A", 2)
print(CB.CA.A)
when I do setattr(CB, "CA.A", 2)
, it doesn't update the attribute A
of the CA
class inCB
. Instead, it creates another attribute, as in the picture:
Then when I print print(CB.CA.A)
, I get a 0 value.
I don't understand why this happend and if a solution exists?