So in playing around with getattr in my code I discovered the following:
myVariable = foo.A.bar
works…but something like this:
B = "A"
myVariable = getattr(foo, B + ".bar")
returns an error that foo does not contain an attribute A.bar. Where am I going wrong? Thanks!
Because there is no attribute
A.baronfoo. Attributebaris a part of the object pointed to byA, which is an attribute offoo. You need eitheror
The generic code for accessing deep attributes is to split on the dot, and go until the last part is found (I’m writing from memory, not tested):