Let's assume, the following case:
class foo(object):
def __init__(self, ref=None):
self.ref = ref
def __str__(self):
return "I'm foolish"
f = foo()
f.ref = foo()
f.ref.ref = foo()
f.ref.ref.ref = foo()
Trying print(getattr(f, "ref.ref.ref"))
raises 'foo' object has no attribute 'ref.ref.ref'.
So, I resolved the issue with the following function:
def get_ref(ref_attr):
for attr in ref_attr.split('.'):
print(getattr(f, attr))
get_ref("ref.ref.ref") retruns the expected result:
I'm foolish
I'm foolish
I'm foolish
I wonder myself, if is an another way to do this or a built-in method that i could used.
Thanks.